@@ -1,60 +1,60 | |||
|
1 | 1 | #include <QTimer> |
|
2 | 2 | #include <QTime> |
|
3 | 3 | #include <QObject> |
|
4 | 4 | #include <cmath> |
|
5 | 5 | #include <qlinechartseries.h> |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | #define PI 3.14159265358979 |
|
10 | 10 | static const int numPoints =100; |
|
11 | 11 | |
|
12 | 12 | class WaveGenerator: public QObject |
|
13 | 13 | { |
|
14 | 14 | Q_OBJECT |
|
15 | 15 | |
|
16 | 16 | public: |
|
17 | 17 | WaveGenerator(QLineChartSeries* series1, QLineChartSeries* series2) : |
|
18 | 18 | m_series1(series1), |
|
19 | 19 | m_series2(series2), |
|
20 | 20 | m_wave(0), |
|
21 | 21 | m_step(2*PI/numPoints) |
|
22 | 22 | { |
|
23 | 23 | |
|
24 | 24 | QTime now = QTime::currentTime(); |
|
25 | 25 | qsrand((uint)now.msec()); |
|
26 | 26 | |
|
27 | 27 | int fluctuate = 100; |
|
28 | 28 | |
|
29 | 29 | for (qreal x = 0; x <= 2*PI; x+=m_step) { |
|
30 | 30 | series1->add(x, fabs(sin(x)*fluctuate)); |
|
31 | 31 | series2->add(x, fabs(cos(x)*fluctuate)); |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(update())); |
|
35 | 35 | m_timer.setInterval(5000); |
|
36 | 36 | m_timer.start(); |
|
37 | 37 | |
|
38 | 38 | }; |
|
39 | 39 | |
|
40 | 40 | public slots: |
|
41 | 41 | void update() |
|
42 | 42 | { |
|
43 | 43 | int fluctuate; |
|
44 | 44 | |
|
45 | 45 | for (qreal i = 0, x = 0; x <= 2*PI; x+=m_step, i++) { |
|
46 | 46 | fluctuate = qrand() % 100; |
|
47 |
m_series1-> |
|
|
47 | m_series1->replace(x, fabs(sin(x)*fluctuate)); | |
|
48 | 48 | fluctuate = qrand() % 100; |
|
49 |
m_series2-> |
|
|
49 | m_series2->replace(x, fabs(cos(x)*fluctuate)); | |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | private: |
|
55 | 55 | QLineChartSeries* m_series1; |
|
56 | 56 | QLineChartSeries* m_series2; |
|
57 | 57 | int m_wave; |
|
58 | 58 | qreal m_step; |
|
59 | 59 | QTimer m_timer; |
|
60 | 60 | }; |
@@ -1,182 +1,195 | |||
|
1 | 1 | #include "qlinechartseries.h" |
|
2 | 2 | |
|
3 | 3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | 4 | |
|
5 | 5 | /*! |
|
6 | 6 | \class QLineChartSeries |
|
7 | 7 | \brief The QLineChartSeries class is used for making line charts. |
|
8 | 8 | |
|
9 | 9 | \mainclass |
|
10 | 10 | |
|
11 | 11 | A line chart is used to show information as a series of data points |
|
12 | 12 | connected by straight lines. |
|
13 | 13 | |
|
14 | 14 | \image linechart.png |
|
15 | 15 | |
|
16 | 16 | To create line charts, users need to first QLineChartSeries object. |
|
17 | 17 | |
|
18 | 18 | \snippet ../example/linechart/main.cpp 1 |
|
19 | 19 | |
|
20 | 20 | Populate with the data |
|
21 | 21 | |
|
22 | 22 | \snippet ../example/linechart/main.cpp 2 |
|
23 | 23 | |
|
24 | 24 | Add created series objects to QChartView or QChart instance. |
|
25 | 25 | |
|
26 | 26 | \snippet ../example/linechart/main.cpp 3 |
|
27 | 27 | |
|
28 | 28 | */ |
|
29 | 29 | |
|
30 | 30 | /*! |
|
31 | 31 | \fn virtual QChartSeriesType QLineChartSeries::type() const |
|
32 | 32 | \brief Returns type of series. |
|
33 | 33 | \sa QChartSeries, QChartSeriesType |
|
34 | 34 | */ |
|
35 | 35 | |
|
36 | 36 | /*! |
|
37 | 37 | \fn QPen QLineChartSeries::pen() const |
|
38 | 38 | \brief Returns the pen used to draw line for this series. |
|
39 | 39 | \sa setPen() |
|
40 | 40 | */ |
|
41 | 41 | |
|
42 | 42 | /*! |
|
43 | 43 | \fn bool QLineChartSeries::isPointsVisible() const |
|
44 | 44 | \brief Returns if the points are drawn for this series. |
|
45 | 45 | \sa setPointsVisible() |
|
46 | 46 | */ |
|
47 | 47 | |
|
48 | 48 | |
|
49 | 49 | /*! |
|
50 | 50 | \fn void QLineChartSeries::changed(int index) |
|
51 | 51 | \brief \internal \a index |
|
52 | 52 | */ |
|
53 | 53 | |
|
54 | 54 | /*! |
|
55 | 55 | Constructs empty series object which is a child of \a parent. |
|
56 | 56 | When series object is added to QChartView or QChart instance ownerships is transfered. |
|
57 | 57 | */ |
|
58 | 58 | QLineChartSeries::QLineChartSeries(QObject* parent):QChartSeries(parent), |
|
59 | 59 | m_pointsVisible(false) |
|
60 | 60 | { |
|
61 | 61 | } |
|
62 | 62 | /*! |
|
63 | 63 | Destroys the object. Series added to QChartView or QChart instances are owned by those, |
|
64 | 64 | and are deleted when mentioned object are destroyed. |
|
65 | 65 | */ |
|
66 | 66 | QLineChartSeries::~QLineChartSeries() |
|
67 | 67 | { |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | /*! |
|
71 | 71 | Adds data point \a x \a y to the series. Points are connected with lines on the chart. |
|
72 | Function returns index, which can be used to modify data. | |
|
73 | 72 | */ |
|
74 |
|
|
|
73 | void QLineChartSeries::add(qreal x,qreal y) | |
|
75 | 74 | { |
|
76 | 75 | m_x<<x; |
|
77 | 76 | m_y<<y; |
|
78 | return m_x.size()-1; | |
|
79 | 77 | } |
|
80 | 78 | |
|
81 | 79 | /*! |
|
82 | 80 | This is an overloaded function. |
|
83 | 81 | Adds data \a point to the series. Points are connected with lines on the chart. |
|
84 | Function returns index, which can be used to modify data. | |
|
85 | 82 | */ |
|
86 |
|
|
|
83 | void QLineChartSeries::add(const QPointF& point) | |
|
87 | 84 | { |
|
88 | 85 | m_x<<point.x(); |
|
89 | 86 | m_y<<point.y(); |
|
90 | return m_x.size()-1; | |
|
91 | 87 | } |
|
92 | 88 | |
|
93 | 89 | /*! |
|
94 | Modifies data within \a index, sets new \a x and \a y values. | |
|
90 | Modifies \a y value for given \a x a value. | |
|
95 | 91 | */ |
|
96 |
void QLineChartSeries:: |
|
|
92 | void QLineChartSeries::replace(qreal x,qreal y) | |
|
97 | 93 | { |
|
94 | int index = m_x.indexOf(x); | |
|
98 | 95 | m_x[index]=x; |
|
99 | 96 | m_y[index]=y; |
|
100 | 97 | emit changed(index); |
|
101 | 98 | } |
|
102 | 99 | |
|
103 | 100 | /*! |
|
104 | 101 | This is an overloaded function. |
|
105 | Modifies data within \a index, sets new \a point value. | |
|
102 | Replaces current y value of for given \a point x value with \a point y value. | |
|
106 | 103 | */ |
|
107 |
void QLineChartSeries:: |
|
|
104 | void QLineChartSeries::replace(const QPointF& point) | |
|
108 | 105 | { |
|
106 | int index = m_x.indexOf(point.x()); | |
|
109 | 107 | m_x[index]=point.x(); |
|
110 | 108 | m_y[index]=point.y(); |
|
111 | 109 | emit changed(index); |
|
112 | 110 | } |
|
113 | 111 | |
|
112 | /*! | |
|
113 | Removes current \a x and y value. | |
|
114 | */ | |
|
115 | void QLineChartSeries::remove(qreal x) | |
|
116 | { | |
|
117 | ||
|
118 | } | |
|
119 | ||
|
120 | /*! | |
|
121 | Removes current \a point x value. Note \point y value is ignored. | |
|
122 | */ | |
|
123 | void QLineChartSeries::remove(const QPointF& point) | |
|
124 | { | |
|
125 | ||
|
126 | } | |
|
114 | 127 | |
|
115 | 128 | /*! |
|
116 | 129 | Clears all the data. |
|
117 | 130 | */ |
|
118 | 131 | void QLineChartSeries::clear() |
|
119 | 132 | { |
|
120 | 133 | m_x.clear(); |
|
121 | 134 | m_y.clear(); |
|
122 | 135 | } |
|
123 | 136 | |
|
124 | 137 | /*! |
|
125 | 138 | \internal \a pos |
|
126 | 139 | */ |
|
127 | 140 | qreal QLineChartSeries::x(int pos) const |
|
128 | 141 | { |
|
129 | 142 | return m_x.at(pos); |
|
130 | 143 | } |
|
131 | 144 | |
|
132 | 145 | /*! |
|
133 | 146 | \internal \a pos |
|
134 | 147 | */ |
|
135 | 148 | qreal QLineChartSeries::y(int pos) const |
|
136 | 149 | { |
|
137 | 150 | return m_y.at(pos); |
|
138 | 151 | } |
|
139 | 152 | |
|
140 | 153 | /*! |
|
141 | 154 | Returns number of data points within series. |
|
142 | 155 | */ |
|
143 | 156 | int QLineChartSeries::count() const |
|
144 | 157 | { |
|
145 | 158 | Q_ASSERT(m_x.size() == m_y.size()); |
|
146 | 159 | |
|
147 | 160 | return m_x.size(); |
|
148 | 161 | |
|
149 | 162 | } |
|
150 | 163 | |
|
151 | 164 | /*! |
|
152 | 165 | Sets \a pen used for drawing given series.. |
|
153 | 166 | */ |
|
154 | 167 | void QLineChartSeries::setPen(const QPen& pen) |
|
155 | 168 | { |
|
156 | 169 | m_pen=pen; |
|
157 | 170 | } |
|
158 | 171 | |
|
159 | 172 | /*! |
|
160 | 173 | Sets if data points are \a visible and should be drawn on line. |
|
161 | 174 | */ |
|
162 | 175 | void QLineChartSeries::setPointsVisible(bool visible) |
|
163 | 176 | { |
|
164 | 177 | m_pointsVisible=visible; |
|
165 | 178 | } |
|
166 | 179 | |
|
167 | 180 | QDebug operator<< (QDebug debug, const QLineChartSeries series) |
|
168 | 181 | { |
|
169 | 182 | Q_ASSERT(series.m_x.size() == series.m_y.size()); |
|
170 | 183 | |
|
171 | 184 | int size = series.m_x.size(); |
|
172 | 185 | |
|
173 | 186 | for (int i=0;i<size;i++) { |
|
174 | 187 | debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") "; |
|
175 | 188 | } |
|
176 | 189 | return debug.space(); |
|
177 | 190 | } |
|
178 | 191 | |
|
179 | 192 | |
|
180 | 193 | #include "moc_qlinechartseries.cpp" |
|
181 | 194 | |
|
182 | 195 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,50 +1,52 | |||
|
1 | 1 | #ifndef QLINECHARTSERIES_H_ |
|
2 | 2 | #define QLINECHARTSERIES_H_ |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "qchartseries.h" |
|
6 | 6 | #include <QDebug> |
|
7 | 7 | #include <QPen> |
|
8 | 8 | #include <QBrush> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | class QTCOMMERCIALCHART_EXPORT QLineChartSeries : public QChartSeries |
|
13 | 13 | { |
|
14 | 14 | Q_OBJECT |
|
15 | 15 | public: |
|
16 | 16 | QLineChartSeries(QObject* parent=0); |
|
17 | 17 | virtual ~QLineChartSeries(); |
|
18 | 18 | |
|
19 | 19 | public: // from QChartSeries |
|
20 | 20 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeLine;} |
|
21 |
|
|
|
22 |
|
|
|
23 |
void |
|
|
24 |
void |
|
|
21 | void add(qreal x, qreal y); | |
|
22 | void add(const QPointF& point); | |
|
23 | void replace(qreal x,qreal y); | |
|
24 | void replace(const QPointF& point); | |
|
25 | void remove(qreal x); | |
|
26 | void remove(const QPointF& point); | |
|
25 | 27 | void clear(); |
|
26 | 28 | |
|
27 | 29 | void setPen(const QPen& pen); |
|
28 | 30 | QPen pen() const { return m_pen;} |
|
29 | 31 | |
|
30 | 32 | void setPointsVisible(bool visible); |
|
31 | 33 | bool isPointsVisible() const {return m_pointsVisible;} |
|
32 | 34 | |
|
33 | 35 | int count() const; |
|
34 | 36 | qreal x(int pos) const; |
|
35 | 37 | qreal y(int pos) const; |
|
36 | 38 | friend QDebug operator<< (QDebug d, const QLineChartSeries series); |
|
37 | 39 | |
|
38 | 40 | signals: |
|
39 | 41 | void changed(int index); |
|
40 | 42 | |
|
41 | 43 | private: |
|
42 | 44 | QVector<qreal> m_x; |
|
43 | 45 | QVector<qreal> m_y; |
|
44 | 46 | QPen m_pen; |
|
45 | 47 | bool m_pointsVisible; |
|
46 | 48 | }; |
|
47 | 49 | |
|
48 | 50 | QTCOMMERCIALCHART_END_NAMESPACE |
|
49 | 51 | |
|
50 | 52 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now