##// END OF EJS Templates
Added QChart::localizeNumbers...
Miikka Heikkinen -
r2707:ef2ab40bd1b9
parent child
Show More
@@ -175,6 +175,15 QTCOMMERCIALCHART_BEGIN_NAMESPACE
175 */
175 */
176
176
177 /*!
177 /*!
178 \qmlproperty bool ChartView::localizeNumbers
179 \since QtCharts 2.0
180 When \c{true}, all generated numbers appearing in various series and axis labels will be
181 localized using the default QLocale of the application, which defaults to the system locale.
182 When \c{false}, the "C" locale is always used.
183 Defaults to \c{false}.
184 */
185
186 /*!
178 \qmlmethod AbstractSeries ChartView::series(int index)
187 \qmlmethod AbstractSeries ChartView::series(int index)
179 Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with
188 Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with
180 the count property of the chart.
189 the count property of the chart.
@@ -710,6 +719,19 QColor QtCommercialChart::DeclarativeChart::plotAreaColor()
710 return m_chart->plotAreaBackgroundBrush().color();
719 return m_chart->plotAreaBackgroundBrush().color();
711 }
720 }
712
721
722 void DeclarativeChart::setLocalizeNumbers(bool localize)
723 {
724 if (m_chart->localizeNumbers() != localize) {
725 m_chart->setLocalizeNumbers(localize);
726 emit localizeNumbersChanged();
727 }
728 }
729
730 bool DeclarativeChart::localizeNumbers() const
731 {
732 return m_chart->localizeNumbers();
733 }
734
713 int DeclarativeChart::count()
735 int DeclarativeChart::count()
714 {
736 {
715 return m_chart->series().count();
737 return m_chart->series().count();
@@ -67,6 +67,7 class DeclarativeChart : public QDECLARATIVE_PAINTED_ITEM
67 #else
67 #else
68 Q_PROPERTY(QDeclarativeListProperty<QAbstractAxis> axes READ axes REVISION 2)
68 Q_PROPERTY(QDeclarativeListProperty<QAbstractAxis> axes READ axes REVISION 2)
69 #endif
69 #endif
70 Q_PROPERTY(bool localizeNumbers READ localizeNumbers WRITE setLocalizeNumbers NOTIFY localizeNumbersChanged REVISION 4)
70 Q_ENUMS(Animation)
71 Q_ENUMS(Animation)
71 Q_ENUMS(Theme)
72 Q_ENUMS(Theme)
72 Q_ENUMS(SeriesType)
73 Q_ENUMS(SeriesType)
@@ -142,6 +143,9 public:
142 QColor backgroundColor();
143 QColor backgroundColor();
143 Q_REVISION(3) void setPlotAreaColor(QColor color);
144 Q_REVISION(3) void setPlotAreaColor(QColor color);
144 Q_REVISION(3) QColor plotAreaColor();
145 Q_REVISION(3) QColor plotAreaColor();
146 Q_REVISION(4) void setLocalizeNumbers(bool localize);
147 Q_REVISION(4) bool localizeNumbers() const;
148
145 int count();
149 int count();
146 void setDropShadowEnabled(bool enabled);
150 void setDropShadowEnabled(bool enabled);
147 bool dropShadowEnabled();
151 bool dropShadowEnabled();
@@ -196,6 +200,7 Q_SIGNALS:
196 void seriesRemoved(QAbstractSeries *series);
200 void seriesRemoved(QAbstractSeries *series);
197 Q_REVISION(3) void plotAreaColorChanged();
201 Q_REVISION(3) void plotAreaColorChanged();
198 Q_REVISION(3) void backgroundRoundnessChanged(qreal diameter);
202 Q_REVISION(3) void backgroundRoundnessChanged(qreal diameter);
203 Q_REVISION(4) bool localizeNumbersChanged();
199
204
200 private Q_SLOTS:
205 private Q_SLOTS:
201 void changeMinimumMargins(int top, int bottom, int left, int right);
206 void changeMinimumMargins(int top, int bottom, int left, int right);
@@ -258,6 +258,7 public:
258 qmlRegisterType<QVBoxPlotModelMapper>(uri, 2, 0, "VBoxPlotModelMapper");
258 qmlRegisterType<QVBoxPlotModelMapper>(uri, 2, 0, "VBoxPlotModelMapper");
259 qmlRegisterUncreatableType<QBoxPlotModelMapper>(uri, 2, 0, "BoxPlotModelMapper",
259 qmlRegisterUncreatableType<QBoxPlotModelMapper>(uri, 2, 0, "BoxPlotModelMapper",
260 QLatin1String("Trying to create uncreatable: BoxPlotModelMapper."));
260 QLatin1String("Trying to create uncreatable: BoxPlotModelMapper."));
261 qmlRegisterType<DeclarativeChart, 4>(uri, 2, 0, "ChartView");
261 }
262 }
262 };
263 };
263
264
@@ -201,8 +201,10 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
201
201
202 if (m_series->upperSeries()) {
202 if (m_series->upperSeries()) {
203 for (int i(0); i < m_series->upperSeries()->count(); i++) {
203 for (int i(0); i < m_series->upperSeries()->count(); i++) {
204 pointLabel.replace(xPointTag, QString::number(m_series->upperSeries()->at(i).x()));
204 pointLabel.replace(xPointTag,
205 pointLabel.replace(yPointTag, QString::number(m_series->upperSeries()->at(i).y()));
205 presenter()->numberToString(m_series->upperSeries()->at(i).x()));
206 pointLabel.replace(yPointTag,
207 presenter()->numberToString(m_series->upperSeries()->at(i).y()));
206
208
207 // Position text in relation to the point
209 // Position text in relation to the point
208 int pointLabelWidth = fm.width(pointLabel);
210 int pointLabelWidth = fm.width(pointLabel);
@@ -216,8 +218,10 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
216
218
217 if (m_series->lowerSeries()) {
219 if (m_series->lowerSeries()) {
218 for (int i(0); i < m_series->lowerSeries()->count(); i++) {
220 for (int i(0); i < m_series->lowerSeries()->count(); i++) {
219 pointLabel.replace(xPointTag, QString::number(m_series->lowerSeries()->at(i).x()));
221 pointLabel.replace(xPointTag,
220 pointLabel.replace(yPointTag, QString::number(m_series->lowerSeries()->at(i).y()));
222 presenter()->numberToString(m_series->lowerSeries()->at(i).x()));
223 pointLabel.replace(yPointTag,
224 presenter()->numberToString(m_series->lowerSeries()->at(i).y()));
221
225
222 // Position text in relation to the point
226 // Position text in relation to the point
223 int pointLabelWidth = fm.width(pointLabel);
227 int pointLabelWidth = fm.width(pointLabel);
@@ -29,12 +29,17
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 static const char *labelFormatMatchString = "%[\\-\\+#\\s\\d\\.lhjztL]*([dicuoxfegXFEG])";
31 static const char *labelFormatMatchString = "%[\\-\\+#\\s\\d\\.lhjztL]*([dicuoxfegXFEG])";
32 static const char *labelFormatMatchLocalizedString = "^([^%]*)%\\.?(\\d)*([defgEG])(.*)$";
32 static QRegExp *labelFormatMatcher = 0;
33 static QRegExp *labelFormatMatcher = 0;
34 static QRegExp *labelFormatMatcherLocalized = 0;
33 class StaticLabelFormatMatcherDeleter
35 class StaticLabelFormatMatcherDeleter
34 {
36 {
35 public:
37 public:
36 StaticLabelFormatMatcherDeleter() {}
38 StaticLabelFormatMatcherDeleter() {}
37 ~StaticLabelFormatMatcherDeleter() { delete labelFormatMatcher; }
39 ~StaticLabelFormatMatcherDeleter() {
40 delete labelFormatMatcher;
41 delete labelFormatMatcherLocalized;
42 }
38 };
43 };
39 static StaticLabelFormatMatcherDeleter staticLabelFormatMatcherDeleter;
44 static StaticLabelFormatMatcherDeleter staticLabelFormatMatcherDeleter;
40
45
@@ -233,75 +238,96 qreal ChartAxisElement::max() const
233 return m_axis->d_ptr->max();
238 return m_axis->d_ptr->max();
234 }
239 }
235
240
236 static void appendFormattedLabel(const QString &capStr, const QByteArray &array,
241 QString ChartAxisElement::formatLabel(const QString &formatSpec, const QByteArray &array,
237 QStringList &labels, qreal value)
242 qreal value, int precision, const QString &preStr,
243 const QString &postStr) const
238 {
244 {
239 if (capStr.isEmpty()) {
245 QString retVal;
240 labels << QString();
246 if (!formatSpec.isEmpty()) {
241 } else if (capStr.at(0) == QLatin1Char('d')
247 if (formatSpec.at(0) == QLatin1Char('d')
242 || capStr.at(0) == QLatin1Char('i')
248 || formatSpec.at(0) == QLatin1Char('i')
243 || capStr.at(0) == QLatin1Char('c')) {
249 || formatSpec.at(0) == QLatin1Char('c')) {
244 labels << QString().sprintf(array, (qint64)value);
250 if (presenter()->localizeNumbers())
245 } else if (capStr.at(0) == QLatin1Char('u')
251 retVal = preStr + presenter()->locale().toString(qint64(value)) + postStr;
246 || capStr.at(0) == QLatin1Char('o')
252 else
247 || capStr.at(0) == QLatin1Char('x')
253 retVal = QString().sprintf(array, qint64(value));
248 || capStr.at(0) == QLatin1Char('X')) {
254 } else if (formatSpec.at(0) == QLatin1Char('u')
249 labels << QString().sprintf(array, (quint64)value);
255 || formatSpec.at(0) == QLatin1Char('o')
250 } else if (capStr.at(0) == QLatin1Char('f')
256 || formatSpec.at(0) == QLatin1Char('x')
251 || capStr.at(0) == QLatin1Char('F')
257 || formatSpec.at(0) == QLatin1Char('X')) {
252 || capStr.at(0) == QLatin1Char('e')
258 // These formats are not supported by localized numbers
253 || capStr.at(0) == QLatin1Char('E')
259 retVal = QString().sprintf(array, quint64(value));
254 || capStr.at(0) == QLatin1Char('g')
260 } else if (formatSpec.at(0) == QLatin1Char('f')
255 || capStr.at(0) == QLatin1Char('G')) {
261 || formatSpec.at(0) == QLatin1Char('F')
256 labels << QString().sprintf(array, value);
262 || formatSpec.at(0) == QLatin1Char('e')
257 } else {
263 || formatSpec.at(0) == QLatin1Char('E')
258 labels << QString();
264 || formatSpec.at(0) == QLatin1Char('g')
265 || formatSpec.at(0) == QLatin1Char('G')) {
266 if (presenter()->localizeNumbers()) {
267 retVal = preStr
268 + presenter()->locale().toString(value, formatSpec.at(0).toLatin1(),
269 precision)
270 + postStr;
271 } else {
272 retVal = QString().sprintf(array, value);
273 }
274 }
259 }
275 }
276 return retVal;
260 }
277 }
261
278
262 QStringList ChartAxisElement::createValueLabels(qreal min, qreal max, int ticks, const QString &format)
279 QStringList ChartAxisElement::createValueLabels(qreal min, qreal max, int ticks,
280 const QString &format) const
263 {
281 {
264 QStringList labels;
282 QStringList labels;
265
283
266 if (max <= min || ticks < 1)
284 if (max <= min || ticks < 1)
267 return labels;
285 return labels;
268
286
269 int n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
270 n++;
271
272 if (format.isNull()) {
287 if (format.isNull()) {
288 int n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0) + 1;
273 for (int i = 0; i < ticks; i++) {
289 for (int i = 0; i < ticks; i++) {
274 qreal value = min + (i * (max - min) / (ticks - 1));
290 qreal value = min + (i * (max - min) / (ticks - 1));
275 labels << QString::number(value, 'f', n);
291 labels << presenter()->numberToString(value, 'f', n);
276 }
292 }
277 } else {
293 } else {
278 QByteArray array = format.toLatin1();
294 QByteArray array = format.toLatin1();
279 QString capStr;
295 QString formatSpec;
280 if (!labelFormatMatcher)
296 QString preStr;
281 labelFormatMatcher = new QRegExp(labelFormatMatchString);
297 QString postStr;
282 if (labelFormatMatcher->indexIn(format, 0) != -1)
298 int precision = 0;
283 capStr = labelFormatMatcher->cap(1);
299 if (presenter()->localizeNumbers()) {
300 if (!labelFormatMatcherLocalized)
301 labelFormatMatcherLocalized = new QRegExp(labelFormatMatchLocalizedString);
302 if (labelFormatMatcherLocalized->indexIn(format, 0) != -1) {
303 preStr = labelFormatMatcherLocalized->cap(1);
304 precision = labelFormatMatcherLocalized->cap(2).toInt();
305 formatSpec = labelFormatMatcherLocalized->cap(3);
306 postStr = labelFormatMatcherLocalized->cap(4);
307 }
308 } else {
309 if (!labelFormatMatcher)
310 labelFormatMatcher = new QRegExp(labelFormatMatchString);
311 if (labelFormatMatcher->indexIn(format, 0) != -1)
312 formatSpec = labelFormatMatcher->cap(1);
313 }
284 for (int i = 0; i < ticks; i++) {
314 for (int i = 0; i < ticks; i++) {
285 qreal value = min + (i * (max - min) / (ticks - 1));
315 qreal value = min + (i * (max - min) / (ticks - 1));
286 appendFormattedLabel(capStr, array, labels, value);
316 labels << formatLabel(formatSpec, array, value, precision, preStr, postStr);
287 }
317 }
288 }
318 }
289
319
290 return labels;
320 return labels;
291 }
321 }
292
322
293 QStringList ChartAxisElement::createLogValueLabels(qreal min, qreal max, qreal base, int ticks, const QString &format)
323 QStringList ChartAxisElement::createLogValueLabels(qreal min, qreal max, qreal base, int ticks,
324 const QString &format) const
294 {
325 {
295 QStringList labels;
326 QStringList labels;
296
327
297 if (max <= min || ticks < 1)
328 if (max <= min || ticks < 1)
298 return labels;
329 return labels;
299
330
300 int n = 0;
301 if (ticks > 1)
302 n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
303 n++;
304
305 int firstTick;
331 int firstTick;
306 if (base > 1)
332 if (base > 1)
307 firstTick = ceil(log10(min) / log10(base));
333 firstTick = ceil(log10(min) / log10(base));
@@ -309,27 +335,46 QStringList ChartAxisElement::createLogValueLabels(qreal min, qreal max, qreal b
309 firstTick = ceil(log10(max) / log10(base));
335 firstTick = ceil(log10(max) / log10(base));
310
336
311 if (format.isNull()) {
337 if (format.isNull()) {
338 int n = 0;
339 if (ticks > 1)
340 n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
341 n++;
312 for (int i = firstTick; i < ticks + firstTick; i++) {
342 for (int i = firstTick; i < ticks + firstTick; i++) {
313 qreal value = qPow(base, i);
343 qreal value = qPow(base, i);
314 labels << QString::number(value, 'f', n);
344 labels << presenter()->numberToString(value, 'f', n);
315 }
345 }
316 } else {
346 } else {
317 QByteArray array = format.toLatin1();
347 QByteArray array = format.toLatin1();
318 QString capStr;
348 QString formatSpec;
319 if (!labelFormatMatcher)
349 QString preStr;
320 labelFormatMatcher = new QRegExp(labelFormatMatchString);
350 QString postStr;
321 if (labelFormatMatcher->indexIn(format, 0) != -1)
351 int precision = 0;
322 capStr = labelFormatMatcher->cap(1);
352 if (presenter()->localizeNumbers()) {
353 if (!labelFormatMatcherLocalized)
354 labelFormatMatcherLocalized = new QRegExp(labelFormatMatchLocalizedString);
355 if (labelFormatMatcherLocalized->indexIn(format, 0) != -1) {
356 preStr = labelFormatMatcherLocalized->cap(1);
357 precision = labelFormatMatcherLocalized->cap(2).toInt();
358 formatSpec = labelFormatMatcherLocalized->cap(3);
359 postStr = labelFormatMatcherLocalized->cap(4);
360 }
361 } else {
362 if (!labelFormatMatcher)
363 labelFormatMatcher = new QRegExp(labelFormatMatchString);
364 if (labelFormatMatcher->indexIn(format, 0) != -1)
365 formatSpec = labelFormatMatcher->cap(1);
366 }
323 for (int i = firstTick; i < ticks + firstTick; i++) {
367 for (int i = firstTick; i < ticks + firstTick; i++) {
324 qreal value = qPow(base, i);
368 qreal value = qPow(base, i);
325 appendFormattedLabel(capStr, array, labels, value);
369 labels << formatLabel(formatSpec, array, value, precision, preStr, postStr);
326 }
370 }
327 }
371 }
328
372
329 return labels;
373 return labels;
330 }
374 }
331
375
332 QStringList ChartAxisElement::createDateTimeLabels(qreal min, qreal max,int ticks,const QString &format)
376 QStringList ChartAxisElement::createDateTimeLabels(qreal min, qreal max,int ticks,
377 const QString &format) const
333 {
378 {
334 QStringList labels;
379 QStringList labels;
335
380
@@ -77,9 +77,10 public:
77 //this flag indicates that axis is used to show intervals it means labels are in between ticks
77 //this flag indicates that axis is used to show intervals it means labels are in between ticks
78 bool intervalAxis() const { return m_intervalAxis; }
78 bool intervalAxis() const { return m_intervalAxis; }
79
79
80 static QStringList createValueLabels(qreal max, qreal min, int ticks, const QString &format);
80 QStringList createValueLabels(qreal max, qreal min, int ticks, const QString &format) const;
81 static QStringList createLogValueLabels(qreal min, qreal max, qreal base, int ticks, const QString &format);
81 QStringList createLogValueLabels(qreal min, qreal max, qreal base, int ticks,
82 static QStringList createDateTimeLabels(qreal max, qreal min, int ticks, const QString &format);
82 const QString &format) const;
83 QStringList createDateTimeLabels(qreal max, qreal min, int ticks, const QString &format) const;
83
84
84 // from QGraphicsLayoutItem
85 // from QGraphicsLayoutItem
85 QRectF boundingRect() const
86 QRectF boundingRect() const
@@ -132,6 +133,9 Q_SIGNALS:
132
133
133 private:
134 private:
134 void connectSlots();
135 void connectSlots();
136 QString formatLabel(const QString &formatSpec, const QByteArray &array,
137 qreal value, int precision, const QString &preStr,
138 const QString &postStr) const;
135
139
136 QAbstractAxis *m_axis;
140 QAbstractAxis *m_axis;
137 AxisAnimation *m_animation;
141 AxisAnimation *m_animation;
@@ -106,14 +106,22 QTCOMMERCIALCHART_BEGIN_NAMESPACE
106 /*!
106 /*!
107 \property QValueAxis::labelFormat
107 \property QValueAxis::labelFormat
108 Defines the label format of the axis.
108 Defines the label format of the axis.
109 Supported specifiers are: d, i, o, x, X, f, F, e, E, g, G, c
109 Supported specifiers are: d, i, o, x, X, f, F, e, E, g, G, and c.
110 See QString::sprintf() for additional details.
110 See QString::sprintf() for additional details.
111
112 If the QChart::localizeNumbers is \c{true}, the supported specifiers are limited to: d, e, E, f,
113 g, and G. Also, only the precision modifier is supported. The rest of the formatting comes from
114 the default QLocale of the application.
111 */
115 */
112 /*!
116 /*!
113 \qmlproperty real ValueAxis::labelFormat
117 \qmlproperty real ValueAxis::labelFormat
114 Defines the label format of the axis.
118 Defines the label format of the axis.
115 Supported specifiers are: d, i, o, x, X, f, F, e, E, g, G, c
119 Supported specifiers are: d, i, o, x, X, f, F, e, E, g, G, and c.
116 See QString::sprintf() for additional details.
120 See QString::sprintf() for additional details.
121
122 If the ChartView::localizeNumbers is \c{true}, the supported specifiers are limited to: d, e, E, f,
123 g, and G. Also, only the precision modifier is supported. The rest of the formatting comes from
124 the default QLocale of the application.
117 */
125 */
118
126
119 /*!
127 /*!
@@ -217,11 +217,14 void AbstractBarChartItem::handleUpdatedBars()
217
217
218 QGraphicsTextItem *label = m_labels.at(itemIndex);
218 QGraphicsTextItem *label = m_labels.at(itemIndex);
219 QString valueLabel;
219 QString valueLabel;
220 if (m_series->labelsFormat().isEmpty()) {
220 if (presenter()) { // At startup presenter is not yet set, yet somehow update comes
221 valueLabel = QString("%1").arg(barSet->value(category));
221 if (m_series->labelsFormat().isEmpty()) {
222 } else {
222 valueLabel = presenter()->numberToString(barSet->value(category));
223 valueLabel = m_series->labelsFormat();
223 } else {
224 valueLabel.replace(valueTag, QString::number(barSet->value(category)));
224 valueLabel = m_series->labelsFormat();
225 valueLabel.replace(valueTag,
226 presenter()->numberToString(barSet->value(category)));
227 }
225 }
228 }
226 label->setHtml(valueLabel);
229 label->setHtml(valueLabel);
227 label->setFont(barSet->m_labelFont);
230 label->setFont(barSet->m_labelFont);
@@ -115,16 +115,15 void HorizontalPercentBarChartItem::handleUpdatedBars()
115 bar->update();
115 bar->update();
116
116
117 QGraphicsTextItem *label = m_labels.at(itemIndex);
117 QGraphicsTextItem *label = m_labels.at(itemIndex);
118 int p = m_series->d_func()->percentageAt(set, category) * 100;
118 qreal p = m_series->d_func()->percentageAt(set, category) * 100.0;
119 QString vString(QString::number(p));
119 QString vString(presenter()->numberToString(p, 'f', 0));
120 vString.truncate(3);
121 vString.append("%");
122 QString valueLabel;
120 QString valueLabel;
123 if (m_series->labelsFormat().isEmpty()) {
121 if (m_series->labelsFormat().isEmpty()) {
122 vString.append("%");
124 valueLabel = vString;
123 valueLabel = vString;
125 } else {
124 } else {
126 valueLabel = m_series->labelsFormat();
125 valueLabel = m_series->labelsFormat();
127 valueLabel.replace(valueTag, QString::number(barSet->value(category)));
126 valueLabel.replace(valueTag, vString);
128 }
127 }
129 label->setHtml(valueLabel);
128 label->setHtml(valueLabel);
130 label->setFont(barSet->m_labelFont);
129 label->setFont(barSet->m_labelFont);
@@ -120,16 +120,15 void PercentBarChartItem::handleUpdatedBars()
120 bar->update();
120 bar->update();
121
121
122 QGraphicsTextItem *label = m_labels.at(itemIndex);
122 QGraphicsTextItem *label = m_labels.at(itemIndex);
123 int p = m_series->d_func()->percentageAt(set, category) * 100;
123 qreal p = m_series->d_func()->percentageAt(set, category) * 100.0;
124 QString vString(QString::number(p));
124 QString vString(presenter()->numberToString(p, 'f', 0));
125 vString.truncate(3);
126 vString.append("%");
127 QString valueLabel;
125 QString valueLabel;
128 if (m_series->labelsFormat().isEmpty()) {
126 if (m_series->labelsFormat().isEmpty()) {
127 vString.append("%");
129 valueLabel = vString;
128 valueLabel = vString;
130 } else {
129 } else {
131 valueLabel = m_series->labelsFormat();
130 valueLabel = m_series->labelsFormat();
132 valueLabel.replace(valueTag, QString::number(barSet->value(category)));
131 valueLabel.replace(valueTag, vString);
133 }
132 }
134 label->setHtml(valueLabel);
133 label->setHtml(valueLabel);
135 label->setFont(barSet->m_labelFont);
134 label->setFont(barSet->m_labelFont);
@@ -44,7 +44,8 ChartPresenter::ChartPresenter(QChart *chart, QChart::ChartType type)
44 m_state(ShowState),
44 m_state(ShowState),
45 m_background(0),
45 m_background(0),
46 m_plotAreaBackground(0),
46 m_plotAreaBackground(0),
47 m_title(0)
47 m_title(0),
48 m_localizeNumbers(false)
48 {
49 {
49 if (type == QChart::ChartTypeCartesian)
50 if (type == QChart::ChartTypeCartesian)
50 m_layout = new CartesianChartLayout(this);
51 m_layout = new CartesianChartLayout(this);
@@ -102,6 +103,7 void ChartPresenter::handleSeriesAdded(QAbstractSeries *series)
102 {
103 {
103 series->d_ptr->initializeGraphics(rootItem());
104 series->d_ptr->initializeGraphics(rootItem());
104 series->d_ptr->initializeAnimations(m_options);
105 series->d_ptr->initializeAnimations(m_options);
106 series->d_ptr->setPresenter(this);
105 ChartItem *chart = series->d_ptr->chartItem();
107 ChartItem *chart = series->d_ptr->chartItem();
106 chart->setPresenter(this);
108 chart->setPresenter(this);
107 chart->setThemeManager(m_chart->d_ptr->m_themeManager);
109 chart->setThemeManager(m_chart->d_ptr->m_themeManager);
@@ -344,6 +346,17 bool ChartPresenter::isBackgroundDropShadowEnabled() const
344 return m_background->isDropShadowEnabled();
346 return m_background->isDropShadowEnabled();
345 }
347 }
346
348
349 void ChartPresenter::setLocalizeNumbers(bool localize)
350 {
351 m_localizeNumbers = localize;
352 m_layout->invalidate();
353 }
354
355 bool ChartPresenter::localizeNumbers() const
356 {
357 return m_localizeNumbers;
358 }
359
347
360
348 AbstractChartLayout *ChartPresenter::layout()
361 AbstractChartLayout *ChartPresenter::layout()
349 {
362 {
@@ -475,6 +488,14 QString ChartPresenter::truncatedText(const QFont &font, const QString &text, qr
475 return truncatedString;
488 return truncatedString;
476 }
489 }
477
490
491 QString ChartPresenter::numberToString(double value, char f, int prec)
492 {
493 if (m_localizeNumbers)
494 return m_locale.toString(value, f, prec);
495 else
496 return QString::number(value, f, prec);
497 }
498
478 #include "moc_chartpresenter_p.cpp"
499 #include "moc_chartpresenter_p.cpp"
479
500
480 QTCOMMERCIALCHART_END_NAMESPACE
501 QTCOMMERCIALCHART_END_NAMESPACE
@@ -34,6 +34,7
34 #include "qchart.h" //because of QChart::ChartThemeId
34 #include "qchart.h" //because of QChart::ChartThemeId
35 #include <QRectF>
35 #include <QRectF>
36 #include <QMargins>
36 #include <QMargins>
37 #include <QLocale>
37
38
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
40
@@ -129,6 +130,9 public:
129 void setBackgroundDropShadowEnabled(bool enabled);
130 void setBackgroundDropShadowEnabled(bool enabled);
130 bool isBackgroundDropShadowEnabled() const;
131 bool isBackgroundDropShadowEnabled() const;
131
132
133 void setLocalizeNumbers(bool localize);
134 bool localizeNumbers() const;
135
132 void setVisible(bool visible);
136 void setVisible(bool visible);
133
137
134 void setAnimationOptions(QChart::AnimationOptions options);
138 void setAnimationOptions(QChart::AnimationOptions options);
@@ -148,6 +152,10 public:
148 static QString truncatedText(const QFont &font, const QString &text, qreal angle,
152 static QString truncatedText(const QFont &font, const QString &text, qreal angle,
149 qreal maxWidth, qreal maxHeight, QRectF &boundingRect);
153 qreal maxWidth, qreal maxHeight, QRectF &boundingRect);
150 inline static qreal textMargin() { return qreal(0.5); }
154 inline static qreal textMargin() { return qreal(0.5); }
155
156 QString numberToString(double value, char f = 'g', int prec = 6);
157 inline const QLocale &locale() const { return m_locale; }
158
151 private:
159 private:
152 void createBackgroundItem();
160 void createBackgroundItem();
153 void createPlotAreaBackgroundItem();
161 void createPlotAreaBackgroundItem();
@@ -173,6 +181,8 private:
173 QAbstractGraphicsShapeItem *m_plotAreaBackground;
181 QAbstractGraphicsShapeItem *m_plotAreaBackground;
174 ChartTitle *m_title;
182 ChartTitle *m_title;
175 QRectF m_rect;
183 QRectF m_rect;
184 bool m_localizeNumbers;
185 QLocale m_locale;
176 };
186 };
177
187
178 QTCOMMERCIALCHART_END_NAMESPACE
188 QTCOMMERCIALCHART_END_NAMESPACE
@@ -303,6 +303,16 void QAbstractSeriesPrivate::setDomain(AbstractDomain* domain)
303 }
303 }
304 }
304 }
305
305
306 void QAbstractSeriesPrivate::setPresenter(ChartPresenter *presenter)
307 {
308 m_presenter = presenter;
309 }
310
311 ChartPresenter *QAbstractSeriesPrivate::presenter() const
312 {
313 return m_presenter;
314 }
315
306 void QAbstractSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
316 void QAbstractSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
307 {
317 {
308 Q_ASSERT(!m_item.isNull());
318 Q_ASSERT(!m_item.isNull());
@@ -73,6 +73,9 public:
73 virtual void setDomain(AbstractDomain* domain);
73 virtual void setDomain(AbstractDomain* domain);
74 AbstractDomain* domain() { return m_domain.data(); }
74 AbstractDomain* domain() { return m_domain.data(); }
75
75
76 virtual void setPresenter(ChartPresenter *presenter);
77 ChartPresenter *presenter() const;
78
76 QChart* chart() { return m_chart; }
79 QChart* chart() { return m_chart; }
77
80
78 Q_SIGNALS:
81 Q_SIGNALS:
@@ -88,6 +91,7 private:
88 QString m_name;
91 QString m_name;
89 bool m_visible;
92 bool m_visible;
90 qreal m_opacity;
93 qreal m_opacity;
94 ChartPresenter *m_presenter;
91
95
92 friend class QAbstractSeries;
96 friend class QAbstractSeries;
93 friend class ChartDataSet;
97 friend class ChartDataSet;
@@ -145,6 +145,15 QTCOMMERCIALCHART_BEGIN_NAMESPACE
145 */
145 */
146
146
147 /*!
147 /*!
148 \property QChart::localizeNumbers
149 \since QtCharts 2.0
150 When \c{true}, all generated numbers appearing in various series and axis labels will be
151 localized using the default QLocale of the application, which defaults to the system locale.
152 When \c{false}, the "C" locale is always used.
153 Defaults to \c{false}.
154 */
155
156 /*!
148 \internal
157 \internal
149 Constructs a chart object of \a type which is a child of a \a parent.
158 Constructs a chart object of \a type which is a child of a \a parent.
150 Parameter \a wFlags is passed to the QGraphicsWidget constructor.
159 Parameter \a wFlags is passed to the QGraphicsWidget constructor.
@@ -561,6 +570,16 bool QChart::isPlotAreaBackgroundVisible() const
561 return d_ptr->m_presenter->isPlotAreaBackgroundVisible();
570 return d_ptr->m_presenter->isPlotAreaBackgroundVisible();
562 }
571 }
563
572
573 void QChart::setLocalizeNumbers(bool localize)
574 {
575 d_ptr->m_presenter->setLocalizeNumbers(localize);
576 }
577
578 bool QChart::localizeNumbers() const
579 {
580 return d_ptr->m_presenter->localizeNumbers();
581 }
582
564 void QChart::setAnimationOptions(AnimationOptions options)
583 void QChart::setAnimationOptions(AnimationOptions options)
565 {
584 {
566 d_ptr->m_presenter->setAnimationOptions(options);
585 d_ptr->m_presenter->setAnimationOptions(options);
@@ -49,6 +49,7 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
49 Q_PROPERTY(QMargins margins READ margins WRITE setMargins)
49 Q_PROPERTY(QMargins margins READ margins WRITE setMargins)
50 Q_PROPERTY(QChart::ChartType chartType READ chartType)
50 Q_PROPERTY(QChart::ChartType chartType READ chartType)
51 Q_PROPERTY(bool plotAreaBackgroundVisible READ isPlotAreaBackgroundVisible WRITE setPlotAreaBackgroundVisible)
51 Q_PROPERTY(bool plotAreaBackgroundVisible READ isPlotAreaBackgroundVisible WRITE setPlotAreaBackgroundVisible)
52 Q_PROPERTY(bool localizeNumbers READ localizeNumbers WRITE setLocalizeNumbers)
52 Q_ENUMS(ChartTheme)
53 Q_ENUMS(ChartTheme)
53 Q_ENUMS(AnimationOption)
54 Q_ENUMS(AnimationOption)
54 Q_ENUMS(ChartType)
55 Q_ENUMS(ChartType)
@@ -151,6 +152,8 public:
151 QPen plotAreaBackgroundPen() const;
152 QPen plotAreaBackgroundPen() const;
152 void setPlotAreaBackgroundVisible(bool visible = true);
153 void setPlotAreaBackgroundVisible(bool visible = true);
153 bool isPlotAreaBackgroundVisible() const;
154 bool isPlotAreaBackgroundVisible() const;
155 void setLocalizeNumbers(bool localize);
156 bool localizeNumbers() const;
154
157
155 QPointF mapToValue(const QPointF &position, QAbstractSeries *series = 0);
158 QPointF mapToValue(const QPointF &position, QAbstractSeries *series = 0);
156 QPointF mapToPosition(const QPointF &value, QAbstractSeries *series = 0);
159 QPointF mapToPosition(const QPointF &value, QAbstractSeries *series = 0);
@@ -785,8 +785,8 void QXYSeriesPrivate::drawSeriesPointLabels(QPainter *painter, const QVector<QP
785
785
786 for (int i(0); i < m_points.size(); i++) {
786 for (int i(0); i < m_points.size(); i++) {
787 QString pointLabel = m_pointLabelsFormat;
787 QString pointLabel = m_pointLabelsFormat;
788 pointLabel.replace(xPointTag, QString::number(m_points.at(i).x()));
788 pointLabel.replace(xPointTag, presenter()->numberToString(m_points.at(i).x()));
789 pointLabel.replace(yPointTag, QString::number(m_points.at(i).y()));
789 pointLabel.replace(yPointTag, presenter()->numberToString(m_points.at(i).y()));
790
790
791 // Position text in relation to the point
791 // Position text in relation to the point
792 int pointLabelWidth = fm.width(pointLabel);
792 int pointLabelWidth = fm.width(pointLabel);
General Comments 0
You need to be logged in to leave comments. Login now