summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTitta Heikkala <titta.heikkala@digia.com>2014-05-27 10:15:14 +0300
committerTitta Heikkala <titta.heikkala@digia.com>2014-05-27 10:54:51 +0300
commit6c409268c150552c11dcbd22e11600f61203da3a (patch)
tree7ac157b016136b4f9a80b86cc47bd1a8cd630bba
parentdb3eff743f503c292f0a0ec639ccc2ac4182c731 (diff)
Fix assert failure with percent bar series
Category groups with zero values are not drawn. Task-number: QTRD-3087 Change-Id: Ie6f85e48398e751213b11d88c0c6a8de2ce8247a Reviewed-by: Miikka Heikkinen <miikka.heikkinen@digia.com>
-rw-r--r--src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp15
-rw-r--r--src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp13
-rw-r--r--src/barchart/vertical/percent/percentbarchartitem.cpp15
-rw-r--r--src/barchart/vertical/percent/qpercentbarseries.cpp8
-rw-r--r--tests/auto/qhorizontalpercentbarseries/tst_qhorizontalpercentbarseries.cpp20
-rw-r--r--tests/auto/qpercentbarseries/tst_qpercentbarseries.cpp20
6 files changed, 75 insertions, 16 deletions
diff --git a/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp b/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp
index 775bfba6..35083660 100644
--- a/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp
+++ b/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp
@@ -75,17 +75,24 @@ QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout()
for (int set = 0; set < setCount; set++) {
qreal value = m_series->barSets().at(set)->at(category);
QRectF rect;
+ qreal topX = 0;
+ if (sum > 0)
+ topX = 100 * sum / categorySum;
+ qreal bottomX = 0;
+ qreal newSum = value + sum;
+ if (newSum > 0)
+ bottomX = 100 * newSum / categorySum;
QPointF topLeft;
if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
- topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : domain()->minX(), category - barWidth/2), m_validData);
+ topLeft = domain()->calculateGeometryPoint(QPointF(set ? topX : domain()->minX(), category - barWidth/2), m_validData);
else
- topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : 0, category - barWidth/2), m_validData);
- QPointF bottomRight = domain()->calculateGeometryPoint(QPointF(100 * (value + sum)/categorySum, category + barWidth/2), m_validData);
+ topLeft = domain()->calculateGeometryPoint(QPointF(set ? topX : 0, category - barWidth/2), m_validData);
+ QPointF bottomRight = domain()->calculateGeometryPoint(QPointF(bottomX, category + barWidth/2), m_validData);
rect.setTopLeft(topLeft);
rect.setBottomRight(bottomRight);
layout.append(rect.normalized());
- sum +=value;
+ sum = newSum;
}
}
return layout;
diff --git a/src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp b/src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp
index 83f1045c..846f77d4 100644
--- a/src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp
+++ b/src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp
@@ -32,14 +32,17 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE
\brief Series for creating horizontal percent bar chart.
\mainclass
- QHorizontalPercentBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
- as groups, where bars in same category are grouped next to each other. QHorizontalPercentBarSeries groups the data
- from sets to categories, which are defined by a QStringList.
+ QHorizontalPercentBarSeries represents a series of data shown as bars. The purpose of this
+ class is to draw bars as groups, where bars in same category are grouped next to each other.
+ QHorizontalPercentBarSeries groups the data from sets to categories, which are defined by a
+ QStringList. Bars with zero value are not drawn.
- See the \l {HorizontalPercentBarChart Example} {horizontal percent bar chart example} to learn how to create a horizontal percent bar chart.
+ See the \l {HorizontalPercentBarChart Example} {horizontal percent bar chart example} to learn
+ how to create a horizontal percent bar chart.
\image examples_horizontalpercentbarchart.png
- \sa QBarSet, QBarSeries, QPercentBarSeries, QAbstractBarSeries, QStackedBarSeries, QHorizontalStackedBarSeries, QHorizontalBarSeries
+ \sa QBarSet, QBarSeries, QPercentBarSeries, QAbstractBarSeries, QStackedBarSeries,
+ QHorizontalStackedBarSeries, QHorizontalBarSeries
*/
#ifdef QDOC_QT5
/*!
diff --git a/src/barchart/vertical/percent/percentbarchartitem.cpp b/src/barchart/vertical/percent/percentbarchartitem.cpp
index 1c595e9d..b7ec6d5f 100644
--- a/src/barchart/vertical/percent/percentbarchartitem.cpp
+++ b/src/barchart/vertical/percent/percentbarchartitem.cpp
@@ -80,17 +80,24 @@ QVector<QRectF> PercentBarChartItem::calculateLayout()
for (int set = 0; set < setCount; set++) {
qreal value = m_series->barSets().at(set)->at(category);
QRectF rect;
- QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, 100 * (value + sum)/categorySum), m_validData);
+ qreal topY = 0;
+ qreal newSum = value + sum;
+ if (newSum > 0)
+ topY = 100 * newSum / categorySum;
+ qreal bottomY = 0;
+ if (sum > 0)
+ bottomY = 100 * sum / categorySum;
+ QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, topY), m_validData);
QPointF bottomRight;
if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
- bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : domain()->minY()), m_validData);
+ bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? bottomY : domain()->minY()), m_validData);
else
- bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : 0), m_validData);
+ bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? bottomY : 0), m_validData);
rect.setTopLeft(topLeft);
rect.setBottomRight(bottomRight);
layout.append(rect.normalized());
- sum +=value;
+ sum = newSum;
}
}
return layout;
diff --git a/src/barchart/vertical/percent/qpercentbarseries.cpp b/src/barchart/vertical/percent/qpercentbarseries.cpp
index e758003c..2fde3658 100644
--- a/src/barchart/vertical/percent/qpercentbarseries.cpp
+++ b/src/barchart/vertical/percent/qpercentbarseries.cpp
@@ -33,11 +33,13 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE
\brief Series for creating percent bar chart.
\mainclass
- QPercentBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
- as stacks, where each bar is shown as percentage of all bars in that category.
+ QPercentBarSeries represents a series of data shown as bars. The purpose of this class is to
+ draw bars as stacks, where each bar is shown as percentage of all bars in that category.
QPercentBarSeries groups the data from sets to categories, which are defined by a QStringList.
+ Bars with zero value are not drawn.
- See the \l {PercentbarChart Example} {percent bar chart example} to learn how to create a percent bar chart.
+ See the \l {PercentbarChart Example} {percent bar chart example} to learn how to create a
+ percent bar chart.
\image examples_percentbarchart.png
\sa QBarSet, QStackedBarSeries, QAbstractBarSeries
diff --git a/tests/auto/qhorizontalpercentbarseries/tst_qhorizontalpercentbarseries.cpp b/tests/auto/qhorizontalpercentbarseries/tst_qhorizontalpercentbarseries.cpp
index 7ff2181a..e03f0d0e 100644
--- a/tests/auto/qhorizontalpercentbarseries/tst_qhorizontalpercentbarseries.cpp
+++ b/tests/auto/qhorizontalpercentbarseries/tst_qhorizontalpercentbarseries.cpp
@@ -51,6 +51,7 @@ private slots:
void mouseclicked();
void mousehovered_data();
void mousehovered();
+ void zeroValuesInSeries();
private:
QHorizontalPercentBarSeries* m_barseries;
@@ -653,6 +654,25 @@ void tst_QHorizontalPercentBarSeries::mousehovered()
QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
}
+void tst_QHorizontalPercentBarSeries::zeroValuesInSeries()
+{
+ QHorizontalPercentBarSeries *series = new QHorizontalPercentBarSeries();
+ QBarSet *set1 = new QBarSet(QString("set 1"));
+ *set1 << 100 << 0.0 << 10;
+ series->append(set1);
+
+ QBarSet *set2 = new QBarSet(QString("set 2"));
+ *set2 << 0.0 << 0.0 << 70;
+ series->append(set2);
+
+ QChartView view(new QChart());
+ view.chart()->addSeries(series);
+ view.chart()->createDefaultAxes();
+ view.show();
+
+ QTest::qWaitForWindowShown(&view);
+}
+
QTEST_MAIN(tst_QHorizontalPercentBarSeries)
#include "tst_qhorizontalpercentbarseries.moc"
diff --git a/tests/auto/qpercentbarseries/tst_qpercentbarseries.cpp b/tests/auto/qpercentbarseries/tst_qpercentbarseries.cpp
index 2d0cef96..caf3eaba 100644
--- a/tests/auto/qpercentbarseries/tst_qpercentbarseries.cpp
+++ b/tests/auto/qpercentbarseries/tst_qpercentbarseries.cpp
@@ -51,6 +51,7 @@ private slots:
void mouseclicked();
void mousehovered_data();
void mousehovered();
+ void zeroValuesInSeries();
private:
QPercentBarSeries* m_barseries;
@@ -657,6 +658,25 @@ void tst_QPercentBarSeries::mousehovered()
QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
}
+void tst_QPercentBarSeries::zeroValuesInSeries()
+{
+ QPercentBarSeries *series = new QPercentBarSeries();
+ QBarSet *set1 = new QBarSet(QString("set 1"));
+ *set1 << 100 << 0.0 << 10;
+ series->append(set1);
+
+ QBarSet *set2 = new QBarSet(QString("set 2"));
+ *set2 << 0.0 << 0.0 << 70;
+ series->append(set2);
+
+ QChartView view(new QChart());
+ view.chart()->addSeries(series);
+ view.chart()->createDefaultAxes();
+ view.show();
+
+ QTest::qWaitForWindowShown(&view);
+}
+
QTEST_MAIN(tst_QPercentBarSeries)
#include "tst_qpercentbarseries.moc"