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