##// END OF EJS Templates
Fix to discard NaN, Inf and -Inf values from chart....
Mika Salmela -
r2424:4543a29c8891
parent child
Show More
@@ -0,0 +1,26
1 #ifndef CHARTHELPERS_P_H
2 #define CHARTHELPERS_P_H
3
4 #include <qnumeric.h>
5 #include <QPointF>
6
7 static inline bool isValidValue(qreal value)
8 {
9 if (qIsNaN(value) || qIsInf(value)) {
10 qWarning("Ignored NaN, Inf, or -Inf value.");
11 return false;
12 }
13 return true;
14 }
15
16 static inline bool isValidValue(qreal x, qreal y)
17 {
18 return (isValidValue(x) && isValidValue(y));
19 }
20
21 static inline bool isValidValue(const QPointF point)
22 {
23 return (isValidValue(point.x()) && isValidValue(point.y()));
24 }
25
26 #endif // CHARTHELPERS_P_H
@@ -555,14 +555,18 QBarSetPrivate::~QBarSetPrivate()
555 555
556 556 void QBarSetPrivate::append(QPointF value)
557 557 {
558 if (isValidValue(value)) {
558 559 m_values.append(value);
559 560 emit restructuredBars();
560 561 }
562 }
561 563
562 564 void QBarSetPrivate::append(QList<QPointF> values)
563 565 {
564 for (int i = 0; i < values.count(); i++)
566 for (int i = 0; i < values.count(); i++) {
567 if (isValidValue(values.at(i)))
565 568 m_values.append(values.at(i));
569 }
566 570 emit restructuredBars();
567 571 }
568 572
@@ -570,9 +574,11 void QBarSetPrivate::append(QList<qreal> values)
570 574 {
571 575 int index = m_values.count();
572 576 for (int i = 0; i < values.count(); i++) {
577 if (isValidValue(values.at(i))) {
573 578 m_values.append(QPointF(index, values.at(i)));
574 579 index++;
575 580 }
581 }
576 582 emit restructuredBars();
577 583 }
578 584
@@ -25,6 +25,7
25 25 #include <QPen>
26 26 #include <QBrush>
27 27 #include <QFont>
28 #include "charthelpers_p.h"
28 29
29 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 31 class QBarSetPrivate;
@@ -27,6 +27,7
27 27 #include "charttheme_p.h"
28 28 #include "qabstractaxis.h"
29 29 #include "pieanimation_p.h"
30 #include "charthelpers_p.h"
30 31
31 32 #include "qpielegendmarker.h"
32 33
@@ -400,6 +401,8 bool QPieSeries::append(QList<QPieSlice *> slices)
400 401 return false;
401 402 if (s->series()) // already added to some series
402 403 return false;
404 if (!isValidValue(s->value()))
405 return false;
403 406 }
404 407
405 408 foreach (QPieSlice *s, slices) {
@@ -436,12 +439,17 QPieSeries &QPieSeries::operator << (QPieSlice *slice)
436 439 /*!
437 440 Appends a single slice to the series with give \a value and \a label.
438 441 Slice ownership is passed to the series.
442 Returns NULL if value is NaN, Inf or -Inf and no slice is added to the series.
439 443 */
440 444 QPieSlice *QPieSeries::append(QString label, qreal value)
441 445 {
446 if (isValidValue(value)) {
442 447 QPieSlice *slice = new QPieSlice(label, value);
443 448 append(slice);
444 449 return slice;
450 } else {
451 return 0;
452 }
445 453 }
446 454
447 455 /*!
@@ -463,6 +471,9 bool QPieSeries::insert(int index, QPieSlice *slice)
463 471 if (slice->series()) // already added to some series
464 472 return false;
465 473
474 if (!isValidValue(slice->value()))
475 return false;
476
466 477 slice->setParent(this);
467 478 QPieSlicePrivate::fromSlice(slice)->m_series = this;
468 479 d->m_slices.insert(index, slice);
@@ -54,7 +54,8 PRIVATE_HEADERS += \
54 54 $$PWD/scroller_p.h \
55 55 $$PWD/qabstractseries_p.h \
56 56 $$PWD/chartlayout_p.h \
57 $$PWD/charttitle_p.h
57 $$PWD/charttitle_p.h \
58 $$PWD/charthelpers_p.h
58 59 PUBLIC_HEADERS += \
59 60 $$PWD/qchart.h \
60 61 $$PWD/qchartglobal.h \
@@ -24,6 +24,7
24 24 #include "qvalueaxis.h"
25 25 #include "xychart_p.h"
26 26 #include "qxylegendmarker.h"
27 #include "charthelpers_p.h"
27 28
28 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 30
@@ -243,9 +244,12 void QXYSeries::append(qreal x, qreal y)
243 244 void QXYSeries::append(const QPointF &point)
244 245 {
245 246 Q_D(QXYSeries);
247
248 if (isValidValue(point)) {
246 249 d->m_points << point;
247 250 emit pointAdded(d->m_points.count() - 1);
248 251 }
252 }
249 253
250 254 /*!
251 255 This is an overloaded function.
@@ -276,9 +280,11 void QXYSeries::replace(const QPointF &oldPoint, const QPointF &newPoint)
276 280 int index = d->m_points.indexOf(oldPoint);
277 281 if (index == -1)
278 282 return;
283 if (isValidValue(newPoint)) {
279 284 d->m_points[index] = newPoint;
280 285 emit pointReplaced(index);
281 286 }
287 }
282 288
283 289 /*!
284 290 Replaces the current points with \a points. This is faster than replacing data points one by one,
@@ -322,9 +328,11 void QXYSeries::remove(const QPointF &point)
322 328 void QXYSeries::insert(int index, const QPointF &point)
323 329 {
324 330 Q_D(QXYSeries);
331 if (isValidValue(point)) {
325 332 d->m_points.insert(index, point);
326 333 emit pointAdded(index);
327 334 }
335 }
328 336
329 337 /*!
330 338 Removes all points from the series.
General Comments 0
You need to be logged in to leave comments. Login now