##// END OF EJS Templates
Handles y-axis in DataSeries....
Alexandre Leroux -
r867:da8b6df7cbcf
parent child
Show More
@@ -8,6 +8,7
8 8 #include <Data/ArrayData.h>
9 9 #include <Data/DataSeriesMergeHelper.h>
10 10 #include <Data/IDataSeries.h>
11 #include <Data/OptionalAxis.h>
11 12
12 13 #include <QLoggingCategory>
13 14 #include <QReadLocker>
@@ -156,7 +157,14 public:
156 157
157 158 bool isEmpty() const noexcept { return m_XAxisData->size() == 0; }
158 159
159 /// Merges into the data series an other data series
160 /// Merges into the data series an other data series.
161 ///
162 /// The two dataseries:
163 /// - must be of the same dimension
164 /// - must have the same y-axis (if defined)
165 ///
166 /// If the prerequisites are not valid, the method does nothing
167 ///
160 168 /// @remarks the data series to merge with is cleared after the operation
161 169 void merge(IDataSeries *dataSeries) override
162 170 {
@@ -164,10 +172,16 public:
164 172 lockWrite();
165 173
166 174 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
175 if (m_YAxis == other->m_YAxis) {
167 176 DataSeriesMergeHelper::merge(*other, *this);
168 177 }
169 178 else {
170 179 qCWarning(LOG_DataSeries())
180 << QObject::tr("Can't merge data series that have not the same y-axis");
181 }
182 }
183 else {
184 qCWarning(LOG_DataSeries())
171 185 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
172 186 }
173 187 unlock();
@@ -325,18 +339,31 public:
325 339 virtual void unlock() { m_Lock.unlock(); }
326 340
327 341 protected:
328 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
329 /// DataSeries with no values will be created.
342 /// Protected ctor (DataSeries is abstract).
343 ///
344 /// Data vectors must be consistent with each other, otherwise an exception will be thrown (@sa
345 /// class description for consistent rules)
330 346 /// @remarks data series is automatically sorted on its x-axis data
347 /// @throws std::invalid_argument if the data are inconsistent with each other
331 348 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
332 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
349 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit,
350 OptionalAxis yAxis = OptionalAxis{})
333 351 : m_XAxisData{xAxisData},
334 352 m_XAxisUnit{xAxisUnit},
335 353 m_ValuesData{valuesData},
336 m_ValuesUnit{valuesUnit}
354 m_ValuesUnit{valuesUnit},
355 m_YAxis{std::move(yAxis)}
337 356 {
338 357 if (m_XAxisData->size() != m_ValuesData->size()) {
339 clear();
358 throw std::invalid_argument{
note

Why an exception? A simple clear() on the data and maybe a method isValid() might be enough.

359 "The number of values by component must be equal to the number of x-axis data"};
360 }
361
362 // Validates y-axis (if defined)
363 if (yAxis.isDefined() && (yAxis.size() != m_ValuesData->componentCount())) {
364 throw std::invalid_argument{
365 "As the y-axis is defined, the number of value components must be equal to the "
366 "number of y-axis data"};
340 367 }
341 368
342 369 // Sorts data if it's not the case
@@ -351,12 +378,16 protected:
351 378 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
352 379 m_XAxisUnit{other.m_XAxisUnit},
353 380 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
354 m_ValuesUnit{other.m_ValuesUnit}
381 m_ValuesUnit{other.m_ValuesUnit},
382 m_YAxis{other.m_YAxis}
355 383 {
356 384 // Since a series is ordered from its construction and is always ordered, it is not
357 385 // necessary to call the sort method here ('other' is sorted)
358 386 }
359 387
388 /// @return the y-axis associated to the data series
389 OptionalAxis yAxis() const { return m_YAxis; }
390
360 391 /// Assignment operator
361 392 template <int D>
362 393 DataSeries &operator=(DataSeries<D> other)
@@ -365,6 +396,7 protected:
365 396 std::swap(m_XAxisUnit, other.m_XAxisUnit);
366 397 std::swap(m_ValuesData, other.m_ValuesData);
367 398 std::swap(m_ValuesUnit, other.m_ValuesUnit);
399 std::swap(m_YAxis, other.m_YAxis);
368 400
369 401 return *this;
370 402 }
@@ -380,11 +412,17 private:
380 412 m_ValuesData = m_ValuesData->sort(permutation);
381 413 }
382 414
415 // x-axis
383 416 std::shared_ptr<ArrayData<1> > m_XAxisData;
384 417 Unit m_XAxisUnit;
418
419 // values
385 420 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
386 421 Unit m_ValuesUnit;
387 422
423 // y-axis (optional)
424 OptionalAxis m_YAxis;
425
388 426 QReadWriteLock m_Lock;
389 427 };
390 428
General Comments 0
You need to be logged in to leave comments. Login now