##// END OF EJS Templates
Added ChartView.plotArea to QML API
Tero Ahola -
r1929:0db37e179fcd
parent child
Show More
@@ -1,677 +1,689
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 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 Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial 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 <QPainter>
23 23 #include <QDeclarativeEngine>
24 24 #include "declarativelineseries.h"
25 25 #include "declarativeareaseries.h"
26 26 #include "declarativebarseries.h"
27 27 #include "declarativepieseries.h"
28 28 #include "declarativesplineseries.h"
29 29 #include "declarativescatterseries.h"
30 30 #include "qbarcategoryaxis.h"
31 31 #include "qvalueaxis.h"
32 32 #include "qcategoryaxis.h"
33 33 #include "qabstractseries_p.h"
34 34 #include "declarativemargins.h"
35 35
36 36 #ifndef QT_ON_ARM
37 37 #include "qdatetimeaxis.h"
38 38 #endif
39 39
40 40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41 41
42 42 /*!
43 43 \qmlclass ChartView DeclarativeChart
44 44
45 45 ChartView element is the parent that is responsible for showing different chart series types.
46 46
47 47 The following QML shows how to create a simple chart with one pie series:
48 48 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 1
49 49 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 2
50 50 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 3
51 51
52 52 \beginfloatleft
53 53 \image examples_qmlpiechart.png
54 54 \endfloat
55 55 \clearfloat
56 56 */
57 57
58 58 /*!
59 59 \qmlproperty Theme ChartView::theme
60 60 Theme defines the visual appearance of the chart, including for example colors, fonts, line
61 61 widths and chart background.
62 62 */
63 63
64 64 /*!
65 65 \qmlproperty Animation ChartView::animation
66 66 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
67 67 ChartView.SeriesAnimations or ChartView.AllAnimations.
68 68 */
69 69
70 70 /*!
71 71 \qmlproperty Font ChartView::titleFont
72 72 The title font of the chart
73 73
74 74 See the \l {Font} {QML Font Element} for detailed documentation.
75 75 */
76 76
77 77 /*!
78 78 \qmlproperty string ChartView::title
79 79 The title of the chart, shown on top of the chart.
80 80 \sa ChartView::titleColor
81 81 */
82 82
83 83 /*!
84 84 \qmlproperty string ChartView::titleColor
85 85 The color of the title text.
86 86 */
87 87
88 88 /*!
89 89 \qmlproperty Axis ChartView::axisX
90 90 The x-axis of the chart.
91 91 */
92 92
93 93 /*!
94 94 \qmlproperty Axis ChartView::axisY
95 95 The default y-axis of the chart.
96 96 */
97 97
98 98 /*!
99 99 \qmlproperty Legend ChartView::legend
100 100 The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart.
101 101 */
102 102
103 103 /*!
104 104 \qmlproperty int ChartView::count
105 105 The count of series added to the chart.
106 106 */
107 107
108 108 /*!
109 109 \qmlproperty color ChartView::backgroundColor
110 110 The color of the chart's background. By default background color is defined by chart theme.
111 111 \sa ChartView::theme
112 112 */
113 113
114 114 /*!
115 115 \qmlproperty bool ChartView::dropShadowEnabled
116 116 The chart's border drop shadow. Set to true to enable drop shadow.
117 117 */
118 118
119 119 /*!
120 120 \qmlproperty real ChartView::topMargin
121 121 Deprecated. Use minimumMargins and plotArea instead.
122 122 */
123 123
124 124 /*!
125 125 \qmlproperty real ChartView::bottomMargin
126 126 Deprecated. Use minimumMargins and plotArea instead.
127 127 */
128 128
129 129 /*!
130 130 \qmlproperty real ChartView::leftMargin
131 131 Deprecated. Use minimumMargins and plotArea instead.
132 132 */
133 133
134 134 /*!
135 135 \qmlproperty real ChartView::rightMargin
136 136 Deprecated. Use minimumMargins and plotArea instead.
137 137 */
138 138
139 139 /*!
140 140 \qmlproperty Margins ChartView::minimumMargins
141 The minimum margins allowed between the outer bounds and the plotArea of the ChartView.
141 The minimum margins allowed between the outer bounds and the plotArea of the ChartView. Margins
142 area of ChartView is used for drawing title, axes and legend. Please note that setting the
143 properties of minimumMargins may be bigger than the defined value, depending on other ChartView
144 properties that affect it's layout. If you need to know the actual plotting area used at any
145 given time, you can check ChartView::plotArea instead.
146 */
147
148 /*!
149 \qmlproperty rect ChartView::plotArea
150 The area on the ChartView that is used for drawing series. This is the ChartView rect without the
151 margins.
152 \sa ChartView::minimumMargins
142 153 */
143 154
144 155 /*!
145 156 \qmlmethod AbstractSeries ChartView::series(int index)
146 157 Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with
147 158 the count property of the chart.
148 159 */
149 160
150 161 /*!
151 162 \qmlmethod AbstractSeries ChartView::series(string name)
152 163 Returns the first series on the chart with \a name. If there is no series with that name, returns null.
153 164 */
154 165
155 166 /*!
156 167 \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name)
157 168 Creates a series object of \a type to the chart. For example:
158 169 \code
159 170 var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series");
160 171 scatter.markerSize = 22;
161 172 scatter.append(1.1, 2.0);
162 173 \endcode
163 174 */
164 175
165 176 /*!
166 177 \qmlmethod Axis ChartView::axisY(QAbstractSeries *series)
167 178 The y-axis of the series. This is the same as the default y-axis of the chart as multiple y-axes are not yet supported.
168 179 */
169 180
170 181 /*!
171 182 \qmlmethod ChartView::zoomY(real factor)
172 183 Zooms in by \a factor on the center of the chart.
173 184 */
174 185
175 186 /*!
176 187 \qmlmethod ChartView::scrollLeft(real pixels)
177 188 Scrolls to left by \a pixels. This is a convenience function that suits for example for key navigation.
178 189 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
179 190 */
180 191
181 192 /*!
182 193 \qmlmethod ChartView::scrollRight(real pixels)
183 194 Scrolls to right by \a pixels. This is a convenience function that suits for example for key navigation.
184 195 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
185 196 */
186 197
187 198 /*!
188 199 \qmlmethod ChartView::scrollUp(real pixels)
189 200 Scrolls up by \a pixels. This is a convenience function that suits for example for key navigation.
190 201 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
191 202 */
192 203
193 204 /*!
194 205 \qmlmethod ChartView::scrollDown(real pixels)
195 206 Scrolls down by \a pixels. This is a convenience function that suits for example for key navigation.
196 207 \sa ValueAxis::min, ValueAxis::max, BarCategoryAxis::min, BarCategoryAxis::max
197 208 */
198 209
199 210 /*!
200 211 \qmlsignal ChartView::onTopMarginChanged(real margin)
201 212 The top margin of the chart view has changed to \a margin. This may happen for example if you modify font size
202 213 related properties of the legend or chart title.
203 214 */
204 215
205 216 /*!
206 217 \qmlsignal ChartView::onBottomMarginChanged(real margin)
207 218 The bottom margin of the chart view has changed to \a margin. This may happen for example if you modify font size
208 219 related properties of the legend or chart title.
209 220 */
210 221
211 222 /*!
212 223 \qmlsignal ChartView::onLeftMarginChanged(real margin)
213 224 The left margin of the chart view has changed to \a margin. This may happen for example if you modify font size
214 225 related properties of the legend or chart title.
215 226 */
216 227
217 228 /*!
218 229 \qmlsignal ChartView::onRightMarginChanged(real margin)
219 230 The right margin of the chart view has changed to \a margin. This may happen for example if you modify font size
220 231 related properties of the legend or chart title.
221 232 */
222 233
223 234 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
224 235 : QDeclarativeItem(parent),
225 236 m_chart(new QChart(this))
226 237 {
227 238 setFlag(QGraphicsItem::ItemHasNoContents, false);
228 239 m_minMargins = new DeclarativeMargins(this);
229 240 connect(m_minMargins, SIGNAL(topChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int)));
230 241 connect(m_minMargins, SIGNAL(bottomChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int)));
231 242 connect(m_minMargins, SIGNAL(leftChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int)));
232 243 connect(m_minMargins, SIGNAL(rightChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int)));
233 244 // TODO: connect to plotAreaChanged signal from m_chart
234 245 }
235 246
236 247 void DeclarativeChart::changeMinimumMargins(int top, int bottom, int left, int right)
237 248 {
238 249 m_chart->setMinimumMargins(QMargins(left, top, right, bottom));
250 plotAreaChanged(m_chart->plotArea());
239 251 }
240 252
241 253 DeclarativeChart::~DeclarativeChart()
242 254 {
243 255 delete m_chart;
244 256 }
245 257
246 258 void DeclarativeChart::childEvent(QChildEvent *event)
247 259 {
248 260 if (event->type() == QEvent::ChildAdded) {
249 261 if (qobject_cast<QAbstractSeries *>(event->child())) {
250 262 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
251 263 }
252 264 }
253 265 }
254 266
255 267 void DeclarativeChart::componentComplete()
256 268 {
257 269 foreach(QObject *child, children()) {
258 270 if (qobject_cast<QAbstractSeries *>(child)) {
259 271 // Add series to the chart
260 272 QAbstractSeries *series = qobject_cast<QAbstractSeries *>(child);
261 273 m_chart->addSeries(series);
262 274
263 275 // Set optional user defined axes and connect axis related signals
264 276 if (qobject_cast<DeclarativeLineSeries *>(child)) {
265 277 DeclarativeLineSeries *s = qobject_cast<DeclarativeLineSeries *>(child);
266 278 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
267 279 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
268 280 setAxisX(s->axisX(), s);
269 281 setAxisY(s->axisY(), s);
270 282 } else if (qobject_cast<DeclarativeSplineSeries *>(child)) {
271 283 DeclarativeSplineSeries *s = qobject_cast<DeclarativeSplineSeries *>(child);
272 284 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
273 285 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
274 286 setAxisX(s->axisX(), s);
275 287 setAxisY(s->axisY(), s);
276 288 } else if (qobject_cast<DeclarativeScatterSeries *>(child)) {
277 289 DeclarativeScatterSeries *s = qobject_cast<DeclarativeScatterSeries *>(child);
278 290 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
279 291 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
280 292 setAxisX(s->axisX(), s);
281 293 setAxisY(s->axisY(), s);
282 294 } else if (qobject_cast<DeclarativeAreaSeries *>(child)) {
283 295 DeclarativeAreaSeries *s = qobject_cast<DeclarativeAreaSeries *>(child);
284 296 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
285 297 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
286 298 setAxisX(s->axisX(), s);
287 299 setAxisY(s->axisY(), s);
288 300 } else if (qobject_cast<DeclarativeBarSeries *>(child)) {
289 301 DeclarativeBarSeries *s = qobject_cast<DeclarativeBarSeries *>(child);
290 302 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
291 303 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
292 304 setAxisX(s->axisX(), s);
293 305 setAxisY(s->axisY(), s);
294 306 } else if (qobject_cast<DeclarativeStackedBarSeries *>(child)) {
295 307 DeclarativeStackedBarSeries *s = qobject_cast<DeclarativeStackedBarSeries *>(child);
296 308 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
297 309 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
298 310 setAxisX(s->axisX(), s);
299 311 setAxisY(s->axisY(), s);
300 312 } else if (qobject_cast<DeclarativePercentBarSeries *>(child)) {
301 313 DeclarativePercentBarSeries *s = qobject_cast<DeclarativePercentBarSeries *>(child);
302 314 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
303 315 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
304 316 setAxisX(s->axisX(), s);
305 317 setAxisY(s->axisY(), s);
306 318 } else if (qobject_cast<DeclarativeHorizontalBarSeries *>(child)) {
307 319 DeclarativeHorizontalBarSeries *s = qobject_cast<DeclarativeHorizontalBarSeries *>(child);
308 320 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
309 321 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
310 322 setAxisX(s->axisX(), s);
311 323 setAxisY(s->axisY(), s);
312 324 } else if (qobject_cast<DeclarativeHorizontalStackedBarSeries *>(child)) {
313 325 DeclarativeHorizontalStackedBarSeries *s = qobject_cast<DeclarativeHorizontalStackedBarSeries *>(child);
314 326 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
315 327 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
316 328 setAxisX(s->axisX(), s);
317 329 setAxisY(s->axisY(), s);
318 330 } else if (qobject_cast<DeclarativeHorizontalPercentBarSeries *>(child)) {
319 331 DeclarativeHorizontalPercentBarSeries *s = qobject_cast<DeclarativeHorizontalPercentBarSeries *>(child);
320 332 connect(s, SIGNAL(axisXChanged(QAbstractAxis *)), this, SLOT(handleAxisXSet(QAbstractAxis *)));
321 333 connect(s, SIGNAL(axisYChanged(QAbstractAxis *)), this, SLOT(handleAxisYSet(QAbstractAxis *)));
322 334 setAxisX(s->axisX(), s);
323 335 setAxisY(s->axisY(), s);
324 336 }
325 337
326 338 // Create the missing axes for the series that cannot be painted without axes
327 339 createDefaultAxes(series);
328 340 } else if(qobject_cast<QAbstractAxis *>(child)) {
329 341 // Do nothing, axes are set for the chart in the context of series
330 342 }
331 343 }
332 344
333 345 QDeclarativeItem::componentComplete();
334 346 }
335 347
336 348 void DeclarativeChart::handleAxisXSet(QAbstractAxis* axis)
337 349 {
338 350 // qDebug() << "DeclarativeChart::handleAxisXSet" << sender() << axis;
339 351 if (axis && qobject_cast<DeclarativeLineSeries *>(sender())) {
340 352 m_chart->setAxisX(axis, qobject_cast<DeclarativeLineSeries *>(sender()));
341 353 }
342 354 }
343 355
344 356 void DeclarativeChart::handleAxisYSet(QAbstractAxis* axis)
345 357 {
346 358 // qDebug() << "DeclarativeChart::handleAxisYSet" << sender() << axis;
347 359 if (axis && qobject_cast<DeclarativeLineSeries *>(sender())) {
348 360 m_chart->setAxisY(axis, qobject_cast<DeclarativeLineSeries *>(sender()));
349 361 }
350 362 }
351 363
352 364 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
353 365 {
354 366 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
355 367 if (newGeometry.isValid()) {
356 368 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
357 369 m_chart->resize(newGeometry.width(), newGeometry.height());
358 370 }
359 371 }
360 372 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
361 373 }
362 374
363 375 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
364 376 {
365 377 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
366 378 if (chartTheme != m_chart->theme())
367 379 m_chart->setTheme(chartTheme);
368 380 }
369 381
370 382 DeclarativeChart::Theme DeclarativeChart::theme()
371 383 {
372 384 return (DeclarativeChart::Theme) m_chart->theme();
373 385 }
374 386
375 387 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
376 388 {
377 389 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
378 390 if (animationOptions != m_chart->animationOptions())
379 391 m_chart->setAnimationOptions(animationOptions);
380 392 }
381 393
382 394 DeclarativeChart::Animation DeclarativeChart::animationOptions()
383 395 {
384 396 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
385 397 return DeclarativeChart::AllAnimations;
386 398 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
387 399 return DeclarativeChart::GridAxisAnimations;
388 400 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
389 401 return DeclarativeChart::SeriesAnimations;
390 402 else
391 403 return DeclarativeChart::NoAnimation;
392 404 }
393 405
394 406 void DeclarativeChart::setTitle(QString title)
395 407 {
396 408 if (title != m_chart->title())
397 409 m_chart->setTitle(title);
398 410 }
399 411 QString DeclarativeChart::title()
400 412 {
401 413 return m_chart->title();
402 414 }
403 415
404 416 QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series)
405 417 {
406 418 return m_chart->axisX(series);
407 419 }
408 420
409 421 QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series)
410 422 {
411 423 return m_chart->axisY(series);
412 424 }
413 425
414 426 QLegend *DeclarativeChart::legend()
415 427 {
416 428 return m_chart->legend();
417 429 }
418 430
419 431 void DeclarativeChart::setTitleColor(QColor color)
420 432 {
421 433 QBrush b = m_chart->titleBrush();
422 434 if (color != b.color()) {
423 435 b.setColor(color);
424 436 m_chart->setTitleBrush(b);
425 437 emit titleColorChanged(color);
426 438 }
427 439 }
428 440
429 441 QFont DeclarativeChart::titleFont() const
430 442 {
431 443 return m_chart->titleFont();
432 444 }
433 445
434 446 void DeclarativeChart::setTitleFont(const QFont& font)
435 447 {
436 448 m_chart->setTitleFont(font);
437 449 }
438 450
439 451 QColor DeclarativeChart::titleColor()
440 452 {
441 453 return m_chart->titleBrush().color();
442 454 }
443 455
444 456 void DeclarativeChart::setBackgroundColor(QColor color)
445 457 {
446 458 QBrush b = m_chart->backgroundBrush();
447 459 if (b.style() != Qt::SolidPattern || color != b.color()) {
448 460 b.setStyle(Qt::SolidPattern);
449 461 b.setColor(color);
450 462 m_chart->setBackgroundBrush(b);
451 463 emit backgroundColorChanged();
452 464 }
453 465 }
454 466
455 467 QColor DeclarativeChart::backgroundColor()
456 468 {
457 469 return m_chart->backgroundBrush().color();
458 470 }
459 471
460 472 int DeclarativeChart::count()
461 473 {
462 474 return m_chart->series().count();
463 475 }
464 476
465 477 void DeclarativeChart::setDropShadowEnabled(bool enabled)
466 478 {
467 479 if (enabled != m_chart->isDropShadowEnabled()) {
468 480 m_chart->setDropShadowEnabled(enabled);
469 481 dropShadowEnabledChanged(enabled);
470 482 }
471 483 }
472 484
473 485 bool DeclarativeChart::dropShadowEnabled()
474 486 {
475 487 return m_chart->isDropShadowEnabled();
476 488 }
477 489
478 490 qreal DeclarativeChart::topMargin()
479 491 {
480 492 qWarning() << "ChartView.topMargin is deprecated. Use minimumMargins and plotArea instead.";
481 493 return m_chart->plotArea().top();
482 494 }
483 495
484 496 qreal DeclarativeChart::bottomMargin()
485 497 {
486 498 qWarning() << "ChartView.bottomMargin is deprecated. Use minimumMargins and plotArea instead.";
487 499 return m_chart->plotArea().bottom();
488 500 }
489 501
490 502 qreal DeclarativeChart::leftMargin()
491 503 {
492 504 qWarning() << "ChartView.leftMargin is deprecated. Use minimumMargins and plotArea instead.";
493 505 return m_chart->plotArea().left();
494 506 }
495 507
496 508 qreal DeclarativeChart::rightMargin()
497 509 {
498 510 qWarning() << "ChartView.rightMargin is deprecated. Use minimumMargins and plotArea instead.";
499 511 return m_chart->plotArea().right();
500 512 }
501 513
502 514 void DeclarativeChart::zoom(qreal factor)
503 515 {
504 516 m_chart->zoom(factor);
505 517 }
506 518
507 519 void DeclarativeChart::scrollLeft(qreal pixels)
508 520 {
509 521 m_chart->scroll(pixels, 0);
510 522 }
511 523
512 524 void DeclarativeChart::scrollRight(qreal pixels)
513 525 {
514 526 m_chart->scroll(-pixels, 0);
515 527 }
516 528
517 529 void DeclarativeChart::scrollUp(qreal pixels)
518 530 {
519 531 m_chart->scroll(0, pixels);
520 532 }
521 533
522 534 void DeclarativeChart::scrollDown(qreal pixels)
523 535 {
524 536 m_chart->scroll(0, -pixels);
525 537 }
526 538
527 539 QAbstractSeries *DeclarativeChart::series(int index)
528 540 {
529 541 if (index < m_chart->series().count()) {
530 542 return m_chart->series().at(index);
531 543 }
532 544 return 0;
533 545 }
534 546
535 547 QAbstractSeries *DeclarativeChart::series(QString seriesName)
536 548 {
537 549 foreach(QAbstractSeries *series, m_chart->series()) {
538 550 if (series->name() == seriesName)
539 551 return series;
540 552 }
541 553 return 0;
542 554 }
543 555
544 556 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
545 557 {
546 558 QAbstractSeries *series = 0;
547 559
548 560 switch (type) {
549 561 case DeclarativeChart::SeriesTypeLine:
550 562 series = new DeclarativeLineSeries();
551 563 break;
552 564 case DeclarativeChart::SeriesTypeArea:
553 565 series = new DeclarativeAreaSeries();
554 566 break;
555 567 case DeclarativeChart::SeriesTypeStackedBar:
556 568 series = new DeclarativeStackedBarSeries();
557 569 break;
558 570 case DeclarativeChart::SeriesTypePercentBar:
559 571 series = new DeclarativePercentBarSeries();
560 572 break;
561 573 case DeclarativeChart::SeriesTypeBar:
562 574 series = new DeclarativeBarSeries();
563 575 break;
564 576 case DeclarativeChart::SeriesTypeHorizontalBar:
565 577 series = new DeclarativeHorizontalBarSeries();
566 578 break;
567 579 case DeclarativeChart::SeriesTypeHorizontalPercentBar:
568 580 series = new DeclarativeHorizontalPercentBarSeries();
569 581 break;
570 582 case DeclarativeChart::SeriesTypeHorizontalStackedBar:
571 583 series = new DeclarativeHorizontalStackedBarSeries();
572 584 break;
573 585 case DeclarativeChart::SeriesTypePie:
574 586 series = new DeclarativePieSeries();
575 587 break;
576 588 case DeclarativeChart::SeriesTypeScatter:
577 589 series = new DeclarativeScatterSeries();
578 590 break;
579 591 case DeclarativeChart::SeriesTypeSpline:
580 592 series = new DeclarativeSplineSeries();
581 593 break;
582 594 default:
583 595 qWarning() << "Illegal series type";
584 596 }
585 597
586 598 if (series) {
587 599 series->setName(name);
588 600 m_chart->addSeries(series);
589 601 createDefaultAxes(series);
590 602 }
591 603
592 604 return series;
593 605 }
594 606
595 607 void DeclarativeChart::setAxisX(QAbstractAxis *axis, QAbstractSeries *series)
596 608 {
597 609 if (axis)
598 610 m_chart->setAxisX(axis, series);
599 611 }
600 612
601 613 void DeclarativeChart::setAxisY(QAbstractAxis *axis, QAbstractSeries *series)
602 614 {
603 615 if (axis)
604 616 m_chart->setAxisY(axis, series);
605 617 }
606 618
607 619 void DeclarativeChart::createDefaultAxes()
608 620 {
609 621 qWarning() << "ChartView.createDefaultAxes() is deprecated. Axes are created automatically.";
610 622 }
611 623
612 624 void DeclarativeChart::createDefaultAxes(QAbstractSeries* series)
613 625 {
614 626 foreach (QAbstractSeries *s, m_chart->series()) {
615 627 // If there is already an x axis of the correct type, re-use it
616 628 if (!m_chart->axisX(series) && s != series && m_chart->axisX(s)
617 629 && m_chart->axisX(s)->type() == series->d_ptr->defaultAxisType(Qt::Horizontal))
618 630 m_chart->setAxisX(m_chart->axisX(s), series);
619 631
620 632 // If there is already a y axis of the correct type, re-use it
621 633 if (!m_chart->axisY(series) && s != series && m_chart->axisY(s)
622 634 && m_chart->axisY(s)->type() == series->d_ptr->defaultAxisType(Qt::Vertical))
623 635 m_chart->setAxisY(m_chart->axisY(s), series);
624 636 }
625 637
626 638 // If no x axis of correct type was found, create a new x axis based of default axis type
627 639 if (!m_chart->axisX(series)) {
628 640 switch (series->d_ptr->defaultAxisType(Qt::Horizontal)) {
629 641 case QAbstractAxis::AxisTypeValue:
630 642 m_chart->setAxisX(new QValueAxis(this), series);
631 643 break;
632 644 case QAbstractAxis::AxisTypeBarCategory:
633 645 m_chart->setAxisX(new QBarCategoryAxis(this), series);
634 646 break;
635 647 case QAbstractAxis::AxisTypeCategory:
636 648 m_chart->setAxisX(new QCategoryAxis(this), series);
637 649 break;
638 650 #ifndef QT_ON_ARM
639 651 case QAbstractAxis::AxisTypeDateTime:
640 652 m_chart->setAxisX(new QDateTimeAxis(this), series);
641 653 break;
642 654 #endif
643 655 default:
644 656 // Do nothing, assume AxisTypeNoAxis
645 657 break;
646 658 }
647 659 }
648 660
649 661 // If no y axis of correct type was found, create a new y axis based of default axis type
650 662 if (!m_chart->axisY(series)) {
651 663 switch (series->d_ptr->defaultAxisType(Qt::Vertical)) {
652 664 case QAbstractAxis::AxisTypeValue:
653 665 m_chart->setAxisY(new QValueAxis(this), series);
654 666 break;
655 667 case QAbstractAxis::AxisTypeBarCategory:
656 668 m_chart->setAxisY(new QBarCategoryAxis(this), series);
657 669 break;
658 670 case QAbstractAxis::AxisTypeCategory:
659 671 m_chart->setAxisY(new QCategoryAxis(this), series);
660 672 break;
661 673 #ifndef QT_ON_ARM
662 674 case QAbstractAxis::AxisTypeDateTime:
663 675 m_chart->setAxisY(new QDateTimeAxis(this), series);
664 676 break;
665 677 #endif
666 678 default:
667 679 // Do nothing, assume AxisTypeNoAxis
668 680 break;
669 681 }
670 682 }
671 683
672 684 //qDebug() << "axis for series" << series << "x:" << m_chart->axisX(series) << "y:" << m_chart->axisY(series);
673 685 }
674 686
675 687 #include "moc_declarativechart.cpp"
676 688
677 689 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,161 +1,164
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 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 Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial 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 DECLARATIVECHART_H
22 22 #define DECLARATIVECHART_H
23 23
24 24 #include <QtCore/QtGlobal>
25 25 #include <QDeclarativeItem>
26 26 #include "qchart.h"
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 class DeclarativeMargins;
31 31
32 32 class DeclarativeChart : public QDeclarativeItem
33 33 {
34 34 Q_OBJECT
35 35 Q_PROPERTY(Theme theme READ theme WRITE setTheme)
36 36 Q_PROPERTY(Animation animationOptions READ animationOptions WRITE setAnimationOptions)
37 37 Q_PROPERTY(QString title READ title WRITE setTitle)
38 38 Q_PROPERTY(QFont titleFont READ titleFont WRITE setTitleFont)
39 39 Q_PROPERTY(QColor titleColor READ titleColor WRITE setTitleColor NOTIFY titleColorChanged)
40 40 Q_PROPERTY(QLegend *legend READ legend)
41 41 Q_PROPERTY(int count READ count)
42 42 Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)
43 43 Q_PROPERTY(bool dropShadowEnabled READ dropShadowEnabled WRITE setDropShadowEnabled NOTIFY dropShadowEnabledChanged)
44 44 Q_PROPERTY(qreal topMargin READ topMargin NOTIFY topMarginChanged)
45 45 Q_PROPERTY(qreal bottomMargin READ bottomMargin NOTIFY bottomMarginChanged)
46 46 Q_PROPERTY(qreal leftMargin READ leftMargin NOTIFY leftMarginChanged)
47 47 Q_PROPERTY(qreal rightMargin READ rightMargin NOTIFY rightMarginChanged)
48 48 Q_PROPERTY(DeclarativeMargins *minimumMargins READ minimumMargins REVISION 1)
49 Q_PROPERTY(QRectF plotArea READ plotArea NOTIFY plotAreaChanged REVISION 1)
49 50 Q_ENUMS(Animation)
50 51 Q_ENUMS(Theme)
51 52 Q_ENUMS(SeriesType)
52 53
53 54 public:
54 55 // duplicating enums from QChart to make the QML api namings 1-to-1 with the C++ api
55 56 enum Theme {
56 57 ChartThemeLight = 0,
57 58 ChartThemeBlueCerulean,
58 59 ChartThemeDark,
59 60 ChartThemeBrownSand,
60 61 ChartThemeBlueNcs,
61 62 ChartThemeHighContrast,
62 63 ChartThemeBlueIcy
63 64 };
64 65
65 66 enum Animation {
66 67 NoAnimation = 0x0,
67 68 GridAxisAnimations = 0x1,
68 69 SeriesAnimations =0x2,
69 70 AllAnimations = 0x3
70 71 };
71 72
72 73 enum SeriesType {
73 74 SeriesTypeLine,
74 75 SeriesTypeArea,
75 76 SeriesTypeBar,
76 77 SeriesTypeStackedBar,
77 78 SeriesTypePercentBar,
78 79 SeriesTypePie,
79 80 SeriesTypeScatter,
80 81 SeriesTypeSpline,
81 82 SeriesTypeHorizontalBar,
82 83 SeriesTypeHorizontalStackedBar,
83 84 SeriesTypeHorizontalPercentBar
84 85 };
85 86
86 87 public:
87 88 DeclarativeChart(QDeclarativeItem *parent = 0);
88 89 ~DeclarativeChart();
89 90
90 91 public: // From QDeclarativeItem/QGraphicsItem
91 92 void childEvent(QChildEvent *event);
92 93 void componentComplete();
93 94 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
94 95
95 96 public:
96 97 void setTheme(DeclarativeChart::Theme theme);
97 98 DeclarativeChart::Theme theme();
98 99 void setAnimationOptions(DeclarativeChart::Animation animations);
99 100 DeclarativeChart::Animation animationOptions();
100 101 void setTitle(QString title);
101 102 QString title();
102 103 QLegend *legend();
103 104 QFont titleFont() const;
104 105 void setTitleFont(const QFont& font);
105 106 void setTitleColor(QColor color);
106 107 QColor titleColor();
107 108 void setBackgroundColor(QColor color);
108 109 QColor backgroundColor();
109 110 int count();
110 111 void setDropShadowEnabled(bool enabled);
111 112 bool dropShadowEnabled();
112 113 qreal topMargin();
113 114 qreal bottomMargin();
114 115 qreal leftMargin();
115 116 qreal rightMargin();
116 117 void createDefaultAxes(QAbstractSeries* series);
117 118 DeclarativeMargins *minimumMargins() { return m_minMargins; }
119 QRectF plotArea() { return m_chart->plotArea(); }
118 120
119 121 public:
120 122 Q_INVOKABLE QAbstractSeries *series(int index);
121 123 Q_INVOKABLE QAbstractSeries *series(QString seriesName);
122 124 Q_INVOKABLE QAbstractSeries *createSeries(DeclarativeChart::SeriesType type, QString name = "");
123 125 Q_INVOKABLE void setAxisX(QAbstractAxis *axis, QAbstractSeries *series = 0);
124 126 Q_INVOKABLE void setAxisY(QAbstractAxis *axis, QAbstractSeries *series = 0);
125 127 Q_INVOKABLE void createDefaultAxes();
126 128 Q_INVOKABLE QAbstractAxis *axisX(QAbstractSeries *series = 0);
127 129 Q_INVOKABLE QAbstractAxis *axisY(QAbstractSeries *series = 0);
128 130
129 131 Q_INVOKABLE void zoom(qreal factor);
130 132 Q_INVOKABLE void scrollLeft(qreal pixels);
131 133 Q_INVOKABLE void scrollRight(qreal pixels);
132 134 Q_INVOKABLE void scrollUp(qreal pixels);
133 135 Q_INVOKABLE void scrollDown(qreal pixels);
134 136
135 137 Q_SIGNALS:
136 138 void axisLabelsChanged();
137 139 void titleColorChanged(QColor color);
138 140 void backgroundColorChanged();
139 141 void dropShadowEnabledChanged(bool enabled);
140 142 void topMarginChanged(qreal margin);
141 143 void bottomMarginChanged(qreal margin);
142 144 void leftMarginChanged(qreal margin);
143 145 void rightMarginChanged(qreal margin);
146 void plotAreaChanged(QRectF plotArea);
144 147
145 148 public Q_SLOTS:
146 149 // void handleMarginsChanged(QRectF newMargins);
147 150 void changeMinimumMargins(int top, int bottom, int left, int right);
148 151 void handleAxisXSet(QAbstractAxis *axis);
149 152 void handleAxisYSet(QAbstractAxis *axis);
150 153
151 154 private:
152 155 // Extending QChart with DeclarativeChart is not possible because QObject does not support
153 156 // multi inheritance, so we now have a QChart as a member instead
154 157 QChart *m_chart;
155 158 //QMargins m_chartMargins;
156 159 DeclarativeMargins *m_minMargins;
157 160 };
158 161
159 162 QTCOMMERCIALCHART_END_NAMESPACE
160 163
161 164 #endif // DECLARATIVECHART_H
@@ -1,113 +1,114
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 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 Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial 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 import QtQuick 1.0
22 22 import QtCommercial.Chart 1.1
23 23
24 24 ChartView {
25 25 id: chartView
26 26 title: "Chart Title"
27 27 anchors.fill: parent
28 28 property variant series: chartView
29 29
30 30 LineSeries {
31 31 name: "line"
32 32 XYPoint { x: 0; y: 0 }
33 33 XYPoint { x: 1.1; y: 2.1 }
34 34 XYPoint { x: 1.9; y: 3.3 }
35 35 XYPoint { x: 2.1; y: 2.1 }
36 36 XYPoint { x: 2.9; y: 4.9 }
37 37 XYPoint { x: 3.4; y: 3.0 }
38 38 XYPoint { x: 4.1; y: 3.3 }
39 39 }
40 40
41 41 onVisibleChanged: console.log("chart.onVisibleChanged: " + visible);
42 42 onTitleColorChanged: console.log("chart.onTitleColorChanged: " + color);
43 43 onBackgroundColorChanged: console.log("chart.onBackgroundColorChanged: " + series.backgroundColor);
44 44 onDropShadowEnabledChanged: console.log("chart.onDropShadowEnabledChanged: " + enabled);
45 45 onTopMarginChanged: {
46 46 console.log("chart.onTopMarginChanged: " + margin);
47 47 marginVisualizer.opacity = 1.0;
48 48 }
49 49 onBottomMarginChanged: {
50 50 console.log("chart.onBottomMarginChanged: " + margin);
51 51 marginVisualizer.opacity = 1.0;
52 52 }
53 53 onLeftMarginChanged: {
54 54 console.log("chart.onLeftMarginChanged: " + margin);
55 55 marginVisualizer.opacity = 1.0;
56 56 }
57 57 onRightMarginChanged: {
58 58 console.log("chart.onRightMarginChanged: " + margin);
59 59 marginVisualizer.opacity = 1.0;
60 60 }
61 61
62 62 legend.onVisibleChanged: console.log("legend.onVisibleChanged: " + series.legend.visible);
63 63 legend.onBackgroundVisibleChanged: console.log("legend.onBackgroundVisibleChanged: " + visible);
64 64 legend.onColorChanged: console.log("legend.onColorChanged: " + color);
65 65 legend.onBorderColorChanged: console.log("legend.onBorderColorChanged: " + color);
66 66 legend.onLabelColorChanged: console.log("legend.onLabelColorChanged: " + color);
67 minimumMargins.onTopChanged: console.log("chart.minimumMargins.onTopChanged: " + top);
68 minimumMargins.onBottomChanged: console.log("chart.minimumMargins.onBottomChanged: " + bottom);
69 minimumMargins.onLeftChanged: console.log("chart.minimumMargins.onLeftChanged: " + left);
70 minimumMargins.onRightChanged: console.log("chart.minimumMargins.onRightChanged: " + right);
67 minimumMargins.onTopChanged: console.log("chart.minimumMargins.onTopChanged: " + top );
68 minimumMargins.onBottomChanged: console.log("chart.minimumMargins.onBottomChanged: " + bottom + " " + chartView.plotArea.height);
69 minimumMargins.onLeftChanged: console.log("chart.minimumMargins.onLeftChanged: " + left + " " + chartView.plotArea.left);
70 minimumMargins.onRightChanged: console.log("chart.minimumMargins.onRightChanged: " + right + " " + chartView.plotArea.right);
71 onPlotAreaChanged: console.log("chart.onPlotAreaChanged, width:" + chartView.plotArea.width + " height: " + chartView.plotArea.height + " y: " + chartView.plotArea.y + " x: " + chartView.plotArea.x);
71 72
72 73 ValueAxis{
73 74 onColorChanged: console.log("axisX.onColorChanged: " + color);
74 75 onLabelsVisibleChanged: console.log("axisX.onLabelsVisibleChanged: " + visible);
75 76 onLabelsColorChanged: console.log("axisX.onLabelsColorChanged: " + color);
76 77 onVisibleChanged: console.log("axisX.onVisibleChanged: " + visible);
77 78 onGridVisibleChanged: console.log("axisX.onGridVisibleChanged: " + visible);
78 79 onShadesVisibleChanged: console.log("axisX.onShadesVisibleChanged: " + visible);
79 80 onShadesColorChanged: console.log("axisX.onShadesColorChanged: " + color);
80 81 onShadesBorderColorChanged: console.log("axisX.onShadesBorderColorChanged: " + color);
81 82 onMinChanged: console.log("axisX.onMinChanged: " + min);
82 83 onMaxChanged: console.log("axisX.onMaxChanged: " + max);
83 84 }
84 85
85 86 ValueAxis{
86 87 onColorChanged: console.log("axisY.onColorChanged: " + color);
87 88 onLabelsVisibleChanged: console.log("axisY.onLabelsVisibleChanged: " + visible);
88 89 onLabelsColorChanged: console.log("axisY.onLabelsColorChanged: " + color);
89 90 onVisibleChanged: console.log("axisY.onVisibleChanged: " + visible);
90 91 onGridVisibleChanged: console.log("axisY.onGridVisibleChanged: " + visible);
91 92 onShadesVisibleChanged: console.log("axisY.onShadesVisibleChanged: " + visible);
92 93 onShadesColorChanged: console.log("axisY.onShadesColorChanged: " + color);
93 94 onShadesBorderColorChanged: console.log("axisY.onShadesBorderColorChanged: " + color);
94 95 onMinChanged: console.log("axisY.onMinChanged: " + min);
95 96 onMaxChanged: console.log("axisY.onMaxChanged: " + max);
96 97 }
97 98
98 99 Rectangle {
99 100 id: marginVisualizer
100 101 color: "transparent"
101 102 border.color: "red"
102 103 anchors.fill: parent
103 104 anchors.topMargin: parent.topMargin
104 105 anchors.bottomMargin: parent.bottomMargin
105 106 anchors.leftMargin: parent.leftMargin
106 107 anchors.rightMargin: parent.rightMargin
107 108 opacity: 0.0
108 109 onOpacityChanged: if (opacity > 0.9) opacity = 0.0;
109 110 Behavior on opacity {
110 111 NumberAnimation { duration: 800 }
111 112 }
112 113 }
113 114 }
General Comments 0
You need to be logged in to leave comments. Login now