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