##// END OF EJS Templates
Adds defulat axis creation for qml plugin
Michal Klocek -
r1597:8fac02c7d947
parent child
Show More
@@ -1,498 +1,504
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 "qcategoriesaxis.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 Axis::min, Axis::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 Axis::min, Axis::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 Axis::min, Axis::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 Axis::min, Axis::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 bool createAxis = true;
253 254 foreach(QObject *child, children()) {
254 255 if (qobject_cast<QAbstractSeries *>(child)) {
255 256 // qDebug() << "DeclarativeChart::componentComplete(), add: " << child;
256 257 // TODO: how about optional y-axis?
257 258 m_chart->addSeries(qobject_cast<QAbstractSeries *>(child));
259 }else if(qobject_cast<QAbstractAxis *>(child)){
260 createAxis = false;
258 261 }
259 262 }
263
264 if(createAxis) m_chart->createDefaultAxes();
265
260 266 QDeclarativeItem::componentComplete();
261 267 }
262 268
263 269 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
264 270 {
265 271 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
266 272 if (newGeometry.isValid()) {
267 273 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
268 274 m_chart->resize(newGeometry.width(), newGeometry.height());
269 275 }
270 276 }
271 277 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
272 278 }
273 279
274 280 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
275 281 {
276 282 Q_UNUSED(option)
277 283 Q_UNUSED(widget)
278 284
279 285 // TODO: optimized?
280 286 painter->setRenderHint(QPainter::Antialiasing, true);
281 287 }
282 288
283 289 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
284 290 {
285 291 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
286 292 if (chartTheme != m_chart->theme())
287 293 m_chart->setTheme(chartTheme);
288 294 }
289 295
290 296 DeclarativeChart::Theme DeclarativeChart::theme()
291 297 {
292 298 return (DeclarativeChart::Theme) m_chart->theme();
293 299 }
294 300
295 301 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
296 302 {
297 303 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
298 304 if (animationOptions != m_chart->animationOptions())
299 305 m_chart->setAnimationOptions(animationOptions);
300 306 }
301 307
302 308 DeclarativeChart::Animation DeclarativeChart::animationOptions()
303 309 {
304 310 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
305 311 return DeclarativeChart::AllAnimations;
306 312 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
307 313 return DeclarativeChart::GridAxisAnimations;
308 314 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
309 315 return DeclarativeChart::SeriesAnimations;
310 316 else
311 317 return DeclarativeChart::NoAnimation;
312 318 }
313 319
314 320 void DeclarativeChart::setTitle(QString title)
315 321 {
316 322 if (title != m_chart->title())
317 323 m_chart->setTitle(title);
318 324 }
319 325 QString DeclarativeChart::title()
320 326 {
321 327 return m_chart->title();
322 328 }
323 329
324 330 QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series)
325 331 {
326 332 return m_chart->axisX(series);
327 333 }
328 334
329 335 QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series)
330 336 {
331 337 return m_chart->axisY(series);
332 338 }
333 339
334 340 QLegend *DeclarativeChart::legend()
335 341 {
336 342 return m_chart->legend();
337 343 }
338 344
339 345 void DeclarativeChart::setTitleColor(QColor color)
340 346 {
341 347 QBrush b = m_chart->titleBrush();
342 348 if (color != b.color()) {
343 349 b.setColor(color);
344 350 m_chart->setTitleBrush(b);
345 351 emit titleColorChanged(color);
346 352 }
347 353 }
348 354
349 355 QFont DeclarativeChart::titleFont() const
350 356 {
351 357 return m_chart->titleFont();
352 358 }
353 359
354 360 void DeclarativeChart::setTitleFont(const QFont& font)
355 361 {
356 362 m_chart->setTitleFont(font);
357 363 }
358 364
359 365 QColor DeclarativeChart::titleColor()
360 366 {
361 367 return m_chart->titleBrush().color();
362 368 }
363 369
364 370 void DeclarativeChart::setBackgroundColor(QColor color)
365 371 {
366 372 QBrush b = m_chart->backgroundBrush();
367 373 if (b.style() != Qt::SolidPattern || color != b.color()) {
368 374 b.setStyle(Qt::SolidPattern);
369 375 b.setColor(color);
370 376 m_chart->setBackgroundBrush(b);
371 377 emit backgroundColorChanged();
372 378 }
373 379 }
374 380
375 381 QColor DeclarativeChart::backgroundColor()
376 382 {
377 383 return m_chart->backgroundBrush().color();
378 384 }
379 385
380 386 int DeclarativeChart::count()
381 387 {
382 388 return m_chart->series().count();
383 389 }
384 390
385 391 void DeclarativeChart::setDropShadowEnabled(bool enabled)
386 392 {
387 393 if (enabled != m_chart->isDropShadowEnabled()) {
388 394 m_chart->setDropShadowEnabled(enabled);
389 395 dropShadowEnabledChanged(enabled);
390 396 }
391 397 }
392 398
393 399 bool DeclarativeChart::dropShadowEnabled()
394 400 {
395 401 return m_chart->isDropShadowEnabled();
396 402 }
397 403
398 404 qreal DeclarativeChart::topMargin()
399 405 {
400 406 return m_chart->margins().top();
401 407 }
402 408
403 409 qreal DeclarativeChart::bottomMargin()
404 410 {
405 411 return m_chart->margins().bottom();
406 412 }
407 413
408 414 qreal DeclarativeChart::leftMargin()
409 415 {
410 416 return m_chart->margins().left();
411 417 }
412 418
413 419 qreal DeclarativeChart::rightMargin()
414 420 {
415 421 return m_chart->margins().right();
416 422 }
417 423
418 424 void DeclarativeChart::zoom(qreal factor)
419 425 {
420 426 m_chart->zoom(factor);
421 427 }
422 428
423 429 void DeclarativeChart::scrollLeft(qreal pixels)
424 430 {
425 431 m_chart->scroll(pixels, 0);
426 432 }
427 433
428 434 void DeclarativeChart::scrollRight(qreal pixels)
429 435 {
430 436 m_chart->scroll(-pixels, 0);
431 437 }
432 438
433 439 void DeclarativeChart::scrollUp(qreal pixels)
434 440 {
435 441 m_chart->scroll(0, pixels);
436 442 }
437 443
438 444 void DeclarativeChart::scrollDown(qreal pixels)
439 445 {
440 446 m_chart->scroll(0, -pixels);
441 447 }
442 448
443 449 QAbstractSeries *DeclarativeChart::series(int index)
444 450 {
445 451 if (index < m_chart->series().count()) {
446 452 return m_chart->series().at(index);
447 453 }
448 454 return 0;
449 455 }
450 456
451 457 QAbstractSeries *DeclarativeChart::series(QString seriesName)
452 458 {
453 459 foreach(QAbstractSeries *series, m_chart->series()) {
454 460 if (series->name() == seriesName)
455 461 return series;
456 462 }
457 463 return 0;
458 464 }
459 465
460 466 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
461 467 {
462 468 QAbstractSeries *series = 0;
463 469 switch (type) {
464 470 case DeclarativeChart::SeriesTypeLine:
465 471 series = new DeclarativeLineSeries();
466 472 break;
467 473 case DeclarativeChart::SeriesTypeArea:
468 474 series = new DeclarativeAreaSeries();
469 475 break;
470 476 case DeclarativeChart::SeriesTypeStackedBar:
471 477 // TODO
472 478 break;
473 479 case DeclarativeChart::SeriesTypePercentBar:
474 480 // TODO
475 481 break;
476 482 case DeclarativeChart::SeriesTypeBar:
477 483 series = new DeclarativeBarSeries();
478 484 break;
479 485 case DeclarativeChart::SeriesTypePie:
480 486 series = new DeclarativePieSeries();
481 487 break;
482 488 case DeclarativeChart::SeriesTypeScatter:
483 489 series = new DeclarativeScatterSeries();
484 490 break;
485 491 case DeclarativeChart::SeriesTypeSpline:
486 492 series = new DeclarativeSplineSeries();
487 493 break;
488 494 default:
489 495 qWarning() << "Illegal series type";
490 496 }
491 497 series->setName(name);
492 498 m_chart->addSeries(series);
493 499 return series;
494 500 }
495 501
496 502 #include "moc_declarativechart.cpp"
497 503
498 504 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now