@@ -1,38 +1,38 | |||
|
1 | 1 | /*! |
|
2 | 2 | \example examples/presenterchart |
|
3 | 3 | \title PresenterChart Example |
|
4 | 4 | \subtitle |
|
5 | 5 | |
|
6 | 6 | The example shows how to create chart which presents the same set of data as line, scatter, spline and area charts. |
|
7 | 7 | ChartPresenter will switch between these four chart types every few seconds. Note: You can also use the |
|
8 | 8 | QAbstractItemModel related APIs to pass data for the different series types. See the \l {Model data example} for |
|
9 | 9 | more details. |
|
10 | 10 | |
|
11 | 11 | \image examples_presenterchart1.png |
|
12 | 12 | \image examples_presenterchart2.png |
|
13 | 13 | \image examples_presenterchart3.png |
|
14 | 14 | \image examples_presenterchart4.png |
|
15 | 15 | |
|
16 | 16 | We create simple ChartView class which derives form QChartView. |
|
17 | 17 | |
|
18 | 18 | \snippet ../examples/presenterchart/chartview.h 1 |
|
19 | 19 | |
|
20 | 20 | Class will implement \c handleTimeout() slot which we are going to use to trigger switching between different chart presentations. |
|
21 | 21 | We are also going to provide \c handlePoitClicked() slot which will show clicked point on the chart. |
|
22 | 22 | Class members \c m_series and \c m_titles are going to store series and related titles. In example we are going to present data as a line chart |
|
23 | 23 | using QLineSeries , as scatter chart using QScatterSeries, as a spline chart using QSplineSeries and a area chart using QAreaSeries. We create needed instances in constructor of ChartView class. |
|
24 | 24 | |
|
25 | 25 | \snippet ../examples/presenterchart/chartview.cpp 1 |
|
26 | 26 | |
|
27 | 27 | We add data to three series. Please note area chart is going to use QLineSeries as the upper line. We can use add() member function. |
|
28 | 28 | |
|
29 | 29 | \snippet ../examples/presenterchart/chartview.cpp 2 |
|
30 | 30 | |
|
31 | 31 | In the end we store references all the created series and matching titles. |
|
32 | 32 | |
|
33 | 33 | \snippet ../examples/presenterchart/chartview.cpp 3 |
|
34 | 34 | |
|
35 | In \c handleTimeout() slot we change currently displayed chart by removing previous series and adding next series from the \c m_series list. We also set proper title. | |
|
35 | In \c handleTimeout() slot we change currently displayed chart by removing previous series and adding next series from the \c m_series list. We also set proper title and create the axes for the chart. | |
|
36 | 36 | |
|
37 | 37 | \snippet ../examples/presenterchart/chartview.cpp 4 |
|
38 | 38 | */ |
@@ -1,95 +1,98 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "chartview.h" |
|
22 | 22 | #include <QLineSeries> |
|
23 | 23 | #include <QScatterSeries> |
|
24 | 24 | #include <QSplineSeries> |
|
25 | 25 | #include <QAreaSeries> |
|
26 | 26 | #include <QTime> |
|
27 | 27 | |
|
28 |
ChartView::ChartView(QChart* chart,QWidget* parent): |
|
|
29 | m_index(-1),m_chart(chart) | |
|
28 | ChartView::ChartView(QChart* chart,QWidget* parent): | |
|
29 | QChartView(chart,parent), | |
|
30 | m_index(-1), | |
|
31 | m_chart(chart) | |
|
30 | 32 | { |
|
31 | 33 | m_chart->setTitle("Charts presenter"); |
|
32 | 34 | m_chart->setDropShadowEnabled(false); |
|
33 | 35 | QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout())); |
|
34 | 36 | m_timer.setInterval(3000); |
|
35 | 37 | |
|
36 | //![1] | |
|
38 | //![1] | |
|
37 | 39 | QLineSeries* series0 = new QLineSeries(); |
|
38 | 40 | series0->setName("line"); |
|
39 | 41 | |
|
40 | 42 | QScatterSeries* series1 = new QScatterSeries(); |
|
41 | 43 | series1->setName("scatter"); |
|
42 | 44 | |
|
43 | 45 | QSplineSeries* series2 = new QSplineSeries(); |
|
44 | 46 | series2->setName("spline"); |
|
45 | 47 | |
|
46 | 48 | QAreaSeries* series3 = new QAreaSeries(series0); |
|
47 | 49 | series3->setName("area"); |
|
48 | //![1] | |
|
50 | //![1] | |
|
49 | 51 | |
|
50 | //![2] | |
|
52 | //![2] | |
|
51 | 53 | int numPoints = 10; |
|
52 | 54 | |
|
53 | 55 | for (int x = 0; x <= numPoints; ++x) { |
|
54 | 56 | qreal y = qrand() % 100; |
|
55 | 57 | series0->append(x,y); |
|
56 | 58 | series1->append(x,y); |
|
57 | 59 | series2->append(x,y); |
|
58 | 60 | } |
|
59 | //![2] | |
|
61 | //![2] | |
|
60 | 62 | |
|
61 | //![3] | |
|
63 | //![3] | |
|
62 | 64 | m_series<<series0; |
|
63 | 65 | m_titles<< m_chart->title()+": LineChart"; |
|
64 | 66 | m_series<<series1; |
|
65 | 67 | m_titles<< m_chart->title()+": ScatterChart"; |
|
66 | 68 | m_series<<series2; |
|
67 | 69 | m_titles<< m_chart->title()+": SplineChart"; |
|
68 | 70 | m_series<<series3; |
|
69 | 71 | m_titles<< m_chart->title()+": AreaChart"; |
|
70 | //![3] | |
|
72 | //![3] | |
|
71 | 73 | |
|
72 | 74 | m_timer.start(); |
|
73 | 75 | handleTimeout(); |
|
74 | 76 | } |
|
75 | 77 | |
|
76 | 78 | ChartView::~ChartView() |
|
77 | 79 | { |
|
78 | if(m_series.size()==0) return; | |
|
80 | if(m_series.size() == 0) return; | |
|
79 | 81 | m_chart->removeSeries(m_series.at(m_index)); |
|
80 | 82 | m_series.removeLast(); //remove QAreaSeries instance since they will be deleted when QLineSeries instance is gone |
|
81 | 83 | qDeleteAll(m_series); |
|
82 | 84 | } |
|
83 | 85 | |
|
84 | 86 | //![4] |
|
85 | 87 | void ChartView::handleTimeout() |
|
86 | 88 | { |
|
87 | if(m_series.size()==0) return; | |
|
88 | if(m_index>=0) | |
|
89 | m_chart->removeSeries(m_series.at(m_index)); | |
|
89 | if(m_series.size() == 0) return; | |
|
90 | if(m_index >= 0) | |
|
91 | m_chart->removeSeries(m_series.at(m_index)); | |
|
90 | 92 | m_index++; |
|
91 | m_index=m_index%m_series.size(); | |
|
93 | m_index = m_index % m_series.size(); | |
|
92 | 94 | m_chart->addSeries(m_series.at(m_index)); |
|
93 | 95 | m_chart->setTitle(m_titles.at(m_index)); |
|
96 | m_chart->createDefaultAxes(); | |
|
94 | 97 | } |
|
95 | 98 | //![4] |
General Comments 0
You need to be logged in to leave comments.
Login now