##// END OF EJS Templates
Add QXYSeries::replace(QVector<QPointF> points) overload....
Miikka Heikkinen -
r2798:412d1b6b2a80
parent child
Show More
@@ -1,89 +1,90
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include "datasource.h"
20 20 #include <QtCharts/QXYSeries>
21 21 #include <QtCharts/QAreaSeries>
22 22 #include <QtQuick/QQuickView>
23 23 #include <QtQuick/QQuickItem>
24 24 #include <QtCore/QDebug>
25 25 #include <QtCore/QtMath>
26 26
27 27 QT_CHARTS_USE_NAMESPACE
28 28
29 29 Q_DECLARE_METATYPE(QAbstractSeries *)
30 30 Q_DECLARE_METATYPE(QAbstractAxis *)
31 31
32 32 DataSource::DataSource(QQuickView *appViewer, QObject *parent) :
33 33 QObject(parent),
34 34 m_appViewer(appViewer),
35 35 m_index(-1)
36 36 {
37 37 qRegisterMetaType<QAbstractSeries*>();
38 38 qRegisterMetaType<QAbstractAxis*>();
39 39
40 40 generateData(0, 5, 1024);
41 41 }
42 42
43 43 void DataSource::update(QAbstractSeries *series)
44 44 {
45 45 if (series) {
46 46 QXYSeries *xySeries = static_cast<QXYSeries *>(series);
47 47 m_index++;
48 48 if (m_index > m_data.count() - 1)
49 49 m_index = 0;
50 50
51 QList<QPointF> points = m_data.at(m_index);
51 QVector<QPointF> points = m_data.at(m_index);
52 52 // Use replace instead of clear + append, it's optimized for performance
53 53 xySeries->replace(points);
54 54 }
55 55 }
56 56
57 57 void DataSource::generateData(int type, int rowCount, int colCount)
58 58 {
59 59 // Remove previous data
60 foreach (QList<QPointF> row, m_data)
60 foreach (QVector<QPointF> row, m_data)
61 61 row.clear();
62 62 m_data.clear();
63 63
64 64 // Append the new data depending on the type
65 65 for (int i(0); i < rowCount; i++) {
66 QList<QPointF> points;
66 QVector<QPointF> points;
67 points.reserve(colCount);
67 68 for (int j(0); j < colCount; j++) {
68 69 qreal x(0);
69 70 qreal y(0);
70 71 switch (type) {
71 72 case 0:
72 73 // data with sin + random component
73 74 y = qSin(3.14159265358979 / 50 * j) + 0.5 + (qreal) rand() / (qreal) RAND_MAX;
74 75 x = j;
75 76 break;
76 77 case 1:
77 78 // linear data
78 79 x = j;
79 80 y = (qreal) i / 10;
80 81 break;
81 82 default:
82 83 // unknown, do nothing
83 84 break;
84 85 }
85 86 points.append(QPointF(x, y));
86 87 }
87 88 m_data.append(points);
88 89 }
89 90 }
@@ -1,49 +1,49
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #ifndef DATASOURCE_H
20 20 #define DATASOURCE_H
21 21
22 22 #include <QtCore/QObject>
23 23 #include <QtCharts/QAbstractSeries>
24 24
25 25 QT_BEGIN_NAMESPACE
26 26 class QQuickView;
27 27 QT_END_NAMESPACE
28 28
29 29 QT_CHARTS_USE_NAMESPACE
30 30
31 31 class DataSource : public QObject
32 32 {
33 33 Q_OBJECT
34 34 public:
35 35 explicit DataSource(QQuickView *appViewer, QObject *parent = 0);
36 36
37 37 Q_SIGNALS:
38 38
39 39 public slots:
40 40 void generateData(int type, int rowCount, int colCount);
41 41 void update(QAbstractSeries *series);
42 42
43 43 private:
44 44 QQuickView *m_appViewer;
45 QList<QList<QPointF> > m_data;
45 QList<QVector<QPointF> > m_data;
46 46 int m_index;
47 47 };
48 48
49 49 #endif // DATASOURCE_H
@@ -1,870 +1,883
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <QtCharts/QXYSeries>
20 20 #include <private/qxyseries_p.h>
21 21 #include <private/abstractdomain_p.h>
22 22 #include <QtCharts/QValueAxis>
23 23 #include <private/xychart_p.h>
24 24 #include <QtCharts/QXYLegendMarker>
25 25 #include <private/charthelpers_p.h>
26 26 #include <private/qchart_p.h>
27 27 #include <QtGui/QPainter>
28 28
29 29 QT_CHARTS_BEGIN_NAMESPACE
30 30
31 31 /*!
32 32 \class QXYSeries
33 33 \inmodule Qt Charts
34 34 \brief The QXYSeries class is a base class for line, spline and scatter series.
35 35 */
36 36 /*!
37 37 \qmltype XYSeries
38 38 \instantiates QXYSeries
39 39 \inqmlmodule QtCharts
40 40
41 41 \inherits AbstractSeries
42 42
43 43 \brief The XYSeries type is a base type for line, spline and scatter series.
44 44
45 45 The XYSeries class is a base class for line, spline and scatter series.
46 46 The class cannot be instantiated directly.
47 47 */
48 48
49 49 /*!
50 50 \qmlproperty AbstractAxis XYSeries::axisX
51 51 The x axis used for the series. If you leave both axisX and axisXTop undefined, a ValueAxis is created for
52 52 the series.
53 53 \sa axisXTop
54 54 */
55 55
56 56 /*!
57 57 \qmlproperty AbstractAxis XYSeries::axisY
58 58 The y axis used for the series. If you leave both axisY and axisYRight undefined, a ValueAxis is created for
59 59 the series.
60 60 \sa axisYRight
61 61 */
62 62
63 63 /*!
64 64 \qmlproperty AbstractAxis XYSeries::axisXTop
65 65 The x axis used for the series, drawn on top of the chart view. Note that you can only provide either axisX or
66 66 axisXTop, but not both.
67 67 \sa axisX
68 68 */
69 69
70 70 /*!
71 71 \qmlproperty AbstractAxis XYSeries::axisYRight
72 72 The y axis used for the series, drawn to the right on the chart view. Note that you can only provide either axisY
73 73 or axisYRight, but not both.
74 74 \sa axisY
75 75 */
76 76
77 77 /*!
78 78 \qmlproperty AbstractAxis XYSeries::axisAngular
79 79 The angular axis used for the series, drawn around the polar chart view.
80 80 \sa axisX
81 81 */
82 82
83 83 /*!
84 84 \qmlproperty AbstractAxis XYSeries::axisRadial
85 85 The radial axis used for the series, drawn inside the polar chart view.
86 86 \sa axisY
87 87 */
88 88
89 89 /*!
90 90 \property QXYSeries::pointsVisible
91 91 Controls if the data points are visible and should be drawn.
92 92 */
93 93 /*!
94 94 \qmlproperty bool XYSeries::pointsVisible
95 95 Controls if the data points are visible and should be drawn.
96 96 */
97 97
98 98 /*!
99 99 \fn QPen QXYSeries::pen() const
100 100 \brief Returns pen used to draw points for series.
101 101 \sa setPen()
102 102 */
103 103
104 104 /*!
105 105 \fn QBrush QXYSeries::brush() const
106 106 \brief Returns brush used to draw points for series.
107 107 \sa setBrush()
108 108 */
109 109
110 110 /*!
111 111 \property QXYSeries::color
112 112 The color of the series. This is line (pen) color in case of QLineSeries or QSplineSeries and
113 113 fill (brush) color in case of QScatterSeries or QAreaSeries.
114 114 \sa QXYSeries::pen(), QXYSeries::brush()
115 115 */
116 116 /*!
117 117 \qmlproperty color XYSeries::color
118 118 The color of the series. This is line (pen) color in case of LineSeries or SplineSeries and
119 119 fill (brush) color in case of ScatterSeries or AreaSeries.
120 120 */
121 121
122 122 /*!
123 123 \property QXYSeries::pointLabelsFormat
124 124 The \a format used for showing labels with series points.
125 125
126 126 QXYSeries supports the following format tags:
127 127 \table
128 128 \row
129 129 \li @xPoint \li The x value of the data point
130 130 \row
131 131 \li @yPoint \li The y value of the data point
132 132 \endtable
133 133
134 134 For example, the following usage of the format tags would produce labels that have the data
135 135 point (x, y) shown inside brackets separated by a comma:
136 136 \code
137 137 series->setPointLabelsFormat("(@xPoint, @yPoint)");
138 138 \endcode
139 139
140 140 By default, the labels format is set to '@xPoint, @yPoint'. The labels are shown on the plot
141 141 area, labels on the edge of the plot area are cut. If the points are close to each other the
142 142 labels may overlap.
143 143
144 144 \sa QXYSeries::pointLabelsVisible, QXYSeries::pointLabelsFont, QXYSeries::pointLabelsColor
145 145 */
146 146 /*!
147 147 \qmlproperty string XYSeries::pointLabelsFormat
148 148 The \a format used for showing labels with series points.
149 149
150 150 \sa QXYSeries::pointLabelsFormat, pointLabelsVisible, pointLabelsFont, pointLabelsColor
151 151 */
152 152 /*!
153 153 \fn void QXYSeries::pointLabelsFormatChanged(const QString &format)
154 154 Signal is emitted when the \a format of data point labels is changed.
155 155 */
156 156 /*!
157 157 \qmlsignal XYSeries::onPointLabelsFormatChanged(string format)
158 158 Signal is emitted when the \a format of data point labels is changed.
159 159 */
160 160
161 161 /*!
162 162 \property QXYSeries::pointLabelsVisible
163 163 Defines the visibility for data point labels. False by default.
164 164
165 165 \sa QXYSeries::pointLabelsFormat
166 166 */
167 167 /*!
168 168 \qmlproperty bool XYSeries::pointLabelsVisible
169 169 Defines the visibility for data point labels.
170 170
171 171 \sa pointLabelsFormat
172 172 */
173 173 /*!
174 174 \fn void QXYSeries::pointLabelsVisibilityChanged(bool visible)
175 175 The visibility of the data point labels is changed to \a visible.
176 176 */
177 177 /*!
178 178 \qmlsignal XYSeries::onPointLabelsVisibilityChanged(bool visible)
179 179 The visibility of the data point labels is changed to \a visible.
180 180 */
181 181
182 182 /*!
183 183 \property QXYSeries::pointLabelsFont
184 184 Defines the font used for data point labels.
185 185
186 186 \sa QXYSeries::pointLabelsFormat
187 187 */
188 188 /*!
189 189 \qmlproperty font XYSeries::pointLabelsFont
190 190 Defines the font used for data point labels.
191 191
192 192 \sa pointLabelsFormat
193 193 */
194 194 /*!
195 195 \fn void QXYSeries::pointLabelsFontChanged(const QFont &font);
196 196 The font used for data point labels is changed to \a font.
197 197 */
198 198 /*!
199 199 \qmlsignal XYSeries::onPointLabelsFontChanged(Font font)
200 200 The font used for data point labels is changed to \a font.
201 201 */
202 202
203 203 /*!
204 204 \property QXYSeries::pointLabelsColor
205 205 Defines the color used for data point labels. By default, the color is the color of the brush
206 206 defined in theme for labels.
207 207
208 208 \sa QXYSeries::pointLabelsFormat
209 209 */
210 210 /*!
211 211 \qmlproperty font XYSeries::pointLabelsColor
212 212 Defines the color used for data point labels. By default, the color is the color of the brush
213 213 defined in theme for labels.
214 214
215 215 \sa pointLabelsFormat
216 216 */
217 217 /*!
218 218 \fn void QXYSeries::pointLabelsColorChanged(const QColor &color);
219 219 The color used for data point labels is changed to \a color.
220 220 */
221 221 /*!
222 222 \qmlsignal XYSeries::onPointLabelsColorChanged(Color color)
223 223 The color used for data point labels is changed to \a color.
224 224 */
225 225
226 226 /*!
227 227 \fn void QXYSeries::clicked(const QPointF& point)
228 228 \brief Signal is emitted when user clicks the \a point on chart. The \a point is the point
229 229 where the press was triggered.
230 230 \sa pressed, released, doubleClicked
231 231 */
232 232 /*!
233 233 \qmlsignal XYSeries::onClicked(QPointF point)
234 234 Signal is emitted when user clicks the \a point on chart. The \a point is the point where the
235 235 press was triggered. For example:
236 236 \code
237 237 LineSeries {
238 238 XYPoint { x: 0; y: 0 }
239 239 XYPoint { x: 1.1; y: 2.1 }
240 240 onClicked: console.log("onClicked: " + point.x + ", " + point.y);
241 241 }
242 242 \endcode
243 243 \sa onPressed, onReleased, onDoubleClicked
244 244 */
245 245
246 246 /*!
247 247 \fn void QXYSeries::hovered(const QPointF &point, bool state)
248 248 This signal is emitted when user has hovered over or away from the series. \a point shows the origin (coordinate)
249 249 of the hover event. \a state is true when user has hovered over the series and false when hover has moved away from
250 250 the series.
251 251 */
252 252 /*!
253 253 \qmlsignal XYSeries::onHovered(point point, bool state)
254 254 This signal is emitted when user has hovered over or away from the series. \a point shows the origin (coordinate)
255 255 of the hover event. \a state is true when user has hovered over the series and false when hover has moved away from
256 256 the series.
257 257 */
258 258
259 259 /*!
260 260 \fn void QXYSeries::pressed(const QPointF& point)
261 261 \brief Signal is emitted when user presses the \a point on chart.
262 262 \sa clicked, released, doubleClicked
263 263 */
264 264 /*!
265 265 \qmlsignal XYSeries::onPressed(QPointF point)
266 266 Signal is emitted when user presses the \a point on chart. For example:
267 267 \code
268 268 LineSeries {
269 269 XYPoint { x: 0; y: 0 }
270 270 XYPoint { x: 1.1; y: 2.1 }
271 271 onPressed: console.log("onPressed: " + point.x + ", " + point.y);
272 272 }
273 273 \endcode
274 274 \sa onClicked, onReleased, onDoubleClicked
275 275 */
276 276
277 277 /*!
278 278 \fn void QXYSeries::released(const QPointF& point)
279 279 \brief Signal is emitted when user releases a press that was triggered on a \a point on chart.
280 280 \sa pressed, clicked, doubleClicked
281 281 */
282 282 /*!
283 283 \qmlsignal XYSeries::onReleased(QPointF point)
284 284 Signal is emitted when user releases a press that was triggered on a \a point on chart.
285 285 For example:
286 286 \code
287 287 LineSeries {
288 288 XYPoint { x: 0; y: 0 }
289 289 XYPoint { x: 1.1; y: 2.1 }
290 290 onReleased: console.log("onReleased: " + point.x + ", " + point.y);
291 291 }
292 292 \endcode
293 293 \sa onPressed, onClicked, onDoubleClicked
294 294 */
295 295
296 296 /*!
297 297 \fn void QXYSeries::doubleClicked(const QPointF& point)
298 298 \brief Signal is emitted when user doubleclicks the \a point on chart. The \a point is the
299 299 point where the first press was triggered.
300 300 \sa pressed, released, clicked
301 301 */
302 302 /*!
303 303 \qmlsignal XYSeries::onDoubleClicked(QPointF point)
304 304 Signal is emitted when user doubleclicks the \a point on chart. The \a point is the point where
305 305 the first press was triggered. For example:
306 306 \code
307 307 LineSeries {
308 308 XYPoint { x: 0; y: 0 }
309 309 XYPoint { x: 1.1; y: 2.1 }
310 310 onDoubleClicked: console.log("onDoubleClicked: " + point.x + ", " + point.y);
311 311 }
312 312 \endcode
313 313 \sa onPressed, onReleased, onClicked
314 314 */
315 315
316 316 /*!
317 317 \fn void QXYSeries::pointReplaced(int index)
318 318 Signal is emitted when a point has been replaced at \a index.
319 319 \sa replace()
320 320 */
321 321 /*!
322 322 \qmlsignal XYSeries::onPointReplaced(int index)
323 323 Signal is emitted when a point has been replaced at \a index.
324 324 */
325 325
326 326 /*!
327 327 \fn void QXYSeries::pointsReplaced()
328 328 Signal is emitted when all points have been replaced with other points.
329 329 \sa replace()
330 330 */
331 331 /*!
332 332 \qmlsignal XYSeries::onPointsReplaced()
333 333 Signal is emitted when all points have been replaced with other points.
334 334 */
335 335
336 336 /*!
337 337 \fn void QXYSeries::pointAdded(int index)
338 338 Signal is emitted when a point has been added at \a index.
339 339 \sa append(), insert()
340 340 */
341 341 /*!
342 342 \qmlsignal XYSeries::onPointAdded(int index)
343 343 Signal is emitted when a point has been added at \a index.
344 344 */
345 345
346 346 /*!
347 347 \fn void QXYSeries::pointRemoved(int index)
348 348 Signal is emitted when a point has been removed from \a index.
349 349 \sa remove()
350 350 */
351 351
352 352 /*!
353 353 \qmlsignal XYSeries::onPointRemoved(int index)
354 354 Signal is emitted when a point has been removed from \a index.
355 355 */
356 356
357 357 /*!
358 358 \fn void QXYSeries::colorChanged(QColor color)
359 359 \brief Signal is emitted when the line (pen) color has changed to \a color.
360 360 */
361 361 /*!
362 362 \qmlsignal XYSeries::onColorChanged(color color)
363 363 Signal is emitted when the line (pen) color has changed to \a color.
364 364 */
365 365
366 366 /*!
367 367 \fn void QXYSeriesPrivate::updated()
368 368 \brief \internal
369 369 */
370 370
371 371 /*!
372 372 \qmlmethod XYSeries::append(real x, real y)
373 373 Append point (\a x, \a y) to the series
374 374 */
375 375
376 376 /*!
377 377 \qmlmethod XYSeries::replace(real oldX, real oldY, real newX, real newY)
378 378 Replaces point (\a oldX, \a oldY) with point (\a newX, \a newY). Does nothing, if point (oldX, oldY) does not
379 379 exist.
380 380 */
381 381
382 382 /*!
383 383 \qmlmethod XYSeries::remove(real x, real y)
384 384 Removes point (\a x, \a y) from the series. Does nothing, if point (x, y) does not exist.
385 385 */
386 386
387 387 /*!
388 388 \qmlmethod XYSeries::insert(int index, real x, real y)
389 389 Inserts point (\a x, \a y) to the \a index. If index is 0 or smaller than 0 the point is prepended to the list of
390 390 points. If index is the same as or bigger than count, the point is appended to the list of points.
391 391 */
392 392
393 393 /*!
394 394 \qmlmethod QPointF XYSeries::at(int index)
395 395 Returns point at \a index. Returns (0, 0) if the index is not valid.
396 396 */
397 397
398 398 /*!
399 399 \internal
400 400
401 401 Constructs empty series object which is a child of \a parent.
402 402 When series object is added to QChart instance ownerships is transferred.
403 403 */
404 404 QXYSeries::QXYSeries(QXYSeriesPrivate &d, QObject *parent)
405 405 : QAbstractSeries(d, parent)
406 406 {
407 407 }
408 408
409 409 /*!
410 410 Destroys the object. Series added to QChart instances are owned by those,
411 411 and are destroyed when QChart instances are destroyed.
412 412 */
413 413 QXYSeries::~QXYSeries()
414 414 {
415 415 }
416 416
417 417 /*!
418 418 Adds data point (\a x, \a y) to the series.
419 419 */
420 420 void QXYSeries::append(qreal x, qreal y)
421 421 {
422 422 append(QPointF(x, y));
423 423 }
424 424
425 425 /*!
426 426 This is an overloaded function.
427 427 Adds data \a point to the series.
428 428 */
429 429 void QXYSeries::append(const QPointF &point)
430 430 {
431 431 Q_D(QXYSeries);
432 432
433 433 if (isValidValue(point)) {
434 434 d->m_points << point;
435 435 emit pointAdded(d->m_points.count() - 1);
436 436 }
437 437 }
438 438
439 439 /*!
440 440 This is an overloaded function.
441 441 Adds list of data \a points to the series.
442 442 */
443 443 void QXYSeries::append(const QList<QPointF> &points)
444 444 {
445 445 foreach (const QPointF &point , points)
446 446 append(point);
447 447 }
448 448
449 449 /*!
450 450 Replaces data point (\a oldX, \a oldY) with data point (\a newX, \a newY).
451 451 \sa QXYSeries::pointReplaced()
452 452 */
453 453 void QXYSeries::replace(qreal oldX, qreal oldY, qreal newX, qreal newY)
454 454 {
455 455 replace(QPointF(oldX, oldY), QPointF(newX, newY));
456 456 }
457 457
458 458 /*!
459 459 Replaces \a oldPoint with \a newPoint.
460 460 \sa QXYSeries::pointReplaced()
461 461 */
462 462 void QXYSeries::replace(const QPointF &oldPoint, const QPointF &newPoint)
463 463 {
464 464 Q_D(QXYSeries);
465 465 int index = d->m_points.indexOf(oldPoint);
466 466 if (index == -1)
467 467 return;
468 468 replace(index, newPoint);
469 469 }
470 470
471 471 /*!
472 472 Replaces the point at \a index with data point (\a newX, \a newY).
473 473 \sa QXYSeries::pointReplaced()
474 474 */
475 475 void QXYSeries::replace(int index, qreal newX, qreal newY)
476 476 {
477 477 replace(index, QPointF(newX, newY));
478 478 }
479 479
480 480 /*!
481 481 Replaces the point at \a index with \a newPoint.
482 482 \sa QXYSeries::pointReplaced()
483 483 */
484 484 void QXYSeries::replace(int index, const QPointF &newPoint)
485 485 {
486 486 Q_D(QXYSeries);
487 487 if (isValidValue(newPoint)) {
488 488 d->m_points[index] = newPoint;
489 489 emit pointReplaced(index);
490 490 }
491 491 }
492 492
493 493 /*!
494 494 Replaces the current points with \a points.
495 495 \note This is much faster than replacing data points one by one,
496 496 or first clearing all data, and then appending the new data. Emits QXYSeries::pointsReplaced()
497 when the points have been replaced.
497 when the points have been replaced. However, note that using the overload that takes
498 \c{QVector<QPointF>} as parameter is slightly faster than using this overload.
498 499 \sa QXYSeries::pointsReplaced()
499 500 */
500 501 void QXYSeries::replace(QList<QPointF> points)
501 502 {
503 replace(points.toVector());
504 }
505
506 /*!
507 Replaces the current points with \a points.
508 \note This is much faster than replacing data points one by one,
509 or first clearing all data, and then appending the new data. Emits QXYSeries::pointsReplaced()
510 when the points have been replaced.
511 \sa QXYSeries::pointsReplaced()
512 */
513 void QXYSeries::replace(QVector<QPointF> points)
514 {
502 515 Q_D(QXYSeries);
503 d->m_points = points.toVector();
516 d->m_points = points;
504 517 emit pointsReplaced();
505 518 }
506 519
507 520 /*!
508 521 Removes the point (\a x, \a y) from the series.
509 522 */
510 523 void QXYSeries::remove(qreal x, qreal y)
511 524 {
512 525 remove(QPointF(x, y));
513 526 }
514 527
515 528 /*!
516 529 Removes the \a point from the series.
517 530 */
518 531 void QXYSeries::remove(const QPointF &point)
519 532 {
520 533 Q_D(QXYSeries);
521 534 int index = d->m_points.indexOf(point);
522 535 if (index == -1)
523 536 return;
524 537 remove(index);
525 538 }
526 539
527 540 /*!
528 541 Removes the point at \a index from the series.
529 542 */
530 543 void QXYSeries::remove(int index)
531 544 {
532 545 Q_D(QXYSeries);
533 546 d->m_points.remove(index);
534 547 emit pointRemoved(index);
535 548 }
536 549
537 550 /*!
538 551 Inserts a \a point in the series at \a index position.
539 552 */
540 553 void QXYSeries::insert(int index, const QPointF &point)
541 554 {
542 555 Q_D(QXYSeries);
543 556 if (isValidValue(point)) {
544 557 index = qMax(0, qMin(index, d->m_points.size()));
545 558 d->m_points.insert(index, point);
546 559 emit pointAdded(index);
547 560 }
548 561 }
549 562
550 563 /*!
551 564 Removes all points from the series.
552 565 */
553 566 void QXYSeries::clear()
554 567 {
555 568 Q_D(QXYSeries);
556 569 for (int i = d->m_points.size() - 1; i >= 0; i--)
557 570 remove(d->m_points.at(i));
558 571 }
559 572
560 573 /*!
561 574 Returns list of points in the series.
562 575 */
563 576 QList<QPointF> QXYSeries::points() const
564 577 {
565 578 Q_D(const QXYSeries);
566 579 return d->m_points.toList();
567 580 }
568 581
569 582 /*!
570 583 Returns point at \a index in internal points vector.
571 584 */
572 585 const QPointF &QXYSeries::at(int index) const
573 586 {
574 587 Q_D(const QXYSeries);
575 588 return d->m_points.at(index);
576 589 }
577 590
578 591 /*!
579 592 Returns number of data points within series.
580 593 */
581 594 int QXYSeries::count() const
582 595 {
583 596 Q_D(const QXYSeries);
584 597 return d->m_points.count();
585 598 }
586 599
587 600
588 601 /*!
589 602 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
590 603 pen from chart theme is used.
591 604 \sa QChart::setTheme()
592 605 */
593 606 void QXYSeries::setPen(const QPen &pen)
594 607 {
595 608 Q_D(QXYSeries);
596 609 if (d->m_pen != pen) {
597 610 bool emitColorChanged = d->m_pen.color() != pen.color();
598 611 d->m_pen = pen;
599 612 emit d->updated();
600 613 if (emitColorChanged)
601 614 emit colorChanged(pen.color());
602 615 }
603 616 }
604 617
605 618 QPen QXYSeries::pen() const
606 619 {
607 620 Q_D(const QXYSeries);
608 621 if (d->m_pen == QChartPrivate::defaultPen())
609 622 return QPen();
610 623 else
611 624 return d->m_pen;
612 625 }
613 626
614 627 /*!
615 628 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
616 629 from chart theme setting is used.
617 630 \sa QChart::setTheme()
618 631 */
619 632 void QXYSeries::setBrush(const QBrush &brush)
620 633 {
621 634 Q_D(QXYSeries);
622 635 if (d->m_brush != brush) {
623 636 d->m_brush = brush;
624 637 emit d->updated();
625 638 }
626 639 }
627 640
628 641 QBrush QXYSeries::brush() const
629 642 {
630 643 Q_D(const QXYSeries);
631 644 if (d->m_brush == QChartPrivate::defaultBrush())
632 645 return QBrush();
633 646 else
634 647 return d->m_brush;
635 648 }
636 649
637 650 void QXYSeries::setColor(const QColor &color)
638 651 {
639 652 QPen p = pen();
640 653 if (p.color() != color) {
641 654 p.setColor(color);
642 655 setPen(p);
643 656 }
644 657 }
645 658
646 659 QColor QXYSeries::color() const
647 660 {
648 661 return pen().color();
649 662 }
650 663
651 664 void QXYSeries::setPointsVisible(bool visible)
652 665 {
653 666 Q_D(QXYSeries);
654 667 if (d->m_pointsVisible != visible) {
655 668 d->m_pointsVisible = visible;
656 669 emit d->updated();
657 670 }
658 671 }
659 672
660 673 bool QXYSeries::pointsVisible() const
661 674 {
662 675 Q_D(const QXYSeries);
663 676 return d->m_pointsVisible;
664 677 }
665 678
666 679 void QXYSeries::setPointLabelsFormat(const QString &format)
667 680 {
668 681 Q_D(QXYSeries);
669 682 if (d->m_pointLabelsFormat != format) {
670 683 d->m_pointLabelsFormat = format;
671 684 emit pointLabelsFormatChanged(format);
672 685 }
673 686 }
674 687
675 688 QString QXYSeries::pointLabelsFormat() const
676 689 {
677 690 Q_D(const QXYSeries);
678 691 return d->m_pointLabelsFormat;
679 692 }
680 693
681 694 void QXYSeries::setPointLabelsVisible(bool visible)
682 695 {
683 696 Q_D(QXYSeries);
684 697 if (d->m_pointLabelsVisible != visible) {
685 698 d->m_pointLabelsVisible = visible;
686 699 emit pointLabelsVisibilityChanged(visible);
687 700 }
688 701 }
689 702
690 703 bool QXYSeries::pointLabelsVisible() const
691 704 {
692 705 Q_D(const QXYSeries);
693 706 return d->m_pointLabelsVisible;
694 707 }
695 708
696 709 void QXYSeries::setPointLabelsFont(const QFont &font)
697 710 {
698 711 Q_D(QXYSeries);
699 712 if (d->m_pointLabelsFont != font) {
700 713 d->m_pointLabelsFont = font;
701 714 emit pointLabelsFontChanged(font);
702 715 }
703 716 }
704 717
705 718 QFont QXYSeries::pointLabelsFont() const
706 719 {
707 720 Q_D(const QXYSeries);
708 721 return d->m_pointLabelsFont;
709 722 }
710 723
711 724 void QXYSeries::setPointLabelsColor(const QColor &color)
712 725 {
713 726 Q_D(QXYSeries);
714 727 if (d->m_pointLabelsColor != color) {
715 728 d->m_pointLabelsColor = color;
716 729 emit pointLabelsColorChanged(color);
717 730 }
718 731 }
719 732
720 733 QColor QXYSeries::pointLabelsColor() const
721 734 {
722 735 Q_D(const QXYSeries);
723 736 if (d->m_pointLabelsColor == QChartPrivate::defaultPen().color())
724 737 return QPen().color();
725 738 else
726 739 return d->m_pointLabelsColor;
727 740 }
728 741
729 742 /*!
730 743 Stream operator for adding a data \a point to the series.
731 744 \sa append()
732 745 */
733 746 QXYSeries &QXYSeries::operator<< (const QPointF &point)
734 747 {
735 748 append(point);
736 749 return *this;
737 750 }
738 751
739 752
740 753 /*!
741 754 Stream operator for adding a list of \a points to the series.
742 755 \sa append()
743 756 */
744 757
745 758 QXYSeries &QXYSeries::operator<< (const QList<QPointF>& points)
746 759 {
747 760 append(points);
748 761 return *this;
749 762 }
750 763
751 764 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
752 765
753 766
754 767 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q)
755 768 : QAbstractSeriesPrivate(q),
756 769 m_pen(QChartPrivate::defaultPen()),
757 770 m_brush(QChartPrivate::defaultBrush()),
758 771 m_pointsVisible(false),
759 772 m_pointLabelsFormat(QLatin1String("@xPoint, @yPoint")),
760 773 m_pointLabelsVisible(false),
761 774 m_pointLabelsFont(QChartPrivate::defaultFont()),
762 775 m_pointLabelsColor(QChartPrivate::defaultPen().color())
763 776 {
764 777 }
765 778
766 779 void QXYSeriesPrivate::initializeDomain()
767 780 {
768 781 qreal minX(0);
769 782 qreal minY(0);
770 783 qreal maxX(1);
771 784 qreal maxY(1);
772 785
773 786 Q_Q(QXYSeries);
774 787
775 788 const QList<QPointF>& points = q->points();
776 789
777 790 if (!points.isEmpty()) {
778 791 minX = points[0].x();
779 792 minY = points[0].y();
780 793 maxX = minX;
781 794 maxY = minY;
782 795
783 796 for (int i = 0; i < points.count(); i++) {
784 797 qreal x = points[i].x();
785 798 qreal y = points[i].y();
786 799 minX = qMin(minX, x);
787 800 minY = qMin(minY, y);
788 801 maxX = qMax(maxX, x);
789 802 maxY = qMax(maxY, y);
790 803 }
791 804 }
792 805
793 806 domain()->setRange(minX, maxX, minY, maxY);
794 807 }
795 808
796 809 QList<QLegendMarker*> QXYSeriesPrivate::createLegendMarkers(QLegend* legend)
797 810 {
798 811 Q_Q(QXYSeries);
799 812 QList<QLegendMarker*> list;
800 813 return list << new QXYLegendMarker(q,legend);
801 814 }
802 815
803 816 void QXYSeriesPrivate::initializeAxes()
804 817 {
805 818
806 819 }
807 820
808 821 QAbstractAxis::AxisType QXYSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
809 822 {
810 823 Q_UNUSED(orientation);
811 824 return QAbstractAxis::AxisTypeValue;
812 825 }
813 826
814 827 QAbstractAxis* QXYSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
815 828 {
816 829 Q_UNUSED(orientation);
817 830 return new QValueAxis;
818 831 }
819 832
820 833 void QXYSeriesPrivate::initializeAnimations(QtCharts::QChart::AnimationOptions options)
821 834 {
822 835 XYChart *item = static_cast<XYChart *>(m_item.data());
823 836 Q_ASSERT(item);
824 837 if (item->animation())
825 838 item->animation()->stopAndDestroyLater();
826 839
827 840 if (options.testFlag(QChart::SeriesAnimations))
828 841 item->setAnimation(new XYAnimation(item));
829 842 else
830 843 item->setAnimation(0);
831 844 QAbstractSeriesPrivate::initializeAnimations(options);
832 845 }
833 846
834 847 void QXYSeriesPrivate::drawSeriesPointLabels(QPainter *painter, const QVector<QPointF> &points,
835 848 const int offset)
836 849 {
837 850 static const QString xPointTag(QLatin1String("@xPoint"));
838 851 static const QString yPointTag(QLatin1String("@yPoint"));
839 852 const int labelOffset = offset + 2;
840 853
841 854 painter->setFont(m_pointLabelsFont);
842 855 painter->setPen(QPen(m_pointLabelsColor));
843 856 QFontMetrics fm(painter->font());
844 857 // m_points is used for the label here as it has the series point information
845 858 // points variable passed is used for positioning because it has the coordinates
846 859 for (int i(0); i < m_points.size(); i++) {
847 860 QString pointLabel = m_pointLabelsFormat;
848 861 pointLabel.replace(xPointTag, presenter()->numberToString(m_points.at(i).x()));
849 862 pointLabel.replace(yPointTag, presenter()->numberToString(m_points.at(i).y()));
850 863
851 864 // Position text in relation to the point
852 865 int pointLabelWidth = fm.width(pointLabel);
853 866 QPointF position(points.at(i));
854 867 if (!reverseXAxis())
855 868 position.setX(position.x() - pointLabelWidth / 2);
856 869 else
857 870 position.setX(domain()->size().width() - position.x() - pointLabelWidth / 2);
858 871 if (!reverseYAxis())
859 872 position.setY(position.y() - labelOffset);
860 873 else
861 874 position.setY(domain()->size().height() - position.y() - labelOffset);
862 875
863 876 painter->drawText(position, pointLabel);
864 877 }
865 878 }
866 879
867 880 #include "moc_qxyseries.cpp"
868 881 #include "moc_qxyseries_p.cpp"
869 882
870 883 QT_CHARTS_END_NAMESPACE
@@ -1,123 +1,124
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #ifndef QXYSERIES_H
20 20 #define QXYSERIES_H
21 21
22 22 #include <QtCharts/QChartGlobal>
23 23 #include <QtCharts/QAbstractSeries>
24 24 #include <QtGui/QPen>
25 25 #include <QtGui/QBrush>
26 26
27 27 QT_BEGIN_NAMESPACE
28 28 class QModelIndex;
29 29 QT_END_NAMESPACE
30 30
31 31 QT_CHARTS_BEGIN_NAMESPACE
32 32
33 33 class QXYSeriesPrivate;
34 34 class QXYModelMapper;
35 35
36 36 class QT_CHARTS_EXPORT QXYSeries : public QAbstractSeries
37 37 {
38 38 Q_OBJECT
39 39 Q_PROPERTY(bool pointsVisible READ pointsVisible WRITE setPointsVisible)
40 40 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
41 41 Q_PROPERTY(QString pointLabelsFormat READ pointLabelsFormat WRITE setPointLabelsFormat NOTIFY pointLabelsFormatChanged)
42 42 Q_PROPERTY(bool pointLabelsVisible READ pointLabelsVisible WRITE setPointLabelsVisible NOTIFY pointLabelsVisibilityChanged)
43 43 Q_PROPERTY(QFont pointLabelsFont READ pointLabelsFont WRITE setPointLabelsFont NOTIFY pointLabelsFontChanged)
44 44 Q_PROPERTY(QColor pointLabelsColor READ pointLabelsColor WRITE setPointLabelsColor NOTIFY pointLabelsColorChanged)
45 45
46 46 protected:
47 47 explicit QXYSeries(QXYSeriesPrivate &d, QObject *parent = 0);
48 48
49 49 public:
50 50 ~QXYSeries();
51 51 void append(qreal x, qreal y);
52 52 void append(const QPointF &point);
53 53 void append(const QList<QPointF> &points);
54 54 void replace(qreal oldX, qreal oldY, qreal newX, qreal newY);
55 55 void replace(const QPointF &oldPoint, const QPointF &newPoint);
56 56 void replace(int index, qreal newX, qreal newY);
57 57 void replace(int index, const QPointF &newPoint);
58 58 void remove(qreal x, qreal y);
59 59 void remove(const QPointF &point);
60 60 void remove(int index);
61 61 void insert(int index, const QPointF &point);
62 62 void clear();
63 63
64 64 int count() const;
65 65 QList<QPointF> points() const;
66 66 const QPointF &at(int index) const;
67 67
68 68 QXYSeries &operator << (const QPointF &point);
69 69 QXYSeries &operator << (const QList<QPointF> &points);
70 70
71 71 virtual void setPen(const QPen &pen);
72 72 QPen pen() const;
73 73
74 74 virtual void setBrush(const QBrush &brush);
75 75 QBrush brush() const;
76 76
77 77 virtual void setColor(const QColor &color);
78 78 virtual QColor color() const;
79 79
80 80 void setPointsVisible(bool visible = true);
81 81 bool pointsVisible() const;
82 82
83 83 void setPointLabelsFormat(const QString &format);
84 84 QString pointLabelsFormat() const;
85 85
86 86 void setPointLabelsVisible(bool visible = true);
87 87 bool pointLabelsVisible() const;
88 88
89 89 void setPointLabelsFont(const QFont &font);
90 90 QFont pointLabelsFont() const;
91 91
92 92 void setPointLabelsColor(const QColor &color);
93 93 QColor pointLabelsColor() const;
94 94
95 95 void replace(QList<QPointF> points);
96 void replace(QVector<QPointF> points);
96 97
97 98 Q_SIGNALS:
98 99 void clicked(const QPointF &point);
99 100 void hovered(const QPointF &point, bool state);
100 101 void pressed(const QPointF &point);
101 102 void released(const QPointF &point);
102 103 void doubleClicked(const QPointF &point);
103 104 void pointReplaced(int index);
104 105 void pointRemoved(int index);
105 106 void pointAdded(int index);
106 107 void colorChanged(QColor color);
107 108 void pointsReplaced();
108 109 void pointLabelsFormatChanged(const QString &format);
109 110 void pointLabelsVisibilityChanged(bool visible);
110 111 void pointLabelsFontChanged(const QFont &font);
111 112 void pointLabelsColorChanged(const QColor &color);
112 113
113 114 private:
114 115 Q_DECLARE_PRIVATE(QXYSeries)
115 116 Q_DISABLE_COPY(QXYSeries)
116 117 friend class QXYLegendMarkerPrivate;
117 118 friend class XYLegendMarker;
118 119 friend class XYChart;
119 120 };
120 121
121 122 QT_CHARTS_END_NAMESPACE
122 123
123 124 #endif // QXYSERIES_H
General Comments 0
You need to be logged in to leave comments. Login now