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