@@ -1,115 +1,116 | |||||
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):QChartView(chart,parent), |
|
28 | ChartView::ChartView(QChart* chart,QWidget* parent):QChartView(chart,parent), | |
29 | m_index(-1),m_chart(chart) |
|
29 | m_index(-1),m_chart(chart) | |
30 | { |
|
30 | { | |
31 | m_chart->setTitle("Charts presenter"); |
|
31 | m_chart->setTitle("Charts presenter"); | |
32 | QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout())); |
|
32 | QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout())); | |
33 | m_timer.setInterval(3000); |
|
33 | m_timer.setInterval(3000); | |
34 |
|
34 | |||
35 | //![1] |
|
35 | //![1] | |
36 |
QLineSeries* series0 = new QLineSeries( |
|
36 | QLineSeries* series0 = new QLineSeries(); | |
37 | QPen blue(Qt::blue); |
|
37 | QPen blue(Qt::blue); | |
38 | blue.setWidth(3); |
|
38 | blue.setWidth(3); | |
39 | series0->setPen(blue); |
|
39 | series0->setPen(blue); | |
40 |
|
40 | |||
41 |
QScatterSeries* series1 = new QScatterSeries( |
|
41 | QScatterSeries* series1 = new QScatterSeries(); | |
42 | QPen red(Qt::red); |
|
42 | QPen red(Qt::red); | |
43 | red.setWidth(3); |
|
43 | red.setWidth(3); | |
44 | series1->setPen(red); |
|
44 | series1->setPen(red); | |
45 | series1->setBrush(Qt::white); |
|
45 | series1->setBrush(Qt::white); | |
46 |
|
46 | |||
47 |
QSplineSeries* series2 = new QSplineSeries( |
|
47 | QSplineSeries* series2 = new QSplineSeries(); | |
48 | QPen green(Qt::green); |
|
48 | QPen green(Qt::green); | |
49 | green.setWidth(3); |
|
49 | green.setWidth(3); | |
50 | series2->setPen(green); |
|
50 | series2->setPen(green); | |
51 |
|
51 | |||
52 | QAreaSeries* series3 = new QAreaSeries(series0); |
|
52 | QAreaSeries* series3 = new QAreaSeries(series0); | |
53 | QPen yellow(Qt::black); |
|
53 | QPen yellow(Qt::black); | |
54 | yellow.setWidth(3); |
|
54 | yellow.setWidth(3); | |
55 | series3->setPen(yellow); |
|
55 | series3->setPen(yellow); | |
56 | series3->setBrush(Qt::yellow); |
|
56 | series3->setBrush(Qt::yellow); | |
57 | //![1] |
|
57 | //![1] | |
58 |
|
58 | |||
59 | //![2] |
|
59 | //![2] | |
60 | int numPoints = 10; |
|
60 | int numPoints = 10; | |
61 |
|
61 | |||
62 | for (int x = 0; x <= numPoints; ++x) { |
|
62 | for (int x = 0; x <= numPoints; ++x) { | |
63 | qreal y = qrand() % 100; |
|
63 | qreal y = qrand() % 100; | |
64 | series0->append(x,y); |
|
64 | series0->append(x,y); | |
65 | series1->append(x,y); |
|
65 | series1->append(x,y); | |
66 | series2->append(x,y); |
|
66 | series2->append(x,y); | |
67 | } |
|
67 | } | |
68 | //![2] |
|
68 | //![2] | |
69 |
|
69 | |||
70 | //![3] |
|
70 | //![3] | |
71 | m_series<<series0; |
|
71 | m_series<<series0; | |
72 | m_titles<< m_chart->title()+": LineChart"; |
|
72 | m_titles<< m_chart->title()+": LineChart"; | |
73 | m_series<<series1; |
|
73 | m_series<<series1; | |
74 | m_titles<< m_chart->title()+": ScatterChart"; |
|
74 | m_titles<< m_chart->title()+": ScatterChart"; | |
75 | m_series<<series2; |
|
75 | m_series<<series2; | |
76 | m_titles<< m_chart->title()+": SplineChart"; |
|
76 | m_titles<< m_chart->title()+": SplineChart"; | |
77 | m_series<<series3; |
|
77 | m_series<<series3; | |
78 | m_titles<< m_chart->title()+": AreaChart"; |
|
78 | m_titles<< m_chart->title()+": AreaChart"; | |
79 | //![3] |
|
79 | //![3] | |
80 |
|
80 | |||
81 | //![4] |
|
81 | //![4] | |
82 | foreach (QSeries* series, m_series) { |
|
82 | foreach (QSeries* series, m_series) { | |
83 | QObject::connect(series,SIGNAL(clicked(QPointF)),this,SLOT(handlePointClicked(QPointF))); |
|
83 | QObject::connect(series,SIGNAL(clicked(QPointF)),this,SLOT(handlePointClicked(QPointF))); | |
84 | } |
|
84 | } | |
85 | //![4] |
|
85 | //![4] | |
86 | m_timer.start(); |
|
86 | m_timer.start(); | |
87 | handleTimeout(); |
|
87 | handleTimeout(); | |
88 | } |
|
88 | } | |
89 |
|
89 | |||
90 | ChartView::~ChartView() |
|
90 | ChartView::~ChartView() | |
91 | { |
|
91 | { | |
92 | if(m_series.size()==0) return; |
|
92 | if(m_series.size()==0) return; | |
93 | m_chart->removeSeries(m_series.at(m_index)); |
|
93 | m_chart->removeSeries(m_series.at(m_index)); | |
|
94 | m_series.removeLast(); //remove QAreaSeries instance since they will be deleted when QLineSeries instance is gone | |||
94 | qDeleteAll(m_series); |
|
95 | qDeleteAll(m_series); | |
95 | } |
|
96 | } | |
96 |
|
97 | |||
97 | //![5] |
|
98 | //![5] | |
98 | void ChartView::handleTimeout() |
|
99 | void ChartView::handleTimeout() | |
99 | { |
|
100 | { | |
100 | if(m_series.size()==0) return; |
|
101 | if(m_series.size()==0) return; | |
101 | if(m_index>=0) |
|
102 | if(m_index>=0) | |
102 | m_chart->removeSeries(m_series.at(m_index)); |
|
103 | m_chart->removeSeries(m_series.at(m_index)); | |
103 | m_index++; |
|
104 | m_index++; | |
104 | m_index=m_index%m_series.size(); |
|
105 | m_index=m_index%m_series.size(); | |
105 | m_chart->addSeries(m_series.at(m_index)); |
|
106 | m_chart->addSeries(m_series.at(m_index)); | |
106 | m_chart->setTitle(m_titles.at(m_index)); |
|
107 | m_chart->setTitle(m_titles.at(m_index)); | |
107 | } |
|
108 | } | |
108 | //![5] |
|
109 | //![5] | |
109 |
|
110 | |||
110 | //![6] |
|
111 | //![6] | |
111 | void ChartView::handlePointClicked(const QPointF& point) |
|
112 | void ChartView::handlePointClicked(const QPointF& point) | |
112 | { |
|
113 | { | |
113 | m_chart->setTitle(m_titles.at(m_index) + QString(" x: %1 y: %2").arg(point.x()).arg(point.y())); |
|
114 | m_chart->setTitle(m_titles.at(m_index) + QString(" x: %1 y: %2").arg(point.x()).arg(point.y())); | |
114 | } |
|
115 | } | |
115 | //![6] |
|
116 | //![6] |
General Comments 0
You need to be logged in to leave comments.
Login now