diff --git a/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp b/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp index 775bfba..3508366 100644 --- a/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp +++ b/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp @@ -75,17 +75,24 @@ QVector 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 83f1045..846f77d 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 1c595e9..b7ec6d5 100644 --- a/src/barchart/vertical/percent/percentbarchartitem.cpp +++ b/src/barchart/vertical/percent/percentbarchartitem.cpp @@ -80,17 +80,24 @@ QVector 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 e758003..2fde365 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 7ff2181..e03f0d0 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 2d0cef9..caf3eab 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"