From 79dc9c599f412117d04cb1ba8c0c9af93bb0b9de 2015-09-21 07:57:13 From: Titta Heikkala Date: 2015-09-21 07:57:13 Subject: [PATCH] Fixed bar label visibility Label for bars with zero value is not shown. This is done to avoid overlapping labels. Change-Id: Ife30c04e8ae4d9ac9df1209967cc3cd3ecb76a4f Task-number: QTRD-3695 Reviewed-by: Miikka Heikkinen --- diff --git a/src/charts/barchart/abstractbarchartitem.cpp b/src/charts/barchart/abstractbarchartitem.cpp index ea0e9a0..6f8ecb7 100644 --- a/src/charts/barchart/abstractbarchartitem.cpp +++ b/src/charts/barchart/abstractbarchartitem.cpp @@ -225,12 +225,17 @@ void AbstractBarChartItem::handleUpdatedBars() QGraphicsTextItem *label = m_labels.at(itemIndex); QString valueLabel; if (presenter()) { // At startup presenter is not yet set, yet somehow update comes - if (m_series->labelsFormat().isEmpty()) { - valueLabel = presenter()->numberToString(barSet->value(category)); + if (barSet->value(category) == 0) { + label->setVisible(false); } else { - valueLabel = m_series->labelsFormat(); - valueLabel.replace(valueTag, + label->setVisible(true); + if (m_series->labelsFormat().isEmpty()) { + valueLabel = presenter()->numberToString(barSet->value(category)); + } else { + valueLabel = m_series->labelsFormat(); + valueLabel.replace(valueTag, presenter()->numberToString(barSet->value(category))); + } } } label->setHtml(valueLabel); diff --git a/src/charts/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp b/src/charts/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp index 6240086..62db806 100644 --- a/src/charts/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp +++ b/src/charts/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp @@ -116,12 +116,17 @@ void HorizontalPercentBarChartItem::handleUpdatedBars() qreal p = m_series->d_func()->percentageAt(set, category) * 100.0; QString vString(presenter()->numberToString(p, 'f', 0)); QString valueLabel; - if (m_series->labelsFormat().isEmpty()) { - vString.append(QStringLiteral("%")); - valueLabel = vString; + if (p == 0) { + label->setVisible(false); } else { - valueLabel = m_series->labelsFormat(); - valueLabel.replace(valueTag, vString); + label->setVisible(true); + if (m_series->labelsFormat().isEmpty()) { + vString.append(QStringLiteral("%")); + valueLabel = vString; + } else { + valueLabel = m_series->labelsFormat(); + valueLabel.replace(valueTag, vString); + } } label->setHtml(valueLabel); label->setFont(barSet->m_labelFont); diff --git a/src/charts/barchart/qbarset.cpp b/src/charts/barchart/qbarset.cpp index d6194b7..4eb1cdb 100644 --- a/src/charts/barchart/qbarset.cpp +++ b/src/charts/barchart/qbarset.cpp @@ -32,6 +32,7 @@ QT_CHARTS_BEGIN_NAMESPACE First value of set is assumed to belong to first category, second to second category and so on. If set has fewer values than there are categories, then the missing values are assumed to be at the end of set. For missing values in middle of a set, numerical value of zero is used. + Labels for zero value sets are not shown. \sa QAbstractBarSeries, QBarSeries, QStackedBarSeries, QPercentBarSeries */ @@ -46,6 +47,7 @@ QT_CHARTS_BEGIN_NAMESPACE First value of set is assumed to belong to first category, second to second category and so on. If set has fewer values than there are categories, then the missing values are assumed to be at the end of set. For missing values in middle of a set, numerical value of zero is used. + Labels for zero value sets are not shown. \sa AbstractBarSeries, BarSeries, StackedBarSeries, PercentBarSeries */ diff --git a/src/charts/barchart/vertical/percent/percentbarchartitem.cpp b/src/charts/barchart/vertical/percent/percentbarchartitem.cpp index fbde59a..6a998df 100644 --- a/src/charts/barchart/vertical/percent/percentbarchartitem.cpp +++ b/src/charts/barchart/vertical/percent/percentbarchartitem.cpp @@ -121,12 +121,17 @@ void PercentBarChartItem::handleUpdatedBars() qreal p = m_series->d_func()->percentageAt(set, category) * 100.0; QString vString(presenter()->numberToString(p, 'f', 0)); QString valueLabel; - if (m_series->labelsFormat().isEmpty()) { - vString.append(QStringLiteral("%")); - valueLabel = vString; + if (p == 0) { + label->setVisible(false); } else { - valueLabel = m_series->labelsFormat(); - valueLabel.replace(valueTag, vString); + label->setVisible(true); + if (m_series->labelsFormat().isEmpty()) { + vString.append(QStringLiteral("%")); + valueLabel = vString; + } else { + valueLabel = m_series->labelsFormat(); + valueLabel.replace(valueTag, vString); + } } label->setHtml(valueLabel); label->setFont(barSet->m_labelFont);