##// END OF EJS Templates
Added plotAreaChanged signal to chart....
Miikka Heikkinen -
r2716:bf30bed1cefb
parent child
Show More
@@ -1,508 +1,509
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20 #include <private/chartpresenter_p.h>
21 21 #include <QtCharts/QChart>
22 22 #include <private/chartitem_p.h>
23 23 #include <private/qchart_p.h>
24 24 #include <QtCharts/QAbstractAxis>
25 25 #include <private/qabstractaxis_p.h>
26 26 #include <private/chartdataset_p.h>
27 27 #include <private/chartanimation_p.h>
28 28 #include <private/qabstractseries_p.h>
29 29 #include <QtCharts/QAreaSeries>
30 30 #include <private/chartaxiselement_p.h>
31 31 #include <private/chartbackground_p.h>
32 32 #include <private/cartesianchartlayout_p.h>
33 33 #include <private/polarchartlayout_p.h>
34 34 #include <private/charttitle_p.h>
35 35 #include <QtCore/QTimer>
36 36 #include <QtGui/QTextDocument>
37 37
38 38 QT_CHARTS_BEGIN_NAMESPACE
39 39
40 40 ChartPresenter::ChartPresenter(QChart *chart, QChart::ChartType type)
41 41 : QObject(chart),
42 42 m_chart(chart),
43 43 m_options(QChart::NoAnimation),
44 44 m_state(ShowState),
45 45 m_background(0),
46 46 m_plotAreaBackground(0),
47 47 m_title(0),
48 48 m_localizeNumbers(false)
49 49 {
50 50 if (type == QChart::ChartTypeCartesian)
51 51 m_layout = new CartesianChartLayout(this);
52 52 else if (type == QChart::ChartTypePolar)
53 53 m_layout = new PolarChartLayout(this);
54 54 Q_ASSERT(m_layout);
55 55 }
56 56
57 57 ChartPresenter::~ChartPresenter()
58 58 {
59 59
60 60 }
61 61
62 62 void ChartPresenter::setGeometry(const QRectF rect)
63 63 {
64 64 if (m_rect != rect) {
65 65 m_rect = rect;
66 66 foreach (ChartItem *chart, m_chartItems) {
67 67 chart->domain()->setSize(rect.size());
68 68 chart->setPos(rect.topLeft());
69 69 }
70 emit plotAreaChanged(m_rect);
70 71 }
71 72 }
72 73
73 74 QRectF ChartPresenter::geometry() const
74 75 {
75 76 return m_rect;
76 77 }
77 78
78 79 void ChartPresenter::handleAxisAdded(QAbstractAxis *axis)
79 80 {
80 81 axis->d_ptr->initializeGraphics(rootItem());
81 82 axis->d_ptr->initializeAnimations(m_options);
82 83 ChartAxisElement *item = axis->d_ptr->axisItem();
83 84 item->setPresenter(this);
84 85 item->setThemeManager(m_chart->d_ptr->m_themeManager);
85 86 m_axisItems<<item;
86 87 m_axes<<axis;
87 88 m_layout->invalidate();
88 89 }
89 90
90 91 void ChartPresenter::handleAxisRemoved(QAbstractAxis *axis)
91 92 {
92 93 ChartAxisElement *item = axis->d_ptr->m_item.take();
93 94 item->hide();
94 95 item->disconnect();
95 96 item->deleteLater();
96 97 m_axisItems.removeAll(item);
97 98 m_axes.removeAll(axis);
98 99 m_layout->invalidate();
99 100 }
100 101
101 102
102 103 void ChartPresenter::handleSeriesAdded(QAbstractSeries *series)
103 104 {
104 105 series->d_ptr->initializeGraphics(rootItem());
105 106 series->d_ptr->initializeAnimations(m_options);
106 107 series->d_ptr->setPresenter(this);
107 108 ChartItem *chart = series->d_ptr->chartItem();
108 109 chart->setPresenter(this);
109 110 chart->setThemeManager(m_chart->d_ptr->m_themeManager);
110 111 chart->domain()->setSize(m_rect.size());
111 112 chart->setPos(m_rect.topLeft());
112 113 chart->handleDomainUpdated(); //this could be moved to intializeGraphics when animator is refactored
113 114 m_chartItems<<chart;
114 115 m_series<<series;
115 116 m_layout->invalidate();
116 117 }
117 118
118 119 void ChartPresenter::handleSeriesRemoved(QAbstractSeries *series)
119 120 {
120 121 ChartItem *chart = series->d_ptr->m_item.take();
121 122 chart->hide();
122 123 chart->disconnect();
123 124 chart->deleteLater();
124 125 m_chartItems.removeAll(chart);
125 126 m_series.removeAll(series);
126 127 m_layout->invalidate();
127 128 }
128 129
129 130 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
130 131 {
131 132 if (m_options != options) {
132 133 QChart::AnimationOptions oldOptions = m_options;
133 134 m_options = options;
134 135 if (options.testFlag(QChart::SeriesAnimations) != oldOptions.testFlag(QChart::SeriesAnimations)) {
135 136 foreach (QAbstractSeries *series, m_series)
136 137 series->d_ptr->initializeAnimations(m_options);
137 138 }
138 139 if (options.testFlag(QChart::GridAxisAnimations) != oldOptions.testFlag(QChart::GridAxisAnimations)) {
139 140 foreach (QAbstractAxis *axis, m_axes)
140 141 axis->d_ptr->initializeAnimations(m_options);
141 142 }
142 143 m_layout->invalidate(); // So that existing animations don't just stop halfway
143 144 }
144 145 }
145 146
146 147 void ChartPresenter::setState(State state,QPointF point)
147 148 {
148 149 m_state=state;
149 150 m_statePoint=point;
150 151 }
151 152
152 153 QChart::AnimationOptions ChartPresenter::animationOptions() const
153 154 {
154 155 return m_options;
155 156 }
156 157
157 158 void ChartPresenter::createBackgroundItem()
158 159 {
159 160 if (!m_background) {
160 161 m_background = new ChartBackground(rootItem());
161 162 m_background->setPen(Qt::NoPen); // Theme doesn't touch pen so don't use default
162 163 m_background->setBrush(QChartPrivate::defaultBrush());
163 164 m_background->setZValue(ChartPresenter::BackgroundZValue);
164 165 }
165 166 }
166 167
167 168 void ChartPresenter::createPlotAreaBackgroundItem()
168 169 {
169 170 if (!m_plotAreaBackground) {
170 171 if (m_chart->chartType() == QChart::ChartTypeCartesian)
171 172 m_plotAreaBackground = new QGraphicsRectItem(rootItem());
172 173 else
173 174 m_plotAreaBackground = new QGraphicsEllipseItem(rootItem());
174 175 // Use transparent pen instead of Qt::NoPen, as Qt::NoPen causes
175 176 // antialising artifacts with axis lines for some reason.
176 177 m_plotAreaBackground->setPen(QPen(Qt::transparent));
177 178 m_plotAreaBackground->setBrush(Qt::NoBrush);
178 179 m_plotAreaBackground->setZValue(ChartPresenter::PlotAreaZValue);
179 180 m_plotAreaBackground->setVisible(false);
180 181 }
181 182 }
182 183
183 184 void ChartPresenter::createTitleItem()
184 185 {
185 186 if (!m_title) {
186 187 m_title = new ChartTitle(rootItem());
187 188 m_title->setZValue(ChartPresenter::BackgroundZValue);
188 189 }
189 190 }
190 191
191 192 void ChartPresenter::startAnimation(ChartAnimation *animation)
192 193 {
193 194 animation->stop();
194 195 QTimer::singleShot(0, animation, SLOT(startChartAnimation()));
195 196 }
196 197
197 198 void ChartPresenter::setBackgroundBrush(const QBrush &brush)
198 199 {
199 200 createBackgroundItem();
200 201 m_background->setBrush(brush);
201 202 m_layout->invalidate();
202 203 }
203 204
204 205 QBrush ChartPresenter::backgroundBrush() const
205 206 {
206 207 if (!m_background)
207 208 return QBrush();
208 209 return m_background->brush();
209 210 }
210 211
211 212 void ChartPresenter::setBackgroundPen(const QPen &pen)
212 213 {
213 214 createBackgroundItem();
214 215 m_background->setPen(pen);
215 216 m_layout->invalidate();
216 217 }
217 218
218 219 QPen ChartPresenter::backgroundPen() const
219 220 {
220 221 if (!m_background)
221 222 return QPen();
222 223 return m_background->pen();
223 224 }
224 225
225 226 void ChartPresenter::setBackgroundRoundness(qreal diameter)
226 227 {
227 228 createBackgroundItem();
228 229 m_background->setDiameter(diameter);
229 230 m_layout->invalidate();
230 231 }
231 232
232 233 qreal ChartPresenter::backgroundRoundness() const
233 234 {
234 235 if (!m_background)
235 236 return 0;
236 237 return m_background->diameter();
237 238 }
238 239
239 240 void ChartPresenter::setPlotAreaBackgroundBrush(const QBrush &brush)
240 241 {
241 242 createPlotAreaBackgroundItem();
242 243 m_plotAreaBackground->setBrush(brush);
243 244 m_layout->invalidate();
244 245 }
245 246
246 247 QBrush ChartPresenter::plotAreaBackgroundBrush() const
247 248 {
248 249 if (!m_plotAreaBackground)
249 250 return QBrush();
250 251 return m_plotAreaBackground->brush();
251 252 }
252 253
253 254 void ChartPresenter::setPlotAreaBackgroundPen(const QPen &pen)
254 255 {
255 256 createPlotAreaBackgroundItem();
256 257 m_plotAreaBackground->setPen(pen);
257 258 m_layout->invalidate();
258 259 }
259 260
260 261 QPen ChartPresenter::plotAreaBackgroundPen() const
261 262 {
262 263 if (!m_plotAreaBackground)
263 264 return QPen();
264 265 return m_plotAreaBackground->pen();
265 266 }
266 267
267 268 void ChartPresenter::setTitle(const QString &title)
268 269 {
269 270 createTitleItem();
270 271 m_title->setText(title);
271 272 m_layout->invalidate();
272 273 }
273 274
274 275 QString ChartPresenter::title() const
275 276 {
276 277 if (!m_title)
277 278 return QString();
278 279 return m_title->text();
279 280 }
280 281
281 282 void ChartPresenter::setTitleFont(const QFont &font)
282 283 {
283 284 createTitleItem();
284 285 m_title->setFont(font);
285 286 m_layout->invalidate();
286 287 }
287 288
288 289 QFont ChartPresenter::titleFont() const
289 290 {
290 291 if (!m_title)
291 292 return QFont();
292 293 return m_title->font();
293 294 }
294 295
295 296 void ChartPresenter::setTitleBrush(const QBrush &brush)
296 297 {
297 298 createTitleItem();
298 299 m_title->setDefaultTextColor(brush.color());
299 300 m_layout->invalidate();
300 301 }
301 302
302 303 QBrush ChartPresenter::titleBrush() const
303 304 {
304 305 if (!m_title)
305 306 return QBrush();
306 307 return QBrush(m_title->defaultTextColor());
307 308 }
308 309
309 310 void ChartPresenter::setBackgroundVisible(bool visible)
310 311 {
311 312 createBackgroundItem();
312 313 m_background->setVisible(visible);
313 314 }
314 315
315 316
316 317 bool ChartPresenter::isBackgroundVisible() const
317 318 {
318 319 if (!m_background)
319 320 return false;
320 321 return m_background->isVisible();
321 322 }
322 323
323 324 void ChartPresenter::setPlotAreaBackgroundVisible(bool visible)
324 325 {
325 326 createPlotAreaBackgroundItem();
326 327 m_plotAreaBackground->setVisible(visible);
327 328 }
328 329
329 330 bool ChartPresenter::isPlotAreaBackgroundVisible() const
330 331 {
331 332 if (!m_plotAreaBackground)
332 333 return false;
333 334 return m_plotAreaBackground->isVisible();
334 335 }
335 336
336 337 void ChartPresenter::setBackgroundDropShadowEnabled(bool enabled)
337 338 {
338 339 createBackgroundItem();
339 340 m_background->setDropShadowEnabled(enabled);
340 341 }
341 342
342 343 bool ChartPresenter::isBackgroundDropShadowEnabled() const
343 344 {
344 345 if (!m_background)
345 346 return false;
346 347 return m_background->isDropShadowEnabled();
347 348 }
348 349
349 350 void ChartPresenter::setLocalizeNumbers(bool localize)
350 351 {
351 352 m_localizeNumbers = localize;
352 353 m_layout->invalidate();
353 354 }
354 355
355 356 void ChartPresenter::setLocale(const QLocale &locale)
356 357 {
357 358 m_locale = locale;
358 359 m_layout->invalidate();
359 360 }
360 361
361 362 AbstractChartLayout *ChartPresenter::layout()
362 363 {
363 364 return m_layout;
364 365 }
365 366
366 367 QLegend *ChartPresenter::legend()
367 368 {
368 369 return m_chart->legend();
369 370 }
370 371
371 372 void ChartPresenter::setVisible(bool visible)
372 373 {
373 374 m_chart->setVisible(visible);
374 375 }
375 376
376 377 ChartBackground *ChartPresenter::backgroundElement()
377 378 {
378 379 return m_background;
379 380 }
380 381
381 382 QAbstractGraphicsShapeItem *ChartPresenter::plotAreaElement()
382 383 {
383 384 return m_plotAreaBackground;
384 385 }
385 386
386 387 QList<ChartAxisElement *> ChartPresenter::axisItems() const
387 388 {
388 389 return m_axisItems;
389 390 }
390 391
391 392 QList<ChartItem *> ChartPresenter::chartItems() const
392 393 {
393 394 return m_chartItems;
394 395 }
395 396
396 397 ChartTitle *ChartPresenter::titleElement()
397 398 {
398 399 return m_title;
399 400 }
400 401
401 402 QRectF ChartPresenter::textBoundingRect(const QFont &font, const QString &text, qreal angle)
402 403 {
403 404 static QGraphicsTextItem dummyTextItem;
404 405 static bool initMargin = true;
405 406 if (initMargin) {
406 407 dummyTextItem.document()->setDocumentMargin(textMargin());
407 408 initMargin = false;
408 409 }
409 410
410 411 dummyTextItem.setFont(font);
411 412 dummyTextItem.setHtml(text);
412 413 QRectF boundingRect = dummyTextItem.boundingRect();
413 414
414 415 // Take rotation into account
415 416 if (angle) {
416 417 QTransform transform;
417 418 transform.rotate(angle);
418 419 boundingRect = transform.mapRect(boundingRect);
419 420 }
420 421
421 422 return boundingRect;
422 423 }
423 424
424 425 // boundingRect parameter returns the rotated bounding rect of the text
425 426 QString ChartPresenter::truncatedText(const QFont &font, const QString &text, qreal angle,
426 427 qreal maxWidth, qreal maxHeight, QRectF &boundingRect)
427 428 {
428 429 QString truncatedString(text);
429 430 boundingRect = textBoundingRect(font, truncatedString, angle);
430 431 if (boundingRect.width() > maxWidth || boundingRect.height() > maxHeight) {
431 432 // It can be assumed that almost any amount of string manipulation is faster
432 433 // than calculating one bounding rectangle, so first prepare a list of truncated strings
433 434 // to try.
434 435 static QRegExp truncateMatcher(QStringLiteral("&#?[0-9a-zA-Z]*;$"));
435 436
436 437 QVector<QString> testStrings(text.length());
437 438 int count(0);
438 439 static QLatin1Char closeTag('>');
439 440 static QLatin1Char openTag('<');
440 441 static QLatin1Char semiColon(';');
441 442 static QLatin1String ellipsis("...");
442 443 while (truncatedString.length() > 1) {
443 444 int chopIndex(-1);
444 445 int chopCount(1);
445 446 QChar lastChar(truncatedString.at(truncatedString.length() - 1));
446 447
447 448 if (lastChar == closeTag)
448 449 chopIndex = truncatedString.lastIndexOf(openTag);
449 450 else if (lastChar == semiColon)
450 451 chopIndex = truncateMatcher.indexIn(truncatedString, 0);
451 452
452 453 if (chopIndex != -1)
453 454 chopCount = truncatedString.length() - chopIndex;
454 455 truncatedString.chop(chopCount);
455 456 testStrings[count] = truncatedString + ellipsis;
456 457 count++;
457 458 }
458 459
459 460 // Binary search for best fit
460 461 int minIndex(0);
461 462 int maxIndex(count - 1);
462 463 int bestIndex(count);
463 464 QRectF checkRect;
464 465
465 466 while (maxIndex >= minIndex) {
466 467 int mid = (maxIndex + minIndex) / 2;
467 468 checkRect = textBoundingRect(font, testStrings.at(mid), angle);
468 469 if (checkRect.width() > maxWidth || checkRect.height() > maxHeight) {
469 470 // Checked index too large, all under this are also too large
470 471 minIndex = mid + 1;
471 472 } else {
472 473 // Checked index fits, all over this also fit
473 474 maxIndex = mid - 1;
474 475 bestIndex = mid;
475 476 boundingRect = checkRect;
476 477 }
477 478 }
478 479 // Default to "..." if nothing fits
479 480 if (bestIndex == count) {
480 481 boundingRect = textBoundingRect(font, ellipsis, angle);
481 482 truncatedString = ellipsis;
482 483 } else {
483 484 truncatedString = testStrings.at(bestIndex);
484 485 }
485 486 }
486 487
487 488 return truncatedString;
488 489 }
489 490
490 491 QString ChartPresenter::numberToString(double value, char f, int prec)
491 492 {
492 493 if (m_localizeNumbers)
493 494 return m_locale.toString(value, f, prec);
494 495 else
495 496 return QString::number(value, f, prec);
496 497 }
497 498
498 499 QString ChartPresenter::numberToString(int value)
499 500 {
500 501 if (m_localizeNumbers)
501 502 return m_locale.toString(value);
502 503 else
503 504 return QString::number(value);
504 505 }
505 506
506 507 #include "moc_chartpresenter_p.cpp"
507 508
508 509 QT_CHARTS_END_NAMESPACE
@@ -1,192 +1,195
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the Qt Enterprise Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef CHARTPRESENTER_H
31 31 #define CHARTPRESENTER_H
32 32
33 33 #include <QtCharts/QChartGlobal>
34 34 #include <QtCharts/QChart> //because of QChart::ChartThemeId
35 35 #include <QtCore/QRectF>
36 36 #include <QtCore/QMargins>
37 37 #include <QtCore/QLocale>
38 38
39 39 QT_CHARTS_BEGIN_NAMESPACE
40 40
41 41 class ChartItem;
42 42 class AxisItem;
43 43 class QAbstractSeries;
44 44 class ChartDataSet;
45 45 class AbstractDomain;
46 46 class ChartAxisElement;
47 47 class ChartAnimator;
48 48 class ChartBackground;
49 49 class ChartTitle;
50 50 class ChartAnimation;
51 51 class AbstractChartLayout;
52 52
53 53 class ChartPresenter: public QObject
54 54 {
55 55 Q_OBJECT
56 56 public:
57 57 enum ZValues {
58 58 BackgroundZValue = -1,
59 59 PlotAreaZValue,
60 60 ShadesZValue,
61 61 GridZValue,
62 62 AxisZValue,
63 63 SeriesZValue,
64 64 LineChartZValue = SeriesZValue,
65 65 SplineChartZValue = SeriesZValue,
66 66 BarSeriesZValue = SeriesZValue,
67 67 ScatterSeriesZValue = SeriesZValue,
68 68 PieSeriesZValue = SeriesZValue,
69 69 BoxPlotSeriesZValue = SeriesZValue,
70 70 LegendZValue,
71 71 TopMostZValue
72 72 };
73 73
74 74 enum State {
75 75 ShowState,
76 76 ScrollUpState,
77 77 ScrollDownState,
78 78 ScrollLeftState,
79 79 ScrollRightState,
80 80 ZoomInState,
81 81 ZoomOutState
82 82 };
83 83
84 84 ChartPresenter(QChart *chart, QChart::ChartType type);
85 85 virtual ~ChartPresenter();
86 86
87 87
88 88 void setGeometry(QRectF rect);
89 89 QRectF geometry() const;
90 90
91 91 QGraphicsItem *rootItem(){ return m_chart; }
92 92 ChartBackground *backgroundElement();
93 93 QAbstractGraphicsShapeItem *plotAreaElement();
94 94 ChartTitle *titleElement();
95 95 QList<ChartAxisElement *> axisItems() const;
96 96 QList<ChartItem *> chartItems() const;
97 97
98 98 QLegend *legend();
99 99
100 100 void setBackgroundBrush(const QBrush &brush);
101 101 QBrush backgroundBrush() const;
102 102
103 103 void setBackgroundPen(const QPen &pen);
104 104 QPen backgroundPen() const;
105 105
106 106 void setBackgroundRoundness(qreal diameter);
107 107 qreal backgroundRoundness() const;
108 108
109 109 void setPlotAreaBackgroundBrush(const QBrush &brush);
110 110 QBrush plotAreaBackgroundBrush() const;
111 111
112 112 void setPlotAreaBackgroundPen(const QPen &pen);
113 113 QPen plotAreaBackgroundPen() const;
114 114
115 115 void setTitle(const QString &title);
116 116 QString title() const;
117 117
118 118 void setTitleFont(const QFont &font);
119 119 QFont titleFont() const;
120 120
121 121 void setTitleBrush(const QBrush &brush);
122 122 QBrush titleBrush() const;
123 123
124 124 void setBackgroundVisible(bool visible);
125 125 bool isBackgroundVisible() const;
126 126
127 127 void setPlotAreaBackgroundVisible(bool visible);
128 128 bool isPlotAreaBackgroundVisible() const;
129 129
130 130 void setBackgroundDropShadowEnabled(bool enabled);
131 131 bool isBackgroundDropShadowEnabled() const;
132 132
133 133 void setLocalizeNumbers(bool localize);
134 134 inline bool localizeNumbers() const { return m_localizeNumbers; }
135 135 void setLocale(const QLocale &locale);
136 136 inline const QLocale &locale() const { return m_locale; }
137 137
138 138 void setVisible(bool visible);
139 139
140 140 void setAnimationOptions(QChart::AnimationOptions options);
141 141 QChart::AnimationOptions animationOptions() const;
142 142
143 143 void startAnimation(ChartAnimation *animation);
144 144
145 145 void setState(State state,QPointF point);
146 146 State state() const { return m_state; }
147 147 QPointF statePoint() const { return m_statePoint; }
148 148 AbstractChartLayout *layout();
149 149
150 150 QChart::ChartType chartType() const { return m_chart->chartType(); }
151 151 QChart *chart() { return m_chart; }
152 152
153 153 static QRectF textBoundingRect(const QFont &font, const QString &text, qreal angle = 0.0);
154 154 static QString truncatedText(const QFont &font, const QString &text, qreal angle,
155 155 qreal maxWidth, qreal maxHeight, QRectF &boundingRect);
156 156 inline static qreal textMargin() { return qreal(0.5); }
157 157
158 158 QString numberToString(double value, char f = 'g', int prec = 6);
159 159 QString numberToString(int value);
160 160
161 161 private:
162 162 void createBackgroundItem();
163 163 void createPlotAreaBackgroundItem();
164 164 void createTitleItem();
165 165
166 166 public Q_SLOTS:
167 167 void handleSeriesAdded(QAbstractSeries *series);
168 168 void handleSeriesRemoved(QAbstractSeries *series);
169 169 void handleAxisAdded(QAbstractAxis *axis);
170 170 void handleAxisRemoved(QAbstractAxis *axis);
171 171
172 signals:
173 void plotAreaChanged(const QRectF &plotArea);
174
172 175 private:
173 176 QChart *m_chart;
174 177 QList<ChartItem *> m_chartItems;
175 178 QList<ChartAxisElement *> m_axisItems;
176 179 QList<QAbstractSeries *> m_series;
177 180 QList<QAbstractAxis *> m_axes;
178 181 QChart::AnimationOptions m_options;
179 182 State m_state;
180 183 QPointF m_statePoint;
181 184 AbstractChartLayout *m_layout;
182 185 ChartBackground *m_background;
183 186 QAbstractGraphicsShapeItem *m_plotAreaBackground;
184 187 ChartTitle *m_title;
185 188 QRectF m_rect;
186 189 bool m_localizeNumbers;
187 190 QLocale m_locale;
188 191 };
189 192
190 193 QT_CHARTS_END_NAMESPACE
191 194
192 195 #endif /* CHARTPRESENTER_H */
@@ -1,872 +1,875
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QtCharts/QChart>
22 22 #include <private/qchart_p.h>
23 23 #include <private/legendscroller_p.h>
24 24 #include <private/qlegend_p.h>
25 25 #include <private/chartbackground_p.h>
26 26 #include <QtCharts/QAbstractAxis>
27 27 #include <private/abstractchartlayout_p.h>
28 28 #include <private/charttheme_p.h>
29 29 #include <private/chartpresenter_p.h>
30 30 #include <private/chartdataset_p.h>
31 31 #include <QtWidgets/QGraphicsScene>
32 32 #include <QGraphicsSceneResizeEvent>
33 33
34 34 QT_CHARTS_BEGIN_NAMESPACE
35 35
36 36 /*!
37 37 \enum QChart::ChartTheme
38 38
39 39 This enum describes the theme used by the chart.
40 40
41 41 \value ChartThemeLight The default theme
42 42 \value ChartThemeBlueCerulean
43 43 \value ChartThemeDark
44 44 \value ChartThemeBrownSand
45 45 \value ChartThemeBlueNcs
46 46 \value ChartThemeHighContrast
47 47 \value ChartThemeBlueIcy
48 48 \value ChartThemeQt
49 49 */
50 50
51 51 /*!
52 52 \enum QChart::AnimationOption
53 53
54 54 For enabling/disabling animations. Defaults to NoAnimation.
55 55
56 56 \value NoAnimation
57 57 \value GridAxisAnimations
58 58 \value SeriesAnimations
59 59 \value AllAnimations
60 60 */
61 61
62 62 /*!
63 63 \enum QChart::ChartType
64 64
65 65 This enum describes the chart type.
66 66
67 67 \value ChartTypeUndefined
68 68 \value ChartTypeCartesian
69 69 \value ChartTypePolar
70 70 */
71 71
72 72 /*!
73 73 \class QChart
74 74 \inmodule Qt Charts
75 75 \brief Main chart API for Qt Charts.
76 76
77 77 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
78 78 representation of different types of series and other chart related objects like legend and
79 79 axes. If you simply want to show a chart in a layout, you can use the
80 80 convenience class QChartView instead of QChart.
81 81 \sa QChartView, QPolarChart
82 82 */
83 83
84 84 /*!
85 85 \property QChart::animationOptions
86 86 The animation \a options for the chart. Animations are enabled/disabled based on this setting.
87 87 */
88 88
89 89 /*!
90 90 \property QChart::backgroundVisible
91 91 Specifies whether the chart background is visible or not.
92 92 \sa setBackgroundBrush(), setBackgroundPen(), plotAreaBackgroundVisible
93 93 */
94 94
95 95 /*!
96 96 \property QChart::dropShadowEnabled
97 97 If set to true, the background drop shadow effect is enabled. If set to false, it is disabled. Note that the drop
98 98 shadow effect depends on theme, which means the setting may be changed if you switch to another theme.
99 99 */
100 100
101 101 /*!
102 102 \property QChart::backgroundRoundness
103 103 The diameter of the rounding cirle at the corners of the chart background.
104 104 */
105 105
106 106 /*!
107 107 \property QChart::minimumMargins
108 108 Minimum margins between the plot area (axes) and the edge of the chart widget.
109 109 This property is deprecated; use margins property instead.
110 110
111 111 \sa margins
112 112 */
113 113
114 114 /*!
115 115 \property QChart::margins
116 116 Margins between the plot area (axes) and the edge of the chart widget.
117 117 */
118 118
119 119 /*!
120 120 \property QChart::theme
121 121 Theme is a built-in collection of UI style related settings applied for all visual elements of a chart, like colors,
122 122 pens, brushes, and fonts of series, axes, title, and legend. \l {Chart themes example} shows an example with a few
123 123 different themes.
124 124 \note Changing the theme will overwrite all customizations previously applied to the series.
125 125 */
126 126
127 127 /*!
128 128 \property QChart::title
129 129 Title is the name (label) of a chart. It is shown as a headline on top of the chart. Chart title supports html formatting.
130 130 */
131 131
132 132 /*!
133 133 \property QChart::chartType
134 134 Chart type indicates if the chart is a cartesian chart or a polar chart.
135 135 This property is set internally and it is read only.
136 136 \sa QPolarChart
137 137 */
138 138
139 139 /*!
140 140 \property QChart::plotAreaBackgroundVisible
141 141 Specifies whether the chart plot area background is visible or not.
142 142 \note By default the plot area background is not visible and the plot area uses
143 143 the general chart background.
144 144 \sa setPlotAreaBackgroundBrush(), setPlotAreaBackgroundPen(), backgroundVisible
145 145 */
146 146
147 147 /*!
148 148 \property QChart::localizeNumbers
149 149 \since QtCharts 2.0
150 150 When \c{true}, all generated numbers appearing in various series and axis labels will be
151 151 localized using the QLocale set with the locale property.
152 152 When \c{false}, the "C" locale is always used.
153 153 Defaults to \c{false}.
154 154 \note This property doesn't affect QDateTimeAxis labels, which always use the QLocale set with
155 155 the locale property.
156 156
157 157 \sa locale
158 158 */
159 159
160 160 /*!
161 161 \property QChart::locale
162 162 \since QtCharts 2.0
163 163 Sets the locale used to format various chart labels when localizeNumbers is \c{true}.
164 164 This also determines the locale used to format QDateTimeAxis labels regardless of
165 165 localizeNumbers property.
166 166 Defaults to application default locale at the time the chart is constructed.
167 167
168 168 \sa localizeNumbers
169 169 */
170 170
171 171 /*!
172 \property QChart::plotArea
173 Holds the rectangle within which the drawing of the chart is done.
174 It does not include the area defined by margins.
175 */
176
177 /*!
172 178 \internal
173 179 Constructs a chart object of \a type which is a child of a \a parent.
174 180 Parameter \a wFlags is passed to the QGraphicsWidget constructor.
175 181 This constructor is called only by subclasses.
176 182 */
177 183 QChart::QChart(QChart::ChartType type, QGraphicsItem *parent, Qt::WindowFlags wFlags)
178 184 : QGraphicsWidget(parent, wFlags),
179 185 d_ptr(new QChartPrivate(this, type))
180 186 {
181 187 d_ptr->init();
182 188 }
183 189
184 190 /*!
185 191 Constructs a chart object which is a child of a \a parent.
186 192 Parameter \a wFlags is passed to the QGraphicsWidget constructor.
187 193 */
188 194 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
189 195 : QGraphicsWidget(parent, wFlags),
190 196 d_ptr(new QChartPrivate(this, ChartTypeCartesian))
191 197 {
192 198 d_ptr->init();
193 199 }
194 200
195 201 /*!
196 202 Destroys the chart object and its children, like series and axis objects added to it.
197 203 */
198 204 QChart::~QChart()
199 205 {
200 206 //start by deleting dataset, it will remove all series and axes
201 207 delete d_ptr->m_dataset;
202 208 d_ptr->m_dataset = 0;
203 209 }
204 210
205 211 /*!
206 212 Adds the \a series onto the chart and takes the ownership of it.
207 213
208 214 \note A newly added series is attached to no axes by default, including any axes that were created for the chart
209 215 using createDefaultAxes() before the series was added to the chart. If no axes are attached to
210 216 the newly added series before the chart is shown, the series will get drawn as if it had axes with ranges
211 217 that exactly fit the series to the plot area of the chart. This can be confusing if the same chart also displays other
212 218 series that have properly attached axes, so always make sure you either call createDefaultAxes() after
213 219 a series has been added or explicitly attach axes for the series.
214 220
215 221 \sa removeSeries(), removeAllSeries(), createDefaultAxes(), QAbstractSeries::attachAxis()
216 222 */
217 223 void QChart::addSeries(QAbstractSeries *series)
218 224 {
219 225 Q_ASSERT(series);
220 226 d_ptr->m_dataset->addSeries(series);
221 227 }
222 228
223 229 /*!
224 230 Removes the \a series from the chart.
225 231 The chart releases its ownership of the specified \a series object.
226 232
227 233 \sa addSeries(), removeAllSeries()
228 234 */
229 235 void QChart::removeSeries(QAbstractSeries *series)
230 236 {
231 237 Q_ASSERT(series);
232 238 d_ptr->m_dataset->removeSeries(series);
233 239 }
234 240
235 241 /*!
236 242 Removes and deletes all series objects that have been added to the chart.
237 243
238 244 \sa addSeries(), removeSeries()
239 245 */
240 246 void QChart::removeAllSeries()
241 247 {
242 248 foreach (QAbstractSeries *s , d_ptr->m_dataset->series()){
243 249 removeSeries(s);
244 250 delete s;
245 251 }
246 252 }
247 253
248 254 /*!
249 255 Sets the \a brush that is used for painting the background of the chart area.
250 256 */
251 257 void QChart::setBackgroundBrush(const QBrush &brush)
252 258 {
253 259 d_ptr->m_presenter->setBackgroundBrush(brush);
254 260 }
255 261
256 262 /*!
257 263 Gets the brush that is used for painting the background of the chart area.
258 264 */
259 265 QBrush QChart::backgroundBrush() const
260 266 {
261 267 return d_ptr->m_presenter->backgroundBrush();
262 268 }
263 269
264 270 /*!
265 271 Sets the \a pen that is used for painting the background of the chart area.
266 272 */
267 273 void QChart::setBackgroundPen(const QPen &pen)
268 274 {
269 275 d_ptr->m_presenter->setBackgroundPen(pen);
270 276 }
271 277
272 278 /*!
273 279 Gets the pen that is used for painting the background of the chart area.
274 280 */
275 281 QPen QChart::backgroundPen() const
276 282 {
277 283 return d_ptr->m_presenter->backgroundPen();
278 284 }
279 285
280 286 void QChart::setTitle(const QString &title)
281 287 {
282 288 d_ptr->m_presenter->setTitle(title);
283 289 }
284 290
285 291 QString QChart::title() const
286 292 {
287 293 return d_ptr->m_presenter->title();
288 294 }
289 295
290 296 /*!
291 297 Sets the \a font that is used for drawing the chart title.
292 298 */
293 299 void QChart::setTitleFont(const QFont &font)
294 300 {
295 301 d_ptr->m_presenter->setTitleFont(font);
296 302 }
297 303
298 304 /*!
299 305 Gets the font that is used for drawing the chart title.
300 306 */
301 307 QFont QChart::titleFont() const
302 308 {
303 309 return d_ptr->m_presenter->titleFont();
304 310 }
305 311
306 312 /*!
307 313 Sets the \a brush used for drawing the title text.
308 314 */
309 315 void QChart::setTitleBrush(const QBrush &brush)
310 316 {
311 317 d_ptr->m_presenter->setTitleBrush(brush);
312 318 }
313 319
314 320 /*!
315 321 Returns the brush used for drawing the title text.
316 322 */
317 323 QBrush QChart::titleBrush() const
318 324 {
319 325 return d_ptr->m_presenter->titleBrush();
320 326 }
321 327
322 328 void QChart::setTheme(QChart::ChartTheme theme)
323 329 {
324 330 d_ptr->m_themeManager->setTheme(theme);
325 331 }
326 332
327 333 QChart::ChartTheme QChart::theme() const
328 334 {
329 335 return d_ptr->m_themeManager->theme()->id();
330 336 }
331 337
332 338 /*!
333 339 Zooms in the view by a factor of two.
334 340 */
335 341 void QChart::zoomIn()
336 342 {
337 343 d_ptr->zoomIn(2.0);
338 344 }
339 345
340 346 /*!
341 347 Zooms in the view to a maximum level at which \a rect is still fully visible.
342 348 \note This is not supported for polar charts.
343 349 */
344 350 void QChart::zoomIn(const QRectF &rect)
345 351 {
346 352 if (d_ptr->m_type == QChart::ChartTypePolar)
347 353 return;
348 354 d_ptr->zoomIn(rect);
349 355 }
350 356
351 357 /*!
352 358 Zooms out the view by a factor of two.
353 359 */
354 360 void QChart::zoomOut()
355 361 {
356 362 d_ptr->zoomOut(2.0);
357 363 }
358 364
359 365 /*!
360 366 Zooms in the view by a custom \a factor.
361 367
362 368 A factor over 1.0 zooms the view in and factor between 0.0 and 1.0 zooms out.
363 369 */
364 370 void QChart::zoom(qreal factor)
365 371 {
366 372 if (qFuzzyCompare(factor, 0))
367 373 return;
368 374
369 375 if (qFuzzyCompare(factor, (qreal)1.0))
370 376 return;
371 377
372 378 if (factor < 0)
373 379 return;
374 380
375 381 if (factor > 1.0)
376 382 d_ptr->zoomIn(factor);
377 383 else
378 384 d_ptr->zoomOut(1.0 / factor);
379 385 }
380 386
381 387
382 388 /*!
383 389 Resets the series domains to what they were before any zoom method was called.
384 390 Note that this will also reset any scrolls and explicit axis range settings done between
385 391 the first zoom operation and calling this method. If no zoom operation has been
386 392 done, this method does nothing.
387 393 */
388 394 void QChart::zoomReset()
389 395 {
390 396 d_ptr->zoomReset();
391 397 }
392 398
393 399 /*!
394 400 Returns true if any series has a zoomed domain.
395 401 */
396 402 bool QChart::isZoomed()
397 403 {
398 404 return d_ptr->isZoomed();
399 405 }
400 406
401 407 /*!
402 408 Returns a pointer to the horizontal axis attached to the specified \a series.
403 409 If no \a series is specified, the first horizontal axis added to the chart is returned.
404 410
405 411 \sa addAxis(), QAbstractSeries::attachAxis()
406 412 */
407 413 QAbstractAxis *QChart::axisX(QAbstractSeries *series) const
408 414 {
409 415 QList<QAbstractAxis *> axisList = axes(Qt::Horizontal, series);
410 416 if (axisList.count())
411 417 return axisList[0];
412 418 return 0;
413 419 }
414 420
415 421 /*!
416 422 Returns a pointer to the vertical axis attached to the specified \a series.
417 423 If no \a series is specified, the first vertical axis added to the chart is returned.
418 424
419 425 \sa addAxis(), QAbstractSeries::attachAxis()
420 426 */
421 427 QAbstractAxis *QChart::axisY(QAbstractSeries *series) const
422 428 {
423 429 QList<QAbstractAxis *> axisList = axes(Qt::Vertical, series);
424 430 if (axisList.count())
425 431 return axisList[0];
426 432 return 0;
427 433 }
428 434
429 435 /*!
430 436 Returns the axes attached to the \a series with \a orientation. If no \a series is provided,
431 437 then all axes added to the chart with the specified orientation are returned.
432 438 \sa addAxis(), createDefaultAxes()
433 439 */
434 440 QList<QAbstractAxis *> QChart::axes(Qt::Orientations orientation, QAbstractSeries *series) const
435 441 {
436 442 QList<QAbstractAxis *> result ;
437 443
438 444 if (series) {
439 445 foreach (QAbstractAxis *axis, series->attachedAxes()){
440 446 if (orientation.testFlag(axis->orientation()))
441 447 result << axis;
442 448 }
443 449 } else {
444 450 foreach (QAbstractAxis *axis, d_ptr->m_dataset->axes()){
445 451 if (orientation.testFlag(axis->orientation()) && !result.contains(axis))
446 452 result << axis;
447 453 }
448 454 }
449 455
450 456 return result;
451 457 }
452 458
453 459 /*!
454 460 Creates axes for the chart based on the series that have already been added to the chart. Any axes previously added to
455 461 the chart will be deleted.
456 462
457 463 \note This function has to be called after all series have been added to the chart. The axes created by this function
458 464 will NOT get automatically attached to any series added to the chart after this function has been called.
459 465 A series with no axes attached will by default scale to utilize the entire plot area of the chart, which can be confusing
460 466 if there are other series with properly attached axes also present.
461 467
462 468 \table
463 469 \header
464 470 \li Series type
465 471 \li X-axis
466 472 \li Y-axis
467 473 \row
468 474 \li QXYSeries
469 475 \li QValueAxis
470 476 \li QValueAxis
471 477 \row
472 478 \li QBarSeries
473 479 \li QBarCategoryAxis
474 480 \li QValueAxis
475 481 \row
476 482 \li QPieSeries
477 483 \li None
478 484 \li None
479 485 \endtable
480 486
481 487 If there are several QXYSeries derived series added to the chart and no series of other types have been added, then only one pair of axes is created.
482 488 If there are several series of different types added to the chart, then each series gets its own axes pair.
483 489
484 490 The axes specific to the series can be later obtained from the chart by providing the series as the parameter for axes() function call.
485 491 QPieSeries does not create any axes.
486 492
487 493 \sa axisX(), axisY(), axes(), setAxisX(), setAxisY(), QAbstractSeries::attachAxis()
488 494 */
489 495 void QChart::createDefaultAxes()
490 496 {
491 497 d_ptr->m_dataset->createDefaultAxes();
492 498 }
493 499
494 500 /*!
495 501 Returns the legend object of the chart. Ownership stays with the chart.
496 502 */
497 503 QLegend *QChart::legend() const
498 504 {
499 505 return d_ptr->m_legend;
500 506 }
501 507
502 508 void QChart::setMinimumMargins(const QMargins &margins)
503 509 {
504 510 qWarning() << "QChart::setMinimumMargins is deprecated. Use QChart::setMargins instead.";
505 511 d_ptr->m_presenter->layout()->setMargins(margins);
506 512 }
507 513
508 514 QMargins QChart::minimumMargins() const
509 515 {
510 516 qWarning() << "QChart::minimumMargins is deprecated. Use QChart::margins instead.";
511 517 return d_ptr->m_presenter->layout()->margins();
512 518 }
513 519
514 520 void QChart::setMargins(const QMargins &margins)
515 521 {
516 522 d_ptr->m_presenter->layout()->setMargins(margins);
517 523 }
518 524
519 525 QMargins QChart::margins() const
520 526 {
521 527 return d_ptr->m_presenter->layout()->margins();
522 528 }
523 529
524 530 QChart::ChartType QChart::chartType() const
525 531 {
526 532 return d_ptr->m_type;
527 533 }
528 534
529 /*!
530 Returns the the rectangle within which the drawing of the chart is done.
531 It does not include the area defined by margins.
532 */
533 535 QRectF QChart::plotArea() const
534 536 {
535 537 return d_ptr->m_presenter->geometry();
536 538 }
537 539
538 540 /*!
539 541 Sets the \a brush for the background of the plot area of the chart.
540 542
541 543 \sa plotArea(), plotAreaBackgroundVisible, setPlotAreaBackgroundPen(), plotAreaBackgroundBrush()
542 544 */
543 545 void QChart::setPlotAreaBackgroundBrush(const QBrush &brush)
544 546 {
545 547 d_ptr->m_presenter->setPlotAreaBackgroundBrush(brush);
546 548 }
547 549
548 550 /*!
549 551 Returns the brush for the background of the plot area of the chart.
550 552
551 553 \sa plotArea(), plotAreaBackgroundVisible, plotAreaBackgroundPen(), setPlotAreaBackgroundBrush()
552 554 */
553 555 QBrush QChart::plotAreaBackgroundBrush() const
554 556 {
555 557 return d_ptr->m_presenter->plotAreaBackgroundBrush();
556 558 }
557 559
558 560 /*!
559 561 Sets the \a pen for the background of the plot area of the chart.
560 562
561 563 \sa plotArea(), plotAreaBackgroundVisible, setPlotAreaBackgroundBrush(), plotAreaBackgroundPen()
562 564 */
563 565 void QChart::setPlotAreaBackgroundPen(const QPen &pen)
564 566 {
565 567 d_ptr->m_presenter->setPlotAreaBackgroundPen(pen);
566 568 }
567 569
568 570 /*!
569 571 Returns the pen for the background of the plot area of the chart.
570 572
571 573 \sa plotArea(), plotAreaBackgroundVisible, plotAreaBackgroundBrush(), setPlotAreaBackgroundPen()
572 574 */
573 575 QPen QChart::plotAreaBackgroundPen() const
574 576 {
575 577 return d_ptr->m_presenter->plotAreaBackgroundPen();
576 578 }
577 579
578 580 void QChart::setPlotAreaBackgroundVisible(bool visible)
579 581 {
580 582 d_ptr->m_presenter->setPlotAreaBackgroundVisible(visible);
581 583 }
582 584
583 585 bool QChart::isPlotAreaBackgroundVisible() const
584 586 {
585 587 return d_ptr->m_presenter->isPlotAreaBackgroundVisible();
586 588 }
587 589
588 590 void QChart::setLocalizeNumbers(bool localize)
589 591 {
590 592 d_ptr->m_presenter->setLocalizeNumbers(localize);
591 593 }
592 594
593 595 bool QChart::localizeNumbers() const
594 596 {
595 597 return d_ptr->m_presenter->localizeNumbers();
596 598 }
597 599
598 600 void QChart::setLocale(const QLocale &locale)
599 601 {
600 602 d_ptr->m_presenter->setLocale(locale);
601 603 }
602 604
603 605 QLocale QChart::locale() const
604 606 {
605 607 return d_ptr->m_presenter->locale();
606 608 }
607 609
608 610 void QChart::setAnimationOptions(AnimationOptions options)
609 611 {
610 612 d_ptr->m_presenter->setAnimationOptions(options);
611 613 }
612 614
613 615 QChart::AnimationOptions QChart::animationOptions() const
614 616 {
615 617 return d_ptr->m_presenter->animationOptions();
616 618 }
617 619
618 620 /*!
619 621 Scrolls the visible area of the chart by the distance defined in the \a dx and \a dy.
620 622
621 623 For polar charts, \a dx indicates the angle along angular axis instead of distance.
622 624 */
623 625 void QChart::scroll(qreal dx, qreal dy)
624 626 {
625 627 d_ptr->scroll(dx,dy);
626 628 }
627 629
628 630 void QChart::setBackgroundVisible(bool visible)
629 631 {
630 632 d_ptr->m_presenter->setBackgroundVisible(visible);
631 633 }
632 634
633 635 bool QChart::isBackgroundVisible() const
634 636 {
635 637 return d_ptr->m_presenter->isBackgroundVisible();
636 638 }
637 639
638 640 void QChart::setDropShadowEnabled(bool enabled)
639 641 {
640 642 d_ptr->m_presenter->setBackgroundDropShadowEnabled(enabled);
641 643 }
642 644
643 645 bool QChart::isDropShadowEnabled() const
644 646 {
645 647 return d_ptr->m_presenter->isBackgroundDropShadowEnabled();
646 648 }
647 649
648 650 void QChart::setBackgroundRoundness(qreal diameter)
649 651 {
650 652 d_ptr->m_presenter->setBackgroundRoundness(diameter);
651 653 }
652 654
653 655 qreal QChart::backgroundRoundness() const
654 656 {
655 657 return d_ptr->m_presenter->backgroundRoundness();
656 658 }
657 659
658 660 /*!
659 661 Returns all series that are added to the chart.
660 662
661 663 \sa addSeries(), removeSeries(), removeAllSeries()
662 664 */
663 665 QList<QAbstractSeries *> QChart::series() const
664 666 {
665 667 return d_ptr->m_dataset->series();
666 668 }
667 669
668 670 /*!
669 671 Adds the \a axis to the chart and attaches it to the \a series as a bottom-aligned horizontal axis.
670 672 The chart takes ownership of both the \a axis and the \a series.
671 673 Any horizontal axes previously attached to the \a series are deleted.
672 674
673 675 \sa axisX(), axisY(), setAxisY(), createDefaultAxes(), QAbstractSeries::attachAxis()
674 676 */
675 677 void QChart::setAxisX(QAbstractAxis *axis ,QAbstractSeries *series)
676 678 {
677 679 QList<QAbstractAxis*> list = axes(Qt::Horizontal, series);
678 680
679 681 foreach (QAbstractAxis* a, list) {
680 682 d_ptr->m_dataset->removeAxis(a);
681 683 delete a;
682 684 }
683 685
684 686 if (!d_ptr->m_dataset->axes().contains(axis))
685 687 d_ptr->m_dataset->addAxis(axis, Qt::AlignBottom);
686 688 d_ptr->m_dataset->attachAxis(series, axis);
687 689 }
688 690
689 691 /*!
690 692 Adds the \a axis to the chart and attaches it to the \a series as a left-aligned vertical axis.
691 693 The chart takes ownership of both the \a axis and the \a series.
692 694 Any vertical axes previously attached to the \a series are deleted.
693 695
694 696 \sa axisX(), axisY(), setAxisX(), createDefaultAxes(), QAbstractSeries::attachAxis()
695 697 */
696 698 void QChart::setAxisY(QAbstractAxis *axis ,QAbstractSeries *series)
697 699 {
698 700 QList<QAbstractAxis*> list = axes(Qt::Vertical, series);
699 701
700 702 foreach (QAbstractAxis* a, list) {
701 703 d_ptr->m_dataset->removeAxis(a);
702 704 delete a;
703 705 }
704 706
705 707 if (!d_ptr->m_dataset->axes().contains(axis))
706 708 d_ptr->m_dataset->addAxis(axis, Qt::AlignLeft);
707 709 d_ptr->m_dataset->attachAxis(series, axis);
708 710 }
709 711
710 712 /*!
711 713 Adds the \a axis to the chart with \a alignment. The chart takes the ownership of the axis.
712 714
713 715 \sa removeAxis(), createDefaultAxes(), QAbstractSeries::attachAxis()
714 716 */
715 717 void QChart::addAxis(QAbstractAxis *axis, Qt::Alignment alignment)
716 718 {
717 719 d_ptr->m_dataset->addAxis(axis, alignment);
718 720 }
719 721
720 722 /*!
721 723 Removes the \a axis from the chart.
722 724 The chart releases its ownership of the specified \a axis object.
723 725
724 726 \sa addAxis(), createDefaultAxes(), QAbstractSeries::detachAxis()
725 727 */
726 728 void QChart::removeAxis(QAbstractAxis *axis)
727 729 {
728 730 d_ptr->m_dataset->removeAxis(axis);
729 731 }
730 732
731 733 /*!
732 734 Returns the value in the \a series domain that corresponds to the \a position relative to chart widget.
733 735 */
734 736 QPointF QChart::mapToValue(const QPointF &position, QAbstractSeries *series)
735 737 {
736 738 return d_ptr->m_dataset->mapToValue(position, series);
737 739 }
738 740
739 741 /*!
740 742 Returns the position on the chart widget that corresponds to the \a value in the \a series domain.
741 743 */
742 744 QPointF QChart::mapToPosition(const QPointF &value, QAbstractSeries *series)
743 745 {
744 746 return d_ptr->m_dataset->mapToPosition(value, series);
745 747 }
746 748
747 749 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
748 750
749 751 QChartPrivate::QChartPrivate(QChart *q, QChart::ChartType type):
750 752 q_ptr(q),
751 753 m_legend(0),
752 754 m_dataset(new ChartDataSet(q)),
753 755 m_presenter(new ChartPresenter(q, type)),
754 756 m_themeManager(new ChartThemeManager(q)),
755 757 m_type(type)
756 758 {
757 759 QObject::connect(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), m_presenter, SLOT(handleSeriesAdded(QAbstractSeries*)));
758 760 QObject::connect(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), m_presenter, SLOT(handleSeriesRemoved(QAbstractSeries*)));
759 761 QObject::connect(m_dataset, SIGNAL(axisAdded(QAbstractAxis*)), m_presenter, SLOT(handleAxisAdded(QAbstractAxis*)));
760 762 QObject::connect(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)), m_presenter, SLOT(handleAxisRemoved(QAbstractAxis*)));
761 763 QObject::connect(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), m_themeManager, SLOT(handleSeriesAdded(QAbstractSeries*)));
762 764 QObject::connect(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), m_themeManager, SLOT(handleSeriesRemoved(QAbstractSeries*)));
763 765 QObject::connect(m_dataset, SIGNAL(axisAdded(QAbstractAxis*)), m_themeManager, SLOT(handleAxisAdded(QAbstractAxis*)));
764 766 QObject::connect(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)), m_themeManager, SLOT(handleAxisRemoved(QAbstractAxis*)));
767 QObject::connect(m_presenter, &ChartPresenter::plotAreaChanged, q, &QChart::plotAreaChanged);
765 768 }
766 769
767 770 QChartPrivate::~QChartPrivate()
768 771 {
769 772 }
770 773
771 774 // Hackish solution to the problem of explicitly assigning the default pen/brush/font
772 775 // to a series or axis and having theme override it:
773 776 // Initialize pens, brushes, and fonts to something nobody is likely to ever use,
774 777 // so that default theme initialization will always set these properly.
775 778 QPen &QChartPrivate::defaultPen()
776 779 {
777 780 static QPen defaultPen(QColor(1, 2, 0), 0.93247536);
778 781 return defaultPen;
779 782 }
780 783
781 784 QBrush &QChartPrivate::defaultBrush()
782 785 {
783 786 static QBrush defaultBrush(QColor(1, 2, 0), Qt::Dense7Pattern);
784 787 return defaultBrush;
785 788 }
786 789
787 790 QFont &QChartPrivate::defaultFont()
788 791 {
789 792 static bool defaultFontInitialized(false);
790 793 static QFont defaultFont;
791 794 if (!defaultFontInitialized) {
792 795 defaultFont.setPointSizeF(8.34563465);
793 796 defaultFontInitialized = true;
794 797 }
795 798 return defaultFont;
796 799 }
797 800
798 801 void QChartPrivate::init()
799 802 {
800 803 m_legend = new LegendScroller(q_ptr);
801 804 q_ptr->setTheme(QChart::ChartThemeLight);
802 805 q_ptr->setLayout(m_presenter->layout());
803 806 }
804 807
805 808 void QChartPrivate::zoomIn(qreal factor)
806 809 {
807 810 QRectF rect = m_presenter->geometry();
808 811 rect.setWidth(rect.width() / factor);
809 812 rect.setHeight(rect.height() / factor);
810 813 rect.moveCenter(m_presenter->geometry().center());
811 814 zoomIn(rect);
812 815 }
813 816
814 817 void QChartPrivate::zoomIn(const QRectF &rect)
815 818 {
816 819 if (!rect.isValid())
817 820 return;
818 821
819 822 QRectF r = rect.normalized();
820 823 const QRectF geometry = m_presenter->geometry();
821 824 r.translate(-geometry.topLeft());
822 825
823 826 if (!r.isValid())
824 827 return;
825 828
826 829 QPointF zoomPoint(r.center().x() / geometry.width(), r.center().y() / geometry.height());
827 830 m_presenter->setState(ChartPresenter::ZoomInState,zoomPoint);
828 831 m_dataset->zoomInDomain(r);
829 832 m_presenter->setState(ChartPresenter::ShowState,QPointF());
830 833
831 834 }
832 835
833 836 void QChartPrivate::zoomReset()
834 837 {
835 838 m_dataset->zoomResetDomain();
836 839 }
837 840
838 841 bool QChartPrivate::isZoomed()
839 842 {
840 843 return m_dataset->isZoomedDomain();
841 844 }
842 845
843 846 void QChartPrivate::zoomOut(qreal factor)
844 847 {
845 848 const QRectF geometry = m_presenter->geometry();
846 849
847 850 QRectF r;
848 851 r.setSize(geometry.size() / factor);
849 852 r.moveCenter(QPointF(geometry.size().width()/2 ,geometry.size().height()/2));
850 853 if (!r.isValid())
851 854 return;
852 855
853 856 QPointF zoomPoint(r.center().x() / geometry.width(), r.center().y() / geometry.height());
854 857 m_presenter->setState(ChartPresenter::ZoomOutState,zoomPoint);
855 858 m_dataset->zoomOutDomain(r);
856 859 m_presenter->setState(ChartPresenter::ShowState,QPointF());
857 860 }
858 861
859 862 void QChartPrivate::scroll(qreal dx, qreal dy)
860 863 {
861 864 if (dx < 0) m_presenter->setState(ChartPresenter::ScrollLeftState,QPointF());
862 865 if (dx > 0) m_presenter->setState(ChartPresenter::ScrollRightState,QPointF());
863 866 if (dy < 0) m_presenter->setState(ChartPresenter::ScrollUpState,QPointF());
864 867 if (dy > 0) m_presenter->setState(ChartPresenter::ScrollDownState,QPointF());
865 868
866 869 m_dataset->scrollDomain(dx, dy);
867 870 m_presenter->setState(ChartPresenter::ShowState,QPointF());
868 871 }
869 872
870 873 #include "moc_qchart.cpp"
871 874
872 875 QT_CHARTS_END_NAMESPACE
@@ -1,183 +1,187
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QCHART_H
22 22 #define QCHART_H
23 23
24 24 #include <QtCharts/QAbstractSeries>
25 25 #include <QtCharts/QLegend>
26 26 #include <QtWidgets/QGraphicsWidget>
27 27 #include <QtCore/QMargins>
28 28
29 29 class QGraphicsSceneResizeEvent;
30 30
31 31 QT_CHARTS_BEGIN_NAMESPACE
32 32
33 33 class QAbstractSeries;
34 34 class QAbstractAxis;
35 35 class QLegend;
36 36 class QChartPrivate;
37 37 class QBoxPlotSeries;
38 38
39 39 class QT_CHARTS_EXPORT QChart : public QGraphicsWidget
40 40 {
41 41 Q_OBJECT
42 42 Q_PROPERTY(QChart::ChartTheme theme READ theme WRITE setTheme)
43 43 Q_PROPERTY(QString title READ title WRITE setTitle)
44 44 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible)
45 45 Q_PROPERTY(bool dropShadowEnabled READ isDropShadowEnabled WRITE setDropShadowEnabled)
46 46 Q_PROPERTY(qreal backgroundRoundness READ backgroundRoundness WRITE setBackgroundRoundness)
47 47 Q_PROPERTY(QChart::AnimationOptions animationOptions READ animationOptions WRITE setAnimationOptions)
48 48 Q_PROPERTY(QMargins minimumMargins READ minimumMargins WRITE setMinimumMargins)
49 49 Q_PROPERTY(QMargins margins READ margins WRITE setMargins)
50 50 Q_PROPERTY(QChart::ChartType chartType READ chartType)
51 51 Q_PROPERTY(bool plotAreaBackgroundVisible READ isPlotAreaBackgroundVisible WRITE setPlotAreaBackgroundVisible)
52 52 Q_PROPERTY(bool localizeNumbers READ localizeNumbers WRITE setLocalizeNumbers)
53 53 Q_PROPERTY(QLocale locale READ locale WRITE setLocale)
54 Q_PROPERTY(QRectF plotArea READ plotArea NOTIFY plotAreaChanged)
54 55 Q_ENUMS(ChartTheme)
55 56 Q_ENUMS(AnimationOption)
56 57 Q_ENUMS(ChartType)
57 58
58 59 public:
59 60 enum ChartType {
60 61 ChartTypeUndefined = 0,
61 62 ChartTypeCartesian,
62 63 ChartTypePolar
63 64 };
64 65
65 66 enum ChartTheme {
66 67 ChartThemeLight = 0,
67 68 ChartThemeBlueCerulean,
68 69 ChartThemeDark,
69 70 ChartThemeBrownSand,
70 71 ChartThemeBlueNcs,
71 72 ChartThemeHighContrast,
72 73 ChartThemeBlueIcy,
73 74 ChartThemeQt
74 75 };
75 76
76 77 enum AnimationOption {
77 78 NoAnimation = 0x0,
78 79 GridAxisAnimations = 0x1,
79 80 SeriesAnimations = 0x2,
80 81 AllAnimations = 0x3
81 82 };
82 83
83 84 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
84 85
85 86 public:
86 87 explicit QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
87 88 ~QChart();
88 89
89 90 void addSeries(QAbstractSeries *series);
90 91 void removeSeries(QAbstractSeries *series);
91 92 void removeAllSeries();
92 93 QList<QAbstractSeries *> series() const;
93 94
94 95 // *** deprecated ***
95 96 void setAxisX(QAbstractAxis *axis, QAbstractSeries *series = 0);
96 97 void setAxisY(QAbstractAxis *axis, QAbstractSeries *series = 0);
97 98 QAbstractAxis *axisX(QAbstractSeries *series = 0) const;
98 99 QAbstractAxis *axisY(QAbstractSeries *series = 0) const;
99 100 // ******************
100 101
101 102 void addAxis(QAbstractAxis *axis, Qt::Alignment alignment);
102 103 void removeAxis(QAbstractAxis *axis);
103 104 QList<QAbstractAxis*> axes(Qt::Orientations orientation = Qt::Horizontal|Qt::Vertical, QAbstractSeries *series = 0) const;
104 105
105 106 void createDefaultAxes();
106 107
107 108 void setTheme(QChart::ChartTheme theme);
108 109 QChart::ChartTheme theme() const;
109 110
110 111 void setTitle(const QString &title);
111 112 QString title() const;
112 113 void setTitleFont(const QFont &font);
113 114 QFont titleFont() const;
114 115 void setTitleBrush(const QBrush &brush);
115 116 QBrush titleBrush() const;
116 117
117 118 void setBackgroundBrush(const QBrush &brush);
118 119 QBrush backgroundBrush() const;
119 120 void setBackgroundPen(const QPen &pen);
120 121 QPen backgroundPen() const;
121 122 void setBackgroundVisible(bool visible = true);
122 123 bool isBackgroundVisible() const;
123 124
124 125 void setDropShadowEnabled(bool enabled = true);
125 126 bool isDropShadowEnabled() const;
126 127 void setBackgroundRoundness(qreal diameter);
127 128 qreal backgroundRoundness() const;
128 129 void setAnimationOptions(AnimationOptions options);
129 130 AnimationOptions animationOptions() const;
130 131
131 132 void zoomIn();
132 133 void zoomOut();
133 134
134 135 void zoomIn(const QRectF &rect);
135 136 void zoom(qreal factor);
136 137 void zoomReset();
137 138 bool isZoomed();
138 139
139 140 void scroll(qreal dx, qreal dy);
140 141
141 142 QLegend *legend() const;
142 143
143 144 void setMinimumMargins(const QMargins& margins);
144 145 QMargins minimumMargins() const;
145 146
146 147 void setMargins(const QMargins &margins);
147 148 QMargins margins() const;
148 149
149 150 QRectF plotArea() const;
150 151 void setPlotAreaBackgroundBrush(const QBrush &brush);
151 152 QBrush plotAreaBackgroundBrush() const;
152 153 void setPlotAreaBackgroundPen(const QPen &pen);
153 154 QPen plotAreaBackgroundPen() const;
154 155 void setPlotAreaBackgroundVisible(bool visible = true);
155 156 bool isPlotAreaBackgroundVisible() const;
156 157 void setLocalizeNumbers(bool localize);
157 158 bool localizeNumbers() const;
158 159 void setLocale(const QLocale &locale);
159 160 QLocale locale() const;
160 161
161 162 QPointF mapToValue(const QPointF &position, QAbstractSeries *series = 0);
162 163 QPointF mapToPosition(const QPointF &value, QAbstractSeries *series = 0);
163 164
164 165 ChartType chartType() const;
165 166
167 signals:
168 void plotAreaChanged(const QRectF &plotArea);
169
166 170 protected:
167 171 explicit QChart(QChart::ChartType type, QGraphicsItem *parent, Qt::WindowFlags wFlags);
168 172 QScopedPointer<QChartPrivate> d_ptr;
169 173 friend class QLegend;
170 174 friend class DeclarativeChart;
171 175 friend class ChartDataSet;
172 176 friend class ChartPresenter;
173 177 friend class ChartThemeManager;
174 178 friend class QAbstractSeries;
175 179 friend class QBoxPlotSeriesPrivate;
176 180 Q_DISABLE_COPY(QChart)
177 181 };
178 182
179 183 QT_CHARTS_END_NAMESPACE
180 184
181 185 Q_DECLARE_OPERATORS_FOR_FLAGS(QT_CHARTS_NAMESPACE::QChart::AnimationOptions)
182 186
183 187 #endif // QCHART_H
@@ -1,1063 +1,1051
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativechart.h"
22 22 #include <QtGui/QPainter>
23 23 #include "declarativelineseries.h"
24 24 #include "declarativeareaseries.h"
25 25 #include "declarativebarseries.h"
26 26 #include "declarativepieseries.h"
27 27 #include "declarativesplineseries.h"
28 28 #include "declarativeboxplotseries.h"
29 29 #include "declarativescatterseries.h"
30 30 #include <QtCharts/QBarCategoryAxis>
31 31 #include <QtCharts/QValueAxis>
32 32 #include <QtCharts/QLogValueAxis>
33 33 #include <QtCharts/QCategoryAxis>
34 34 #include <private/qabstractseries_p.h>
35 35 #include "declarativemargins.h"
36 36 #include <private/chartdataset_p.h>
37 37 #include "declarativeaxes.h"
38 38 #include <private/qchart_p.h>
39 39 #include <QtCharts/QPolarChart>
40 40
41 41 #ifndef QT_ON_ARM
42 42 #include <QtCharts/QDateTimeAxis>
43 43 #endif
44 44
45 45 #include <QtWidgets/QGraphicsSceneMouseEvent>
46 46 #include <QtWidgets/QGraphicsSceneHoverEvent>
47 47 #include <QtWidgets/QApplication>
48 48 #include <QtCore/QTimer>
49 49 #include <QtCore/QThread>
50 50
51 51 QT_CHARTS_BEGIN_NAMESPACE
52 52
53 53 /*!
54 54 \qmltype ChartView
55 55 \instantiates DeclarativeChart
56 56 \inqmlmodule QtCharts
57 57
58 58 ChartView element is the parent that is responsible for showing different chart series types.
59 59
60 60 The following QML shows how to create a simple chart with one pie series:
61 61 \snippet qmlpiechart/qml/qmlpiechart/main.qml 1
62 62 \snippet qmlpiechart/qml/qmlpiechart/main.qml 2
63 63 \snippet qmlpiechart/qml/qmlpiechart/main.qml 3
64 64
65 65 \beginfloatleft
66 66 \image examples_qmlpiechart.png
67 67 \endfloat
68 68 \clearfloat
69 69 */
70 70
71 71 /*!
72 72 \qmlproperty Theme ChartView::theme
73 73 Theme defines the visual appearance of the chart, including for example colors, fonts, line
74 74 widths and chart background.
75 75 */
76 76
77 77 /*!
78 78 \qmlproperty Animation ChartView::animationOptions
79 79 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
80 80 ChartView.SeriesAnimations or ChartView.AllAnimations.
81 81 */
82 82
83 83 /*!
84 84 \qmlproperty Font ChartView::titleFont
85 85 The title font of the chart.
86 86
87 87 See the Qt documentation for more details of Font.
88 88 */
89 89
90 90 /*!
91 91 \qmlproperty string ChartView::title
92 92 The title of the chart, shown on top of the chart.
93 93 \sa ChartView::titleColor
94 94 */
95 95
96 96 /*!
97 97 \qmlproperty color ChartView::titleColor
98 98 The color of the title text.
99 99 */
100 100
101 101 /*!
102 102 \qmlproperty Legend ChartView::legend
103 103 The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart.
104 104 */
105 105
106 106 /*!
107 107 \qmlproperty int ChartView::count
108 108 The count of series added to the chart.
109 109 */
110 110
111 111 /*!
112 112 \qmlproperty color ChartView::backgroundColor
113 113 The color of the chart's background. By default background color is defined by chart theme.
114 114 \sa ChartView::theme
115 115 */
116 116
117 117 /*!
118 118 \qmlproperty real ChartView::backgroundRoundness
119 119 The diameter of the rounding cirle at the corners of the chart background.
120 120 */
121 121
122 122 /*!
123 123 \qmlproperty color ChartView::plotAreaColor
124 124 The color of the background of the chart's plot area. By default plot area background uses chart's
125 125 background color.
126 126 \sa ChartView::backgroundColor
127 127 */
128 128
129 129 /*!
130 130 \qmlproperty bool ChartView::dropShadowEnabled
131 131 The chart's border drop shadow. Set to true to enable drop shadow.
132 132 */
133 133
134 134 /*!
135 135 \qmlproperty real ChartView::topMargin
136 136 Deprecated; use margins instead.
137 137 */
138 138
139 139 /*!
140 140 \qmlproperty real ChartView::bottomMargin
141 141 Deprecated; use margins instead.
142 142 */
143 143
144 144 /*!
145 145 \qmlproperty real ChartView::leftMargin
146 146 Deprecated; use margins instead.
147 147 */
148 148
149 149 /*!
150 150 \qmlproperty real ChartView::rightMargin
151 151 Deprecated; use margins instead.
152 152 */
153 153
154 154 /*!
155 155 \qmlproperty Margins ChartView::minimumMargins
156 156 Deprecated; use margins instead.
157 157 The minimum margins allowed between the outer bounds and the plotArea of the ChartView. Margins
158 158 area of ChartView is used for drawing title, axes and legend. Please note that setting the
159 159 properties of minimumMargins may be bigger than the defined value, depending on other ChartView
160 160 properties that affect it's layout. If you need to know the actual plotting area used at any
161 161 given time, you can check ChartView::plotArea instead.
162 162 */
163 163
164 164 /*!
165 165 \qmlproperty rect ChartView::plotArea
166 166 The area on the ChartView that is used for drawing series. This is the ChartView rect without the
167 167 margins.
168 168 \sa ChartView::minimumMargins
169 169 */
170 170
171 171 /*!
172 172 \qmlproperty Margins ChartView::margins
173 173 The minimum margins allowed between the outer bounds and the plotArea of the ChartView. Margins
174 174 area of ChartView is used for drawing title, axes and legend.
175 175 */
176 176
177 177 /*!
178 178 \qmlproperty bool ChartView::localizeNumbers
179 179 \since QtCharts 2.0
180 180 When \c{true}, all generated numbers appearing in various series and axis labels will be
181 181 localized using the default QLocale of the application, which defaults to the system locale.
182 182 When \c{false}, the "C" locale is always used.
183 183 Defaults to \c{false}.
184 184
185 185 \sa locale
186 186 */
187 187
188 188 /*!
189 189 \qmlproperty locale ChartView::locale
190 190 \since QtCharts 2.0
191 191 Sets the locale used to format various chart labels when localizeNumbers is \c{true}.
192 192 This also determines the locale used to format DateTimeAxis labels regardless of
193 193 localizeNumbers property.
194 194 Defaults to application default locale at the time the chart is constructed.
195 195
196 196 \sa localizeNumbers
197 197 */
198 198
199 199 /*!
200 200 \qmlmethod AbstractSeries ChartView::series(int index)
201 201 Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with
202 202 the count property of the chart.
203 203 */
204 204
205 205 /*!
206 206 \qmlmethod AbstractSeries ChartView::series(string name)
207 207 Returns the first series on the chart with \a name. If there is no series with that name, returns null.
208 208 */
209 209
210 210 /*!
211 211 \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name, AbstractAxis axisX, AbstractAxis axisY)
212 212 Creates a series object of \a type to the chart with name \a name, optional axis \a axisX and
213 213 optional axis \a axisY. For example:
214 214 \code
215 215 // lineSeries is a LineSeries object that has already been added to the ChartView; re-use it's axes
216 216 var myAxisX = chartView.axisX(lineSeries);
217 217 var myAxisY = chartView.axisY(lineSeries);
218 218 var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series", myAxisX, myAxisY);
219 219 \endcode
220 220 */
221 221
222 222 /*!
223 223 \qmlmethod ChartView::removeSeries(AbstractSeries series)
224 224 Removes the \a series from the chart. The series object is also destroyed.
225 225 */
226 226
227 227 /*!
228 228 \qmlmethod ChartView::removeAllSeries()
229 229 Removes all series from the chart. All the series objects are also destroyed.
230 230 */
231 231
232 232 /*!
233 233 \qmlmethod Axis ChartView::axisX(AbstractSeries series)
234 234 The x-axis of the series.
235 235 */
236 236
237 237 /*!
238 238 \qmlmethod Axis ChartView::axisY(AbstractSeries series)
239 239 The y-axis of the series.
240 240 */
241 241
242 242 /*!
243 243 \qmlmethod ChartView::zoomY(real factor)
244 244 Zooms in by \a factor on the center of the chart.
245 245 */
246 246
247 247 /*!
248 248 \qmlmethod ChartView::scrollLeft(real pixels)
249 249 Scrolls to left by \a pixels. This is a convenience function that suits for example for key navigation.
250 250 */
251 251
252 252 /*!
253 253 \qmlmethod ChartView::scrollRight(real pixels)
254 254 Scrolls to right by \a pixels. This is a convenience function that suits for example for key navigation.
255 255 */
256 256
257 257 /*!
258 258 \qmlmethod ChartView::scrollUp(real pixels)
259 259 Scrolls up by \a pixels. This is a convenience function that suits for example for key navigation.
260 260 */
261 261
262 262 /*!
263 263 \qmlmethod ChartView::scrollDown(real pixels)
264 264 Scrolls down by \a pixels. This is a convenience function that suits for example for key navigation.
265 265 */
266 266
267 267 /*!
268 \qmlsignal ChartView::onPlotAreaChanged(rect plotArea)
269 The plot area of the chart has changed. This may happen for example, if you modify minimumMargins
270 or if you resize the chart, or if you modify font size related properties of the legend or chart
271 title.
272 */
273
274 /*!
275 268 \qmlsignal ChartView::seriesAdded(AbstractSeries series)
276 269 The \a series has been added to the chart.
277 270 */
278 271
279 272 /*!
280 273 \qmlsignal ChartView::seriesRemoved(AbstractSeries series)
281 274 The \a series has been removed from the chart. Please note that \a series is no longer a valid
282 275 object after the signal handler has completed.
283 276 */
284 277
285 278 DeclarativeChart::DeclarativeChart(QQuickItem *parent)
286 279 : QQuickPaintedItem(parent)
287 280 {
288 281 initChart(QChart::ChartTypeCartesian);
289 282 }
290 283
291 284 DeclarativeChart::DeclarativeChart(QChart::ChartType type, QQuickItem *parent)
292 285 : QQuickPaintedItem(parent)
293 286 {
294 287 initChart(type);
295 288 }
296 289
297 290 void DeclarativeChart::initChart(QChart::ChartType type)
298 291 {
299 292 m_currentSceneImage = 0;
300 293 m_guiThreadId = QThread::currentThreadId();
301 294 m_paintThreadId = 0;
302 295 m_updatePending = false;
303 296
304 297 if (type == QChart::ChartTypePolar)
305 298 m_chart = new QPolarChart();
306 299 else
307 300 m_chart = new QChart();
308 301
309 302 m_scene = new QGraphicsScene(this);
310 303 m_scene->addItem(m_chart);
311 304
312 305 setAntialiasing(QQuickItem::antialiasing());
313 306 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(sceneChanged(QList<QRectF>)));
314 307 connect(this, SIGNAL(antialiasingChanged(bool)), this, SLOT(handleAntialiasingChanged(bool)));
315 308
316 309 setAcceptedMouseButtons(Qt::AllButtons);
317 310 setAcceptHoverEvents(true);
318 311
319 312 m_margins = new DeclarativeMargins(this);
320 313 m_margins->setTop(m_chart->margins().top());
321 314 m_margins->setLeft(m_chart->margins().left());
322 315 m_margins->setRight(m_chart->margins().right());
323 316 m_margins->setBottom(m_chart->margins().bottom());
324 317 connect(m_margins, SIGNAL(topChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
325 318 connect(m_margins, SIGNAL(bottomChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
326 319 connect(m_margins, SIGNAL(leftChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
327 320 connect(m_margins, SIGNAL(rightChanged(int,int,int,int)), this, SLOT(changeMinimumMargins(int,int,int,int)));
328 321 connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), this, SLOT(handleSeriesAdded(QAbstractSeries*)));
329 322 connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), this, SIGNAL(seriesRemoved(QAbstractSeries*)));
323 connect(m_chart, &QChart::plotAreaChanged, this, &DeclarativeChart::plotAreaChanged);
330 324 }
331 325
332 326 void DeclarativeChart::handleSeriesAdded(QAbstractSeries *series)
333 327 {
334 328 emit seriesAdded(series);
335 329 }
336 330
337 331 void DeclarativeChart::changeMinimumMargins(int top, int bottom, int left, int right)
338 332 {
339 333 m_chart->setMargins(QMargins(left, top, right, bottom));
340 334 emit minimumMarginsChanged();
341 emit plotAreaChanged(m_chart->plotArea());
342 335 }
343 336
344 337 DeclarativeChart::~DeclarativeChart()
345 338 {
346 339 delete m_chart;
347 340 m_sceneImageLock.lock();
348 341 delete m_currentSceneImage;
349 342 m_currentSceneImage = 0;
350 343 m_sceneImageLock.unlock();
351 344 }
352 345
353 346 void DeclarativeChart::childEvent(QChildEvent *event)
354 347 {
355 348 if (event->type() == QEvent::ChildAdded) {
356 349 if (qobject_cast<QAbstractSeries *>(event->child())) {
357 350 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
358 351 }
359 352 }
360 353 }
361 354
362 355 void DeclarativeChart::componentComplete()
363 356 {
364 357 foreach (QObject *child, children()) {
365 358 if (qobject_cast<QAbstractSeries *>(child)) {
366 359 // Add series to the chart
367 360 QAbstractSeries *series = qobject_cast<QAbstractSeries *>(child);
368 361 m_chart->addSeries(series);
369 362
370 363 // Connect to axis changed signals (unless this is a pie series)
371 364 if (!qobject_cast<DeclarativePieSeries *>(series)) {
372 365 connect(series, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
373 366 connect(series, SIGNAL(axisXTopChanged(QAbstractAxis*)), this, SLOT(handleAxisXTopSet(QAbstractAxis*)));
374 367 connect(series, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
375 368 connect(series, SIGNAL(axisYRightChanged(QAbstractAxis*)), this, SLOT(handleAxisYRightSet(QAbstractAxis*)));
376 369 }
377 370
378 371 initializeAxes(series);
379 372 }
380 373 }
381 374
382 375 QQuickItem::componentComplete();
383 376 }
384 377
385 378 void DeclarativeChart::handleAxisXSet(QAbstractAxis *axis)
386 379 {
387 380 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
388 381 if (axis && s) {
389 382 if (!m_chart->axes(Qt::Horizontal).contains(axis))
390 383 m_chart->setAxisX(axis, s);
391 384 if (!s->attachedAxes().contains(axis))
392 385 s->attachAxis(axis);
393 386 } else {
394 387 qWarning() << "Trying to set axisX to null.";
395 388 }
396 389 }
397 390
398 391 void DeclarativeChart::handleAxisXTopSet(QAbstractAxis *axis)
399 392 {
400 393 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
401 394 if (axis && s) {
402 395 if (!m_chart->axes(Qt::Horizontal).contains(axis)) {
403 396 QList<QAbstractAxis *> oldAxes = m_chart->axes(Qt::Horizontal, s);
404 397 foreach (QAbstractAxis* a, oldAxes) {
405 398 m_chart->removeAxis(a);
406 399 delete a;
407 400 }
408 401 m_chart->addAxis(axis, Qt::AlignTop);
409 402 }
410 403 if (!s->attachedAxes().contains(axis))
411 404 s->attachAxis(axis);
412 405 } else {
413 406 qWarning() << "Trying to set axisXTop to null.";
414 407 }
415 408 }
416 409
417 410 void DeclarativeChart::handleAxisYSet(QAbstractAxis *axis)
418 411 {
419 412 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
420 413 if (axis && s) {
421 414 if (!m_chart->axes(Qt::Vertical).contains(axis))
422 415 m_chart->setAxisY(axis, s);
423 416 if (!s->attachedAxes().contains(axis))
424 417 s->attachAxis(axis);
425 418 } else {
426 419 qWarning() << "Trying to set axisY to null.";
427 420 }
428 421 }
429 422
430 423 void DeclarativeChart::handleAxisYRightSet(QAbstractAxis *axis)
431 424 {
432 425 QAbstractSeries *s = qobject_cast<QAbstractSeries *>(sender());
433 426 if (axis && s) {
434 427 if (!m_chart->axes(Qt::Vertical).contains(axis)) {
435 428 QList<QAbstractAxis *> oldAxes = m_chart->axes((Qt::Vertical), s);
436 429 foreach (QAbstractAxis* a, oldAxes) {
437 430 m_chart->removeAxis(a);
438 431 delete a;
439 432 }
440 433 m_chart->addAxis(axis, Qt::AlignRight);
441 434 }
442 435 if (!s->attachedAxes().contains(axis))
443 436 s->attachAxis(axis);
444 437 } else {
445 438 qWarning() << "Trying to set axisYRight to null.";
446 439 }
447 440 }
448 441
449 442 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
450 443 {
451 444 if (newGeometry.isValid()) {
452 445 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
453 446 m_chart->resize(newGeometry.width(), newGeometry.height());
454 447 }
455 448 }
456 449 QQuickItem::geometryChanged(newGeometry, oldGeometry);
457
458 // It would be better to trigger the plotAreaChanged signal from QChart::plotAreaChanged or
459 // similar. Since that kind of a signal is not clearly needed in the C++ API the work-around is
460 // to implement it here for the QML API purposes.
461 emit plotAreaChanged(m_chart->plotArea());
462 450 }
463 451
464 452 void DeclarativeChart::sceneChanged(QList<QRectF> region)
465 453 {
466 454 Q_UNUSED(region);
467 455
468 456 if (m_guiThreadId == m_paintThreadId) {
469 457 // Rendering in gui thread, no need for shenannigans, just update
470 458 update();
471 459 } else {
472 460 // Multi-threaded rendering, need to ensure scene is actually rendered in gui thread
473 461 if (!m_updatePending) {
474 462 m_updatePending = true;
475 463 // Do async render to avoid some unnecessary renders.
476 464 QTimer::singleShot(0, this, SLOT(renderScene()));
477 465 }
478 466 }
479 467 }
480 468
481 469 void DeclarativeChart::renderScene()
482 470 {
483 471 m_updatePending = false;
484 472 m_sceneImageLock.lock();
485 473 delete m_currentSceneImage;
486 474 m_currentSceneImage = new QImage(m_chart->size().toSize(), QImage::Format_ARGB32);
487 475 m_currentSceneImage->fill(Qt::transparent);
488 476 QPainter painter(m_currentSceneImage);
489 477 if (antialiasing())
490 478 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
491 479 QRect renderRect(QPoint(0, 0), m_chart->size().toSize());
492 480 m_scene->render(&painter, renderRect, renderRect);
493 481 m_sceneImageLock.unlock();
494 482
495 483 update();
496 484 }
497 485
498 486 void DeclarativeChart::paint(QPainter *painter)
499 487 {
500 488 if (!m_paintThreadId) {
501 489 m_paintThreadId = QThread::currentThreadId();
502 490 if (m_guiThreadId == m_paintThreadId) {
503 491 // No need for scene image in single threaded rendering, so delete
504 492 // the one that got made by default before the rendering type was
505 493 // detected.
506 494 delete m_currentSceneImage;
507 495 m_currentSceneImage = 0;
508 496 }
509 497 }
510 498
511 499 if (m_guiThreadId == m_paintThreadId) {
512 500 QRectF renderRect(QPointF(0, 0), m_chart->size());
513 501 m_scene->render(painter, renderRect, renderRect);
514 502 } else {
515 503 m_sceneImageLock.lock();
516 504 if (m_currentSceneImage) {
517 505 QRect imageRect(QPoint(0, 0), m_currentSceneImage->size());
518 506 QRect itemRect(QPoint(0, 0), QSize(width(), height()));
519 507 painter->drawImage(itemRect, *m_currentSceneImage, imageRect);
520 508 }
521 509 m_sceneImageLock.unlock();
522 510 }
523 511 }
524 512
525 513 void DeclarativeChart::mousePressEvent(QMouseEvent *event)
526 514 {
527 515 m_mousePressScenePoint = event->pos();
528 516 m_mousePressScreenPoint = event->globalPos();
529 517 m_lastMouseMoveScenePoint = m_mousePressScenePoint;
530 518 m_lastMouseMoveScreenPoint = m_mousePressScreenPoint;
531 519 m_mousePressButton = event->button();
532 520 m_mousePressButtons = event->buttons();
533 521
534 522 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMousePress);
535 523 mouseEvent.setWidget(0);
536 524 mouseEvent.setButtonDownScenePos(m_mousePressButton, m_mousePressScenePoint);
537 525 mouseEvent.setButtonDownScreenPos(m_mousePressButton, m_mousePressScreenPoint);
538 526 mouseEvent.setScenePos(m_mousePressScenePoint);
539 527 mouseEvent.setScreenPos(m_mousePressScreenPoint);
540 528 mouseEvent.setLastScenePos(m_lastMouseMoveScenePoint);
541 529 mouseEvent.setLastScreenPos(m_lastMouseMoveScreenPoint);
542 530 mouseEvent.setButtons(m_mousePressButtons);
543 531 mouseEvent.setButton(m_mousePressButton);
544 532 mouseEvent.setModifiers(event->modifiers());
545 533 mouseEvent.setAccepted(false);
546 534
547 535 QApplication::sendEvent(m_scene, &mouseEvent);
548 536 }
549 537
550 538 void DeclarativeChart::mouseReleaseEvent(QMouseEvent *event)
551 539 {
552 540 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseRelease);
553 541 mouseEvent.setWidget(0);
554 542 mouseEvent.setButtonDownScenePos(m_mousePressButton, m_mousePressScenePoint);
555 543 mouseEvent.setButtonDownScreenPos(m_mousePressButton, m_mousePressScreenPoint);
556 544 mouseEvent.setScenePos(event->pos());
557 545 mouseEvent.setScreenPos(event->globalPos());
558 546 mouseEvent.setLastScenePos(m_lastMouseMoveScenePoint);
559 547 mouseEvent.setLastScreenPos(m_lastMouseMoveScreenPoint);
560 548 mouseEvent.setButtons(event->buttons());
561 549 mouseEvent.setButton(event->button());
562 550 mouseEvent.setModifiers(event->modifiers());
563 551 mouseEvent.setAccepted(false);
564 552
565 553 QApplication::sendEvent(m_scene, &mouseEvent);
566 554
567 555 m_mousePressButtons = event->buttons();
568 556 m_mousePressButton = Qt::NoButton;
569 557 }
570 558
571 559 void DeclarativeChart::hoverMoveEvent(QHoverEvent *event)
572 560 {
573 561 // Convert hover move to mouse move, since we don't seem to get actual mouse move events.
574 562 // QGraphicsScene generates hover events from mouse move events, so we don't need
575 563 // to pass hover events there.
576 564 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
577 565 mouseEvent.setWidget(0);
578 566 mouseEvent.setButtonDownScenePos(m_mousePressButton, m_mousePressScenePoint);
579 567 mouseEvent.setButtonDownScreenPos(m_mousePressButton, m_mousePressScreenPoint);
580 568 mouseEvent.setScenePos(event->pos());
581 569 // Hover events do not have global pos in them, and the screen position doesn't seem to
582 570 // matter anyway in this use case, so just pass event pos instead of trying to
583 571 // calculate the real screen position.
584 572 mouseEvent.setScreenPos(event->pos());
585 573 mouseEvent.setLastScenePos(m_lastMouseMoveScenePoint);
586 574 mouseEvent.setLastScreenPos(m_lastMouseMoveScreenPoint);
587 575 mouseEvent.setButtons(m_mousePressButtons);
588 576 mouseEvent.setButton(m_mousePressButton);
589 577 mouseEvent.setModifiers(event->modifiers());
590 578 m_lastMouseMoveScenePoint = mouseEvent.scenePos();
591 579 m_lastMouseMoveScreenPoint = mouseEvent.screenPos();
592 580 mouseEvent.setAccepted(false);
593 581
594 582 QApplication::sendEvent(m_scene, &mouseEvent);
595 583 }
596 584
597 585 void DeclarativeChart::handleAntialiasingChanged(bool enable)
598 586 {
599 587 setAntialiasing(enable);
600 588 }
601 589
602 590 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
603 591 {
604 592 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
605 593 if (chartTheme != m_chart->theme())
606 594 m_chart->setTheme(chartTheme);
607 595 }
608 596
609 597 DeclarativeChart::Theme DeclarativeChart::theme()
610 598 {
611 599 return (DeclarativeChart::Theme) m_chart->theme();
612 600 }
613 601
614 602 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
615 603 {
616 604 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
617 605 if (animationOptions != m_chart->animationOptions())
618 606 m_chart->setAnimationOptions(animationOptions);
619 607 }
620 608
621 609 DeclarativeChart::Animation DeclarativeChart::animationOptions()
622 610 {
623 611 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
624 612 return DeclarativeChart::AllAnimations;
625 613 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
626 614 return DeclarativeChart::GridAxisAnimations;
627 615 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
628 616 return DeclarativeChart::SeriesAnimations;
629 617 else
630 618 return DeclarativeChart::NoAnimation;
631 619 }
632 620
633 621 void DeclarativeChart::setTitle(QString title)
634 622 {
635 623 if (title != m_chart->title())
636 624 m_chart->setTitle(title);
637 625 }
638 626 QString DeclarativeChart::title()
639 627 {
640 628 return m_chart->title();
641 629 }
642 630
643 631 QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series)
644 632 {
645 633 QList<QAbstractAxis *> axes = m_chart->axes(Qt::Horizontal, series);
646 634 if (axes.count())
647 635 return axes[0];
648 636 return 0;
649 637 }
650 638
651 639 QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series)
652 640 {
653 641 QList<QAbstractAxis *> axes = m_chart->axes(Qt::Vertical, series);
654 642 if (axes.count())
655 643 return axes[0];
656 644 return 0;
657 645 }
658 646
659 647 QLegend *DeclarativeChart::legend()
660 648 {
661 649 return m_chart->legend();
662 650 }
663 651
664 652 void DeclarativeChart::setTitleColor(QColor color)
665 653 {
666 654 QBrush b = m_chart->titleBrush();
667 655 if (color != b.color()) {
668 656 b.setColor(color);
669 657 m_chart->setTitleBrush(b);
670 658 emit titleColorChanged(color);
671 659 }
672 660 }
673 661
674 662 QFont DeclarativeChart::titleFont() const
675 663 {
676 664 return m_chart->titleFont();
677 665 }
678 666
679 667 void DeclarativeChart::setTitleFont(const QFont &font)
680 668 {
681 669 m_chart->setTitleFont(font);
682 670 }
683 671
684 672 QColor DeclarativeChart::titleColor()
685 673 {
686 674 return m_chart->titleBrush().color();
687 675 }
688 676
689 677 void DeclarativeChart::setBackgroundColor(QColor color)
690 678 {
691 679 QBrush b = m_chart->backgroundBrush();
692 680 if (b.style() != Qt::SolidPattern || color != b.color()) {
693 681 b.setStyle(Qt::SolidPattern);
694 682 b.setColor(color);
695 683 m_chart->setBackgroundBrush(b);
696 684 emit backgroundColorChanged();
697 685 }
698 686 }
699 687
700 688 QColor DeclarativeChart::backgroundColor()
701 689 {
702 690 return m_chart->backgroundBrush().color();
703 691 }
704 692
705 693 void QtCharts::DeclarativeChart::setPlotAreaColor(QColor color)
706 694 {
707 695 QBrush b = m_chart->plotAreaBackgroundBrush();
708 696 if (b.style() != Qt::SolidPattern || color != b.color()) {
709 697 b.setStyle(Qt::SolidPattern);
710 698 b.setColor(color);
711 699 m_chart->setPlotAreaBackgroundBrush(b);
712 700 m_chart->setPlotAreaBackgroundVisible(true);
713 701 emit plotAreaColorChanged();
714 702 }
715 703 }
716 704
717 705 QColor QtCharts::DeclarativeChart::plotAreaColor()
718 706 {
719 707 return m_chart->plotAreaBackgroundBrush().color();
720 708 }
721 709
722 710 void DeclarativeChart::setLocalizeNumbers(bool localize)
723 711 {
724 712 if (m_chart->localizeNumbers() != localize) {
725 713 m_chart->setLocalizeNumbers(localize);
726 714 emit localizeNumbersChanged();
727 715 }
728 716 }
729 717
730 718 bool DeclarativeChart::localizeNumbers() const
731 719 {
732 720 return m_chart->localizeNumbers();
733 721 }
734 722
735 723 void QtCharts::DeclarativeChart::setLocale(const QLocale &locale)
736 724 {
737 725 if (m_chart->locale() != locale) {
738 726 m_chart->setLocale(locale);
739 727 emit localeChanged();
740 728 }
741 729 }
742 730
743 731 QLocale QtCharts::DeclarativeChart::locale() const
744 732 {
745 733 return m_chart->locale();
746 734 }
747 735
748 736 int DeclarativeChart::count()
749 737 {
750 738 return m_chart->series().count();
751 739 }
752 740
753 741 void DeclarativeChart::setDropShadowEnabled(bool enabled)
754 742 {
755 743 if (enabled != m_chart->isDropShadowEnabled()) {
756 744 m_chart->setDropShadowEnabled(enabled);
757 745 dropShadowEnabledChanged(enabled);
758 746 }
759 747 }
760 748
761 749 bool DeclarativeChart::dropShadowEnabled()
762 750 {
763 751 return m_chart->isDropShadowEnabled();
764 752 }
765 753
766 754 qreal DeclarativeChart::backgroundRoundness() const
767 755 {
768 756 return m_chart->backgroundRoundness();
769 757 }
770 758
771 759 void DeclarativeChart::setBackgroundRoundness(qreal diameter)
772 760 {
773 761 if (m_chart->backgroundRoundness() != diameter) {
774 762 m_chart->setBackgroundRoundness(diameter);
775 763 emit backgroundRoundnessChanged(diameter);
776 764 }
777 765 }
778 766
779 767 qreal DeclarativeChart::topMargin()
780 768 {
781 769 qWarning() << "ChartView.topMargin is deprecated. Use margins instead.";
782 770 return m_chart->margins().top();
783 771 }
784 772
785 773 qreal DeclarativeChart::bottomMargin()
786 774 {
787 775 qWarning() << "ChartView.bottomMargin is deprecated. Use margins instead.";
788 776 return m_chart->margins().bottom();
789 777 }
790 778
791 779 qreal DeclarativeChart::leftMargin()
792 780 {
793 781 qWarning() << "ChartView.leftMargin is deprecated. Use margins instead.";
794 782 return m_chart->margins().left();
795 783 }
796 784
797 785 qreal DeclarativeChart::rightMargin()
798 786 {
799 787 qWarning() << "ChartView.rightMargin is deprecated. Use margins instead.";
800 788 return m_chart->margins().right();
801 789 }
802 790
803 791 void DeclarativeChart::zoom(qreal factor)
804 792 {
805 793 m_chart->zoom(factor);
806 794 }
807 795
808 796 void DeclarativeChart::scrollLeft(qreal pixels)
809 797 {
810 798 m_chart->scroll(-pixels, 0);
811 799 }
812 800
813 801 void DeclarativeChart::scrollRight(qreal pixels)
814 802 {
815 803 m_chart->scroll(pixels, 0);
816 804 }
817 805
818 806 void DeclarativeChart::scrollUp(qreal pixels)
819 807 {
820 808 m_chart->scroll(0, pixels);
821 809 }
822 810
823 811 void DeclarativeChart::scrollDown(qreal pixels)
824 812 {
825 813 m_chart->scroll(0, -pixels);
826 814 }
827 815
828 816 QQmlListProperty<QAbstractAxis> DeclarativeChart::axes()
829 817 {
830 818 return QQmlListProperty<QAbstractAxis>(this, 0,
831 819 &DeclarativeChart::axesAppendFunc,
832 820 &DeclarativeChart::axesCountFunc,
833 821 &DeclarativeChart::axesAtFunc,
834 822 &DeclarativeChart::axesClearFunc);
835 823 }
836 824
837 825 void DeclarativeChart::axesAppendFunc(QQmlListProperty<QAbstractAxis> *list, QAbstractAxis *element)
838 826 {
839 827 // Empty implementation
840 828 Q_UNUSED(list);
841 829 Q_UNUSED(element);
842 830 }
843 831
844 832 int DeclarativeChart::axesCountFunc(QQmlListProperty<QAbstractAxis> *list)
845 833 {
846 834 if (qobject_cast<DeclarativeChart *>(list->object)) {
847 835 DeclarativeChart *chart = qobject_cast<DeclarativeChart *>(list->object);
848 836 return chart->m_chart->axes(Qt::Horizontal | Qt::Vertical).count();
849 837 }
850 838 return 0;
851 839 }
852 840
853 841 QAbstractAxis *DeclarativeChart::axesAtFunc(QQmlListProperty<QAbstractAxis> *list, int index)
854 842 {
855 843 if (qobject_cast<DeclarativeChart *>(list->object)) {
856 844 DeclarativeChart *chart = qobject_cast<DeclarativeChart *>(list->object);
857 845 QList<QAbstractAxis *> axes = chart->m_chart->axes(Qt::Horizontal | Qt::Vertical, chart->m_chart->series()[0]);
858 846 return axes.at(index);
859 847 }
860 848 return 0;
861 849 }
862 850
863 851 void DeclarativeChart::axesClearFunc(QQmlListProperty<QAbstractAxis> *list)
864 852 {
865 853 // Empty implementation
866 854 Q_UNUSED(list);
867 855 }
868 856
869 857
870 858 QAbstractSeries *DeclarativeChart::series(int index)
871 859 {
872 860 if (index < m_chart->series().count()) {
873 861 return m_chart->series().at(index);
874 862 }
875 863 return 0;
876 864 }
877 865
878 866 QAbstractSeries *DeclarativeChart::series(QString seriesName)
879 867 {
880 868 foreach (QAbstractSeries *series, m_chart->series()) {
881 869 if (series->name() == seriesName)
882 870 return series;
883 871 }
884 872 return 0;
885 873 }
886 874
887 875 QAbstractSeries *DeclarativeChart::createSeries(int type, QString name, QAbstractAxis *axisX, QAbstractAxis *axisY)
888 876 {
889 877 QAbstractSeries *series = 0;
890 878
891 879 switch (type) {
892 880 case DeclarativeChart::SeriesTypeLine:
893 881 series = new DeclarativeLineSeries();
894 882 break;
895 883 case DeclarativeChart::SeriesTypeArea: {
896 884 DeclarativeAreaSeries *area = new DeclarativeAreaSeries();
897 885 area->setUpperSeries(new DeclarativeLineSeries());
898 886 series = area;
899 887 break;
900 888 }
901 889 case DeclarativeChart::SeriesTypeStackedBar:
902 890 series = new DeclarativeStackedBarSeries();
903 891 break;
904 892 case DeclarativeChart::SeriesTypePercentBar:
905 893 series = new DeclarativePercentBarSeries();
906 894 break;
907 895 case DeclarativeChart::SeriesTypeBar:
908 896 series = new DeclarativeBarSeries();
909 897 break;
910 898 case DeclarativeChart::SeriesTypeHorizontalBar:
911 899 series = new DeclarativeHorizontalBarSeries();
912 900 break;
913 901 case DeclarativeChart::SeriesTypeHorizontalPercentBar:
914 902 series = new DeclarativeHorizontalPercentBarSeries();
915 903 break;
916 904 case DeclarativeChart::SeriesTypeHorizontalStackedBar:
917 905 series = new DeclarativeHorizontalStackedBarSeries();
918 906 break;
919 907 case DeclarativeChart::SeriesTypeBoxPlot:
920 908 series = new DeclarativeBoxPlotSeries();
921 909 break;
922 910 case DeclarativeChart::SeriesTypePie:
923 911 series = new DeclarativePieSeries();
924 912 break;
925 913 case DeclarativeChart::SeriesTypeScatter:
926 914 series = new DeclarativeScatterSeries();
927 915 break;
928 916 case DeclarativeChart::SeriesTypeSpline:
929 917 series = new DeclarativeSplineSeries();
930 918 break;
931 919 default:
932 920 qWarning() << "Illegal series type";
933 921 }
934 922
935 923 if (series) {
936 924 // Connect to axis changed signals (unless this is a pie series)
937 925 if (!qobject_cast<DeclarativePieSeries *>(series)) {
938 926 connect(series, SIGNAL(axisXChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
939 927 connect(series, SIGNAL(axisXTopChanged(QAbstractAxis*)), this, SLOT(handleAxisXSet(QAbstractAxis*)));
940 928 connect(series, SIGNAL(axisYChanged(QAbstractAxis*)), this, SLOT(handleAxisYSet(QAbstractAxis*)));
941 929 connect(series, SIGNAL(axisYRightChanged(QAbstractAxis*)), this, SLOT(handleAxisYRightSet(QAbstractAxis*)));
942 930 }
943 931
944 932 series->setName(name);
945 933 m_chart->addSeries(series);
946 934
947 935 if (axisX)
948 936 setAxisX(axisX, series);
949 937 if (axisY)
950 938 setAxisY(axisY, series);
951 939
952 940 if (series->attachedAxes().count() < 2)
953 941 initializeAxes(series);
954 942 }
955 943
956 944 return series;
957 945 }
958 946
959 947 void DeclarativeChart::removeSeries(QAbstractSeries *series)
960 948 {
961 949 if (series)
962 950 m_chart->removeSeries(series);
963 951 else
964 952 qWarning("removeSeries: cannot remove null");
965 953 }
966 954
967 955 void DeclarativeChart::setAxisX(QAbstractAxis *axis, QAbstractSeries *series)
968 956 {
969 957 if (axis)
970 958 m_chart->setAxisX(axis, series);
971 959 }
972 960
973 961 void DeclarativeChart::setAxisY(QAbstractAxis *axis, QAbstractSeries *series)
974 962 {
975 963 if (axis)
976 964 m_chart->setAxisY(axis, series);
977 965 }
978 966
979 967 void DeclarativeChart::createDefaultAxes()
980 968 {
981 969 qWarning() << "ChartView.createDefaultAxes() is deprecated. Axes are created automatically.";
982 970 }
983 971
984 972 QAbstractAxis *DeclarativeChart::defaultAxis(Qt::Orientation orientation, QAbstractSeries *series)
985 973 {
986 974 if (!series) {
987 975 qWarning() << "No axis type defined for null series";
988 976 return 0;
989 977 }
990 978
991 979 foreach (QAbstractAxis *existingAxis, m_chart->axes(orientation)) {
992 980 if (existingAxis->type() == series->d_ptr->defaultAxisType(orientation))
993 981 return existingAxis;
994 982 }
995 983
996 984 switch (series->d_ptr->defaultAxisType(orientation)) {
997 985 case QAbstractAxis::AxisTypeValue:
998 986 return new QValueAxis(this);
999 987 case QAbstractAxis::AxisTypeBarCategory:
1000 988 return new QBarCategoryAxis(this);
1001 989 case QAbstractAxis::AxisTypeCategory:
1002 990 return new QCategoryAxis(this);
1003 991 #ifndef QT_ON_ARM
1004 992 case QAbstractAxis::AxisTypeDateTime:
1005 993 return new QDateTimeAxis(this);
1006 994 #endif
1007 995 case QAbstractAxis::AxisTypeLogValue:
1008 996 return new QLogValueAxis(this);
1009 997 default:
1010 998 // assume AxisTypeNoAxis
1011 999 return 0;
1012 1000 }
1013 1001 }
1014 1002
1015 1003 void DeclarativeChart::initializeAxes(QAbstractSeries *series)
1016 1004 {
1017 1005 if (qobject_cast<DeclarativeLineSeries *>(series))
1018 1006 doInitializeAxes(series, qobject_cast<DeclarativeLineSeries *>(series)->m_axes);
1019 1007 else if (qobject_cast<DeclarativeScatterSeries *>(series))
1020 1008 doInitializeAxes(series, qobject_cast<DeclarativeScatterSeries *>(series)->m_axes);
1021 1009 else if (qobject_cast<DeclarativeSplineSeries *>(series))
1022 1010 doInitializeAxes(series, qobject_cast<DeclarativeSplineSeries *>(series)->m_axes);
1023 1011 else if (qobject_cast<DeclarativeAreaSeries *>(series))
1024 1012 doInitializeAxes(series, qobject_cast<DeclarativeAreaSeries *>(series)->m_axes);
1025 1013 else if (qobject_cast<DeclarativeBarSeries *>(series))
1026 1014 doInitializeAxes(series, qobject_cast<DeclarativeBarSeries *>(series)->m_axes);
1027 1015 else if (qobject_cast<DeclarativeStackedBarSeries *>(series))
1028 1016 doInitializeAxes(series, qobject_cast<DeclarativeStackedBarSeries *>(series)->m_axes);
1029 1017 else if (qobject_cast<DeclarativePercentBarSeries *>(series))
1030 1018 doInitializeAxes(series, qobject_cast<DeclarativePercentBarSeries *>(series)->m_axes);
1031 1019 else if (qobject_cast<DeclarativeHorizontalBarSeries *>(series))
1032 1020 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalBarSeries *>(series)->m_axes);
1033 1021 else if (qobject_cast<DeclarativeHorizontalStackedBarSeries *>(series))
1034 1022 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalStackedBarSeries *>(series)->m_axes);
1035 1023 else if (qobject_cast<DeclarativeHorizontalPercentBarSeries *>(series))
1036 1024 doInitializeAxes(series, qobject_cast<DeclarativeHorizontalPercentBarSeries *>(series)->m_axes);
1037 1025 else if (qobject_cast<DeclarativeBoxPlotSeries *>(series))
1038 1026 doInitializeAxes(series, qobject_cast<DeclarativeBoxPlotSeries *>(series)->m_axes);
1039 1027 // else: do nothing
1040 1028 }
1041 1029
1042 1030 void DeclarativeChart::doInitializeAxes(QAbstractSeries *series, DeclarativeAxes *axes)
1043 1031 {
1044 1032 // Initialize axis X
1045 1033 if (axes->axisX())
1046 1034 axes->emitAxisXChanged();
1047 1035 else if (axes->axisXTop())
1048 1036 axes->emitAxisXTopChanged();
1049 1037 else
1050 1038 axes->setAxisX(defaultAxis(Qt::Horizontal, series));
1051 1039
1052 1040 // Initialize axis Y
1053 1041 if (axes->axisY())
1054 1042 axes->emitAxisYChanged();
1055 1043 else if (axes->axisYRight())
1056 1044 axes->emitAxisYRightChanged();
1057 1045 else
1058 1046 axes->setAxisY(defaultAxis(Qt::Vertical, series));
1059 1047 }
1060 1048
1061 1049 #include "moc_declarativechart.cpp"
1062 1050
1063 1051 QT_CHARTS_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now