summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKari Oikarinen <kari.oikarinen@qt.io>2017-12-21 09:35:19 +0200
committerKari Oikarinen <kari.oikarinen@qt.io>2017-12-21 11:21:35 +0000
commit146e375210f406e62fd952d150bc1bcd926d60cb (patch)
tree291b363d277d8d57c0450cace788ea97e8431818 /src
parent6b3a1a975e1f0237e78c3d329411c15f00577b01 (diff)
Add labelsPrecision property to bar charts
Allow choosing the number of significant digits in the value labels for the bars. Before this this has been hardcoded to 6, so the default value stays as 6 for backward compatibility. Task-number: QTBUG-64370 Change-Id: I022bdad21aca7a7750ed46181b4ec97726bd9dc5 Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/charts/barchart/abstractbarchartitem.cpp10
-rw-r--r--src/charts/barchart/qabstractbarseries.cpp36
-rw-r--r--src/charts/barchart/qabstractbarseries.h5
-rw-r--r--src/charts/barchart/qabstractbarseries_p.h1
4 files changed, 49 insertions, 3 deletions
diff --git a/src/charts/barchart/abstractbarchartitem.cpp b/src/charts/barchart/abstractbarchartitem.cpp
index 910066b5..86bfbf8d 100644
--- a/src/charts/barchart/abstractbarchartitem.cpp
+++ b/src/charts/barchart/abstractbarchartitem.cpp
@@ -75,6 +75,10 @@ AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphics
connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
this, SLOT(handleLabelsPositionChanged()));
connect(series, SIGNAL(labelsAngleChanged(qreal)), this, SLOT(positionLabels()));
+ connect(series, &QAbstractBarSeries::labelsPrecisionChanged,
+ this, &AbstractBarChartItem::handleUpdatedBars);
+ connect(series, &QAbstractBarSeries::labelsPrecisionChanged,
+ this, &AbstractBarChartItem::positionLabels);
connect(series->chart()->d_ptr->m_dataset, &ChartDataSet::seriesAdded,
this, &AbstractBarChartItem::handleSeriesAdded);
connect(series->chart()->d_ptr->m_dataset, &ChartDataSet::seriesRemoved,
@@ -543,13 +547,15 @@ QString AbstractBarChartItem::generateLabelText(int set, int category, qreal val
Q_UNUSED(set);
Q_UNUSED(category);
static const QString valueTag(QLatin1String("@value"));
+ QString valueString = presenter()->numberToString(value, 'g', m_series->labelsPrecision());
QString valueLabel;
if (m_series->labelsFormat().isEmpty()) {
- valueLabel = presenter()->numberToString(value);
+ valueLabel = valueString;
} else {
valueLabel = m_series->labelsFormat();
- valueLabel.replace(valueTag, presenter()->numberToString(value));
+ valueLabel.replace(valueTag, valueString);
}
+
return valueLabel;
}
diff --git a/src/charts/barchart/qabstractbarseries.cpp b/src/charts/barchart/qabstractbarseries.cpp
index cd27cd1c..410087f2 100644
--- a/src/charts/barchart/qabstractbarseries.cpp
+++ b/src/charts/barchart/qabstractbarseries.cpp
@@ -185,7 +185,7 @@ QT_CHARTS_BEGIN_NAMESPACE
after the value. The labels are shown on the plot area, whereas labels on the edge of the plot
area are cut. If the bars are close to each other, the labels may overlap.
- \sa labelsVisible, labelsPosition
+ \sa labelsVisible, labelsPosition, labelsPrecision
*/
/*!
\qmlproperty string AbstractBarSeries::labelsFormat
@@ -250,6 +250,23 @@ QT_CHARTS_BEGIN_NAMESPACE
*/
/*!
+ \property QAbstractBarSeries::labelsPrecision
+ \brief The maximum amount of significant digits shown in value labels.
+
+ Default value is 6.
+*/
+/*!
+ \qmlproperty real AbstractBarSeries::labelsPrecision
+ The maximum amount of significant digits shown in value labels.
+
+ Default value is 6.
+*/
+/*!
+ \fn void QAbstractBarSeries::labelsPrecisionChanged(int precision)
+ This signal is emitted when the \a precision of the value labels changes.
+*/
+
+/*!
\fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
This signal is emitted when the user clicks the bar specified by \a index
in the bar set specified by \a barset.
@@ -629,6 +646,22 @@ QAbstractBarSeries::LabelsPosition QAbstractBarSeries::labelsPosition() const
return d->m_labelsPosition;
}
+void QAbstractBarSeries::setLabelsPrecision(int precision)
+{
+ Q_D(QAbstractBarSeries);
+ if (d->m_labelsPrecision != precision) {
+ d->m_labelsPrecision = precision;
+ d->setLabelsDirty(true);
+ emit labelsPrecisionChanged(precision);
+ }
+}
+
+int QAbstractBarSeries::labelsPrecision() const
+{
+ Q_D(const QAbstractBarSeries);
+ return d->m_labelsPrecision;
+}
+
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
@@ -640,6 +673,7 @@ QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
m_labelsFormat(),
m_labelsPosition(QAbstractBarSeries::LabelsCenter),
m_labelsAngle(0),
+ m_labelsPrecision(6),
m_visualsDirty(true),
m_labelsDirty(true)
{
diff --git a/src/charts/barchart/qabstractbarseries.h b/src/charts/barchart/qabstractbarseries.h
index 363c7bfe..82000d34 100644
--- a/src/charts/barchart/qabstractbarseries.h
+++ b/src/charts/barchart/qabstractbarseries.h
@@ -48,6 +48,7 @@ class QT_CHARTS_EXPORT QAbstractBarSeries : public QAbstractSeries
Q_PROPERTY(QString labelsFormat READ labelsFormat WRITE setLabelsFormat NOTIFY labelsFormatChanged)
Q_PROPERTY(LabelsPosition labelsPosition READ labelsPosition WRITE setLabelsPosition NOTIFY labelsPositionChanged)
Q_PROPERTY(qreal labelsAngle READ labelsAngle WRITE setLabelsAngle NOTIFY labelsAngleChanged)
+ Q_PROPERTY(int labelsPrecision READ labelsPrecision WRITE setLabelsPrecision NOTIFY labelsPrecisionChanged)
Q_ENUMS(LabelsPosition)
public:
@@ -85,6 +86,9 @@ public:
void setLabelsPosition(QAbstractBarSeries::LabelsPosition position);
QAbstractBarSeries::LabelsPosition labelsPosition() const;
+ void setLabelsPrecision(int precision);
+ int labelsPrecision() const;
+
protected:
explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent = nullptr);
@@ -99,6 +103,7 @@ Q_SIGNALS:
void labelsFormatChanged(const QString &format);
void labelsPositionChanged(QAbstractBarSeries::LabelsPosition position);
void labelsAngleChanged(qreal angle);
+ void labelsPrecisionChanged(int precision);
void barsetsAdded(QList<QBarSet *> sets);
void barsetsRemoved(QList<QBarSet *> sets);
diff --git a/src/charts/barchart/qabstractbarseries_p.h b/src/charts/barchart/qabstractbarseries_p.h
index 62ddb420..9b3ce6e1 100644
--- a/src/charts/barchart/qabstractbarseries_p.h
+++ b/src/charts/barchart/qabstractbarseries_p.h
@@ -133,6 +133,7 @@ protected:
QString m_labelsFormat;
QAbstractBarSeries::LabelsPosition m_labelsPosition;
qreal m_labelsAngle;
+ int m_labelsPrecision;
bool m_visualsDirty;
bool m_labelsDirty;