##// END OF EJS Templates
Replace to QScatterSeries
Tero Ahola -
r395:9f537617b108
parent child
Show More
@@ -1,57 +1,59
1 #include <QtGui/QApplication>
1 #include <QtGui/QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartglobal.h>
3 #include <qchartglobal.h>
4 #include <qchartview.h>
4 #include <qchartview.h>
5 #include <qscatterseries.h>
5 #include <qscatterseries.h>
6
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
8
9 int main(int argc, char *argv[])
9 int main(int argc, char *argv[])
10 {
10 {
11 QApplication a(argc, argv);
11 QApplication a(argc, argv);
12
12
13 //! [1]
13 //! [1]
14 // Create chart view
14 // Create chart view
15 QChartView *chartView = new QChartView();
15 QChartView *chartView = new QChartView();
16 chartView->setChartTheme(QChart::ChartThemeIcy);
16 chartView->setChartTheme(QChart::ChartThemeIcy);
17 // Add scatter series with simple test data
17 // Add scatter series with simple test data
18 QScatterSeries *scatter = new QScatterSeries();
18 QScatterSeries *scatter = new QScatterSeries();
19 *scatter << QPointF(0.5, 5.0) << QPointF(1.0, 4.5) << QPointF(1.0, 5.5) << QPointF(1.5, 5.0);
19 *scatter << QPointF(0.5, 5.0) << QPointF(1.0, 4.5) << QPointF(1.0, 5.5) << QPointF(1.5, 5.0);
20 // Chart takes ownership
20 // Chart takes ownership
21 chartView->addSeries(scatter);
21 chartView->addSeries(scatter);
22 //! [1]
22 //! [1]
23 // scatter->replace(0, QPointF(0.75, 5.0));
23
24
24 // And more
25 // And more
25 //! [3]
26 //! [3]
26 *scatter << QPointF(2.0, 5.5) << QPointF(2.2, 5.4);
27 *scatter << QPointF(2.0, 5.5) << QPointF(2.2, 5.4);
27 //! [3]
28 //! [3]
28
29
29 // Add another scatter series (re-use the previous pointer)
30 // Add another scatter series (re-use the previous pointer because the code used as snippets
31 // in the docs)
30 // - more data with random component
32 // - more data with random component
31 scatter = new QScatterSeries();
33 scatter = new QScatterSeries();
32 for (qreal i(0.0); i < 20; i += 0.15) {
34 for (qreal i(0.0); i < 20; i += 0.15) {
33 (*scatter) << QPointF(i + (qreal)(rand() % 100) / 100.0,
35 (*scatter) << QPointF(i + (qreal)(rand() % 100) / 100.0,
34 i + (qreal)(rand() % 100) / 100.0);
36 i + (qreal)(rand() % 100) / 100.0);
35 }
37 }
36 //! [4]
38 //! [4]
37 QBrush brush(QColor(255, 0, 0, 100), Qt::SolidPattern);
39 QBrush brush(QColor(255, 0, 0, 100), Qt::SolidPattern);
38 scatter->setBrush(brush);
40 scatter->setBrush(brush);
39 //! [4]
41 //! [4]
40 //! [5]
42 //! [5]
41 QPen pen(QColor(0, 255, 0, 80), 3);
43 QPen pen(QColor(0, 255, 0, 80), 3);
42 scatter->setPen(pen);
44 scatter->setPen(pen);
43 //! [5]
45 //! [5]
44 //! [6]
46 //! [6]
45 scatter->setShape(QScatterSeries::MarkerShapeRectangle);
47 scatter->setShape(QScatterSeries::MarkerShapeRectangle);
46 //! [6]
48 //! [6]
47 chartView->addSeries(scatter);
49 chartView->addSeries(scatter);
48
50
49 // Use the chart widget as the central widget
51 // Use the chart widget as the central widget
50 QMainWindow w;
52 QMainWindow w;
51 w.resize(400, 300);
53 w.resize(400, 300);
52 w.setCentralWidget(chartView);
54 w.setCentralWidget(chartView);
53 w.setWindowFlags(Qt::FramelessWindowHint);
55 w.setWindowFlags(Qt::FramelessWindowHint);
54 w.show();
56 w.show();
55
57
56 return a.exec();
58 return a.exec();
57 }
59 }
@@ -1,277 +1,291
1 #include "qscatterseries.h"
1 #include "qscatterseries.h"
2 #include "scatterseries_p.h"
2 #include "scatterseries_p.h"
3 #include "qchart.h"
3 #include "qchart.h"
4
4
5 /*!
5 /*!
6 \class QScatterSeries
6 \class QScatterSeries
7 \brief QtCommercial Chart series API for showing scatter series.
7 \brief QtCommercial Chart series API for showing scatter series.
8
8
9 \mainclass
9 \mainclass
10
10
11 Example on how to create a chart with scatter series:
11 Example on how to create a chart with scatter series:
12 \snippet ../example/scatter/main.cpp 1
12 \snippet ../example/scatter/main.cpp 1
13
13
14 The example code would result the following:
14 The example code would result the following:
15
15
16 \image scatter_example1.jpg
16 \image scatter_example1.jpg
17 */
17 */
18
18
19 /*!
19 /*!
20 \enum QScatterSeries::MarkerShape
20 \enum QScatterSeries::MarkerShape
21
21
22 This enum describes the shape used when rendering marker items.
22 This enum describes the shape used when rendering marker items.
23
23
24 \value MarkerShapeDefault
24 \value MarkerShapeDefault
25 \value MarkerShapePoint
25 \value MarkerShapePoint
26 \value MarkerShapeX
26 \value MarkerShapeX
27 \value MarkerShapeRectangle
27 \value MarkerShapeRectangle
28 \value MarkerShapeTiltedRectangle
28 \value MarkerShapeTiltedRectangle
29 \value MarkerShapeTriangle
29 \value MarkerShapeTriangle
30 \value MarkerShapeCircle
30 \value MarkerShapeCircle
31 */
31 */
32
32
33 /*!
33 /*!
34 \fn QChartSeriesType QScatterSeries::type() const
34 \fn QChartSeriesType QScatterSeries::type() const
35 \brief Returns QChartSeries::SeriesTypeScatter.
35 \brief Returns QChartSeries::SeriesTypeScatter.
36 */
36 */
37
37
38 /*!
38 /*!
39 \fn void QScatterSeries::clicked(QPointF coordinate)
39 \fn void QScatterSeries::clicked(QPointF coordinate)
40 User clicked the scatter series. Note that the \a coordinate is the chart coordinate that the
40 User clicked the scatter series. Note that the \a coordinate is the chart coordinate that the
41 click occurred on; not necessarily a data point coordinate. To find the corresponding (closest)
41 click occurred on; not necessarily a data point coordinate. To find the corresponding (closest)
42 data point you can use closestPoint().
42 data point you can use closestPoint().
43 */
43 */
44
44
45 /*!
45 /*!
46 \fn void QScatterSeries::changed()
46 \fn void QScatterSeries::changed()
47 \brief TODO
47 \brief TODO
48 */
48 */
49
49
50 QTCOMMERCIALCHART_BEGIN_NAMESPACE
50 QTCOMMERCIALCHART_BEGIN_NAMESPACE
51
51
52 QScatterSeriesPrivate::QScatterSeriesPrivate() :
52 QScatterSeriesPrivate::QScatterSeriesPrivate() :
53 m_data(QList<QPointF>()),
53 m_data(QList<QPointF>()),
54 m_markerPen(QPen(QColor::Invalid)),
54 m_markerPen(QPen(QColor::Invalid)),
55 m_markerBrush(QBrush(QColor::Invalid)),
55 m_markerBrush(QBrush(QColor::Invalid)),
56 m_markerShape(QScatterSeries::MarkerShapeDefault)
56 m_markerShape(QScatterSeries::MarkerShapeDefault)
57 {
57 {
58 }
58 }
59
59
60 /*!
60 /*!
61 Constructs a series object which is a child of \a parent.
61 Constructs a series object which is a child of \a parent.
62 */
62 */
63 QScatterSeries::QScatterSeries(QObject *parent) :
63 QScatterSeries::QScatterSeries(QObject *parent) :
64 QSeries(parent),
64 QSeries(parent),
65 d(new QScatterSeriesPrivate())
65 d(new QScatterSeriesPrivate())
66 {
66 {
67 }
67 }
68
68
69 /*!
69 /*!
70 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
70 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
71 */
71 */
72 QScatterSeries::~QScatterSeries()
72 QScatterSeries::~QScatterSeries()
73 {
73 {
74 delete d;
74 delete d;
75 }
75 }
76
76
77 /*!
77 /*!
78 Add single data point with \a x and \a y coordinates to the series.
78 Add single data point with \a x and \a y coordinates to the series.
79 */
79 */
80 void QScatterSeries::add(qreal x, qreal y)
80 void QScatterSeries::add(qreal x, qreal y)
81 {
81 {
82 d->m_data.append(QPointF(x, y));
82 d->m_data.append(QPointF(x, y));
83 emit changed();
83 emit changed();
84 }
84 }
85
85
86 /*!
86 /*!
87 Add single data point with \a value to the series.
87 Add single data point with \a value to the series.
88 */
88 */
89 void QScatterSeries::add(QPointF value)
89 void QScatterSeries::add(QPointF value)
90 {
90 {
91 d->m_data.append(value);
91 d->m_data.append(value);
92 emit changed();
92 emit changed();
93 }
93 }
94
94
95 /*!
95 /*!
96 Add list of \a points to the series.
96 Add list of \a points to the series.
97 */
97 */
98 void QScatterSeries::add(QList<QPointF> points)
98 void QScatterSeries::add(QList<QPointF> points)
99 {
99 {
100 d->m_data.append(points);
100 d->m_data.append(points);
101 emit changed();
101 emit changed();
102 }
102 }
103
103
104 /*!
104 /*!
105 Stream operator for adding a data point with \a value to the series.
105 Stream operator for adding a data point with \a value to the series.
106 \sa add()
106 \sa add()
107
107
108 For example:
108 For example:
109 \snippet ../example/scatter/main.cpp 3
109 \snippet ../example/scatter/main.cpp 3
110 */
110 */
111 QScatterSeries& QScatterSeries::operator << (const QPointF &value)
111 QScatterSeries& QScatterSeries::operator << (const QPointF &value)
112 {
112 {
113 d->m_data.append(value);
113 d->m_data.append(value);
114 emit changed();
114 emit changed();
115 return *this;
115 return *this;
116 }
116 }
117
117
118 /*!
118 /*!
119 Stream operator for adding a list of points to the series.
119 Stream operator for adding a list of points to the series.
120 \sa add()
120 \sa add()
121 */
121 */
122 QScatterSeries& QScatterSeries::operator << (QList<QPointF> value)
122 QScatterSeries& QScatterSeries::operator << (QList<QPointF> value)
123 {
123 {
124 d->m_data.append(value);
124 d->m_data.append(value);
125 emit changed();
125 emit changed();
126 return *this;
126 return *this;
127 }
127 }
128
128
129 /*!
129 /*!
130 Replaces the data of the series with the given list of data \a points.
130 Replaces the data of the series with the given list of data \a points.
131 */
131 */
132 void QScatterSeries::setData(QList<QPointF> points)
132 void QScatterSeries::setData(QList<QPointF> points)
133 {
133 {
134 d->m_data = points;
134 d->m_data = points;
135 emit changed();
135 emit changed();
136 }
136 }
137
137
138 /*!
138 /*!
139 Returns the current list of data points of the series.
139 Returns the current list of data points of the series.
140 */
140 */
141 QList<QPointF> QScatterSeries::data()
141 QList<QPointF> QScatterSeries::data()
142 {
142 {
143 return d->m_data;
143 return d->m_data;
144 }
144 }
145
145
146 /*!
146 /*!
147 Remove the data point at \a pointIndex. Returns true if a point was removed, false if the point
147 Replaces the point at \a index with \a newPoint. Returns true if \a index is a valid position
148 at \a pointIndex does not exist on the series.
148 in the series data, false otherwise.
149 */
149 */
150 bool QScatterSeries::removeAt(int pointIndex)
150 bool QScatterSeries::replace(int index, QPointF newPoint)
151 {
151 {
152 if (pointIndex >=0 && pointIndex < d->m_data.count()) {
152 if (index >= 0 && index < d->m_data.count()) {
153 d->m_data.removeAt(pointIndex);
153 d->m_data.replace(index, newPoint);
154 emit changed();
155 return true;
156 }
157 return false;
158 }
159
160 /*!
161 Remove the data point at \a index. Returns true if a point was removed, false if the point
162 at \a index does not exist on the series.
163 */
164 bool QScatterSeries::removeAt(int index)
165 {
166 if (index >=0 && index < d->m_data.count()) {
167 d->m_data.removeAt(index);
154 emit changed();
168 emit changed();
155 return true;
169 return true;
156 }
170 }
157 return false;
171 return false;
158 }
172 }
159
173
160 /*!
174 /*!
161 Remove all occurrences of \a point from the series and returns the number of points removed.
175 Remove all occurrences of \a point from the series and returns the number of points removed.
162 */
176 */
163 int QScatterSeries::removeAll(QPointF point)
177 int QScatterSeries::removeAll(QPointF point)
164 {
178 {
165 int count = d->m_data.removeAll(point);
179 int count = d->m_data.removeAll(point);
166 emit changed();
180 emit changed();
167 return count;
181 return count;
168 }
182 }
169
183
170 /*!
184 /*!
171 Remove all data points from the series.
185 Remove all data points from the series.
172 */
186 */
173 void QScatterSeries::clear()
187 void QScatterSeries::clear()
174 {
188 {
175 d->m_data.clear();
189 d->m_data.clear();
176 emit changed();
190 emit changed();
177 }
191 }
178
192
179 /*!
193 /*!
180 Returns the index of the data point that is closest to \a coordinate. If several data points
194 Returns the index of the data point that is closest to \a coordinate. If several data points
181 are at the same distance from the \a coordinate, returns the last one. If no points exist,
195 are at the same distance from the \a coordinate, returns the last one. If no points exist,
182 returns -1.
196 returns -1.
183 */
197 */
184 int QScatterSeries::closestPoint(QPointF coordinate)
198 int QScatterSeries::closestPoint(QPointF coordinate)
185 {
199 {
186 qreal distance(-1);
200 qreal distance(-1);
187 int pointIndex(-1);
201 int pointIndex(-1);
188 for (int i(0); i < d->m_data.count(); i++) {
202 for (int i(0); i < d->m_data.count(); i++) {
189 QPointF dataPoint = d->m_data.at(i);
203 QPointF dataPoint = d->m_data.at(i);
190 QPointF difference = dataPoint - coordinate;
204 QPointF difference = dataPoint - coordinate;
191 if (i == 0 || difference.manhattanLength() <= distance) {
205 if (i == 0 || difference.manhattanLength() <= distance) {
192 distance = difference.manhattanLength();
206 distance = difference.manhattanLength();
193 pointIndex = i;
207 pointIndex = i;
194 }
208 }
195 }
209 }
196 return pointIndex;
210 return pointIndex;
197 }
211 }
198
212
199 /*!
213 /*!
200 Overrides the default pen used for drawing a marker item with a user defined \a pen. The
214 Overrides the default pen used for drawing a marker item with a user defined \a pen. The
201 default pen is defined by chart theme setting.
215 default pen is defined by chart theme setting.
202
216
203 For example:
217 For example:
204 \snippet ../example/scatter/main.cpp 5
218 \snippet ../example/scatter/main.cpp 5
205
219
206 Would present your scatter markers with an opaque, uglyish green outlines instead of the
220 Would present your scatter markers with an opaque, uglyish green outlines instead of the
207 beatiful markers defined by the chart's theme:
221 beatiful markers defined by the chart's theme:
208 \image scatter_example_pen.jpg
222 \image scatter_example_pen.jpg
209
223
210 \sa setBrush()
224 \sa setBrush()
211 \sa QChart::setChartTheme()
225 \sa QChart::setChartTheme()
212 */
226 */
213 void QScatterSeries::setPen(QPen pen)
227 void QScatterSeries::setPen(QPen pen)
214 {
228 {
215 d->m_markerPen = pen;
229 d->m_markerPen = pen;
216 }
230 }
217
231
218 /*!
232 /*!
219 Returns the pen used for drawing markers.
233 Returns the pen used for drawing markers.
220 */
234 */
221 QPen QScatterSeries::pen()
235 QPen QScatterSeries::pen()
222 {
236 {
223 return d->m_markerPen;
237 return d->m_markerPen;
224 }
238 }
225
239
226 /*!
240 /*!
227 Overrides the default brush of the marker items with a user defined \a brush. The default brush
241 Overrides the default brush of the marker items with a user defined \a brush. The default brush
228 is defined by chart theme setting.
242 is defined by chart theme setting.
229
243
230 For example:
244 For example:
231 \snippet ../example/scatter/main.cpp 4
245 \snippet ../example/scatter/main.cpp 4
232
246
233 Would fill your scatter markers with an opaque red color:
247 Would fill your scatter markers with an opaque red color:
234 \image scatter_example_brush.jpg
248 \image scatter_example_brush.jpg
235
249
236 \sa setPen()
250 \sa setPen()
237 \sa QChart::setChartTheme()
251 \sa QChart::setChartTheme()
238 */
252 */
239 void QScatterSeries::setBrush(QBrush brush)
253 void QScatterSeries::setBrush(QBrush brush)
240 {
254 {
241 d->m_markerBrush = brush;
255 d->m_markerBrush = brush;
242 }
256 }
243
257
244 /*!
258 /*!
245 Returns the brush used for drawing markers.
259 Returns the brush used for drawing markers.
246 */
260 */
247 QBrush QScatterSeries::brush()
261 QBrush QScatterSeries::brush()
248 {
262 {
249 return d->m_markerBrush;
263 return d->m_markerBrush;
250 }
264 }
251
265
252 /*!
266 /*!
253 Overrides the default shape of the marker items with a user defined \a shape. The default shape
267 Overrides the default shape of the marker items with a user defined \a shape. The default shape
254 is defined by chart theme setting.
268 is defined by chart theme setting.
255
269
256 For example:
270 For example:
257 \snippet ../example/scatter/main.cpp 6
271 \snippet ../example/scatter/main.cpp 6
258
272
259 Would make your scatter marker items rectangle:
273 Would make your scatter marker items rectangle:
260 \image scatter_example_shape.jpg
274 \image scatter_example_shape.jpg
261 */
275 */
262 void QScatterSeries::setShape(MarkerShape shape)
276 void QScatterSeries::setShape(MarkerShape shape)
263 {
277 {
264 d->m_markerShape = shape;
278 d->m_markerShape = shape;
265 }
279 }
266
280
267 /*!
281 /*!
268 Returns the shape used for drawing markers.
282 Returns the shape used for drawing markers.
269 */
283 */
270 QScatterSeries::MarkerShape QScatterSeries::shape()
284 QScatterSeries::MarkerShape QScatterSeries::shape()
271 {
285 {
272 return (QScatterSeries::MarkerShape) d->m_markerShape;
286 return (QScatterSeries::MarkerShape) d->m_markerShape;
273 }
287 }
274
288
275 #include "moc_qscatterseries.cpp"
289 #include "moc_qscatterseries.cpp"
276
290
277 QTCOMMERCIALCHART_END_NAMESPACE
291 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +1,73
1 #ifndef QSCATTERSERIES_H
1 #ifndef QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
3
3
4 #include "qseries.h"
4 #include "qseries.h"
5 #include <QRectF>
5 #include <QRectF>
6 #include <QColor>
6 #include <QColor>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 class QScatterSeriesPrivate;
9 class QScatterSeriesPrivate;
10
10
11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QSeries
11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QSeries
12 {
12 {
13 Q_OBJECT
13 Q_OBJECT
14
14
15 public:
15 public:
16 enum MarkerShape {
16 enum MarkerShape {
17 // TODO: to be defined by the graphics design
17 // TODO: to be defined by the graphics design
18 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
18 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
19 MarkerShapeDefault = 0,
19 MarkerShapeDefault = 0,
20 MarkerShapePoint,
20 MarkerShapePoint,
21 MarkerShapeX,
21 MarkerShapeX,
22 MarkerShapeRectangle,
22 MarkerShapeRectangle,
23 MarkerShapeTiltedRectangle,
23 MarkerShapeTiltedRectangle,
24 MarkerShapeTriangle,
24 MarkerShapeTriangle,
25 MarkerShapeCircle
25 MarkerShapeCircle
26 };
26 };
27
27
28 public:
28 public:
29 QScatterSeries(QObject *parent = 0);
29 QScatterSeries(QObject *parent = 0);
30 ~QScatterSeries();
30 ~QScatterSeries();
31
31
32 public: // from QChartSeries
32 public: // from QChartSeries
33 QSeriesType type() const { return QSeries::SeriesTypeScatter; }
33 QSeriesType type() const { return QSeries::SeriesTypeScatter; }
34
34
35 public:
35 public:
36 void add(qreal x, qreal y);
36 void add(qreal x, qreal y);
37 void add(QPointF value);
37 void add(QPointF value);
38 void add(QList<QPointF> points);
38 void add(QList<QPointF> points);
39 void setData(QList<QPointF> points);
39 void setData(QList<QPointF> points);
40 QScatterSeries& operator << (const QPointF &value);
40 QScatterSeries& operator << (const QPointF &value);
41 QScatterSeries& operator << (QList<QPointF> points);
41 QScatterSeries& operator << (QList<QPointF> points);
42 QList<QPointF> data();
42 QList<QPointF> data();
43 bool removeAt(int pointIndex);
43 bool replace(int index, QPointF newPoint);
44 bool removeAt(int index);
44 int removeAll(QPointF point);
45 int removeAll(QPointF point);
45 void clear();
46 void clear();
46 int closestPoint(QPointF coordinate);
47 int closestPoint(QPointF coordinate);
47 //TODO: insert, replace...?
48 //TODO: insert, replace...?
48
49
49 QPen pen();
50 QPen pen();
50 void setPen(QPen pen);
51 void setPen(QPen pen);
51 QBrush brush();
52 QBrush brush();
52 void setBrush(QBrush brush);
53 void setBrush(QBrush brush);
53 MarkerShape shape();
54 MarkerShape shape();
54 void setShape(MarkerShape shape);
55 void setShape(MarkerShape shape);
55 // TODO: marker size?
56 // TODO: marker size?
56
57
57 Q_SIGNALS:
58 Q_SIGNALS:
58 void clicked(QPointF coordinate);
59 void clicked(QPointF coordinate);
59 // TODO: move to PIMPL for simplicity or does the user ever need changed signals?
60 // TODO: move to PIMPL for simplicity or does the user ever need changed signals?
60 // TODO: more finegrained signaling for performance reasons
61 // TODO: more finegrained signaling for performance reasons
61 // (check QPieSeries implementation with change sets)
62 // (check QPieSeries implementation with change sets)
62 void changed();
63 void changed();
63
64
64 private:
65 private:
65 Q_DECLARE_PRIVATE(QScatterSeries)
66 Q_DECLARE_PRIVATE(QScatterSeries)
66 Q_DISABLE_COPY(QScatterSeries)
67 Q_DISABLE_COPY(QScatterSeries)
67 QScatterSeriesPrivate *const d;
68 QScatterSeriesPrivate *const d;
68 };
69 };
69
70
70 QTCOMMERCIALCHART_END_NAMESPACE
71 QTCOMMERCIALCHART_END_NAMESPACE
71
72
72 #endif // QSCATTERSERIES_H
73 #endif // QSCATTERSERIES_H
General Comments 0
You need to be logged in to leave comments. Login now