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