##// END OF EJS Templates
added stacked and percent bar series creation to declarativechart
sauimone -
r1690:8e491322cb07
parent child
Show More
@@ -1,515 +1,515
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 "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 "declarativescatterseries.h"
29 29 #include "qbarcategoriesaxis.h"
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 /*!
34 34 \qmlclass ChartView DeclarativeChart
35 35
36 36 ChartView element is the parent that is responsible for showing different chart series types.
37 37
38 38 The following QML shows how to create a simple chart with one pie series:
39 39 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 1
40 40 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 2
41 41 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 3
42 42
43 43 \beginfloatleft
44 44 \image examples_qmlpiechart.png
45 45 \endfloat
46 46 \clearfloat
47 47 */
48 48
49 49 /*!
50 50 \qmlproperty Theme ChartView::theme
51 51 Theme defines the visual appearance of the chart, including for example colors, fonts, line
52 52 widths and chart background.
53 53 */
54 54
55 55 /*!
56 56 \qmlproperty Animation ChartView::animation
57 57 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
58 58 ChartView.SeriesAnimations or ChartView.AllAnimations.
59 59 */
60 60
61 61 /*!
62 62 \qmlproperty Font ChartView::titleFont
63 63 The title font of the chart
64 64
65 65 See the \l {Font} {QML Font Element} for detailed documentation.
66 66 */
67 67
68 68 /*!
69 69 \qmlproperty string ChartView::title
70 70 The title of the chart, shown on top of the chart.
71 71 \sa ChartView::titleColor
72 72 */
73 73
74 74 /*!
75 75 \qmlproperty string ChartView::titleColor
76 76 The color of the title text.
77 77 */
78 78
79 79 /*!
80 80 \qmlproperty Axis ChartView::axisX
81 81 The x-axis of the chart.
82 82 */
83 83
84 84 /*!
85 85 \qmlproperty Axis ChartView::axisY
86 86 The default y-axis of the chart.
87 87 */
88 88
89 89 /*!
90 90 \qmlproperty Legend ChartView::legend
91 91 The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart.
92 92 */
93 93
94 94 /*!
95 95 \qmlproperty int ChartView::count
96 96 The count of series added to the chart.
97 97 */
98 98
99 99 /*!
100 100 \qmlproperty color ChartView::backgroundColor
101 101 The color of the chart's background. By default background color is defined by chart theme.
102 102 \sa ChartView::theme
103 103 */
104 104
105 105 /*!
106 106 \qmlproperty bool ChartView::dropShadowEnabled
107 107 The chart's border drop shadow. Set to true to enable drop shadow.
108 108 */
109 109
110 110 /*!
111 111 \qmlproperty real ChartView::topMargin
112 112 The space between the top of chart view and the top of the plot area. The title (if non-empty) is drawn on top margin
113 113 area of the chart view. Top margin area is also used by legend, if aligned to top.
114 114 */
115 115
116 116 /*!
117 117 \qmlproperty real ChartView::bottomMargin
118 118 The space between the bottom of chart view and the bottom of the plot area. The bottom margin area may be used by
119 119 legend (if aligned to bottom), x-axis, x-axis labels and x-axis tick marks.
120 120 */
121 121
122 122 /*!
123 123 \qmlproperty real ChartView::leftMargin
124 124 The space between the left side of chart view and the left side of the plot area. The left margin area may be used by
125 125 legend (if aligned to left), y-axis, y-axis labels and y-axis tick marks.
126 126 */
127 127
128 128 /*!
129 129 \qmlproperty real ChartView::rightMargin
130 130 The space between the right side of chart view and the right side of the plot area. The right margin area may be used
131 131 by legend (if aligned to right).
132 132 */
133 133
134 134 /*!
135 135 \qmlmethod AbstractSeries ChartView::series(int index)
136 136 Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with
137 137 the count property of the chart.
138 138 */
139 139
140 140 /*!
141 141 \qmlmethod AbstractSeries ChartView::series(string name)
142 142 Returns the first series on the chart with \a name. If there is no series with that name, returns null.
143 143 */
144 144
145 145 /*!
146 146 \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name)
147 147 Creates a series object of \a type to the chart. For example:
148 148 \code
149 149 var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series");
150 150 scatter.markerSize = 22;
151 151 scatter.append(1.1, 2.0);
152 152 \endcode
153 153 */
154 154
155 155 /*!
156 156 \qmlmethod Axis ChartView::axisY(QAbstractSeries *series)
157 157 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.
158 158 */
159 159
160 160 /*!
161 161 \qmlmethod ChartView::zoomY(real factor)
162 162 Zooms in by \a factor on the center of the chart.
163 163 */
164 164
165 165 /*!
166 166 \qmlmethod ChartView::scrollLeft(real pixels)
167 167 Scrolls to left by \a pixels. This is a convenience function that suits for example for key navigation.
168 168 \sa ValuesAxis::min, ValuesAxis::max, BarCategoriesAxis::min, BarCategoriesAxis::max
169 169 */
170 170
171 171 /*!
172 172 \qmlmethod ChartView::scrollRight(real pixels)
173 173 Scrolls to right by \a pixels. This is a convenience function that suits for example for key navigation.
174 174 \sa ValuesAxis::min, ValuesAxis::max, BarCategoriesAxis::min, BarCategoriesAxis::max
175 175 */
176 176
177 177 /*!
178 178 \qmlmethod ChartView::scrollUp(real pixels)
179 179 Scrolls up by \a pixels. This is a convenience function that suits for example for key navigation.
180 180 \sa ValuesAxis::min, ValuesAxis::max, BarCategoriesAxis::min, BarCategoriesAxis::max
181 181 */
182 182
183 183 /*!
184 184 \qmlmethod ChartView::scrollDown(real pixels)
185 185 Scrolls down by \a pixels. This is a convenience function that suits for example for key navigation.
186 186 \sa ValuesAxis::min, ValuesAxis::max, BarCategoriesAxis::min, BarCategoriesAxis::max
187 187 */
188 188
189 189 /*!
190 190 \qmlsignal ChartView::onTopMarginChanged(real margin)
191 191 The top margin of the chart view has changed to \a margin. This may happen for example if you modify font size
192 192 related properties of the legend or chart title.
193 193 */
194 194
195 195 /*!
196 196 \qmlsignal ChartView::onBottomMarginChanged(real margin)
197 197 The bottom margin of the chart view has changed to \a margin. This may happen for example if you modify font size
198 198 related properties of the legend or chart title.
199 199 */
200 200
201 201 /*!
202 202 \qmlsignal ChartView::onLeftMarginChanged(real margin)
203 203 The left margin of the chart view has changed to \a margin. This may happen for example if you modify font size
204 204 related properties of the legend or chart title.
205 205 */
206 206
207 207 /*!
208 208 \qmlsignal ChartView::onRightMarginChanged(real margin)
209 209 The right margin of the chart view has changed to \a margin. This may happen for example if you modify font size
210 210 related properties of the legend or chart title.
211 211 */
212 212
213 213 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
214 214 : QDeclarativeItem(parent),
215 215 m_chart(new QChart(this))
216 216 {
217 217 setFlag(QGraphicsItem::ItemHasNoContents, false);
218 218 // m_chart->axisX()->setNiceNumbersEnabled(false);
219 219 m_chartMargins = m_chart->margins();
220 220 connect(m_chart, SIGNAL(marginsChanged(QRectF)), this, SLOT(handleMarginsChanged(QRectF)));
221 221 }
222 222
223 223 void DeclarativeChart::handleMarginsChanged(QRectF newMargins)
224 224 {
225 225 if (m_chartMargins.top() != newMargins.top())
226 226 topMarginChanged(m_chart->margins().top());
227 227 if (m_chartMargins.bottom() != newMargins.bottom())
228 228 bottomMarginChanged(m_chart->margins().bottom());
229 229 if (m_chartMargins.left() != newMargins.left())
230 230 leftMarginChanged(m_chart->margins().left());
231 231 if (m_chartMargins.right() != newMargins.right())
232 232 rightMarginChanged(m_chart->margins().right());
233 233
234 234 m_chartMargins = m_chart->margins();
235 235 }
236 236
237 237 DeclarativeChart::~DeclarativeChart()
238 238 {
239 239 delete m_chart;
240 240 }
241 241
242 242 void DeclarativeChart::childEvent(QChildEvent *event)
243 243 {
244 244 if (event->type() == QEvent::ChildAdded) {
245 245 if (qobject_cast<QAbstractSeries *>(event->child())) {
246 246 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
247 247 }
248 248 }
249 249 }
250 250
251 251 void DeclarativeChart::componentComplete()
252 252 {
253 253 foreach(QObject *child, children()) {
254 254 if (qobject_cast<QAbstractSeries *>(child)) {
255 255 // qDebug() << "DeclarativeChart::componentComplete(), add: " << child;
256 256 // TODO: how about optional y-axis?
257 257 m_chart->addSeries(qobject_cast<QAbstractSeries *>(child));
258 258 }else if(qobject_cast<QAbstractAxis *>(child)){
259 259
260 260 }
261 261 }
262 262 QDeclarativeItem::componentComplete();
263 263 }
264 264
265 265 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
266 266 {
267 267 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
268 268 if (newGeometry.isValid()) {
269 269 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
270 270 m_chart->resize(newGeometry.width(), newGeometry.height());
271 271 }
272 272 }
273 273 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
274 274 }
275 275
276 276 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
277 277 {
278 278 Q_UNUSED(option)
279 279 Q_UNUSED(widget)
280 280
281 281 // TODO: optimized?
282 282 painter->setRenderHint(QPainter::Antialiasing, true);
283 283 }
284 284
285 285 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
286 286 {
287 287 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
288 288 if (chartTheme != m_chart->theme())
289 289 m_chart->setTheme(chartTheme);
290 290 }
291 291
292 292 DeclarativeChart::Theme DeclarativeChart::theme()
293 293 {
294 294 return (DeclarativeChart::Theme) m_chart->theme();
295 295 }
296 296
297 297 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
298 298 {
299 299 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
300 300 if (animationOptions != m_chart->animationOptions())
301 301 m_chart->setAnimationOptions(animationOptions);
302 302 }
303 303
304 304 DeclarativeChart::Animation DeclarativeChart::animationOptions()
305 305 {
306 306 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
307 307 return DeclarativeChart::AllAnimations;
308 308 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
309 309 return DeclarativeChart::GridAxisAnimations;
310 310 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
311 311 return DeclarativeChart::SeriesAnimations;
312 312 else
313 313 return DeclarativeChart::NoAnimation;
314 314 }
315 315
316 316 void DeclarativeChart::setTitle(QString title)
317 317 {
318 318 if (title != m_chart->title())
319 319 m_chart->setTitle(title);
320 320 }
321 321 QString DeclarativeChart::title()
322 322 {
323 323 return m_chart->title();
324 324 }
325 325
326 326 QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series)
327 327 {
328 328 return m_chart->axisX(series);
329 329 }
330 330
331 331 QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series)
332 332 {
333 333 return m_chart->axisY(series);
334 334 }
335 335
336 336 QLegend *DeclarativeChart::legend()
337 337 {
338 338 return m_chart->legend();
339 339 }
340 340
341 341 void DeclarativeChart::setTitleColor(QColor color)
342 342 {
343 343 QBrush b = m_chart->titleBrush();
344 344 if (color != b.color()) {
345 345 b.setColor(color);
346 346 m_chart->setTitleBrush(b);
347 347 emit titleColorChanged(color);
348 348 }
349 349 }
350 350
351 351 QFont DeclarativeChart::titleFont() const
352 352 {
353 353 return m_chart->titleFont();
354 354 }
355 355
356 356 void DeclarativeChart::setTitleFont(const QFont& font)
357 357 {
358 358 m_chart->setTitleFont(font);
359 359 }
360 360
361 361 QColor DeclarativeChart::titleColor()
362 362 {
363 363 return m_chart->titleBrush().color();
364 364 }
365 365
366 366 void DeclarativeChart::setBackgroundColor(QColor color)
367 367 {
368 368 QBrush b = m_chart->backgroundBrush();
369 369 if (b.style() != Qt::SolidPattern || color != b.color()) {
370 370 b.setStyle(Qt::SolidPattern);
371 371 b.setColor(color);
372 372 m_chart->setBackgroundBrush(b);
373 373 emit backgroundColorChanged();
374 374 }
375 375 }
376 376
377 377 QColor DeclarativeChart::backgroundColor()
378 378 {
379 379 return m_chart->backgroundBrush().color();
380 380 }
381 381
382 382 int DeclarativeChart::count()
383 383 {
384 384 return m_chart->series().count();
385 385 }
386 386
387 387 void DeclarativeChart::setDropShadowEnabled(bool enabled)
388 388 {
389 389 if (enabled != m_chart->isDropShadowEnabled()) {
390 390 m_chart->setDropShadowEnabled(enabled);
391 391 dropShadowEnabledChanged(enabled);
392 392 }
393 393 }
394 394
395 395 bool DeclarativeChart::dropShadowEnabled()
396 396 {
397 397 return m_chart->isDropShadowEnabled();
398 398 }
399 399
400 400 qreal DeclarativeChart::topMargin()
401 401 {
402 402 return m_chart->margins().top();
403 403 }
404 404
405 405 qreal DeclarativeChart::bottomMargin()
406 406 {
407 407 return m_chart->margins().bottom();
408 408 }
409 409
410 410 qreal DeclarativeChart::leftMargin()
411 411 {
412 412 return m_chart->margins().left();
413 413 }
414 414
415 415 qreal DeclarativeChart::rightMargin()
416 416 {
417 417 return m_chart->margins().right();
418 418 }
419 419
420 420 void DeclarativeChart::zoom(qreal factor)
421 421 {
422 422 m_chart->zoom(factor);
423 423 }
424 424
425 425 void DeclarativeChart::scrollLeft(qreal pixels)
426 426 {
427 427 m_chart->scroll(pixels, 0);
428 428 }
429 429
430 430 void DeclarativeChart::scrollRight(qreal pixels)
431 431 {
432 432 m_chart->scroll(-pixels, 0);
433 433 }
434 434
435 435 void DeclarativeChart::scrollUp(qreal pixels)
436 436 {
437 437 m_chart->scroll(0, pixels);
438 438 }
439 439
440 440 void DeclarativeChart::scrollDown(qreal pixels)
441 441 {
442 442 m_chart->scroll(0, -pixels);
443 443 }
444 444
445 445 QAbstractSeries *DeclarativeChart::series(int index)
446 446 {
447 447 if (index < m_chart->series().count()) {
448 448 return m_chart->series().at(index);
449 449 }
450 450 return 0;
451 451 }
452 452
453 453 QAbstractSeries *DeclarativeChart::series(QString seriesName)
454 454 {
455 455 foreach(QAbstractSeries *series, m_chart->series()) {
456 456 if (series->name() == seriesName)
457 457 return series;
458 458 }
459 459 return 0;
460 460 }
461 461
462 462 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
463 463 {
464 464 QAbstractSeries *series = 0;
465 465 switch (type) {
466 466 case DeclarativeChart::SeriesTypeLine:
467 467 series = new DeclarativeLineSeries();
468 468 break;
469 469 case DeclarativeChart::SeriesTypeArea:
470 470 series = new DeclarativeAreaSeries();
471 471 break;
472 472 case DeclarativeChart::SeriesTypeStackedBar:
473 // TODO
473 series = new DeclarativeStackedBarSeries();
474 474 break;
475 475 case DeclarativeChart::SeriesTypePercentBar:
476 // TODO
476 series = new DeclarativePercentBarSeries();
477 477 break;
478 478 case DeclarativeChart::SeriesTypeBar:
479 479 series = new DeclarativeBarSeries();
480 480 break;
481 481 case DeclarativeChart::SeriesTypePie:
482 482 series = new DeclarativePieSeries();
483 483 break;
484 484 case DeclarativeChart::SeriesTypeScatter:
485 485 series = new DeclarativeScatterSeries();
486 486 break;
487 487 case DeclarativeChart::SeriesTypeSpline:
488 488 series = new DeclarativeSplineSeries();
489 489 break;
490 490 default:
491 491 qWarning() << "Illegal series type";
492 492 }
493 493 series->setName(name);
494 494 m_chart->addSeries(series);
495 495 return series;
496 496 }
497 497
498 498 void DeclarativeChart::setAxisX(QAbstractAxis* axis, QAbstractSeries *series)
499 499 {
500 500 m_chart->setAxisX(axis,series);
501 501 }
502 502
503 503 void DeclarativeChart::setAxisY(QAbstractAxis* axis, QAbstractSeries *series)
504 504 {
505 505 m_chart->setAxisY(axis,series);
506 506 }
507 507
508 508 void DeclarativeChart::createDefaultAxes()
509 509 {
510 510 m_chart->createDefaultAxes();
511 511 }
512 512
513 513 #include "moc_declarativechart.cpp"
514 514
515 515 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now