From 4543a29c88914acb73ef0d2678126504b8105e9a 2013-02-13 13:21:13 From: Mika Salmela Date: 2013-02-13 13:21:13 Subject: [PATCH] Fix to discard NaN, Inf and -Inf values from chart. This correction adds test to several append funtions to test that NaN, Inf and -Inf values are not added to chart. This solution follows the QPainterPath behaviour. Task-number: QTRD-1893 Reviewed-by: Miikka Heikkinen --- diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index ff5f0ba..9c86014 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -555,14 +555,18 @@ QBarSetPrivate::~QBarSetPrivate() void QBarSetPrivate::append(QPointF value) { - m_values.append(value); - emit restructuredBars(); + if (isValidValue(value)) { + m_values.append(value); + emit restructuredBars(); + } } void QBarSetPrivate::append(QList values) { - for (int i = 0; i < values.count(); i++) - m_values.append(values.at(i)); + for (int i = 0; i < values.count(); i++) { + if (isValidValue(values.at(i))) + m_values.append(values.at(i)); + } emit restructuredBars(); } @@ -570,8 +574,10 @@ void QBarSetPrivate::append(QList values) { int index = m_values.count(); for (int i = 0; i < values.count(); i++) { - m_values.append(QPointF(index, values.at(i))); - index++; + if (isValidValue(values.at(i))) { + m_values.append(QPointF(index, values.at(i))); + index++; + } } emit restructuredBars(); } diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index c2bfb5a..d2b2d99 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -25,6 +25,7 @@ #include #include #include +#include "charthelpers_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE class QBarSetPrivate; diff --git a/src/charthelpers_p.h b/src/charthelpers_p.h new file mode 100644 index 0000000..63e5cfa --- /dev/null +++ b/src/charthelpers_p.h @@ -0,0 +1,26 @@ +#ifndef CHARTHELPERS_P_H +#define CHARTHELPERS_P_H + +#include +#include + +static inline bool isValidValue(qreal value) +{ + if (qIsNaN(value) || qIsInf(value)) { + qWarning("Ignored NaN, Inf, or -Inf value."); + return false; + } + return true; +} + +static inline bool isValidValue(qreal x, qreal y) +{ + return (isValidValue(x) && isValidValue(y)); +} + +static inline bool isValidValue(const QPointF point) +{ + return (isValidValue(point.x()) && isValidValue(point.y())); +} + +#endif // CHARTHELPERS_P_H diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 3927ffb..df690a9 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -27,6 +27,7 @@ #include "charttheme_p.h" #include "qabstractaxis.h" #include "pieanimation_p.h" +#include "charthelpers_p.h" #include "qpielegendmarker.h" @@ -400,6 +401,8 @@ bool QPieSeries::append(QList slices) return false; if (s->series()) // already added to some series return false; + if (!isValidValue(s->value())) + return false; } foreach (QPieSlice *s, slices) { @@ -436,12 +439,17 @@ QPieSeries &QPieSeries::operator << (QPieSlice *slice) /*! Appends a single slice to the series with give \a value and \a label. Slice ownership is passed to the series. + Returns NULL if value is NaN, Inf or -Inf and no slice is added to the series. */ QPieSlice *QPieSeries::append(QString label, qreal value) { - QPieSlice *slice = new QPieSlice(label, value); - append(slice); - return slice; + if (isValidValue(value)) { + QPieSlice *slice = new QPieSlice(label, value); + append(slice); + return slice; + } else { + return 0; + } } /*! @@ -463,6 +471,9 @@ bool QPieSeries::insert(int index, QPieSlice *slice) if (slice->series()) // already added to some series return false; + if (!isValidValue(slice->value())) + return false; + slice->setParent(this); QPieSlicePrivate::fromSlice(slice)->m_series = this; d->m_slices.insert(index, slice); diff --git a/src/src.pro b/src/src.pro index 90a3be9..48a83a3 100644 --- a/src/src.pro +++ b/src/src.pro @@ -54,7 +54,8 @@ PRIVATE_HEADERS += \ $$PWD/scroller_p.h \ $$PWD/qabstractseries_p.h \ $$PWD/chartlayout_p.h \ - $$PWD/charttitle_p.h + $$PWD/charttitle_p.h \ + $$PWD/charthelpers_p.h PUBLIC_HEADERS += \ $$PWD/qchart.h \ $$PWD/qchartglobal.h \ diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index f3e8960..05d2abc 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -24,6 +24,7 @@ #include "qvalueaxis.h" #include "xychart_p.h" #include "qxylegendmarker.h" +#include "charthelpers_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -243,8 +244,11 @@ void QXYSeries::append(qreal x, qreal y) void QXYSeries::append(const QPointF &point) { Q_D(QXYSeries); - d->m_points << point; - emit pointAdded(d->m_points.count() - 1); + + if (isValidValue(point)) { + d->m_points << point; + emit pointAdded(d->m_points.count() - 1); + } } /*! @@ -276,8 +280,10 @@ void QXYSeries::replace(const QPointF &oldPoint, const QPointF &newPoint) int index = d->m_points.indexOf(oldPoint); if (index == -1) return; - d->m_points[index] = newPoint; - emit pointReplaced(index); + if (isValidValue(newPoint)) { + d->m_points[index] = newPoint; + emit pointReplaced(index); + } } /*! @@ -322,8 +328,10 @@ void QXYSeries::remove(const QPointF &point) void QXYSeries::insert(int index, const QPointF &point) { Q_D(QXYSeries); - d->m_points.insert(index, point); - emit pointAdded(index); + if (isValidValue(point)) { + d->m_points.insert(index, point); + emit pointAdded(index); + } } /*!