##// END OF EJS Templates
Added parametric zoom on QChart....
jeandet -
r2900:7532b52dd10c
parent child
Show More
@@ -1,895 +1,931
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2016 The Qt Company Ltd.
4 4 ** Contact: https://www.qt.io/licensing/
5 5 **
6 6 ** This file is part of the Qt Charts module of the Qt Toolkit.
7 7 **
8 8 ** $QT_BEGIN_LICENSE:GPL$
9 9 ** Commercial License Usage
10 10 ** Licensees holding valid commercial Qt licenses may use this file in
11 11 ** accordance with the 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 The Qt Company. For licensing terms
14 14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 15 ** information use the contact form at https://www.qt.io/contact-us.
16 16 **
17 17 ** GNU General Public License Usage
18 18 ** Alternatively, this file may be used under the terms of the GNU
19 19 ** General Public License version 3 or (at your option) any later version
20 20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 22 ** included in the packaging of this file. Please review the following
23 23 ** information to ensure the GNU General Public License requirements will
24 24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 25 **
26 26 ** $QT_END_LICENSE$
27 27 **
28 28 ****************************************************************************/
29 29
30 30 #include <QtCharts/QChart>
31 31 #include <private/qchart_p.h>
32 32 #include <private/legendscroller_p.h>
33 33 #include <private/qlegend_p.h>
34 34 #include <private/chartbackground_p.h>
35 35 #include <QtCharts/QAbstractAxis>
36 36 #include <private/abstractchartlayout_p.h>
37 37 #include <private/charttheme_p.h>
38 38 #include <private/chartpresenter_p.h>
39 39 #include <private/chartdataset_p.h>
40 40 #include <QtWidgets/QGraphicsScene>
41 41 #include <QGraphicsSceneResizeEvent>
42 42
43 43 QT_CHARTS_BEGIN_NAMESPACE
44 44
45 45 /*!
46 46 \enum QChart::ChartTheme
47 47
48 48 This enum describes the theme used by the chart.
49 49
50 50 \value ChartThemeLight The default theme
51 51 \value ChartThemeBlueCerulean
52 52 \value ChartThemeDark
53 53 \value ChartThemeBrownSand
54 54 \value ChartThemeBlueNcs
55 55 \value ChartThemeHighContrast
56 56 \value ChartThemeBlueIcy
57 57 \value ChartThemeQt
58 58 */
59 59
60 60 /*!
61 61 \enum QChart::AnimationOption
62 62
63 63 For enabling/disabling animations. Defaults to NoAnimation.
64 64
65 65 \value NoAnimation
66 66 \value GridAxisAnimations
67 67 \value SeriesAnimations
68 68 \value AllAnimations
69 69 */
70 70
71 71 /*!
72 72 \enum QChart::ChartType
73 73
74 74 This enum describes the chart type.
75 75
76 76 \value ChartTypeUndefined
77 77 \value ChartTypeCartesian
78 78 \value ChartTypePolar
79 79 */
80 80
81 81 /*!
82 82 \class QChart
83 83 \inmodule Qt Charts
84 84 \brief Main chart API for Qt Charts.
85 85
86 86 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
87 87 representation of different types of series and other chart related objects like legend and
88 88 axes. If you simply want to show a chart in a layout, you can use the
89 89 convenience class QChartView instead of QChart.
90 90 \sa QChartView, QPolarChart
91 91 */
92 92
93 93 /*!
94 94 \property QChart::animationOptions
95 95 The animation \a options for the chart. Animations are enabled/disabled based on this setting.
96 96 */
97 97
98 98 /*!
99 99 \property QChart::animationDuration
100 100 The duration of the animation for the chart.
101 101 */
102 102
103 103 /*!
104 104 \property QChart::animationEasingCurve
105 105 The easing curve of the animation for the chart.
106 106 */
107 107
108 108 /*!
109 109 \property QChart::backgroundVisible
110 110 Specifies whether the chart background is visible or not.
111 111 \sa setBackgroundBrush(), setBackgroundPen(), plotAreaBackgroundVisible
112 112 */
113 113
114 114 /*!
115 115 \property QChart::dropShadowEnabled
116 116 If set to true, the background drop shadow effect is enabled. If set to false, it is disabled. Note that the drop
117 117 shadow effect depends on theme, which means the setting may be changed if you switch to another theme.
118 118 */
119 119
120 120 /*!
121 121 \property QChart::backgroundRoundness
122 122 The diameter of the rounding circle at the corners of the chart background.
123 123 */
124 124
125 125 /*!
126 126 \property QChart::margins
127 127 Margins between the plot area (axes) and the edge of the chart widget.
128 128 */
129 129
130 130 /*!
131 131 \property QChart::theme
132 132 Theme is a built-in collection of UI style related settings applied for all visual elements of a chart, like colors,
133 133 pens, brushes, and fonts of series, axes, title, and legend. \l {Chart themes example} shows an example with a few
134 134 different themes.
135 135 \note Changing the theme will overwrite all customizations previously applied to the series.
136 136 */
137 137
138 138 /*!
139 139 \property QChart::title
140 140 Title is the name (label) of a chart. It is shown as a headline on top of the chart. Chart title supports html formatting.
141 141 */
142 142
143 143 /*!
144 144 \property QChart::chartType
145 145 Chart type indicates if the chart is a cartesian chart or a polar chart.
146 146 This property is set internally and it is read only.
147 147 \sa QPolarChart
148 148 */
149 149
150 150 /*!
151 151 \property QChart::plotAreaBackgroundVisible
152 152 Specifies whether the chart plot area background is visible or not.
153 153 \note By default the plot area background is not visible and the plot area uses
154 154 the general chart background.
155 155 \sa setPlotAreaBackgroundBrush(), setPlotAreaBackgroundPen(), backgroundVisible
156 156 */
157 157
158 158 /*!
159 159 \property QChart::localizeNumbers
160 160 \since QtCharts 2.0
161 161 When \c{true}, all generated numbers appearing in various series and axis labels will be
162 162 localized using the QLocale set with the locale property.
163 163 When \c{false}, the "C" locale is always used.
164 164 Defaults to \c{false}.
165 165 \note This property doesn't affect QDateTimeAxis labels, which always use the QLocale set with
166 166 the locale property.
167 167
168 168 \sa locale
169 169 */
170 170
171 171 /*!
172 172 \property QChart::locale
173 173 \since QtCharts 2.0
174 174 Sets the locale used to format various chart labels when localizeNumbers is \c{true}.
175 175 This also determines the locale used to format QDateTimeAxis labels regardless of
176 176 localizeNumbers property.
177 177 Defaults to application default locale at the time the chart is constructed.
178 178
179 179 \sa localizeNumbers
180 180 */
181 181
182 182 /*!
183 183 \property QChart::plotArea
184 184 Holds the rectangle within which the drawing of the chart is done.
185 185 It does not include the area defined by margins.
186 186 */
187 187
188 188 /*!
189 189 \internal
190 190 Constructs a chart object of \a type which is a child of a \a parent.
191 191 Parameter \a wFlags is passed to the QGraphicsWidget constructor.
192 192 This constructor is called only by subclasses.
193 193 */
194 194 QChart::QChart(QChart::ChartType type, QGraphicsItem *parent, Qt::WindowFlags wFlags)
195 195 : QGraphicsWidget(parent, wFlags),
196 196 d_ptr(new QChartPrivate(this, type))
197 197 {
198 198 d_ptr->init();
199 199 }
200 200
201 201 /*!
202 202 Constructs a chart object which is a child of a \a parent.
203 203 Parameter \a wFlags is passed to the QGraphicsWidget constructor.
204 204 */
205 205 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
206 206 : QGraphicsWidget(parent, wFlags),
207 207 d_ptr(new QChartPrivate(this, ChartTypeCartesian))
208 208 {
209 209 d_ptr->init();
210 210 }
211 211
212 212 /*!
213 213 Destroys the chart object and its children, like series and axis objects added to it.
214 214 */
215 215 QChart::~QChart()
216 216 {
217 217 //start by deleting dataset, it will remove all series and axes
218 218 delete d_ptr->m_dataset;
219 219 d_ptr->m_dataset = 0;
220 220 }
221 221
222 222 /*!
223 223 Adds the \a series onto the chart and takes the ownership of it.
224 224
225 225 \note A newly added series is attached to no axes by default, including any axes that were created for the chart
226 226 using createDefaultAxes() before the series was added to the chart. If no axes are attached to
227 227 the newly added series before the chart is shown, the series will get drawn as if it had axes with ranges
228 228 that exactly fit the series to the plot area of the chart. This can be confusing if the same chart also displays other
229 229 series that have properly attached axes, so always make sure you either call createDefaultAxes() after
230 230 a series has been added or explicitly attach axes for the series.
231 231
232 232 \sa removeSeries(), removeAllSeries(), createDefaultAxes(), QAbstractSeries::attachAxis()
233 233 */
234 234 void QChart::addSeries(QAbstractSeries *series)
235 235 {
236 236 Q_ASSERT(series);
237 237 d_ptr->m_dataset->addSeries(series);
238 238 }
239 239
240 240 /*!
241 241 Removes the \a series from the chart.
242 242 The chart releases its ownership of the specified \a series object.
243 243
244 244 \sa addSeries(), removeAllSeries()
245 245 */
246 246 void QChart::removeSeries(QAbstractSeries *series)
247 247 {
248 248 Q_ASSERT(series);
249 249 d_ptr->m_dataset->removeSeries(series);
250 250 }
251 251
252 252 /*!
253 253 Removes and deletes all series objects that have been added to the chart.
254 254
255 255 \sa addSeries(), removeSeries()
256 256 */
257 257 void QChart::removeAllSeries()
258 258 {
259 259 foreach (QAbstractSeries *s , d_ptr->m_dataset->series()){
260 260 removeSeries(s);
261 261 delete s;
262 262 }
263 263 }
264 264
265 265 /*!
266 266 Sets the \a brush that is used for painting the background of the chart area.
267 267 */
268 268 void QChart::setBackgroundBrush(const QBrush &brush)
269 269 {
270 270 d_ptr->m_presenter->setBackgroundBrush(brush);
271 271 }
272 272
273 273 /*!
274 274 Gets the brush that is used for painting the background of the chart area.
275 275 */
276 276 QBrush QChart::backgroundBrush() const
277 277 {
278 278 return d_ptr->m_presenter->backgroundBrush();
279 279 }
280 280
281 281 /*!
282 282 Sets the \a pen that is used for painting the background of the chart area.
283 283 */
284 284 void QChart::setBackgroundPen(const QPen &pen)
285 285 {
286 286 d_ptr->m_presenter->setBackgroundPen(pen);
287 287 }
288 288
289 289 /*!
290 290 Gets the pen that is used for painting the background of the chart area.
291 291 */
292 292 QPen QChart::backgroundPen() const
293 293 {
294 294 return d_ptr->m_presenter->backgroundPen();
295 295 }
296 296
297 297 void QChart::setTitle(const QString &title)
298 298 {
299 299 d_ptr->m_presenter->setTitle(title);
300 300 }
301 301
302 302 QString QChart::title() const
303 303 {
304 304 return d_ptr->m_presenter->title();
305 305 }
306 306
307 307 /*!
308 308 Sets the \a font that is used for drawing the chart title.
309 309 */
310 310 void QChart::setTitleFont(const QFont &font)
311 311 {
312 312 d_ptr->m_presenter->setTitleFont(font);
313 313 }
314 314
315 315 /*!
316 316 Gets the font that is used for drawing the chart title.
317 317 */
318 318 QFont QChart::titleFont() const
319 319 {
320 320 return d_ptr->m_presenter->titleFont();
321 321 }
322 322
323 323 /*!
324 324 Sets the \a brush used for drawing the title text.
325 325 */
326 326 void QChart::setTitleBrush(const QBrush &brush)
327 327 {
328 328 d_ptr->m_presenter->setTitleBrush(brush);
329 329 }
330 330
331 331 /*!
332 332 Returns the brush used for drawing the title text.
333 333 */
334 334 QBrush QChart::titleBrush() const
335 335 {
336 336 return d_ptr->m_presenter->titleBrush();
337 337 }
338 338
339 339 void QChart::setTheme(QChart::ChartTheme theme)
340 340 {
341 341 d_ptr->m_themeManager->setTheme(theme);
342 342 }
343 343
344 344 QChart::ChartTheme QChart::theme() const
345 345 {
346 346 return d_ptr->m_themeManager->theme()->id();
347 347 }
348 348
349 349 /*!
350 350 Zooms in the view by a factor of two.
351 351 */
352 352 void QChart::zoomIn()
353 353 {
354 354 d_ptr->zoomIn(2.0);
355 355 }
356 356
357 void QChart::zoomIn2(double factor)
358 {
359 d_ptr->zoomIn2(factor);
360 }
361
357 362 /*!
358 363 Zooms in the view to a maximum level at which \a rect is still fully visible.
359 364 \note This is not supported for polar charts.
360 365 */
361 366 void QChart::zoomIn(const QRectF &rect)
362 367 {
363 368 if (d_ptr->m_type == QChart::ChartTypePolar)
364 369 return;
365 370 d_ptr->zoomIn(rect);
366 371 }
367 372
368 373 /*!
369 374 Zooms out the view by a factor of two.
370 375 */
371 376 void QChart::zoomOut()
372 377 {
373 378 d_ptr->zoomOut(2.0);
374 379 }
375 380
381 void QChart::zoomOut2(double factor)
382 {
383 d_ptr->zoomOut2(factor);
384 }
385
376 386 /*!
377 387 Zooms in the view by a custom \a factor.
378 388
379 389 A factor over 1.0 zooms the view in and factor between 0.0 and 1.0 zooms out.
380 390 */
381 391 void QChart::zoom(qreal factor)
382 392 {
383 393 if (qFuzzyCompare(factor, 0))
384 394 return;
385 395
386 396 if (qFuzzyCompare(factor, (qreal)1.0))
387 397 return;
388 398
389 399 if (factor < 0)
390 400 return;
391 401
392 402 if (factor > 1.0)
393 403 d_ptr->zoomIn(factor);
394 404 else
395 405 d_ptr->zoomOut(1.0 / factor);
396 406 }
397 407
398 408
399 409 /*!
400 410 Resets the series domains to what they were before any zoom method was called.
401 411 Note that this will also reset any scrolls and explicit axis range settings done between
402 412 the first zoom operation and calling this method. If no zoom operation has been
403 413 done, this method does nothing.
404 414 */
405 415 void QChart::zoomReset()
406 416 {
407 417 d_ptr->zoomReset();
408 418 }
409 419
410 420 /*!
411 421 Returns true if any series has a zoomed domain.
412 422 */
413 423 bool QChart::isZoomed()
414 424 {
415 425 return d_ptr->isZoomed();
416 426 }
417 427
418 428 /*!
419 429 Returns a pointer to the horizontal axis attached to the specified \a series.
420 430 If no \a series is specified, the first horizontal axis added to the chart is returned.
421 431
422 432 \sa addAxis(), QAbstractSeries::attachAxis()
423 433 */
424 434 QAbstractAxis *QChart::axisX(QAbstractSeries *series) const
425 435 {
426 436 QList<QAbstractAxis *> axisList = axes(Qt::Horizontal, series);
427 437 if (axisList.count())
428 438 return axisList[0];
429 439 return 0;
430 440 }
431 441
432 442 /*!
433 443 Returns a pointer to the vertical axis attached to the specified \a series.
434 444 If no \a series is specified, the first vertical axis added to the chart is returned.
435 445
436 446 \sa addAxis(), QAbstractSeries::attachAxis()
437 447 */
438 448 QAbstractAxis *QChart::axisY(QAbstractSeries *series) const
439 449 {
440 450 QList<QAbstractAxis *> axisList = axes(Qt::Vertical, series);
441 451 if (axisList.count())
442 452 return axisList[0];
443 453 return 0;
444 454 }
445 455
446 456 /*!
447 457 Returns the axes attached to the \a series with \a orientation. If no \a series is provided,
448 458 then all axes added to the chart with the specified orientation are returned.
449 459 \sa addAxis(), createDefaultAxes()
450 460 */
451 461 QList<QAbstractAxis *> QChart::axes(Qt::Orientations orientation, QAbstractSeries *series) const
452 462 {
453 463 QList<QAbstractAxis *> result ;
454 464
455 465 if (series) {
456 466 foreach (QAbstractAxis *axis, series->attachedAxes()){
457 467 if (orientation.testFlag(axis->orientation()))
458 468 result << axis;
459 469 }
460 470 } else {
461 471 foreach (QAbstractAxis *axis, d_ptr->m_dataset->axes()){
462 472 if (orientation.testFlag(axis->orientation()) && !result.contains(axis))
463 473 result << axis;
464 474 }
465 475 }
466 476
467 477 return result;
468 478 }
469 479
470 480 /*!
471 481 Creates axes for the chart based on the series that have already been added to the chart. Any axes previously added to
472 482 the chart will be deleted.
473 483
474 484 \note This function has to be called after all series have been added to the chart. The axes created by this function
475 485 will NOT get automatically attached to any series added to the chart after this function has been called.
476 486 A series with no axes attached will by default scale to utilize the entire plot area of the chart, which can be confusing
477 487 if there are other series with properly attached axes also present.
478 488
479 489 \table
480 490 \header
481 491 \li Series type
482 492 \li X-axis
483 493 \li Y-axis
484 494 \row
485 495 \li QXYSeries
486 496 \li QValueAxis
487 497 \li QValueAxis
488 498 \row
489 499 \li QBarSeries
490 500 \li QBarCategoryAxis
491 501 \li QValueAxis
492 502 \row
493 503 \li QPieSeries
494 504 \li None
495 505 \li None
496 506 \endtable
497 507
498 508 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.
499 509 If there are several series of different types added to the chart, then each series gets its own axes pair.
500 510
501 511 The axes specific to the series can be later obtained from the chart by providing the series as the parameter for axes() function call.
502 512 QPieSeries does not create any axes.
503 513
504 514 \sa axisX(), axisY(), axes(), setAxisX(), setAxisY(), QAbstractSeries::attachAxis()
505 515 */
506 516 void QChart::createDefaultAxes()
507 517 {
508 518 d_ptr->m_dataset->createDefaultAxes();
509 519 }
510 520
511 521 /*!
512 522 Returns the legend object of the chart. Ownership stays with the chart.
513 523 */
514 524 QLegend *QChart::legend() const
515 525 {
516 526 return d_ptr->m_legend;
517 527 }
518 528
519 529 void QChart::setMargins(const QMargins &margins)
520 530 {
521 531 d_ptr->m_presenter->layout()->setMargins(margins);
522 532 }
523 533
524 534 QMargins QChart::margins() const
525 535 {
526 536 return d_ptr->m_presenter->layout()->margins();
527 537 }
528 538
529 539 QChart::ChartType QChart::chartType() const
530 540 {
531 541 return d_ptr->m_type;
532 542 }
533 543
534 544 QRectF QChart::plotArea() const
535 545 {
536 546 return d_ptr->m_presenter->geometry();
537 547 }
538 548
539 549 /*!
540 550 Sets the \a brush for the background of the plot area of the chart.
541 551
542 552 \sa plotArea(), plotAreaBackgroundVisible, setPlotAreaBackgroundPen(), plotAreaBackgroundBrush()
543 553 */
544 554 void QChart::setPlotAreaBackgroundBrush(const QBrush &brush)
545 555 {
546 556 d_ptr->m_presenter->setPlotAreaBackgroundBrush(brush);
547 557 }
548 558
549 559 /*!
550 560 Returns the brush for the background of the plot area of the chart.
551 561
552 562 \sa plotArea(), plotAreaBackgroundVisible, plotAreaBackgroundPen(), setPlotAreaBackgroundBrush()
553 563 */
554 564 QBrush QChart::plotAreaBackgroundBrush() const
555 565 {
556 566 return d_ptr->m_presenter->plotAreaBackgroundBrush();
557 567 }
558 568
559 569 /*!
560 570 Sets the \a pen for the background of the plot area of the chart.
561 571
562 572 \sa plotArea(), plotAreaBackgroundVisible, setPlotAreaBackgroundBrush(), plotAreaBackgroundPen()
563 573 */
564 574 void QChart::setPlotAreaBackgroundPen(const QPen &pen)
565 575 {
566 576 d_ptr->m_presenter->setPlotAreaBackgroundPen(pen);
567 577 }
568 578
569 579 /*!
570 580 Returns the pen for the background of the plot area of the chart.
571 581
572 582 \sa plotArea(), plotAreaBackgroundVisible, plotAreaBackgroundBrush(), setPlotAreaBackgroundPen()
573 583 */
574 584 QPen QChart::plotAreaBackgroundPen() const
575 585 {
576 586 return d_ptr->m_presenter->plotAreaBackgroundPen();
577 587 }
578 588
579 589 void QChart::setPlotAreaBackgroundVisible(bool visible)
580 590 {
581 591 d_ptr->m_presenter->setPlotAreaBackgroundVisible(visible);
582 592 }
583 593
584 594 bool QChart::isPlotAreaBackgroundVisible() const
585 595 {
586 596 return d_ptr->m_presenter->isPlotAreaBackgroundVisible();
587 597 }
588 598
589 599 void QChart::setLocalizeNumbers(bool localize)
590 600 {
591 601 d_ptr->m_presenter->setLocalizeNumbers(localize);
592 602 }
593 603
594 604 bool QChart::localizeNumbers() const
595 605 {
596 606 return d_ptr->m_presenter->localizeNumbers();
597 607 }
598 608
599 609 void QChart::setLocale(const QLocale &locale)
600 610 {
601 611 d_ptr->m_presenter->setLocale(locale);
602 612 }
603 613
604 614 QLocale QChart::locale() const
605 615 {
606 616 return d_ptr->m_presenter->locale();
607 617 }
608 618
609 619 void QChart::setAnimationOptions(AnimationOptions options)
610 620 {
611 621 d_ptr->m_presenter->setAnimationOptions(options);
612 622 }
613 623
614 624 QChart::AnimationOptions QChart::animationOptions() const
615 625 {
616 626 return d_ptr->m_presenter->animationOptions();
617 627 }
618 628
619 629 void QChart::setAnimationDuration(int msecs)
620 630 {
621 631 d_ptr->m_presenter->setAnimationDuration(msecs);
622 632 }
623 633
624 634 int QChart::animationDuration() const
625 635 {
626 636 return d_ptr->m_presenter->animationDuration();
627 637 }
628 638
629 639 void QChart::setAnimationEasingCurve(const QEasingCurve &curve)
630 640 {
631 641 d_ptr->m_presenter->setAnimationEasingCurve(curve);
632 642 }
633 643
634 644 QEasingCurve QChart::animationEasingCurve() const
635 645 {
636 646 return d_ptr->m_presenter->animationEasingCurve();
637 647 }
638 648
639 649 /*!
640 650 Scrolls the visible area of the chart by the distance defined in the \a dx and \a dy.
641 651
642 652 For polar charts, \a dx indicates the angle along angular axis instead of distance.
643 653 */
644 654 void QChart::scroll(qreal dx, qreal dy)
645 655 {
646 656 d_ptr->scroll(dx,dy);
647 657 }
648 658
649 659 void QChart::setBackgroundVisible(bool visible)
650 660 {
651 661 d_ptr->m_presenter->setBackgroundVisible(visible);
652 662 }
653 663
654 664 bool QChart::isBackgroundVisible() const
655 665 {
656 666 return d_ptr->m_presenter->isBackgroundVisible();
657 667 }
658 668
659 669 void QChart::setDropShadowEnabled(bool enabled)
660 670 {
661 671 d_ptr->m_presenter->setBackgroundDropShadowEnabled(enabled);
662 672 }
663 673
664 674 bool QChart::isDropShadowEnabled() const
665 675 {
666 676 return d_ptr->m_presenter->isBackgroundDropShadowEnabled();
667 677 }
668 678
669 679 void QChart::setBackgroundRoundness(qreal diameter)
670 680 {
671 681 d_ptr->m_presenter->setBackgroundRoundness(diameter);
672 682 }
673 683
674 684 qreal QChart::backgroundRoundness() const
675 685 {
676 686 return d_ptr->m_presenter->backgroundRoundness();
677 687 }
678 688
679 689 /*!
680 690 Returns all series that are added to the chart.
681 691
682 692 \sa addSeries(), removeSeries(), removeAllSeries()
683 693 */
684 694 QList<QAbstractSeries *> QChart::series() const
685 695 {
686 696 return d_ptr->m_dataset->series();
687 697 }
688 698
689 699 /*!
690 700 Adds the \a axis to the chart and attaches it to the \a series as a bottom-aligned horizontal axis.
691 701 The chart takes ownership of both the \a axis and the \a series.
692 702 Any horizontal axes previously attached to the \a series are deleted.
693 703
694 704 \sa axisX(), axisY(), setAxisY(), createDefaultAxes(), QAbstractSeries::attachAxis()
695 705 */
696 706 void QChart::setAxisX(QAbstractAxis *axis ,QAbstractSeries *series)
697 707 {
698 708 QList<QAbstractAxis*> list = axes(Qt::Horizontal, series);
699 709
700 710 foreach (QAbstractAxis* a, list) {
701 711 d_ptr->m_dataset->removeAxis(a);
702 712 delete a;
703 713 }
704 714
705 715 if (!d_ptr->m_dataset->axes().contains(axis))
706 716 d_ptr->m_dataset->addAxis(axis, Qt::AlignBottom);
707 717 d_ptr->m_dataset->attachAxis(series, axis);
708 718 }
709 719
710 720 /*!
711 721 Adds the \a axis to the chart and attaches it to the \a series as a left-aligned vertical axis.
712 722 The chart takes ownership of both the \a axis and the \a series.
713 723 Any vertical axes previously attached to the \a series are deleted.
714 724
715 725 \sa axisX(), axisY(), setAxisX(), createDefaultAxes(), QAbstractSeries::attachAxis()
716 726 */
717 727 void QChart::setAxisY(QAbstractAxis *axis ,QAbstractSeries *series)
718 728 {
719 729 QList<QAbstractAxis*> list = axes(Qt::Vertical, series);
720 730
721 731 foreach (QAbstractAxis* a, list) {
722 732 d_ptr->m_dataset->removeAxis(a);
723 733 delete a;
724 734 }
725 735
726 736 if (!d_ptr->m_dataset->axes().contains(axis))
727 737 d_ptr->m_dataset->addAxis(axis, Qt::AlignLeft);
728 738 d_ptr->m_dataset->attachAxis(series, axis);
729 739 }
730 740
731 741 /*!
732 742 Adds the \a axis to the chart with \a alignment. The chart takes the ownership of the axis.
733 743
734 744 \sa removeAxis(), createDefaultAxes(), QAbstractSeries::attachAxis()
735 745 */
736 746 void QChart::addAxis(QAbstractAxis *axis, Qt::Alignment alignment)
737 747 {
738 748 d_ptr->m_dataset->addAxis(axis, alignment);
739 749 }
740 750
741 751 /*!
742 752 Removes the \a axis from the chart.
743 753 The chart releases its ownership of the specified \a axis object.
744 754
745 755 \sa addAxis(), createDefaultAxes(), QAbstractSeries::detachAxis()
746 756 */
747 757 void QChart::removeAxis(QAbstractAxis *axis)
748 758 {
749 759 d_ptr->m_dataset->removeAxis(axis);
750 760 }
751 761
752 762 /*!
753 763 Returns the value in the \a series domain that corresponds to the \a position relative to chart widget.
754 764 */
755 765 QPointF QChart::mapToValue(const QPointF &position, QAbstractSeries *series)
756 766 {
757 767 return d_ptr->m_dataset->mapToValue(position, series);
758 768 }
759 769
760 770 /*!
761 771 Returns the position on the chart widget that corresponds to the \a value in the \a series domain.
762 772 */
763 773 QPointF QChart::mapToPosition(const QPointF &value, QAbstractSeries *series)
764 774 {
765 775 return d_ptr->m_dataset->mapToPosition(value, series);
766 776 }
767 777
768 778 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
769 779
770 780 QChartPrivate::QChartPrivate(QChart *q, QChart::ChartType type):
771 781 q_ptr(q),
772 782 m_legend(0),
773 783 m_dataset(new ChartDataSet(q)),
774 784 m_presenter(new ChartPresenter(q, type)),
775 785 m_themeManager(new ChartThemeManager(q)),
776 786 m_type(type)
777 787 {
778 788 QObject::connect(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), m_presenter, SLOT(handleSeriesAdded(QAbstractSeries*)));
779 789 QObject::connect(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), m_presenter, SLOT(handleSeriesRemoved(QAbstractSeries*)));
780 790 QObject::connect(m_dataset, SIGNAL(axisAdded(QAbstractAxis*)), m_presenter, SLOT(handleAxisAdded(QAbstractAxis*)));
781 791 QObject::connect(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)), m_presenter, SLOT(handleAxisRemoved(QAbstractAxis*)));
782 792 QObject::connect(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), m_themeManager, SLOT(handleSeriesAdded(QAbstractSeries*)));
783 793 QObject::connect(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), m_themeManager, SLOT(handleSeriesRemoved(QAbstractSeries*)));
784 794 QObject::connect(m_dataset, SIGNAL(axisAdded(QAbstractAxis*)), m_themeManager, SLOT(handleAxisAdded(QAbstractAxis*)));
785 795 QObject::connect(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)), m_themeManager, SLOT(handleAxisRemoved(QAbstractAxis*)));
786 796 QObject::connect(m_presenter, &ChartPresenter::plotAreaChanged, q, &QChart::plotAreaChanged);
787 797 }
788 798
789 799 QChartPrivate::~QChartPrivate()
790 800 {
791 801 delete m_themeManager;
792 802 }
793 803
794 804 // Hackish solution to the problem of explicitly assigning the default pen/brush/font
795 805 // to a series or axis and having theme override it:
796 806 // Initialize pens, brushes, and fonts to something nobody is likely to ever use,
797 807 // so that default theme initialization will always set these properly.
798 808 QPen &QChartPrivate::defaultPen()
799 809 {
800 810 static QPen defaultPen(QColor(1, 2, 0), 0.93247536);
801 811 return defaultPen;
802 812 }
803 813
804 814 QBrush &QChartPrivate::defaultBrush()
805 815 {
806 816 static QBrush defaultBrush(QColor(1, 2, 0), Qt::Dense7Pattern);
807 817 return defaultBrush;
808 818 }
809 819
810 820 QFont &QChartPrivate::defaultFont()
811 821 {
812 822 static bool defaultFontInitialized(false);
813 823 static QFont defaultFont;
814 824 if (!defaultFontInitialized) {
815 825 defaultFont.setPointSizeF(8.34563465);
816 826 defaultFontInitialized = true;
817 827 }
818 828 return defaultFont;
819 829 }
820 830
821 831 void QChartPrivate::init()
822 832 {
823 833 m_legend = new LegendScroller(q_ptr);
824 834 q_ptr->setTheme(QChart::ChartThemeLight);
825 835 q_ptr->setLayout(m_presenter->layout());
826 836 }
827 837
828 838 void QChartPrivate::zoomIn(qreal factor)
829 839 {
830 840 QRectF rect = m_presenter->geometry();
831 841 rect.setWidth(rect.width() / factor);
832 842 rect.setHeight(rect.height() / factor);
833 843 rect.moveCenter(m_presenter->geometry().center());
834 844 zoomIn(rect);
835 845 }
836 846
847 void QChartPrivate::zoomIn2(qreal factor)
848 {
849 QRectF rect = m_presenter->geometry();
850 rect.setWidth(rect.width() / factor);
851 rect.moveCenter(m_presenter->geometry().center());
852 zoomIn(rect);
853 }
854
837 855 void QChartPrivate::zoomIn(const QRectF &rect)
838 856 {
839 857 if (!rect.isValid())
840 858 return;
841 859
842 860 QRectF r = rect.normalized();
843 861 const QRectF geometry = m_presenter->geometry();
844 862 r.translate(-geometry.topLeft());
845 863
846 864 if (!r.isValid())
847 865 return;
848 866
849 867 QPointF zoomPoint(r.center().x() / geometry.width(), r.center().y() / geometry.height());
850 868 m_presenter->setState(ChartPresenter::ZoomInState,zoomPoint);
851 869 m_dataset->zoomInDomain(r);
852 870 m_presenter->setState(ChartPresenter::ShowState,QPointF());
853 871
854 872 }
855 873
856 874 void QChartPrivate::zoomReset()
857 875 {
858 876 m_dataset->zoomResetDomain();
859 877 }
860 878
861 879 bool QChartPrivate::isZoomed()
862 880 {
863 881 return m_dataset->isZoomedDomain();
864 882 }
865 883
866 884 void QChartPrivate::zoomOut(qreal factor)
867 885 {
868 886 const QRectF geometry = m_presenter->geometry();
869 887
870 888 QRectF r;
871 889 r.setSize(geometry.size() / factor);
872 890 r.moveCenter(QPointF(geometry.size().width()/2 ,geometry.size().height()/2));
873 891 if (!r.isValid())
874 892 return;
875 893
876 894 QPointF zoomPoint(r.center().x() / geometry.width(), r.center().y() / geometry.height());
877 895 m_presenter->setState(ChartPresenter::ZoomOutState,zoomPoint);
878 896 m_dataset->zoomOutDomain(r);
879 897 m_presenter->setState(ChartPresenter::ShowState,QPointF());
880 898 }
881 899
900 void QChartPrivate::zoomOut2(qreal factor)
901 {
902 const QRectF geometry = m_presenter->geometry();
903
904 QRectF r;
905 QSizeF size = geometry.size();
906 size.setWidth(size.width()/factor);
907 r.setSize(size);
908 r.moveCenter(QPointF(geometry.size().width()/2 ,geometry.size().height()/2));
909 if (!r.isValid())
910 return;
911
912 QPointF zoomPoint(r.center().x() / geometry.width(), r.center().y() / geometry.height());
913 m_presenter->setState(ChartPresenter::ZoomOutState,zoomPoint);
914 m_dataset->zoomOutDomain(r);
915 m_presenter->setState(ChartPresenter::ShowState,QPointF());
916 }
917
882 918 void QChartPrivate::scroll(qreal dx, qreal dy)
883 919 {
884 920 if (dx < 0) m_presenter->setState(ChartPresenter::ScrollLeftState,QPointF());
885 921 if (dx > 0) m_presenter->setState(ChartPresenter::ScrollRightState,QPointF());
886 922 if (dy < 0) m_presenter->setState(ChartPresenter::ScrollUpState,QPointF());
887 923 if (dy > 0) m_presenter->setState(ChartPresenter::ScrollDownState,QPointF());
888 924
889 925 m_dataset->scrollDomain(dx, dy);
890 926 m_presenter->setState(ChartPresenter::ShowState,QPointF());
891 927 }
892 928
893 929 #include "moc_qchart.cpp"
894 930
895 931 QT_CHARTS_END_NAMESPACE
@@ -1,203 +1,205
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2016 The Qt Company Ltd.
4 4 ** Contact: https://www.qt.io/licensing/
5 5 **
6 6 ** This file is part of the Qt Charts module of the Qt Toolkit.
7 7 **
8 8 ** $QT_BEGIN_LICENSE:GPL$
9 9 ** Commercial License Usage
10 10 ** Licensees holding valid commercial Qt licenses may use this file in
11 11 ** accordance with the 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 The Qt Company. For licensing terms
14 14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 15 ** information use the contact form at https://www.qt.io/contact-us.
16 16 **
17 17 ** GNU General Public License Usage
18 18 ** Alternatively, this file may be used under the terms of the GNU
19 19 ** General Public License version 3 or (at your option) any later version
20 20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 22 ** included in the packaging of this file. Please review the following
23 23 ** information to ensure the GNU General Public License requirements will
24 24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 25 **
26 26 ** $QT_END_LICENSE$
27 27 **
28 28 ****************************************************************************/
29 29
30 30 #ifndef QCHART_H
31 31 #define QCHART_H
32 32
33 33 #include <QtCharts/QAbstractSeries>
34 34 #include <QtCharts/QLegend>
35 35 #include <QtWidgets/QGraphicsWidget>
36 36 #include <QtCore/QMargins>
37 37
38 38 QT_BEGIN_NAMESPACE
39 39 class QGraphicsSceneResizeEvent;
40 40 QT_END_NAMESPACE
41 41
42 42 QT_CHARTS_BEGIN_NAMESPACE
43 43
44 44 class QAbstractSeries;
45 45 class QAbstractAxis;
46 46 class QLegend;
47 47 class QChartPrivate;
48 48 class QBoxPlotSeries;
49 49
50 50 class QT_CHARTS_EXPORT QChart : public QGraphicsWidget
51 51 {
52 52 Q_OBJECT
53 53 Q_PROPERTY(QChart::ChartTheme theme READ theme WRITE setTheme)
54 54 Q_PROPERTY(QString title READ title WRITE setTitle)
55 55 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible)
56 56 Q_PROPERTY(bool dropShadowEnabled READ isDropShadowEnabled WRITE setDropShadowEnabled)
57 57 Q_PROPERTY(qreal backgroundRoundness READ backgroundRoundness WRITE setBackgroundRoundness)
58 58 Q_PROPERTY(QChart::AnimationOptions animationOptions READ animationOptions WRITE setAnimationOptions)
59 59 Q_PROPERTY(int animationDuration READ animationDuration WRITE setAnimationDuration)
60 60 Q_PROPERTY(QEasingCurve animationEasingCurve READ animationEasingCurve WRITE setAnimationEasingCurve)
61 61 Q_PROPERTY(QMargins margins READ margins WRITE setMargins)
62 62 Q_PROPERTY(QChart::ChartType chartType READ chartType)
63 63 Q_PROPERTY(bool plotAreaBackgroundVisible READ isPlotAreaBackgroundVisible WRITE setPlotAreaBackgroundVisible)
64 64 Q_PROPERTY(bool localizeNumbers READ localizeNumbers WRITE setLocalizeNumbers)
65 65 Q_PROPERTY(QLocale locale READ locale WRITE setLocale)
66 66 Q_PROPERTY(QRectF plotArea READ plotArea NOTIFY plotAreaChanged)
67 67 Q_ENUMS(ChartTheme)
68 68 Q_ENUMS(AnimationOption)
69 69 Q_ENUMS(ChartType)
70 70
71 71 public:
72 72 enum ChartType {
73 73 ChartTypeUndefined = 0,
74 74 ChartTypeCartesian,
75 75 ChartTypePolar
76 76 };
77 77
78 78 enum ChartTheme {
79 79 ChartThemeLight = 0,
80 80 ChartThemeBlueCerulean,
81 81 ChartThemeDark,
82 82 ChartThemeBrownSand,
83 83 ChartThemeBlueNcs,
84 84 ChartThemeHighContrast,
85 85 ChartThemeBlueIcy,
86 86 ChartThemeQt
87 87 };
88 88
89 89 enum AnimationOption {
90 90 NoAnimation = 0x0,
91 91 GridAxisAnimations = 0x1,
92 92 SeriesAnimations = 0x2,
93 93 AllAnimations = 0x3
94 94 };
95 95
96 96 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
97 97
98 98 public:
99 99 explicit QChart(QGraphicsItem *parent = Q_NULLPTR, Qt::WindowFlags wFlags = Qt::WindowFlags());
100 100 ~QChart();
101 101
102 102 void addSeries(QAbstractSeries *series);
103 103 void removeSeries(QAbstractSeries *series);
104 104 void removeAllSeries();
105 105 QList<QAbstractSeries *> series() const;
106 106
107 107 // *** deprecated ***
108 108 void setAxisX(QAbstractAxis *axis, QAbstractSeries *series = Q_NULLPTR);
109 109 void setAxisY(QAbstractAxis *axis, QAbstractSeries *series = Q_NULLPTR);
110 110 QAbstractAxis *axisX(QAbstractSeries *series = Q_NULLPTR) const;
111 111 QAbstractAxis *axisY(QAbstractSeries *series = Q_NULLPTR) const;
112 112 // ******************
113 113
114 114 void addAxis(QAbstractAxis *axis, Qt::Alignment alignment);
115 115 void removeAxis(QAbstractAxis *axis);
116 116 QList<QAbstractAxis*> axes(Qt::Orientations orientation = Qt::Horizontal|Qt::Vertical, QAbstractSeries *series = Q_NULLPTR) const;
117 117
118 118 void createDefaultAxes();
119 119
120 120 void setTheme(QChart::ChartTheme theme);
121 121 QChart::ChartTheme theme() const;
122 122
123 123 void setTitle(const QString &title);
124 124 QString title() const;
125 125 void setTitleFont(const QFont &font);
126 126 QFont titleFont() const;
127 127 void setTitleBrush(const QBrush &brush);
128 128 QBrush titleBrush() const;
129 129
130 130 void setBackgroundBrush(const QBrush &brush);
131 131 QBrush backgroundBrush() const;
132 132 void setBackgroundPen(const QPen &pen);
133 133 QPen backgroundPen() const;
134 134 void setBackgroundVisible(bool visible = true);
135 135 bool isBackgroundVisible() const;
136 136
137 137 void setDropShadowEnabled(bool enabled = true);
138 138 bool isDropShadowEnabled() const;
139 139 void setBackgroundRoundness(qreal diameter);
140 140 qreal backgroundRoundness() const;
141 141
142 142 void setAnimationOptions(AnimationOptions options);
143 143 AnimationOptions animationOptions() const;
144 144 void setAnimationDuration(int msecs);
145 145 int animationDuration() const;
146 146 void setAnimationEasingCurve(const QEasingCurve &curve);
147 147 QEasingCurve animationEasingCurve() const;
148 148
149 149 void zoomIn();
150 void zoomIn2(double factor);
150 151 void zoomOut();
152 void zoomOut2(double factor);
151 153
152 154 void zoomIn(const QRectF &rect);
153 155 void zoom(qreal factor);
154 156 void zoomReset();
155 157 bool isZoomed();
156 158
157 159 void scroll(qreal dx, qreal dy);
158 160
159 161 QLegend *legend() const;
160 162
161 163 void setMargins(const QMargins &margins);
162 164 QMargins margins() const;
163 165
164 166 QRectF plotArea() const;
165 167 void setPlotAreaBackgroundBrush(const QBrush &brush);
166 168 QBrush plotAreaBackgroundBrush() const;
167 169 void setPlotAreaBackgroundPen(const QPen &pen);
168 170 QPen plotAreaBackgroundPen() const;
169 171 void setPlotAreaBackgroundVisible(bool visible = true);
170 172 bool isPlotAreaBackgroundVisible() const;
171 173 void setLocalizeNumbers(bool localize);
172 174 bool localizeNumbers() const;
173 175 void setLocale(const QLocale &locale);
174 176 QLocale locale() const;
175 177
176 178 QPointF mapToValue(const QPointF &position, QAbstractSeries *series = Q_NULLPTR);
177 179 QPointF mapToPosition(const QPointF &value, QAbstractSeries *series = Q_NULLPTR);
178 180
179 181 ChartType chartType() const;
180 182
181 183 Q_SIGNALS:
182 184 void plotAreaChanged(const QRectF &plotArea);
183 185
184 186 protected:
185 187 explicit QChart(QChart::ChartType type, QGraphicsItem *parent, Qt::WindowFlags wFlags);
186 188 QScopedPointer<QChartPrivate> d_ptr;
187 189 friend class QLegend;
188 190 friend class DeclarativeChart;
189 191 friend class ChartDataSet;
190 192 friend class ChartPresenter;
191 193 friend class ChartThemeManager;
192 194 friend class QAbstractSeries;
193 195 friend class QBoxPlotSeriesPrivate;
194 196
195 197 private:
196 198 Q_DISABLE_COPY(QChart)
197 199 };
198 200
199 201 QT_CHARTS_END_NAMESPACE
200 202
201 203 Q_DECLARE_OPERATORS_FOR_FLAGS(QT_CHARTS_NAMESPACE::QChart::AnimationOptions)
202 204
203 205 #endif // QCHART_H
@@ -1,79 +1,81
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2016 The Qt Company Ltd.
4 4 ** Contact: https://www.qt.io/licensing/
5 5 **
6 6 ** This file is part of the Qt Charts module of the Qt Toolkit.
7 7 **
8 8 ** $QT_BEGIN_LICENSE:GPL$
9 9 ** Commercial License Usage
10 10 ** Licensees holding valid commercial Qt licenses may use this file in
11 11 ** accordance with the 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 The Qt Company. For licensing terms
14 14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 15 ** information use the contact form at https://www.qt.io/contact-us.
16 16 **
17 17 ** GNU General Public License Usage
18 18 ** Alternatively, this file may be used under the terms of the GNU
19 19 ** General Public License version 3 or (at your option) any later version
20 20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 22 ** included in the packaging of this file. Please review the following
23 23 ** information to ensure the GNU General Public License requirements will
24 24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 25 **
26 26 ** $QT_END_LICENSE$
27 27 **
28 28 ****************************************************************************/
29 29
30 30 // W A R N I N G
31 31 // -------------
32 32 //
33 33 // This file is not part of the Qt Chart API. It exists purely as an
34 34 // implementation detail. This header file may change from version to
35 35 // version without notice, or even be removed.
36 36 //
37 37 // We mean it.
38 38
39 39 #ifndef QCHART_P_H
40 40 #define QCHART_P_H
41 41
42 42 #include <QtCharts/QChartGlobal>
43 43 #include <QtCharts/QChart>
44 44
45 45 QT_CHARTS_BEGIN_NAMESPACE
46 46
47 47 class ChartThemeManager;
48 48 class ChartPresenter;
49 49 class QLegend;
50 50 class ChartDataSet;
51 51
52 52 class QChartPrivate
53 53 {
54 54
55 55 public:
56 56 QChartPrivate(QChart *q, QChart::ChartType type);
57 57 ~QChartPrivate();
58 58 QChart *q_ptr;
59 59 QLegend *m_legend;
60 60 ChartDataSet *m_dataset;
61 61 ChartPresenter *m_presenter;
62 62 ChartThemeManager *m_themeManager;
63 63 QChart::ChartType m_type;
64 64
65 65 static QPen &defaultPen();
66 66 static QBrush &defaultBrush();
67 67 static QFont &defaultFont();
68 68
69 69 void init();
70 70 void zoomIn(qreal factor);
71 void zoomIn2(qreal factor);
71 72 void zoomOut(qreal factor);
73 void zoomOut2(qreal factor);
72 74 void zoomIn(const QRectF &rect);
73 75 void zoomReset();
74 76 bool isZoomed();
75 77 void scroll(qreal dx, qreal dy);
76 78 };
77 79
78 80 QT_CHARTS_END_NAMESPACE
79 81 #endif
General Comments 0
You need to be logged in to leave comments. Login now