##// END OF EJS Templates
Fix to domain initialization
Marek Rosa -
r1740:29786dde6bc4
parent child
Show More
@@ -1,450 +1,449
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qxyseries.h"
22 22 #include "qxyseries_p.h"
23 23 #include "domain_p.h"
24 24 #include "legendmarker_p.h"
25 25 #include "qvaluesaxis.h"
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 /*!
30 30 \class QXYSeries
31 31 \brief The QXYSeries class is a base class for line, spline and scatter series.
32 32 */
33 33 /*!
34 34 \qmlclass XYSeries
35 35 \inherits AbstractSeries
36 36 The XYSeries class is a base class for line, spline and scatter series.
37 37
38 38 The class cannot be instantiated directly.
39 39 */
40 40
41 41 /*!
42 42 \property QXYSeries::pointsVisible
43 43 Controls if the data points are visible and should be drawn.
44 44 */
45 45 /*!
46 46 \qmlproperty bool XYSeries::pointsVisible
47 47 Controls if the data points are visible and should be drawn.
48 48 */
49 49
50 50 /*!
51 51 \fn QPen QXYSeries::pen() const
52 52 \brief Returns pen used to draw points for series.
53 53 \sa setPen()
54 54 */
55 55
56 56 /*!
57 57 \fn QBrush QXYSeries::brush() const
58 58 \brief Returns brush used to draw points for series.
59 59 \sa setBrush()
60 60 */
61 61
62 62 /*!
63 63 \property QXYSeries::color
64 64 The color of the series. This is line (pen) color in case of QLineSeries or QSplineSeries and
65 65 fill (brush) color in case of QScatterSeries or QAreaSeries.
66 66 \sa QXYSeries::pen(), QXYSeries::brush()
67 67 */
68 68 /*!
69 69 \qmlproperty color XYSeries::color
70 70 The color of the series. This is line (pen) color in case of LineSeries or SplineSeries and
71 71 fill (brush) color in case of ScatterSeries or AreaSeries.
72 72 */
73 73
74 74 /*!
75 75 \fn void QXYSeries::clicked(const QPointF& point)
76 76 \brief Signal is emitted when user clicks the \a point on chart.
77 77 */
78 78 /*!
79 79 \qmlsignal XYSeries::onClicked(QPointF point)
80 80 Signal is emitted when user clicks the \a point on chart. For example:
81 81 \code
82 82 LineSeries {
83 83 XYPoint { x: 0; y: 0 }
84 84 XYPoint { x: 1.1; y: 2.1 }
85 85 onClicked: console.log("onClicked: " + point.x + ", " + point.y);
86 86 }
87 87 \endcode
88 88 */
89 89
90 90 /*!
91 91 \fn void QXYSeries::pointReplaced(int index)
92 92 Signal is emitted when a point has been replaced at \a index.
93 93 \sa replace()
94 94 */
95 95 /*!
96 96 \qmlsignal XYSeries::onPointReplaced(int index)
97 97 Signal is emitted when a point has been replaced at \a index.
98 98 */
99 99
100 100 /*!
101 101 \fn void QXYSeries::pointAdded(int index)
102 102 Signal is emitted when a point has been added at \a index.
103 103 \sa append(), insert()
104 104 */
105 105 /*!
106 106 \qmlsignal XYSeries::onPointAdded(int index)
107 107 Signal is emitted when a point has been added at \a index.
108 108 */
109 109
110 110 /*!
111 111 \fn void QXYSeries::pointRemoved(int index)
112 112 Signal is emitted when a point has been removed from \a index.
113 113 \sa remove()
114 114 */
115 115 /*!
116 116 \qmlsignal XYSeries::onPointRemoved(int index)
117 117 Signal is emitted when a point has been removed from \a index.
118 118 */
119 119
120 120 /*!
121 121 \fn void QXYSeries::colorChanged(QColor color)
122 122 \brief Signal is emitted when the line (pen) color has changed to \a color.
123 123 */
124 124 /*!
125 125 \qmlsignal XYSeries::onColorChanged(color color)
126 126 Signal is emitted when the line (pen) color has changed to \a color.
127 127 */
128 128
129 129 /*!
130 130 \fn void QXYSeriesPrivate::updated()
131 131 \brief \internal
132 132 */
133 133
134 134 /*!
135 135 \qmlmethod XYSeries::append(real x, real y)
136 136 Append point (\a x, \a y) to the series
137 137 */
138 138
139 139 /*!
140 140 \qmlmethod XYSeries::replace(real oldX, real oldY, real newX, real newY)
141 141 Replaces point (\a oldX, \a oldY) with point (\a newX, \a newY). Does nothing, if point (oldX, oldY) does not
142 142 exist.
143 143 */
144 144
145 145 /*!
146 146 \qmlmethod XYSeries::remove(real x, real y)
147 147 Removes point (\a x, \a y) from the series. Does nothing, if point (x, y) does not exist.
148 148 */
149 149
150 150 /*!
151 151 \qmlmethod XYSeries::insert(int index, real x, real y)
152 152 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
153 153 points. If index is the same as or bigger than count, the point is appended to the list of points.
154 154 */
155 155
156 156 /*!
157 157 \qmlmethod QPointF XYSeries::at(int index)
158 158 Returns point at \a index. Returns (0, 0) if the index is not valid.
159 159 */
160 160
161 161 /*!
162 162 \internal
163 163
164 164 Constructs empty series object which is a child of \a parent.
165 165 When series object is added to QChartView or QChart instance ownerships is transferred.
166 166 */
167 167 QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent)
168 168 {
169 169 }
170 170
171 171 /*!
172 172 Destroys the object. Series added to QChartView or QChart instances are owned by those,
173 173 and are deleted when mentioned object are destroyed.
174 174 */
175 175 QXYSeries::~QXYSeries()
176 176 {
177 177 }
178 178
179 179 /*!
180 180 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
181 181 */
182 182 void QXYSeries::append(qreal x,qreal y)
183 183 {
184 184 append(QPointF(x,y));
185 185 }
186 186
187 187 /*!
188 188 This is an overloaded function.
189 189 Adds data \a point to the series. Points are connected with lines on the chart.
190 190 */
191 191 void QXYSeries::append(const QPointF &point)
192 192 {
193 193 Q_D(QXYSeries);
194 194 d->m_points<<point;
195 195 // emit d->pointAdded(d->m_points.count()-1);
196 196 emit pointAdded(d->m_points.count()-1);
197 197 }
198 198
199 199 /*!
200 200 This is an overloaded function.
201 201 Adds list of data \a points to the series. Points are connected with lines on the chart.
202 202 */
203 203 void QXYSeries::append(const QList<QPointF> &points)
204 204 {
205 205 foreach(const QPointF& point , points) {
206 206 append(point);
207 207 }
208 208 }
209 209
210 210 /*!
211 211 Replaces data point \a oldX \a oldY with data point \a newX \a newY.
212 212 */
213 213 void QXYSeries::replace(qreal oldX,qreal oldY,qreal newX,qreal newY)
214 214 {
215 215 replace(QPointF(oldX,oldY),QPointF(newX,newY));
216 216 }
217 217
218 218 /*!
219 219 Replaces \a oldPoint with \a newPoint.
220 220 */
221 221 void QXYSeries::replace(const QPointF &oldPoint,const QPointF &newPoint)
222 222 {
223 223 Q_D(QXYSeries);
224 224 int index = d->m_points.indexOf(oldPoint);
225 225 if(index==-1) return;
226 226 d->m_points[index] = newPoint;
227 227 // emit d->pointReplaced(index);
228 228 emit pointReplaced(index);
229 229 }
230 230
231 231 /*!
232 232 Removes current \a x and \a y value.
233 233 */
234 234 void QXYSeries::remove(qreal x,qreal y)
235 235 {
236 236 remove(QPointF(x,y));
237 237 }
238 238
239 239 /*!
240 240 Removes current \a point x value.
241 241
242 242 Note: point y value is ignored.
243 243 */
244 244 void QXYSeries::remove(const QPointF &point)
245 245 {
246 246 Q_D(QXYSeries);
247 247 int index = d->m_points.indexOf(point);
248 248 if(index==-1) return;
249 249 d->m_points.remove(index);
250 250 // emit d->pointRemoved(index);
251 251 emit pointRemoved(index);
252 252 }
253 253
254 254 /*!
255 255 Inserts a \a point in the series at \a index position.
256 256 */
257 257 void QXYSeries::insert(int index, const QPointF &point)
258 258 {
259 259 Q_D(QXYSeries);
260 260 d->m_points.insert(index, point);
261 261 // emit d->pointAdded(index);
262 262 emit pointAdded(index);
263 263 }
264 264
265 265 /*!
266 266 Removes all points from the series.
267 267 */
268 268 void QXYSeries::clear()
269 269 {
270 270 Q_D(QXYSeries);
271 271 for (int i = d->m_points.size() - 1; i >= 0; i--)
272 272 remove(d->m_points.at(i));
273 273 }
274 274
275 275 /*!
276 276 Returns list of points in the series.
277 277 */
278 278 QList<QPointF> QXYSeries::points() const
279 279 {
280 280 Q_D(const QXYSeries);
281 281 return d->m_points.toList();
282 282 }
283 283
284 284 /*!
285 285 Returns number of data points within series.
286 286 */
287 287 int QXYSeries::count() const
288 288 {
289 289 Q_D(const QXYSeries);
290 290 return d->m_points.count();
291 291 }
292 292
293 293
294 294 /*!
295 295 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
296 296 pen from chart theme is used.
297 297 \sa QChart::setTheme()
298 298 */
299 299 void QXYSeries::setPen(const QPen &pen)
300 300 {
301 301 Q_D(QXYSeries);
302 302 if (d->m_pen != pen) {
303 303 bool emitColorChanged = d->m_pen.color() != pen.color();
304 304 d->m_pen = pen;
305 305 emit d->updated();
306 306 if (emitColorChanged)
307 307 emit colorChanged(pen.color());
308 308 }
309 309 }
310 310
311 311 QPen QXYSeries::pen() const
312 312 {
313 313 Q_D(const QXYSeries);
314 314 return d->m_pen;
315 315 }
316 316
317 317 /*!
318 318 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
319 319 from chart theme setting is used.
320 320 \sa QChart::setTheme()
321 321 */
322 322 void QXYSeries::setBrush(const QBrush &brush)
323 323 {
324 324 Q_D(QXYSeries);
325 325 if (d->m_brush!=brush) {
326 326 d->m_brush = brush;
327 327 emit d->updated();
328 328 }
329 329 }
330 330
331 331 QBrush QXYSeries::brush() const
332 332 {
333 333 Q_D(const QXYSeries);
334 334 return d->m_brush;
335 335 }
336 336
337 337 void QXYSeries::setColor(const QColor &color)
338 338 {
339 339 QPen p = pen();
340 340 if (p.color() != color) {
341 341 p.setColor(color);
342 342 setPen(p);
343 343 }
344 344 }
345 345
346 346 QColor QXYSeries::color() const
347 347 {
348 348 return pen().color();
349 349 }
350 350
351 351 void QXYSeries::setPointsVisible(bool visible)
352 352 {
353 353 Q_D(QXYSeries);
354 354 if (d->m_pointsVisible != visible){
355 355 d->m_pointsVisible = visible;
356 356 emit d->updated();
357 357 }
358 358 }
359 359
360 360 bool QXYSeries::pointsVisible() const
361 361 {
362 362 Q_D(const QXYSeries);
363 363 return d->m_pointsVisible;
364 364 }
365 365
366 366
367 367 /*!
368 368 Stream operator for adding a data \a point to the series.
369 369 \sa append()
370 370 */
371 371 QXYSeries& QXYSeries::operator<< (const QPointF &point)
372 372 {
373 373 append(point);
374 374 return *this;
375 375 }
376 376
377 377
378 378 /*!
379 379 Stream operator for adding a list of \a points to the series.
380 380 \sa append()
381 381 */
382 382
383 383 QXYSeries& QXYSeries::operator<< (const QList<QPointF>& points)
384 384 {
385 385 append(points);
386 386 return *this;
387 387 }
388 388
389 389 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
390 390
391 391
392 392 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q) :
393 393 QAbstractSeriesPrivate(q),
394 394 m_pointsVisible(false)
395 395 {
396 396 }
397 397
398 398 void QXYSeriesPrivate::scaleDomain(Domain& domain)
399 399 {
400 qreal minX(domain.minX());
401 qreal minY(domain.minY());
402 qreal maxX(domain.maxX());
403 qreal maxY(domain.maxY());
400 qreal minX(0);
401 qreal minY(0);
402 qreal maxX(1);
403 qreal maxY(1);
404 404
405 405 Q_Q(QXYSeries);
406 406
407 407 const QList<QPointF>& points = q->points();
408 408
409
410 if (points.isEmpty()){
411 minX = qMin(minX, (qreal)0.0);
412 minY = qMin(minY, (qreal)0.0);
413 maxX = qMax(maxX, (qreal)1.0);
414 maxY = qMax(maxY, (qreal)1.0);
415 }
416
417 for (int i = 0; i < points.count(); i++) {
418 qreal x = points[i].x();
419 qreal y = points[i].y();
420 minX = qMin(minX, x);
421 minY = qMin(minY, y);
422 maxX = qMax(maxX, x);
423 maxY = qMax(maxY, y);
409 if (!points.isEmpty()){
410 minX = points[0].x();
411 minY = points[0].y();
412 maxX = minX;
413 maxY = minY;
414
415 for (int i = 0; i < points.count(); i++) {
416 qreal x = points[i].x();
417 qreal y = points[i].y();
418 minX = qMin(minX, x);
419 minY = qMin(minY, y);
420 maxX = qMax(maxX, x);
421 maxY = qMax(maxY, y);
422 }
424 423 }
425 424
426 425 domain.setRange(minX,maxX,minY,maxY);
427 426 }
428 427
429 428 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
430 429 {
431 430 Q_Q(QXYSeries);
432 431 QList<LegendMarker*> list;
433 432 return list << new XYLegendMarker(q,legend);
434 433 }
435 434
436 435 void QXYSeriesPrivate::initializeAxis(QAbstractAxis* axis)
437 436 {
438 437 Q_UNUSED(axis);
439 438 }
440 439
441 440 QAbstractAxis::AxisType QXYSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
442 441 {
443 442 Q_UNUSED(orientation);
444 443 return QAbstractAxis::AxisTypeValues;
445 444 }
446 445
447 446 #include "moc_qxyseries.cpp"
448 447 #include "moc_qxyseries_p.cpp"
449 448
450 449 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now