@@ -1,117 +1,115 | |||||
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( |
|
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(this); |
|
36 | QLineSeries* series0 = new QLineSeries(this); | |
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(this); |
|
41 | QScatterSeries* series1 = new QScatterSeries(this); | |
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(this); |
|
47 | QSplineSeries* series2 = new QSplineSeries(this); | |
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 | m_chart->addSeries(series0); |
|
|||
82 | m_chart->setTitle(m_titles.at(0)); |
|
|||
83 |
|
||||
84 | //![4] |
|
81 | //![4] | |
85 | foreach (QSeries* series, m_series) { |
|
82 | foreach (QSeries* series, m_series) { | |
86 | QObject::connect(series,SIGNAL(clicked(const QPointF&)),this,SLOT(handlePointClicked(const QPointF&))); |
|
83 | QObject::connect(series,SIGNAL(clicked(const QPointF&)),this,SLOT(handlePointClicked(const QPointF&))); | |
87 | } |
|
84 | } | |
88 | //![4] |
|
85 | //![4] | |
89 |
|
86 | QTimer::singleShot(0,this,SLOT(handleTimeout())); | ||
90 | m_timer.start(); |
|
87 | m_timer.start(); | |
91 | } |
|
88 | } | |
92 |
|
89 | |||
93 | ChartView::~ChartView() |
|
90 | ChartView::~ChartView() | |
94 | { |
|
91 | { | |
95 | if(m_series.size()==0) return; |
|
92 | if(m_series.size()==0) return; | |
96 | m_chart->removeSeries(m_series.at(m_index)); |
|
93 | m_chart->removeSeries(m_series.at(m_index)); | |
97 | qDeleteAll(m_series); |
|
94 | qDeleteAll(m_series); | |
98 | } |
|
95 | } | |
99 |
|
96 | |||
100 | //![5] |
|
97 | //![5] | |
101 | void ChartView::handleTimeout() |
|
98 | void ChartView::handleTimeout() | |
102 | { |
|
99 | { | |
103 | if(m_series.size()==0) return; |
|
100 | if(m_series.size()==0) return; | |
|
101 | if(m_index>=0) | |||
104 | m_chart->removeSeries(m_series.at(m_index)); |
|
102 | m_chart->removeSeries(m_series.at(m_index)); | |
105 | m_index++; |
|
103 | m_index++; | |
106 | m_index=m_index%m_series.size(); |
|
104 | m_index=m_index%m_series.size(); | |
107 | m_chart->addSeries(m_series.at(m_index)); |
|
105 | m_chart->addSeries(m_series.at(m_index)); | |
108 | m_chart->setTitle(m_titles.at(m_index)); |
|
106 | m_chart->setTitle(m_titles.at(m_index)); | |
109 | } |
|
107 | } | |
110 | //![5] |
|
108 | //![5] | |
111 |
|
109 | |||
112 | //![6] |
|
110 | //![6] | |
113 | void ChartView::handlePointClicked(const QPointF& point) |
|
111 | void ChartView::handlePointClicked(const QPointF& point) | |
114 | { |
|
112 | { | |
115 | m_chart->setTitle(m_titles.at(m_index) + QString(" x: %1 y: %2").arg(point.x()).arg(point.y())); |
|
113 | m_chart->setTitle(m_titles.at(m_index) + QString(" x: %1 y: %2").arg(point.x()).arg(point.y())); | |
116 | } |
|
114 | } | |
117 | //![6] |
|
115 | //![6] |
General Comments 0
You need to be logged in to leave comments.
Login now