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