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