##// END OF EJS Templates
Return empty brush/pen/font via API rather than the weird default one...
Miikka Heikkinen -
r2519:9ea7d7fe3500
parent child
Show More
@@ -1,453 +1,459
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 "qareaseries.h"
22 22 #include "qareaseries_p.h"
23 23 #include "qlineseries.h"
24 24 #include "areachartitem_p.h"
25 25 #include "abstractdomain_p.h"
26 26 #include "chartdataset_p.h"
27 27 #include "charttheme_p.h"
28 28 #include "qvalueaxis.h"
29 29 #include "qarealegendmarker.h"
30 30 #include "qchart_p.h"
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 /*!
35 35 \class QAreaSeries
36 36 \brief The QAreaSeries class is used for making area charts.
37 37
38 38 \mainclass
39 39
40 40 An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line
41 41 is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance,
42 42 which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line.
43 43 In that case QAreaSeries should be initiated with two QLineSeries instances. Please note terms "upper" and "lower" boundary can be misleading in cases
44 44 where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled.
45 45
46 46 See the \l {AreaChart Example} {area chart example} to learn how to create a simple area chart.
47 47 \image examples_areachart.png
48 48 */
49 49 /*!
50 50 \qmlclass AreaSeries QAreaSeries
51 51
52 52 The following QML shows how to create a simple area chart:
53 53 \snippet ../demos/qmlchart/qml/qmlchart/View4.qml 1
54 54 \beginfloatleft
55 55 \image demos_qmlchart4.png
56 56 \endfloat
57 57 \clearfloat
58 58 */
59 59
60 60 /*!
61 61 \property QAreaSeries::upperSeries
62 62 \brief The upper one of the two line series used to define area series boundaries.
63 63 */
64 64 /*!
65 65 \qmlproperty LineSeries AreaSeries::upperSeries
66 66 The upper one of the two line series used to define area series boundaries.
67 67 */
68 68
69 69 /*!
70 70 \property QAreaSeries::lowerSeries
71 71 The lower one of the two line series used to define are series boundaries. Note if
72 72 QAreaSeries was constructed without a\ lowerSeries this is null.
73 73 */
74 74 /*!
75 75 \qmlproperty LineSeries AreaSeries::lowerSeries
76 76 The lower one of the two line series used to define are series boundaries. Note if
77 77 AreaSeries was constructed without a\ lowerSeries this is null.
78 78 */
79 79
80 80 /*!
81 81 \property QAreaSeries::color
82 82 Fill (brush) color of the series. This is a convenience property for modifying the color of brush.
83 83 \sa QAreaSeries::brush()
84 84 */
85 85 /*!
86 86 \qmlproperty color AreaSeries::color
87 87 Fill (brush) color of the series.
88 88 */
89 89
90 90 /*!
91 91 \property QAreaSeries::borderColor
92 92 Line (pen) color of the series. This is a convenience property for modifying the color of pen.
93 93 \sa QAreaSeries::pen()
94 94 */
95 95 /*!
96 96 \qmlproperty color AreaSeries::borderColor
97 97 Line (pen) color of the series.
98 98 */
99 99
100 100 /*!
101 101 \qmlproperty real AreaSeries::borderWidth
102 102 The width of the border line. By default the width is 2.0.
103 103 */
104 104
105 105 /*!
106 106 \fn QPen QAreaSeries::pen() const
107 107 \brief Returns the pen used to draw line for this series.
108 108 \sa setPen()
109 109 */
110 110
111 111 /*!
112 112 \fn QPen QAreaSeries::brush() const
113 113 \brief Returns the brush used to draw line for this series.
114 114 \sa setBrush()
115 115 */
116 116
117 117 /*!
118 118 \fn void QAreaSeries::colorChanged(QColor color)
119 119 \brief Signal is emitted when the fill (brush) color has changed to \a color.
120 120 */
121 121 /*!
122 122 \qmlsignal AreaSeries::onColorChanged(color color)
123 123 Signal is emitted when the fill (brush) color has changed to \a color.
124 124 */
125 125
126 126 /*!
127 127 \fn void QAreaSeries::borderColorChanged(QColor color)
128 128 \brief Signal is emitted when the line (pen) color has changed to \a color.
129 129 */
130 130 /*!
131 131 \qmlsignal AreaSeries::onBorderColorChanged(color color)
132 132 Signal is emitted when the line (pen) color has changed to \a color.
133 133 */
134 134
135 135 /*!
136 136 \fn void QAreaSeries::clicked(const QPointF& point)
137 137 \brief Signal is emitted when user clicks the \a point on area chart.
138 138 */
139 139 /*!
140 140 \qmlsignal AreaSeries::onClicked(QPointF point)
141 141 Signal is emitted when user clicks the \a point on area chart.
142 142 */
143 143
144 144 /*!
145 145 \fn void QAreaSeries::hovered(const QPointF &point, bool state)
146 146 This signal is emitted when user has hovered over or away from the series. \a point shows the origin (coordinate)
147 147 of the hover event. \a state is true when user has hovered over the series and false when hover has moved away from
148 148 the series.
149 149 */
150 150 /*!
151 151 \qmlsignal AreaSeries::onHovered(point point, bool state)
152 152 This signal is emitted when user has hovered over or away from the series. \a point shows the origin (coordinate)
153 153 of the hover event. \a state is true when user has hovered over the series and false when hover has moved away from
154 154 the series.
155 155 */
156 156
157 157 /*!
158 158 \fn void QAreaSeries::selected()
159 159 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
160 160 implemented by the user of QAreaSeries API.
161 161 */
162 162 /*!
163 163 \qmlsignal AreaSeries::onSelected()
164 164 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
165 165 implemented by the user of AreaSeries API.
166 166 */
167 167
168 168 /*!
169 169 \fn void QAreaSeriesPrivate::updated()
170 170 \brief \internal
171 171 */
172 172
173 173 /*!
174 174 Constructs area series object which is a child of \a upperSeries. Area will be spanned between \a
175 175 upperSeries line and \a lowerSeries line. If no \a lowerSeries is passed to constructor, area is specified by axis x (y=0) instead.
176 176 When series object is added to QChartView or QChart instance ownerships is transferred.
177 177 */
178 178 QAreaSeries::QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries)
179 179 : QAbstractSeries(*new QAreaSeriesPrivate(upperSeries, lowerSeries, this), upperSeries)
180 180 {
181 181 }
182 182
183 183 /*!
184 184 Constructs area series object without upper or lower series with \a parent object.
185 185 */
186 186 QAreaSeries::QAreaSeries(QObject *parent)
187 187 : QAbstractSeries(*new QAreaSeriesPrivate(0, 0, this), parent)
188 188 {
189 189 }
190 190
191 191 /*!
192 192 Destroys the object.
193 193 */
194 194 QAreaSeries::~QAreaSeries()
195 195 {
196 196 Q_D(QAreaSeries);
197 197 if (d->m_chart)
198 198 d->m_chart->removeSeries(this);
199 199 }
200 200
201 201 /*!
202 202 Returns QChartSeries::SeriesTypeArea.
203 203 */
204 204 QAbstractSeries::SeriesType QAreaSeries::type() const
205 205 {
206 206 return QAbstractSeries::SeriesTypeArea;
207 207 }
208 208
209 209 /*!
210 210 Sets the \a series that is to be used as the area chart upper series.
211 211 */
212 212 void QAreaSeries::setUpperSeries(QLineSeries *series)
213 213 {
214 214 Q_D(QAreaSeries);
215 215 if (d->m_upperSeries != series)
216 216 d->m_upperSeries = series;
217 217 }
218 218
219 219 QLineSeries *QAreaSeries::upperSeries() const
220 220 {
221 221 Q_D(const QAreaSeries);
222 222 return d->m_upperSeries;
223 223 }
224 224
225 225 /*!
226 226 Sets the \a series that is to be used as the area chart lower series.
227 227 */
228 228 void QAreaSeries::setLowerSeries(QLineSeries *series)
229 229 {
230 230 Q_D(QAreaSeries);
231 231 d->m_lowerSeries = series;
232 232 }
233 233
234 234 QLineSeries *QAreaSeries::lowerSeries() const
235 235 {
236 236 Q_D(const QAreaSeries);
237 237 return d->m_lowerSeries;
238 238 }
239 239
240 240 /*!
241 241 Sets \a pen used for drawing area outline.
242 242 */
243 243 void QAreaSeries::setPen(const QPen &pen)
244 244 {
245 245 Q_D(QAreaSeries);
246 246 if (d->m_pen != pen) {
247 247 d->m_pen = pen;
248 248 emit d->updated();
249 249 }
250 250 }
251 251
252 QPen QAreaSeries::pen() const
252 QPen QAreaSeries::pen() const
253 253 {
254 254 Q_D(const QAreaSeries);
255 return d->m_pen;
255 if (d->m_pen == QChartPrivate::defaultPen())
256 return QPen();
257 else
258 return d->m_pen;
256 259 }
257 260
258 261 /*!
259 262 Sets \a brush used for filling the area.
260 263 */
261 264 void QAreaSeries::setBrush(const QBrush &brush)
262 265 {
263 266 Q_D(QAreaSeries);
264 267 if (d->m_brush != brush) {
265 268 bool emitColorChanged = brush.color() != d->m_brush.color();
266 269 d->m_brush = brush;
267 270 emit d->updated();
268 271 if (emitColorChanged)
269 272 emit colorChanged(brush.color());
270 273 }
271 274 }
272 275
273 276 QBrush QAreaSeries::brush() const
274 277 {
275 278 Q_D(const QAreaSeries);
276 return d->m_brush;
279 if (d->m_brush == QChartPrivate::defaultBrush())
280 return QBrush();
281 else
282 return d->m_brush;
277 283 }
278 284
279 285 void QAreaSeries::setColor(const QColor &color)
280 286 {
281 287 QBrush b = brush();
282 288 if (b == QBrush())
283 289 b.setStyle(Qt::SolidPattern);
284 290 b.setColor(color);
285 291 setBrush(b);
286 292 }
287 293
288 294 QColor QAreaSeries::color() const
289 295 {
290 296 return brush().color();
291 297 }
292 298
293 299 void QAreaSeries::setBorderColor(const QColor &color)
294 300 {
295 301 QPen p = pen();
296 302 if (p.color() != color) {
297 303 p.setColor(color);
298 304 setPen(p);
299 305 emit borderColorChanged(color);
300 306 }
301 307 }
302 308
303 309 QColor QAreaSeries::borderColor() const
304 310 {
305 311 return pen().color();
306 312 }
307 313
308 314 /*!
309 315 Sets if data points are \a visible and should be drawn on line.
310 316 */
311 317 void QAreaSeries::setPointsVisible(bool visible)
312 318 {
313 319 Q_D(QAreaSeries);
314 320 if (d->m_pointsVisible != visible) {
315 321 d->m_pointsVisible = visible;
316 322 emit d->updated();
317 323 }
318 324 }
319 325
320 326 /*!
321 327 Returns if the points are drawn for this series.
322 328 \sa setPointsVisible()
323 329 */
324 330 bool QAreaSeries::pointsVisible() const
325 331 {
326 332 Q_D(const QAreaSeries);
327 333 return d->m_pointsVisible;
328 334 }
329 335
330 336 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
331 337
332 338 QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries, QAreaSeries *q)
333 339 : QAbstractSeriesPrivate(q),
334 340 m_brush(QChartPrivate::defaultBrush()),
335 341 m_pen(QChartPrivate::defaultPen()),
336 342 m_upperSeries(upperSeries),
337 343 m_lowerSeries(lowerSeries),
338 344 m_pointsVisible(false)
339 345 {
340 346 }
341 347
342 348 void QAreaSeriesPrivate::initializeDomain()
343 349 {
344 350 Q_Q(QAreaSeries);
345 351
346 352 qreal minX(domain()->minX());
347 353 qreal minY(domain()->minY());
348 354 qreal maxX(domain()->maxX());
349 355 qreal maxY(domain()->maxY());
350 356
351 357 QLineSeries *upperSeries = q->upperSeries();
352 358 QLineSeries *lowerSeries = q->lowerSeries();
353 359
354 360 const QList<QPointF>& points = upperSeries->points();
355 361
356 362 for (int i = 0; i < points.count(); i++) {
357 363 qreal x = points[i].x();
358 364 qreal y = points[i].y();
359 365 minX = qMin(minX, x);
360 366 minY = qMin(minY, y);
361 367 maxX = qMax(maxX, x);
362 368 maxY = qMax(maxY, y);
363 369 }
364 370 if (lowerSeries) {
365 371
366 372 const QList<QPointF>& points = lowerSeries->points();
367 373
368 374 for (int i = 0; i < points.count(); i++) {
369 375 qreal x = points[i].x();
370 376 qreal y = points[i].y();
371 377 minX = qMin(minX, x);
372 378 minY = qMin(minY, y);
373 379 maxX = qMax(maxX, x);
374 380 maxY = qMax(maxY, y);
375 381 }
376 382 }
377 383
378 384 domain()->setRange(minX, maxX, minY, maxY);
379 385 }
380 386
381 387 void QAreaSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
382 388 {
383 389 Q_Q(QAreaSeries);
384 390 AreaChartItem *area = new AreaChartItem(q,parent);
385 391 m_item.reset(area);
386 392 QAbstractSeriesPrivate::initializeGraphics(parent);
387 393 }
388 394 void QAreaSeriesPrivate::initializeAnimations(QChart::AnimationOptions options)
389 395 {
390 396 Q_Q(QAreaSeries);
391 397 AreaChartItem *area = static_cast<AreaChartItem *>(m_item.data());
392 398 if (options.testFlag(QChart::SeriesAnimations)) {
393 399 area->upperLineItem()->setAnimation(new XYAnimation(area->upperLineItem()));
394 400 if (q->lowerSeries())
395 401 area->lowerLineItem()->setAnimation(new XYAnimation(area->lowerLineItem()));
396 402 }else{
397 403 area->upperLineItem()->setAnimation(0);
398 404 if (q->lowerSeries())
399 405 area->lowerLineItem()->setAnimation(0);
400 406 }
401 407 QAbstractSeriesPrivate::initializeAnimations(options);
402 408 }
403 409
404 410 QList<QLegendMarker*> QAreaSeriesPrivate::createLegendMarkers(QLegend* legend)
405 411 {
406 412 Q_Q(QAreaSeries);
407 413 QList<QLegendMarker*> list;
408 414 return list << new QAreaLegendMarker(q,legend);
409 415 }
410 416
411 417
412 418 void QAreaSeriesPrivate::initializeAxes()
413 419 {
414 420
415 421 }
416 422
417 423 QAbstractAxis::AxisType QAreaSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
418 424 {
419 425 Q_UNUSED(orientation);
420 426 return QAbstractAxis::AxisTypeValue;
421 427 }
422 428
423 429 QAbstractAxis* QAreaSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
424 430 {
425 431 Q_UNUSED(orientation);
426 432 return 0;
427 433 }
428 434
429 435 void QAreaSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
430 436 {
431 437 Q_Q(QAreaSeries);
432 438
433 439 const QList<QGradient> gradients = theme->seriesGradients();
434 440 const QList<QColor> colors = theme->seriesColors();
435 441
436 442 if (forced || QChartPrivate::defaultPen() == m_pen) {
437 443 QPen pen;
438 444 pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0));
439 445 pen.setWidthF(2);
440 446 q->setPen(pen);
441 447 }
442 448
443 449 if (forced || QChartPrivate::defaultBrush() == m_brush) {
444 450 QBrush brush(colors.at(index % colors.size()));
445 451 q->setBrush(brush);
446 452 }
447 453 }
448 454
449 455
450 456 #include "moc_qareaseries.cpp"
451 457 #include "moc_qareaseries_p.cpp"
452 458
453 459 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,990 +1,1020
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 "qabstractaxis.h"
22 22 #include "qabstractaxis_p.h"
23 23 #include "chartdataset_p.h"
24 24 #include "charttheme_p.h"
25 25 #include "qchart_p.h"
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 /*!
30 30 \class QAbstractAxis
31 31 \brief The QAbstractAxis class is used for manipulating chart's axis.
32 32 \mainclass
33 33
34 34 Each series can be bound to one or more horizontal and vertical axes, but mixing axis types
35 35 that would result in different domains is not supported, such as specifying
36 36 QValueAxis and QLogValueAxis on the same orientation.
37 37
38 38 Properties and visibility of various axis elements such as axis line, title, labels, grid lines,
39 39 and shades can be individually controlled.
40 40 */
41 41
42 42 /*!
43 43 \qmlclass AbstractAxis QAbstractAxis
44 44 \brief The AbstractAxis is a base element used for specialized axis elements
45 45
46 46 Each series can be bound to only one horizontal and vertical axis.
47 47
48 48 Properties and visibility of various axis elements such as axis line, title, labels, grid lines,
49 49 and shades can be individually controlled.
50 50 */
51 51
52 52 /*!
53 53 \enum QAbstractAxis::AxisType
54 54
55 55 The type of the series object.
56 56
57 57 \value AxisTypeNoAxis
58 58 \value AxisTypeValue
59 59 \value AxisTypeBarCategory
60 60 \value AxisTypeCategory
61 61 \value AxisTypeDateTime
62 62 \value AxisTypeLogValue
63 63 */
64 64
65 65 /*!
66 66 *\fn void QAbstractAxis::type() const
67 67 Returns the type of the axis
68 68 */
69 69
70 70 /*!
71 71 \property QAbstractAxis::lineVisible
72 72 The visibility of the axis line
73 73 */
74 74 /*!
75 75 \qmlproperty bool AbstractAxis::lineVisible
76 76 The visibility of the axis line
77 77 */
78 78
79 79 /*!
80 80 \property QAbstractAxis::linePen
81 81 The pen of the line.
82 82 */
83 83
84 84 /*!
85 85 \property QAbstractAxis::labelsVisible
86 86 Defines if axis labels are visible.
87 87 */
88 88 /*!
89 89 \qmlproperty bool AbstractAxis::labelsVisible
90 90 Defines if axis labels are visible.
91 91 */
92 92
93 93 /*!
94 94 \property QAbstractAxis::labelsPen
95 95 The pen of the labels.
96 96 */
97 97
98 98 /*!
99 99 \property QAbstractAxis::labelsBrush
100 100 The brush of the labels.
101 101 */
102 102
103 103 /*!
104 104 \property QAbstractAxis::visible
105 105 The visibility of the axis.
106 106 */
107 107 /*!
108 108 \qmlproperty bool AbstractAxis::visible
109 109 The visibility of the axis.
110 110 */
111 111
112 112 /*!
113 113 \property QAbstractAxis::gridVisible
114 114 The visibility of the grid lines.
115 115 */
116 116 /*!
117 117 \qmlproperty bool AbstractAxis::gridVisible
118 118 The visibility of the grid lines.
119 119 */
120 120
121 121 /*!
122 122 \property QAbstractAxis::color
123 123 The color of the axis and ticks.
124 124 */
125 125 /*!
126 126 \qmlproperty color AbstractAxis::color
127 127 The color of the axis and ticks.
128 128 */
129 129
130 130 /*!
131 131 \property QAbstractAxis::gridLinePen
132 132 The pen of the grid line.
133 133 */
134 134
135 135 /*!
136 136 \property QAbstractAxis::labelsFont
137 137 The font of the axis labels.
138 138 */
139 139
140 140 /*!
141 141 \qmlproperty Font AbstractAxis::labelsFont
142 142 The font of the axis labels.
143 143
144 144 See the \l {Font} {QML Font Element} for detailed documentation.
145 145 */
146 146
147 147 /*!
148 148 \property QAbstractAxis::labelsColor
149 149 The color of the axis labels.
150 150 */
151 151 /*!
152 152 \qmlproperty color AbstractAxis::labelsColor
153 153 The color of the axis labels.
154 154 */
155 155
156 156 /*!
157 157 \property QAbstractAxis::labelsAngle
158 158 The angle of the axis labels in degrees.
159 159 */
160 160 /*!
161 161 \qmlproperty int AbstractAxis::labelsAngle
162 162 The angle of the axis labels in degrees.
163 163 */
164 164
165 165 /*!
166 166 \property QAbstractAxis::shadesVisible
167 167 The visibility of the axis shades.
168 168 */
169 169 /*!
170 170 \qmlproperty bool AbstractAxis::shadesVisible
171 171 The visibility of the axis shades.
172 172 */
173 173
174 174 /*!
175 175 \property QAbstractAxis::shadesColor
176 176 The fill (brush) color of the axis shades.
177 177 */
178 178 /*!
179 179 \qmlproperty color AbstractAxis::shadesColor
180 180 The fill (brush) color of the axis shades.
181 181 */
182 182
183 183 /*!
184 184 \property QAbstractAxis::shadesBorderColor
185 185 The border (pen) color of the axis shades.
186 186 */
187 187 /*!
188 188 \qmlproperty color AbstractAxis::shadesBorderColor
189 189 The border (pen) color of the axis shades.
190 190 */
191 191
192 192 /*!
193 193 \property QAbstractAxis::shadesPen
194 194 The pen of the axis shades (area between grid lines).
195 195 */
196 196
197 197 /*!
198 198 \property QAbstractAxis::shadesBrush
199 199 The brush of the axis shades (area between grid lines).
200 200 */
201 201
202 202 /*!
203 203 \property QAbstractAxis::titleVisible
204 204 The visibility of the axis title. By default the value is true.
205 205 */
206 206 /*!
207 207 \qmlproperty bool AbstractAxis::titleVisible
208 208 The visibility of the axis title. By default the value is true.
209 209 */
210 210
211 211 /*!
212 212 \property QAbstractAxis::titleText
213 213 The title of the axis. Empty by default.
214 214 */
215 215 /*!
216 216 \qmlproperty String AbstractAxis::titleText
217 217 The title of the axis. Empty by default.
218 218 */
219 219
220 220 /*!
221 221 \property QAbstractAxis::titlePen
222 222 The pen of the title text.
223 223 */
224 224
225 225 /*!
226 226 \property QAbstractAxis::titleBrush
227 227 The brush of the title text.
228 228 */
229 229
230 230 /*!
231 231 \property QAbstractAxis::titleFont
232 232 The font of the title of the axis.
233 233 */
234 234 /*!
235 235 \qmlproperty Font AbstractAxis::titleFont
236 236 The font of the title of the axis.
237 237 */
238 238
239 239 /*!
240 240 \property QAbstractAxis::orientation
241 241 The orientation of the axis. Fixed to either Qt::Horizontal or Qt::Vertical when you add the axis to a chart.
242 242 */
243 243 /*!
244 244 \qmlproperty Qt.Orientation AbstractAxis::orientation
245 245 The orientation of the axis. Fixed to either Qt.Horizontal or Qt.Vertical when the axis is set to a series.
246 246 */
247 247
248 248 /*!
249 249 \property QAbstractAxis::alignment
250 250 The alignment of the axis. Can be Qt::AlignLeft, Qt::AlignRight, Qt::AlignBottom, or Qt::AlignTop.
251 251 */
252 252 /*!
253 253 \qmlproperty alignment AbstractAxis::alignment
254 254 The alignment of the axis. Can be Qt.AlignLeft, Qt.AlignRight, Qt.AlignBottom, or Qt.AlignTop.
255 255 */
256 256
257 257 /*!
258 258 \fn void QAbstractAxis::visibleChanged(bool visible)
259 259 Visibility of the axis has changed to \a visible.
260 260 */
261 261 /*!
262 262 \qmlsignal AbstractAxis::onVisibleChanged(bool visible)
263 263 Visibility of the axis has changed to \a visible.
264 264 */
265 265
266 266 /*!
267 267 \fn void QAbstractAxis::linePenChanged(const QPen& pen)
268 268 The pen of the line of the axis has changed to \a pen.
269 269 */
270 270
271 271 /*!
272 272 \fn void QAbstractAxis::lineVisibleChanged(bool visible)
273 273 Visibility of the axis line has changed to \a visible.
274 274 */
275 275 /*!
276 276 \qmlsignal AbstractAxis::onLineVisibleChanged(bool visible)
277 277 Visibility of the axis line has changed to \a visible.
278 278 */
279 279
280 280 /*!
281 281 \fn void QAbstractAxis::labelsVisibleChanged(bool visible)
282 282 Visibility of the labels of the axis has changed to \a visible.
283 283 */
284 284 /*!
285 285 \qmlsignal AbstractAxis::onLabelsVisibleChanged(bool visible)
286 286 Visibility of the labels of the axis has changed to \a visible.
287 287 */
288 288
289 289 /*!
290 290 \fn void QAbstractAxis::labelsFontChanged(const QFont& font)
291 291 The font of the axis labels has changed to \a font.
292 292 */
293 293 /*!
294 294 \qmlsignal AbstractAxis::onLabelsFontChanged(Font font)
295 295 The font of the axis labels has changed to \a font.
296 296 */
297 297
298 298 /*!
299 299 \fn void QAbstractAxis::labelsPenChanged(const QPen& pen)
300 300 The pen of the axis labels has changed to \a pen.
301 301 */
302 302
303 303 /*!
304 304 \fn void QAbstractAxis::labelsBrushChanged(const QBrush& brush)
305 305 The brush of the axis labels has changed to \a brush.
306 306 */
307 307
308 308 /*!
309 309 \fn void QAbstractAxis::labelsAngleChanged(int angle)
310 310 The angle of the axis labels has changed to \a angle.
311 311 */
312 312 /*!
313 313 \qmlsignal AbstractAxis::onLabelsAngleChanged(int angle)
314 314 The angle of the axis labels has changed to \a angle.
315 315 */
316 316
317 317 /*!
318 318 \fn void QAbstractAxis::gridVisibleChanged(bool visible)
319 319 Visibility of the grid lines of the axis has changed to \a visible.
320 320 */
321 321 /*!
322 322 \qmlsignal AbstractAxis::onGridVisibleChanged(bool visible)
323 323 Visibility of the grid lines of the axis has changed to \a visible.
324 324 */
325 325
326 326 /*!
327 327 \fn void QAbstractAxis::gridLinePenChanged(const QPen& pen)
328 328 The pen of the grid line has changed to \a pen.
329 329 */
330 330
331 331 /*!
332 332 \fn void QAbstractAxis::colorChanged(QColor color)
333 333 Emitted if the \a color of the axis is changed.
334 334 */
335 335 /*!
336 336 \qmlsignal AbstractAxis::onColorChanged(QColor color)
337 337 Emitted if the \a color of the axis is changed.
338 338 */
339 339
340 340 /*!
341 341 \fn void QAbstractAxis::labelsColorChanged(QColor color)
342 342 Emitted if the \a color of the axis labels is changed.
343 343 */
344 344 /*!
345 345 \qmlsignal AbstractAxis::onLabelsColorChanged(QColor color)
346 346 Emitted if the \a color of the axis labels is changed.
347 347 */
348 348
349 349 /*!
350 350 \fn void QAbstractAxis::titleVisibleChanged(bool visible)
351 351 Visibility of the title text of the axis has changed to \a visible.
352 352 */
353 353 /*!
354 354 \qmlsignal AbstractAxis::onTitleVisibleChanged(bool visible)
355 355 Visibility of the title text of the axis has changed to \a visible.
356 356 */
357 357
358 358 /*!
359 359 \fn void QAbstractAxis::titleTextChanged(const QString& text)
360 360 The text of the axis title has changed to \a text.
361 361 */
362 362 /*!
363 363 \qmlsignal AbstractAxis::onTitleTextChanged(String text)
364 364 The text of the axis title has changed to \a text.
365 365 */
366 366
367 367 /*!
368 368 \fn void QAbstractAxis::titlePenChanged(const QPen& pen)
369 369 The pen of the axis shades has changed to \a pen.
370 370 */
371 371
372 372 /*!
373 373 \fn void QAbstractAxis::titleBrushChanged(const QBrush& brush)
374 374 The brush of the axis title has changed to \a brush.
375 375 */
376 376
377 377 /*!
378 378 \fn void QAbstractAxis::titleFontChanged(const QFont& font)
379 379 The font of the axis title has changed to \a font.
380 380 */
381 381 /*!
382 382 \qmlsignal AbstractAxis::onTitleFontChanged(Font font)
383 383 The font of the axis title has changed to \a font.
384 384 */
385 385
386 386 /*!
387 387 \fn void QAbstractAxis::shadesVisibleChanged(bool)
388 388 Emitted if the visibility of the axis shades is changed to \a visible.
389 389 */
390 390 /*!
391 391 \qmlsignal AbstractAxis::onShadesVisibleChanged(bool visible)
392 392 Emitted if the visibility of the axis shades is changed to \a visible.
393 393 */
394 394
395 395 /*!
396 396 \fn void QAbstractAxis::shadesColorChanged(QColor color)
397 397 Emitted if the \a color of the axis shades is changed.
398 398 */
399 399 /*!
400 400 \qmlsignal AbstractAxis::onShadesColorChanged(QColor color)
401 401 Emitted if the \a color of the axis shades is changed.
402 402 */
403 403
404 404 /*!
405 405 \fn void QAbstractAxis::shadesBorderColorChanged(QColor)
406 406 Emitted if the border \a color of the axis shades is changed.
407 407 */
408 408 /*!
409 409 \qmlsignal AbstractAxis::onBorderColorChanged(QColor color)
410 410 Emitted if the border \a color of the axis shades is changed.
411 411 */
412 412
413 413 /*!
414 414 \fn void QAbstractAxis::shadesBrushChanged(const QBrush& brush)
415 415 The brush of the axis shades has changed to \a brush.
416 416 */
417 417
418 418 /*!
419 419 \fn void QAbstractAxis::shadesPenChanged(const QPen& pen)
420 420 The pen of the axis shades has changed to \a pen.
421 421 */
422 422
423 423 /*!
424 424 \internal
425 425 Constructs new axis object which is a child of \a parent. Ownership is taken by
426 426 QChart when axis added.
427 427 */
428 428
429 429 QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent)
430 430 : QObject(parent),
431 431 d_ptr(&d)
432 432 {
433 433 }
434 434
435 435 /*!
436 436 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
437 437 */
438 438
439 439 QAbstractAxis::~QAbstractAxis()
440 440 {
441 441 if (d_ptr->m_chart)
442 442 qFatal("Still binded axis detected !");
443 443 }
444 444
445 445 /*!
446 446 Sets \a pen used to draw axis line and ticks.
447 447 */
448 448 void QAbstractAxis::setLinePen(const QPen &pen)
449 449 {
450 450 if (d_ptr->m_axisPen != pen) {
451 451 d_ptr->m_axisPen = pen;
452 452 emit linePenChanged(pen);
453 453 }
454 454 }
455 455
456 456 /*!
457 457 Returns pen used to draw axis and ticks.
458 458 */
459 459 QPen QAbstractAxis::linePen() const
460 460 {
461 return d_ptr->m_axisPen;
461 if (d_ptr->m_axisPen == QChartPrivate::defaultPen())
462 return QPen();
463 else
464 return d_ptr->m_axisPen;
462 465 }
463 466
464 467 //TODO: remove me 2.0
465 468 void QAbstractAxis::setLinePenColor(QColor color)
466 469 {
467 470 QPen p = d_ptr->m_axisPen;
468 471 if (p.color() != color) {
469 472 p.setColor(color);
470 473 setLinePen(p);
471 474 emit colorChanged(color);
472 475 }
473 476 }
474 477
475 478 QColor QAbstractAxis::linePenColor() const
476 479 {
477 return d_ptr->m_axisPen.color();
480 return linePen().color();
478 481 }
479 482
480 483 /*!
481 484 Sets if axis and ticks are \a visible.
482 485 */
483 486 void QAbstractAxis::setLineVisible(bool visible)
484 487 {
485 488 if (d_ptr->m_arrowVisible != visible) {
486 489 d_ptr->m_arrowVisible = visible;
487 490 emit lineVisibleChanged(visible);
488 491 }
489 492 }
490 493
491 494 bool QAbstractAxis::isLineVisible() const
492 495 {
493 496 return d_ptr->m_arrowVisible;
494 497 }
495 498
496 499 void QAbstractAxis::setGridLineVisible(bool visible)
497 500 {
498 501 if (d_ptr->m_gridLineVisible != visible) {
499 502 d_ptr->m_gridLineVisible = visible;
500 503 emit gridVisibleChanged(visible);
501 504 }
502 505 }
503 506
504 507 bool QAbstractAxis::isGridLineVisible() const
505 508 {
506 509 return d_ptr->m_gridLineVisible;
507 510 }
508 511
509 512 /*!
510 513 Sets \a pen used to draw grid line.
511 514 */
512 515 void QAbstractAxis::setGridLinePen(const QPen &pen)
513 516 {
514 517 if (d_ptr->m_gridLinePen != pen) {
515 518 d_ptr->m_gridLinePen = pen;
516 519 emit gridLinePenChanged(pen);
517 520 }
518 521 }
519 522
520 523 /*!
521 524 Returns pen used to draw grid.
522 525 */
523 526 QPen QAbstractAxis::gridLinePen() const
524 527 {
525 return d_ptr->m_gridLinePen;
528 if (d_ptr->m_gridLinePen == QChartPrivate::defaultPen())
529 return QPen();
530 else
531 return d_ptr->m_gridLinePen;
526 532 }
527 533
528 534 void QAbstractAxis::setLabelsVisible(bool visible)
529 535 {
530 536 if (d_ptr->m_labelsVisible != visible) {
531 537 d_ptr->m_labelsVisible = visible;
532 538 emit labelsVisibleChanged(visible);
533 539 }
534 540 }
535 541
536 542 bool QAbstractAxis::labelsVisible() const
537 543 {
538 544 return d_ptr->m_labelsVisible;
539 545 }
540 546
541 547 /*!
542 548 Sets \a pen used to draw labels.
543 549 */
544 550 void QAbstractAxis::setLabelsPen(const QPen &pen)
545 551 {
546 552 if (d_ptr->m_labelsPen != pen) {
547 553 d_ptr->m_labelsPen = pen;
548 554 emit labelsPenChanged(pen);
549 555 }
550 556 }
551 557
552 558 /*!
553 559 Returns the pen used to labels.
554 560 */
555 561 QPen QAbstractAxis::labelsPen() const
556 562 {
557 return d_ptr->m_labelsPen;
563 if (d_ptr->m_labelsPen == QChartPrivate::defaultPen())
564 return QPen();
565 else
566 return d_ptr->m_labelsPen;
558 567 }
559 568
560 569 /*!
561 570 Sets \a brush used to draw labels.
562 571 */
563 572 void QAbstractAxis::setLabelsBrush(const QBrush &brush)
564 573 {
565 574 if (d_ptr->m_labelsBrush != brush) {
566 575 d_ptr->m_labelsBrush = brush;
567 576 emit labelsBrushChanged(brush);
568 577 }
569 578 }
570 579
571 580 /*!
572 581 Returns brush used to draw labels.
573 582 */
574 583 QBrush QAbstractAxis::labelsBrush() const
575 584 {
576 return d_ptr->m_labelsBrush;
585 if (d_ptr->m_labelsBrush == QChartPrivate::defaultBrush())
586 return QBrush();
587 else
588 return d_ptr->m_labelsBrush;
577 589 }
578 590
579 591 /*!
580 592 Sets \a font used to draw labels.
581 593 */
582 594 void QAbstractAxis::setLabelsFont(const QFont &font)
583 595 {
584 596 if (d_ptr->m_labelsFont != font) {
585 597 d_ptr->m_labelsFont = font;
586 598 emit labelsFontChanged(font);
587 599 }
588 600 }
589 601
590 602 /*!
591 603 Returns font used to draw labels.
592 604 */
593 605 QFont QAbstractAxis::labelsFont() const
594 606 {
595 return d_ptr->m_labelsFont;
607 if (d_ptr->m_labelsFont == QChartPrivate::defaultFont())
608 return QFont();
609 else
610 return d_ptr->m_labelsFont;
596 611 }
597 612
598 613 void QAbstractAxis::setLabelsAngle(int angle)
599 614 {
600 615 if (d_ptr->m_labelsAngle != angle) {
601 616 d_ptr->m_labelsAngle = angle;
602 617 emit labelsAngleChanged(angle);
603 618 }
604 619 }
605 620
606 621 int QAbstractAxis::labelsAngle() const
607 622 {
608 623 return d_ptr->m_labelsAngle;
609 624 }
610 625 //TODO: remove me 2.0
611 626 void QAbstractAxis::setLabelsColor(QColor color)
612 627 {
613 628 QBrush b = d_ptr->m_labelsBrush;
614 629 if (b.color() != color) {
615 630 b.setColor(color);
616 631 setLabelsBrush(b);
617 632 emit labelsColorChanged(color);
618 633 }
619 634 }
620 635
621 636 QColor QAbstractAxis::labelsColor() const
622 637 {
623 return d_ptr->m_labelsBrush.color();
638 return labelsBrush().color();
624 639 }
625 640
626 641 void QAbstractAxis::setTitleVisible(bool visible)
627 642 {
628 643 if (d_ptr->m_titleVisible != visible) {
629 644 d_ptr->m_titleVisible = visible;
630 645 emit titleVisibleChanged(visible);
631 646 }
632 647 }
633 648
634 649 bool QAbstractAxis::isTitleVisible() const
635 650 {
636 651 return d_ptr->m_titleVisible;
637 652 }
638 653
639 654 /*!
640 655 Sets \a pen used to draw title.
641 656 */
642 657 void QAbstractAxis::setTitlePen(const QPen &pen)
643 658 {
644 659 if (d_ptr->m_titlePen != pen) {
645 660 d_ptr->m_titlePen = pen;
646 661 emit titlePenChanged(pen);
647 662 }
648 663 }
649 664
650 665 /*!
651 666 Returns the pen used to title.
652 667 */
653 668 QPen QAbstractAxis::titlePen() const
654 669 {
655 return d_ptr->m_titlePen;
670 if (d_ptr->m_titlePen == QChartPrivate::defaultPen())
671 return QPen();
672 else
673 return d_ptr->m_titlePen;
656 674 }
657 675
658 676 /*!
659 677 Sets \a brush used to draw title.
660 678 */
661 679 void QAbstractAxis::setTitleBrush(const QBrush &brush)
662 680 {
663 681 if (d_ptr->m_titleBrush != brush) {
664 682 d_ptr->m_titleBrush = brush;
665 683 emit titleBrushChanged(brush);
666 684 }
667 685 }
668 686
669 687 /*!
670 688 Returns brush used to draw title.
671 689 */
672 QBrush QAbstractAxis::titleBrush() const
690 QBrush QAbstractAxis::titleBrush() const
673 691 {
674 return d_ptr->m_titleBrush;
692 if (d_ptr->m_titleBrush == QChartPrivate::defaultBrush())
693 return QBrush();
694 else
695 return d_ptr->m_titleBrush;
675 696 }
676 697
677 698 /*!
678 699 Sets \a font used to draw title.
679 700 */
680 701 void QAbstractAxis::setTitleFont(const QFont &font)
681 702 {
682 703 if (d_ptr->m_titleFont != font) {
683 704 d_ptr->m_titleFont = font;
684 705 emit titleFontChanged(font);
685 706 }
686 707 }
687 708
688 709 /*!
689 710 Returns font used to draw title.
690 711 */
691 712 QFont QAbstractAxis::titleFont() const
692 713 {
693 return d_ptr->m_titleFont;
714 if (d_ptr->m_titleFont == QChartPrivate::defaultFont())
715 return QFont();
716 else
717 return d_ptr->m_titleFont;
694 718 }
695 719
696 720 void QAbstractAxis::setTitleText(const QString &title)
697 721 {
698 722 if (d_ptr->m_title != title) {
699 723 d_ptr->m_title = title;
700 724 emit titleTextChanged(title);
701 725 }
702 726 }
703 727
704 728 QString QAbstractAxis::titleText() const
705 729 {
706 730 return d_ptr->m_title;
707 731 }
708 732
709 733
710 734 void QAbstractAxis::setShadesVisible(bool visible)
711 735 {
712 736 if (d_ptr->m_shadesVisible != visible) {
713 737 d_ptr->m_shadesVisible = visible;
714 738 emit shadesVisibleChanged(visible);
715 739 }
716 740 }
717 741
718 742 bool QAbstractAxis::shadesVisible() const
719 743 {
720 744 return d_ptr->m_shadesVisible;
721 745 }
722 746
723 747 /*!
724 748 Sets \a pen used to draw shades.
725 749 */
726 750 void QAbstractAxis::setShadesPen(const QPen &pen)
727 751 {
728 752 if (d_ptr->m_shadesPen != pen) {
729 753 d_ptr->m_shadesPen = pen;
730 754 emit shadesPenChanged(pen);
731 755 }
732 756 }
733 757
734 758 /*!
735 759 Returns pen used to draw shades.
736 760 */
737 761 QPen QAbstractAxis::shadesPen() const
738 762 {
739 return d_ptr->m_shadesPen;
763 if (d_ptr->m_shadesPen == QChartPrivate::defaultPen())
764 return QPen();
765 else
766 return d_ptr->m_shadesPen;
740 767 }
741 768
742 769 /*!
743 770 Sets \a brush used to draw shades.
744 771 */
745 772 void QAbstractAxis::setShadesBrush(const QBrush &brush)
746 773 {
747 774 if (d_ptr->m_shadesBrush != brush) {
748 775 d_ptr->m_shadesBrush = brush;
749 776 emit shadesBrushChanged(brush);
750 777 }
751 778 }
752 779
753 780 /*!
754 781 Returns brush used to draw shades.
755 782 */
756 783 QBrush QAbstractAxis::shadesBrush() const
757 784 {
758 return d_ptr->m_shadesBrush;
785 if (d_ptr->m_shadesBrush == QChartPrivate::defaultBrush())
786 return QBrush(Qt::SolidPattern);
787 else
788 return d_ptr->m_shadesBrush;
759 789 }
760 790
761 791 void QAbstractAxis::setShadesColor(QColor color)
762 792 {
763 793 QBrush b = d_ptr->m_shadesBrush;
764 794 if (b.color() != color) {
765 795 b.setColor(color);
766 796 setShadesBrush(b);
767 797 emit shadesColorChanged(color);
768 798 }
769 799 }
770 800
771 801 QColor QAbstractAxis::shadesColor() const
772 802 {
773 return d_ptr->m_shadesBrush.color();
803 return shadesBrush().color();
774 804 }
775 805
776 806 void QAbstractAxis::setShadesBorderColor(QColor color)
777 807 {
778 808 QPen p = d_ptr->m_shadesPen;
779 809 if (p.color() != color) {
780 810 p.setColor(color);
781 811 setShadesPen(p);
782 812 emit shadesColorChanged(color);
783 813 }
784 814 }
785 815
786 816 QColor QAbstractAxis::shadesBorderColor() const
787 817 {
788 return d_ptr->m_shadesPen.color();
818 return shadesPen().color();
789 819 }
790 820
791 821
792 822 bool QAbstractAxis::isVisible() const
793 823 {
794 824 return d_ptr->m_visible;
795 825 }
796 826
797 827 /*!
798 828 Sets axis, shades, labels and grid lines to be visible.
799 829 */
800 830 void QAbstractAxis::setVisible(bool visible)
801 831 {
802 832 if (d_ptr->m_visible != visible) {
803 833 d_ptr->m_visible = visible;
804 834 emit visibleChanged(visible);
805 835 }
806 836 }
807 837
808 838
809 839 /*!
810 840 Sets axis, shades, labels and grid lines to be visible.
811 841 */
812 842 void QAbstractAxis::show()
813 843 {
814 844 setVisible(true);
815 845 }
816 846
817 847 /*!
818 848 Sets axis, shades, labels and grid lines to not be visible.
819 849 */
820 850 void QAbstractAxis::hide()
821 851 {
822 852 setVisible(false);
823 853 }
824 854
825 855 /*!
826 856 Sets the minimum value shown on the axis.
827 857 Depending on the actual axis type the \a min parameter is converted to appropriate type.
828 858 If the conversion is impossible then the function call does nothing
829 859 */
830 860 void QAbstractAxis::setMin(const QVariant &min)
831 861 {
832 862 d_ptr->setMin(min);
833 863 }
834 864
835 865 /*!
836 866 Sets the maximum value shown on the axis.
837 867 Depending on the actual axis type the \a max parameter is converted to appropriate type.
838 868 If the conversion is impossible then the function call does nothing
839 869 */
840 870 void QAbstractAxis::setMax(const QVariant &max)
841 871 {
842 872 d_ptr->setMax(max);
843 873 }
844 874
845 875 /*!
846 876 Sets the range shown on the axis.
847 877 Depending on the actual axis type the \a min and \a max parameters are converted to appropriate types.
848 878 If the conversion is impossible then the function call does nothing.
849 879 */
850 880 void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
851 881 {
852 882 d_ptr->setRange(min, max);
853 883 }
854 884
855 885
856 886 /*!
857 887 Returns the orientation in which the axis is being used (Vertical or Horizontal)
858 888 */
859 889 // NOTE: should have const but it breaks BC:
860 890 // http://techbase.kde.org/Policies/Binary_Compatibility_Examples#Change_the_CV-qualifiers_of_a_member_function
861 891 Qt::Orientation QAbstractAxis::orientation()
862 892 {
863 893 return d_ptr->orientation();
864 894 }
865 895
866 896 Qt::Alignment QAbstractAxis::alignment() const
867 897 {
868 898 return d_ptr->alignment();
869 899 }
870 900
871 901 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
872 902
873 903 QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis *q)
874 904 : q_ptr(q),
875 905 m_chart(0),
876 906 m_alignment(0),
877 907 m_orientation(Qt::Orientation(0)),
878 908 m_visible(true),
879 909 m_arrowVisible(true),
880 910 m_axisPen(QChartPrivate::defaultPen()),
881 911 m_axisBrush(QChartPrivate::defaultBrush()),
882 912 m_gridLineVisible(true),
883 913 m_gridLinePen(QChartPrivate::defaultPen()),
884 914 m_labelsVisible(true),
885 915 m_labelsPen(QChartPrivate::defaultPen()),
886 916 m_labelsBrush(QChartPrivate::defaultBrush()),
887 917 m_labelsFont(QChartPrivate::defaultFont()),
888 918 m_labelsAngle(0),
889 919 m_titleVisible(true),
890 920 m_titlePen(QChartPrivate::defaultPen()),
891 921 m_titleBrush(QChartPrivate::defaultBrush()),
892 922 m_titleFont(QChartPrivate::defaultFont()),
893 923 m_shadesVisible(false),
894 924 m_shadesBrush(QChartPrivate::defaultBrush()),
895 925 m_shadesPen(QChartPrivate::defaultPen()),
896 926 m_shadesOpacity(1.0),
897 927 m_dirty(false)
898 928 {
899 929 }
900 930
901 931 QAbstractAxisPrivate::~QAbstractAxisPrivate()
902 932 {
903 933 }
904 934
905 935 void QAbstractAxisPrivate::setAlignment( Qt::Alignment alignment)
906 936 {
907 937 switch(alignment) {
908 938 case Qt::AlignTop:
909 939 case Qt::AlignBottom:
910 940 m_orientation = Qt::Horizontal;
911 941 break;
912 942 case Qt::AlignLeft:
913 943 case Qt::AlignRight:
914 944 m_orientation = Qt::Vertical;
915 945 break;
916 946 default:
917 947 qWarning()<<"No alignment specified !";
918 948 break;
919 949 };
920 950 m_alignment=alignment;
921 951 }
922 952
923 953 void QAbstractAxisPrivate::initializeTheme(ChartTheme* theme, bool forced)
924 954 {
925 955 //TODO: introduce axis brush
926 956 if (forced || QChartPrivate::defaultPen() == m_axisPen)
927 957 q_ptr->setLinePen(theme->axisLinePen());
928 958
929 959 if (forced || QChartPrivate::defaultPen() == m_gridLinePen)
930 960 q_ptr->setGridLinePen(theme->girdLinePen());
931 961
932 962 if (forced || QChartPrivate::defaultBrush() == m_labelsBrush)
933 963 q_ptr->setLabelsBrush(theme->labelBrush());
934 964 if (forced || QChartPrivate::defaultPen() == m_labelsPen)
935 965 q_ptr->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
936 966 if (forced || QChartPrivate::defaultFont() == m_labelsFont)
937 967 q_ptr->setLabelsFont(theme->labelFont());
938 968
939 969 if (forced || QChartPrivate::defaultBrush() == m_titleBrush)
940 970 q_ptr->setTitleBrush(theme->labelBrush());
941 971 if (forced || QChartPrivate::defaultPen() == m_titlePen)
942 972 q_ptr->setTitlePen(Qt::NoPen); // NoPen for performance reasons
943 973 if (forced || QChartPrivate::defaultFont() == m_titleFont) {
944 974 QFont font(m_labelsFont);
945 975 font.setBold(true);
946 976 q_ptr->setTitleFont(font);
947 977 }
948 978
949 979 if (forced || QChartPrivate::defaultBrush() == m_shadesBrush)
950 980 q_ptr->setShadesBrush(theme->backgroundShadesBrush());
951 981 if (forced || QChartPrivate::defaultPen() == m_shadesPen)
952 982 q_ptr->setShadesPen(theme->backgroundShadesPen());
953 983
954 984 bool axisX = m_orientation == Qt::Horizontal;
955 985 if (forced && (theme->backgroundShades() == ChartTheme::BackgroundShadesBoth
956 986 || (theme->backgroundShades() == ChartTheme::BackgroundShadesVertical && axisX)
957 987 || (theme->backgroundShades() == ChartTheme::BackgroundShadesHorizontal && !axisX))) {
958 988 q_ptr->setShadesVisible(true);
959 989 } else if (forced) {
960 990 q_ptr->setShadesVisible(false);
961 991 }
962 992 }
963 993
964 994 void QAbstractAxisPrivate::handleRangeChanged(qreal min, qreal max)
965 995 {
966 996 setRange(min,max);
967 997 }
968 998
969 999 void QAbstractAxisPrivate::initializeGraphics(QGraphicsItem* parent)
970 1000 {
971 1001 Q_UNUSED(parent);
972 1002 }
973 1003
974 1004 void QAbstractAxisPrivate::initializeAnimations(QChart::AnimationOptions options)
975 1005 {
976 1006 ChartAxisElement *axis = m_item.data();
977 1007 Q_ASSERT(axis);
978 1008 if (options.testFlag(QChart::GridAxisAnimations)) {
979 1009 axis->setAnimation(new AxisAnimation(axis));
980 1010 } else {
981 1011 axis->setAnimation(0);
982 1012 }
983 1013 }
984 1014
985 1015
986 1016
987 1017 #include "moc_qabstractaxis.cpp"
988 1018 #include "moc_qabstractaxis_p.cpp"
989 1019
990 1020 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,899 +1,899
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 "qabstractbarseries.h"
22 22 #include "qabstractbarseries_p.h"
23 23 #include "qbarset.h"
24 24 #include "qbarset_p.h"
25 25 #include "abstractdomain_p.h"
26 26 #include "chartdataset_p.h"
27 27 #include "charttheme_p.h"
28 28 #include "qvalueaxis.h"
29 29 #include "qbarcategoryaxis.h"
30 30 #include "qbarlegendmarker.h"
31 31 #include "baranimation_p.h"
32 32 #include "abstractbarchartitem_p.h"
33 33 #include "qchart_p.h"
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 37 /*!
38 38 \class QAbstractBarSeries
39 39 \brief Series for creating a bar chart
40 40 \mainclass
41 41
42 42 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
43 43 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
44 44 and y-value is the height of the bar. The category names are ignored with this series and x-axis
45 45 shows the x-values.
46 46
47 47 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
48 48 \image examples_barchart.png
49 49
50 50 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
51 51 */
52 52 /*!
53 53 \qmlclass AbstractBarSeries QAbstractBarSeries
54 54 \inherits QAbstractSeries
55 55
56 56 The following QML shows how to create a simple bar chart:
57 57 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
58 58
59 59 \beginfloatleft
60 60 \image demos_qmlchart6.png
61 61 \endfloat
62 62 \clearfloat
63 63 */
64 64
65 65 /*!
66 66 \qmlproperty AbstractAxis AbstractBarSeries::axisX
67 67 The x axis used for the series. If you leave both axisX and axisXTop undefined, a BarCategoriesAxis is created for
68 68 the series.
69 69 \sa axisXTop
70 70 */
71 71
72 72 /*!
73 73 \qmlproperty AbstractAxis AbstractBarSeries::axisY
74 74 The y axis used for the series. If you leave both axisY and axisYRight undefined, a ValueAxis is created for
75 75 the series.
76 76 \sa axisYRight
77 77 */
78 78
79 79 /*!
80 80 \qmlproperty AbstractAxis AbstractBarSeries::axisXTop
81 81 The x axis used for the series, drawn on top of the chart view. Note that you can only provide either axisX or
82 82 axisXTop, but not both.
83 83 \sa axisX
84 84 */
85 85
86 86 /*!
87 87 \qmlproperty AbstractAxis AbstractBarSeries::axisYRight
88 88 The y axis used for the series, drawn to the right on the chart view. Note that you can only provide either axisY
89 89 or axisYRight, but not both.
90 90 \sa axisY
91 91 */
92 92
93 93 /*!
94 94 \property QAbstractBarSeries::barWidth
95 95 The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
96 96 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
97 97 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
98 98 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
99 99 \sa QBarSeries
100 100 */
101 101 /*!
102 102 \qmlproperty real AbstractBarSeries::barWidth
103 103 The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars
104 104 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
105 105 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
106 106 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
107 107 */
108 108
109 109 /*!
110 110 \property QAbstractBarSeries::count
111 111 Holds the number of sets in series.
112 112 */
113 113 /*!
114 114 \qmlproperty int AbstractBarSeries::count
115 115 Holds the number of sets in series.
116 116 */
117 117
118 118 /*!
119 119 \property QAbstractBarSeries::labelsVisible
120 120 Defines the visibility of the labels in series
121 121 */
122 122 /*!
123 123 \qmlproperty bool AbstractBarSeries::labelsVisible
124 124 Defines the visibility of the labels in series
125 125 */
126 126
127 127 /*!
128 128 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
129 129 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
130 130 Clicked bar inside set is indexed by \a index
131 131 */
132 132 /*!
133 133 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
134 134 The signal is emitted if the user clicks with a mouse on top of BarSet.
135 135 Clicked bar inside set is indexed by \a index
136 136 */
137 137
138 138 /*!
139 139 \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset)
140 140
141 141 The signal is emitted if mouse is hovered on top of series.
142 142 Parameter \a barset is the pointer of barset, where hover happened.
143 143 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
144 144 */
145 145 /*!
146 146 \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset)
147 147
148 148 The signal is emitted if mouse is hovered on top of series.
149 149 Parameter \a barset is the pointer of barset, where hover happened.
150 150 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
151 151 */
152 152
153 153 /*!
154 154 \fn void QAbstractBarSeries::countChanged()
155 155 This signal is emitted when barset count has been changed, for example by append or remove.
156 156 */
157 157 /*!
158 158 \qmlsignal AbstractBarSeries::onCountChanged()
159 159 This signal is emitted when barset count has been changed, for example by append or remove.
160 160 */
161 161
162 162 /*!
163 163 \fn void QAbstractBarSeries::labelsVisibleChanged()
164 164 This signal is emitted when labels visibility have changed.
165 165 \sa isLabelsVisible(), setLabelsVisible()
166 166 */
167 167
168 168 /*!
169 169 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
170 170 This signal is emitted when \a sets have been added to the series.
171 171 \sa append(), insert()
172 172 */
173 173 /*!
174 174 \qmlsignal AbstractBarSeries::onBarsetsAdded(BarSet barset)
175 175 Emitted when \a barset has been added to the series.
176 176 */
177 177
178 178 /*!
179 179 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
180 180 This signal is emitted when \a sets have been removed from the series.
181 181 \sa remove()
182 182 */
183 183 /*!
184 184 \qmlsignal AbstractBarSeries::onBarsetsRemoved(BarSet barset)
185 185 Emitted when \a barset has been removed from the series.
186 186 */
187 187
188 188 /*!
189 189 \qmlmethod BarSet AbstractBarSeries::at(int index)
190 190 Returns bar set at \a index. Returns null if the index is not valid.
191 191 */
192 192
193 193 /*!
194 194 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
195 195 Adds a new bar set with \a label and \a values to \a index. Values is a list of reals.
196 196 For example:
197 197 \code
198 198 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
199 199 \endcode
200 200 */
201 201
202 202 /*!
203 203 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
204 204 Inserts a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
205 205 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
206 206 appended.
207 207 \sa AbstractBarSeries::append()
208 208 */
209 209
210 210 /*!
211 211 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
212 212 Removes the barset from the series. Returns true if successful, false otherwise.
213 213 */
214 214
215 215 /*!
216 216 \qmlmethod AbstractBarSeries::clear()
217 217 Removes all barsets from the series.
218 218 */
219 219
220 220 /*!
221 221 Destructs abstractbarseries and owned barsets.
222 222 */
223 223 QAbstractBarSeries::~QAbstractBarSeries()
224 224 {
225 225
226 226 }
227 227
228 228 /*!
229 229 \internal
230 230 */
231 231 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent)
232 232 : QAbstractSeries(o, parent)
233 233 {
234 234 Q_D(QAbstractSeries);
235 235 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
236 236 }
237 237
238 238 /*!
239 239 Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
240 240 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
241 241 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
242 242 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
243 243 */
244 244 void QAbstractBarSeries::setBarWidth(qreal width)
245 245 {
246 246 Q_D(QAbstractBarSeries);
247 247 d->setBarWidth(width);
248 248 }
249 249
250 250 /*!
251 251 Returns the width of the bars of the series.
252 252 \sa setBarWidth()
253 253 */
254 254 qreal QAbstractBarSeries::barWidth() const
255 255 {
256 256 Q_D(const QAbstractBarSeries);
257 257 return d->barWidth();
258 258 }
259 259
260 260 /*!
261 261 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
262 262 Returns true, if appending succeeded.
263 263 */
264 264 bool QAbstractBarSeries::append(QBarSet *set)
265 265 {
266 266 Q_D(QAbstractBarSeries);
267 267 bool success = d->append(set);
268 268 if (success) {
269 269 QList<QBarSet *> sets;
270 270 sets.append(set);
271 271 set->setParent(this);
272 272 emit barsetsAdded(sets);
273 273 emit countChanged();
274 274 }
275 275 return success;
276 276 }
277 277
278 278 /*!
279 279 Removes barset from series. Releases ownership of \a set. Deletes the set, if remove
280 280 was successful.
281 281 Returns true, if set was removed.
282 282 */
283 283 bool QAbstractBarSeries::remove(QBarSet *set)
284 284 {
285 285 Q_D(QAbstractBarSeries);
286 286 bool success = d->remove(set);
287 287 if (success) {
288 288 QList<QBarSet *> sets;
289 289 sets.append(set);
290 290 set->setParent(0);
291 291 emit barsetsRemoved(sets);
292 292 emit countChanged();
293 293 delete set;
294 294 set = 0;
295 295 }
296 296 return success;
297 297 }
298 298
299 299 /*!
300 300 Takes a single \a set from the series. Does not delete the barset object.
301 301
302 302 NOTE: The series remains as the barset's parent object. You must set the
303 303 parent object to take full ownership.
304 304
305 305 Returns true if take was successful.
306 306 */
307 307 bool QAbstractBarSeries::take(QBarSet *set)
308 308 {
309 309 Q_D(QAbstractBarSeries);
310 310 bool success = d->remove(set);
311 311 if (success) {
312 312 QList<QBarSet *> sets;
313 313 sets.append(set);
314 314 emit barsetsRemoved(sets);
315 315 emit countChanged();
316 316 }
317 317 return success;
318 318 }
319 319
320 320 /*!
321 321 Adds a list of barsets to series. Takes ownership of \a sets.
322 322 Returns true, if all sets were appended successfully. If any of the sets is null or is already appended to series,
323 323 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
324 324 and function returns false.
325 325 */
326 326 bool QAbstractBarSeries::append(QList<QBarSet *> sets)
327 327 {
328 328 Q_D(QAbstractBarSeries);
329 329 bool success = d->append(sets);
330 330 if (success) {
331 331 emit barsetsAdded(sets);
332 332 emit countChanged();
333 333 }
334 334 return success;
335 335 }
336 336
337 337 /*!
338 338 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
339 339 Returns true, if inserting succeeded.
340 340
341 341 */
342 342 bool QAbstractBarSeries::insert(int index, QBarSet *set)
343 343 {
344 344 Q_D(QAbstractBarSeries);
345 345 bool success = d->insert(index, set);
346 346 if (success) {
347 347 QList<QBarSet *> sets;
348 348 sets.append(set);
349 349 emit barsetsAdded(sets);
350 350 emit countChanged();
351 351 }
352 352 return success;
353 353 }
354 354
355 355 /*!
356 356 Removes all barsets from the series. Deletes removed sets.
357 357 */
358 358 void QAbstractBarSeries::clear()
359 359 {
360 360 Q_D(QAbstractBarSeries);
361 361 QList<QBarSet *> sets = barSets();
362 362 bool success = d->remove(sets);
363 363 if (success) {
364 364 emit barsetsRemoved(sets);
365 365 emit countChanged();
366 366 foreach (QBarSet *set, sets)
367 367 delete set;
368 368 }
369 369 }
370 370
371 371 /*!
372 372 Returns number of sets in series.
373 373 */
374 374 int QAbstractBarSeries::count() const
375 375 {
376 376 Q_D(const QAbstractBarSeries);
377 377 return d->m_barSets.count();
378 378 }
379 379
380 380 /*!
381 381 Returns a list of sets in series. Keeps ownership of sets.
382 382 */
383 383 QList<QBarSet *> QAbstractBarSeries::barSets() const
384 384 {
385 385 Q_D(const QAbstractBarSeries);
386 386 return d->m_barSets;
387 387 }
388 388
389 389 /*!
390 390 Sets the visibility of labels in series to \a visible
391 391 */
392 392 void QAbstractBarSeries::setLabelsVisible(bool visible)
393 393 {
394 394 Q_D(QAbstractBarSeries);
395 395 if (d->m_labelsVisible != visible) {
396 396 d->setLabelsVisible(visible);
397 397 emit labelsVisibleChanged();
398 398 }
399 399 }
400 400
401 401 /*!
402 402 Returns the visibility of labels
403 403 */
404 404 bool QAbstractBarSeries::isLabelsVisible() const
405 405 {
406 406 Q_D(const QAbstractBarSeries);
407 407 return d->m_labelsVisible;
408 408 }
409 409
410 410 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
411 411
412 412 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
413 413 QAbstractSeriesPrivate(q),
414 414 m_barWidth(0.5), // Default value is 50% of category width
415 415 m_labelsVisible(false),
416 416 m_visible(true)
417 417 {
418 418 }
419 419
420 420 int QAbstractBarSeriesPrivate::categoryCount() const
421 421 {
422 422 // No categories defined. return count of longest set.
423 423 int count = 0;
424 424 for (int i = 0; i < m_barSets.count(); i++) {
425 425 if (m_barSets.at(i)->count() > count)
426 426 count = m_barSets.at(i)->count();
427 427 }
428 428
429 429 return count;
430 430 }
431 431
432 432 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
433 433 {
434 434 if (width < 0.0)
435 435 width = 0.0;
436 436 m_barWidth = width;
437 437 emit updatedLayout();
438 438 }
439 439
440 440 qreal QAbstractBarSeriesPrivate::barWidth() const
441 441 {
442 442 return m_barWidth;
443 443 }
444 444
445 445 QBarSet *QAbstractBarSeriesPrivate::barsetAt(int index)
446 446 {
447 447 return m_barSets.at(index);
448 448 }
449 449
450 450 void QAbstractBarSeriesPrivate::setVisible(bool visible)
451 451 {
452 452 m_visible = visible;
453 453 emit visibleChanged();
454 454 }
455 455
456 456 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
457 457 {
458 458 m_labelsVisible = visible;
459 459 emit labelsVisibleChanged(visible);
460 460 }
461 461
462 462 qreal QAbstractBarSeriesPrivate::min()
463 463 {
464 464 if (m_barSets.count() <= 0)
465 465 return 0;
466 466
467 467 qreal min = INT_MAX;
468 468
469 469 for (int i = 0; i < m_barSets.count(); i++) {
470 470 int categoryCount = m_barSets.at(i)->count();
471 471 for (int j = 0; j < categoryCount; j++) {
472 472 qreal temp = m_barSets.at(i)->at(j);
473 473 if (temp < min)
474 474 min = temp;
475 475 }
476 476 }
477 477 return min;
478 478 }
479 479
480 480 qreal QAbstractBarSeriesPrivate::max()
481 481 {
482 482 if (m_barSets.count() <= 0)
483 483 return 0;
484 484
485 485 qreal max = INT_MIN;
486 486
487 487 for (int i = 0; i < m_barSets.count(); i++) {
488 488 int categoryCount = m_barSets.at(i)->count();
489 489 for (int j = 0; j < categoryCount; j++) {
490 490 qreal temp = m_barSets.at(i)->at(j);
491 491 if (temp > max)
492 492 max = temp;
493 493 }
494 494 }
495 495
496 496 return max;
497 497 }
498 498
499 499 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
500 500 {
501 501 if ((set < 0) || (set >= m_barSets.count()))
502 502 return 0; // No set, no value.
503 503 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
504 504 return 0; // No category, no value.
505 505
506 506 return m_barSets.at(set)->at(category);
507 507 }
508 508
509 509 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
510 510 {
511 511 if ((set < 0) || (set >= m_barSets.count()))
512 512 return 0; // No set, no value.
513 513 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
514 514 return 0; // No category, no value.
515 515
516 516 qreal value = m_barSets.at(set)->at(category);
517 517 qreal sum = categorySum(category);
518 518 if (qFuzzyCompare(sum, 0))
519 519 return 0;
520 520
521 521 return value / sum;
522 522 }
523 523
524 524 qreal QAbstractBarSeriesPrivate::categorySum(int category)
525 525 {
526 526 qreal sum(0);
527 527 int count = m_barSets.count(); // Count sets
528 528 for (int set = 0; set < count; set++) {
529 529 if (category < m_barSets.at(set)->count())
530 530 sum += m_barSets.at(set)->at(category);
531 531 }
532 532 return sum;
533 533 }
534 534
535 535 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
536 536 {
537 537 qreal sum(0);
538 538 int count = m_barSets.count(); // Count sets
539 539 for (int set = 0; set < count; set++) {
540 540 if (category < m_barSets.at(set)->count())
541 541 sum += qAbs(m_barSets.at(set)->at(category));
542 542 }
543 543 return sum;
544 544 }
545 545
546 546 qreal QAbstractBarSeriesPrivate::maxCategorySum()
547 547 {
548 548 qreal max = INT_MIN;
549 549 int count = categoryCount();
550 550 for (int i = 0; i < count; i++) {
551 551 qreal sum = categorySum(i);
552 552 if (sum > max)
553 553 max = sum;
554 554 }
555 555 return max;
556 556 }
557 557
558 558 qreal QAbstractBarSeriesPrivate::minX()
559 559 {
560 560 if (m_barSets.count() <= 0)
561 561 return 0;
562 562
563 563 qreal min = INT_MAX;
564 564
565 565 for (int i = 0; i < m_barSets.count(); i++) {
566 566 int categoryCount = m_barSets.at(i)->count();
567 567 for (int j = 0; j < categoryCount; j++) {
568 568 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
569 569 if (temp < min)
570 570 min = temp;
571 571 }
572 572 }
573 573 return min;
574 574 }
575 575
576 576 qreal QAbstractBarSeriesPrivate::maxX()
577 577 {
578 578 if (m_barSets.count() <= 0)
579 579 return 0;
580 580
581 581 qreal max = INT_MIN;
582 582
583 583 for (int i = 0; i < m_barSets.count(); i++) {
584 584 int categoryCount = m_barSets.at(i)->count();
585 585 for (int j = 0; j < categoryCount; j++) {
586 586 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
587 587 if (temp > max)
588 588 max = temp;
589 589 }
590 590 }
591 591
592 592 return max;
593 593 }
594 594
595 595 qreal QAbstractBarSeriesPrivate::categoryTop(int category)
596 596 {
597 597 // Returns top (sum of all positive values) of category.
598 598 // Returns 0, if all values are negative
599 599 qreal top(0);
600 600 int count = m_barSets.count();
601 601 for (int set = 0; set < count; set++) {
602 602 if (category < m_barSets.at(set)->count()) {
603 603 qreal temp = m_barSets.at(set)->at(category);
604 604 if (temp > 0) {
605 605 top += temp;
606 606 }
607 607 }
608 608 }
609 609 return top;
610 610 }
611 611
612 612 qreal QAbstractBarSeriesPrivate::categoryBottom(int category)
613 613 {
614 614 // Returns bottom (sum of all negative values) of category
615 615 // Returns 0, if all values are positive
616 616 qreal bottom(0);
617 617 int count = m_barSets.count();
618 618 for (int set = 0; set < count; set++) {
619 619 if (category < m_barSets.at(set)->count()) {
620 620 qreal temp = m_barSets.at(set)->at(category);
621 621 if (temp < 0) {
622 622 bottom += temp;
623 623 }
624 624 }
625 625 }
626 626 return bottom;
627 627 }
628 628
629 629 qreal QAbstractBarSeriesPrivate::top()
630 630 {
631 631 // Returns top of all categories
632 632 qreal top(0);
633 633 int count = categoryCount();
634 634 for (int i = 0; i < count; i++) {
635 635 qreal temp = categoryTop(i);
636 636 if (temp > top)
637 637 top = temp;
638 638 }
639 639 return top;
640 640 }
641 641
642 642 qreal QAbstractBarSeriesPrivate::bottom()
643 643 {
644 644 // Returns bottom of all categories
645 645 qreal bottom(0);
646 646 int count = categoryCount();
647 647 for (int i = 0; i < count; i++) {
648 648 qreal temp = categoryBottom(i);
649 649 if (temp < bottom)
650 650 bottom = temp;
651 651 }
652 652 return bottom;
653 653 }
654 654
655 655
656 656 void QAbstractBarSeriesPrivate::initializeDomain()
657 657 {
658 658 qreal minX(domain()->minX());
659 659 qreal minY(domain()->minY());
660 660 qreal maxX(domain()->maxX());
661 661 qreal maxY(domain()->maxY());
662 662
663 663 qreal seriesMinX = this->minX();
664 664 qreal seriesMaxX = this->maxX();
665 665 qreal y = max();
666 666 minX = qMin(minX, seriesMinX - (qreal)0.5);
667 667 minY = qMin(minY, y);
668 668 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
669 669 maxY = qMax(maxY, y);
670 670
671 671 domain()->setRange(minX, maxX, minY, maxY);
672 672 }
673 673
674 674 QList<QLegendMarker*> QAbstractBarSeriesPrivate::createLegendMarkers(QLegend* legend)
675 675 {
676 676 Q_Q(QAbstractBarSeries);
677 677 QList<QLegendMarker*> markers;
678 678
679 679 foreach(QBarSet* set, q->barSets()) {
680 680 QBarLegendMarker* marker = new QBarLegendMarker(q,set,legend);
681 681 markers << marker;
682 682 }
683 683 return markers;
684 684 }
685 685
686 686
687 687 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
688 688 {
689 689 if ((m_barSets.contains(set)) || (set == 0))
690 690 return false; // Fail if set is already in list or set is null.
691 691
692 692 m_barSets.append(set);
693 693 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
694 694 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
695 695 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
696 696
697 697 emit restructuredBars(); // this notifies barchartitem
698 698 return true;
699 699 }
700 700
701 701 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
702 702 {
703 703 if (!m_barSets.contains(set))
704 704 return false; // Fail if set is not in list
705 705
706 706 m_barSets.removeOne(set);
707 707 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
708 708 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
709 709 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
710 710
711 711 emit restructuredBars(); // this notifies barchartitem
712 712 return true;
713 713 }
714 714
715 715 bool QAbstractBarSeriesPrivate::append(QList<QBarSet * > sets)
716 716 {
717 717 foreach (QBarSet *set, sets) {
718 718 if ((set == 0) || (m_barSets.contains(set)))
719 719 return false; // Fail if any of the sets is null or is already appended.
720 720 if (sets.count(set) != 1)
721 721 return false; // Also fail if same set is more than once in given list.
722 722 }
723 723
724 724 foreach (QBarSet *set, sets) {
725 725 m_barSets.append(set);
726 726 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
727 727 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
728 728 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
729 729 }
730 730
731 731 emit restructuredBars(); // this notifies barchartitem
732 732 return true;
733 733 }
734 734
735 735 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet * > sets)
736 736 {
737 737 if (sets.count() == 0)
738 738 return false;
739 739
740 740 foreach (QBarSet *set, sets) {
741 741 if ((set == 0) || (!m_barSets.contains(set)))
742 742 return false; // Fail if any of the sets is null or is not in series
743 743 if (sets.count(set) != 1)
744 744 return false; // Also fail if same set is more than once in given list.
745 745 }
746 746
747 747 foreach (QBarSet *set, sets) {
748 748 m_barSets.removeOne(set);
749 749 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
750 750 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
751 751 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
752 752 }
753 753
754 754 emit restructuredBars(); // this notifies barchartitem
755 755
756 756 return true;
757 757 }
758 758
759 759 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
760 760 {
761 761 if ((m_barSets.contains(set)) || (set == 0))
762 762 return false; // Fail if set is already in list or set is null.
763 763
764 764 m_barSets.insert(index, set);
765 765 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
766 766 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
767 767 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
768 768
769 769 emit restructuredBars(); // this notifies barchartitem
770 770 return true;
771 771 }
772 772
773 773 void QAbstractBarSeriesPrivate::initializeAxes()
774 774 {
775 775 Q_Q(QAbstractBarSeries);
776 776
777 777 foreach(QAbstractAxis* axis, m_axes) {
778 778
779 779 if (axis->type() == QAbstractAxis::AxisTypeBarCategory) {
780 780 switch (q->type()) {
781 781 case QAbstractSeries::SeriesTypeHorizontalBar:
782 782 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
783 783 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
784 784 if (axis->orientation() == Qt::Vertical)
785 785 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
786 786 break;
787 787 case QAbstractSeries::SeriesTypeBar:
788 788 case QAbstractSeries::SeriesTypePercentBar:
789 789 case QAbstractSeries::SeriesTypeStackedBar:
790 790 if (axis->orientation() == Qt::Horizontal)
791 791 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
792 792 break;
793 793 default:
794 794 qWarning() << "Unexpected series type";
795 795 break;
796 796 }
797 797 }
798 798 }
799 799 }
800 800
801 801 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
802 802 {
803 803 Q_Q(const QAbstractBarSeries);
804 804
805 805 switch (q->type()) {
806 806 case QAbstractSeries::SeriesTypeHorizontalBar:
807 807 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
808 808 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
809 809 if (orientation == Qt::Vertical)
810 810 return QAbstractAxis::AxisTypeBarCategory;
811 811 break;
812 812 case QAbstractSeries::SeriesTypeBar:
813 813 case QAbstractSeries::SeriesTypePercentBar:
814 814 case QAbstractSeries::SeriesTypeStackedBar:
815 815 if (orientation == Qt::Horizontal)
816 816 return QAbstractAxis::AxisTypeBarCategory;
817 817 break;
818 818 default:
819 819 qWarning() << "Unexpected series type";
820 820 break;
821 821 }
822 822 return QAbstractAxis::AxisTypeValue;
823 823
824 824 }
825 825
826 826 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis *axis)
827 827 {
828 828 QStringList categories;
829 829 if (axis->categories().isEmpty()) {
830 830 for (int i(1); i < categoryCount() + 1; i++)
831 831 categories << QString::number(i);
832 832 axis->append(categories);
833 833 }
834 834 }
835 835
836 836 QAbstractAxis* QAbstractBarSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
837 837 {
838 838 Q_UNUSED(orientation);
839 839 return 0;
840 840 }
841 841
842 842 void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
843 843 {
844 844 const QList<QGradient> gradients = theme->seriesGradients();
845 845
846 846 qreal takeAtPos = 0.5;
847 847 qreal step = 0.2;
848 848 if (m_barSets.count() > 1) {
849 849 step = 1.0 / (qreal) m_barSets.count();
850 850 if (m_barSets.count() % gradients.count())
851 851 step *= gradients.count();
852 852 else
853 853 step *= (gradients.count() - 1);
854 854 }
855 855
856 856 for (int i(0); i < m_barSets.count(); i++) {
857 857 int colorIndex = (index + i) % gradients.count();
858 858 if (i > 0 && i %gradients.count() == 0) {
859 859 // There is no dedicated base color for each sets, generate more colors
860 860 takeAtPos += step;
861 861 if (takeAtPos == 1.0)
862 862 takeAtPos += step;
863 863 takeAtPos -= (int) takeAtPos;
864 864 }
865 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->brush())
865 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->d_ptr->m_brush)
866 866 m_barSets.at(i)->setBrush(ChartThemeManager::colorAt(gradients.at(colorIndex), takeAtPos));
867 867
868 868 // Pick label color from the opposite end of the gradient.
869 869 // 0.3 as a boundary seems to work well.
870 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->labelBrush()) {
870 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->d_ptr->m_labelBrush) {
871 871 if (takeAtPos < 0.3)
872 872 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1));
873 873 else
874 874 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0));
875 875 }
876 876
877 if (forced || QChartPrivate::defaultPen() == m_barSets.at(i)->pen()) {
877 if (forced || QChartPrivate::defaultPen() == m_barSets.at(i)->d_ptr->m_pen) {
878 878 QColor c = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0);
879 879 m_barSets.at(i)->setPen(c);
880 880 }
881 881 }
882 882 }
883 883
884 884 void QAbstractBarSeriesPrivate::initializeAnimations(QChart::AnimationOptions options)
885 885 {
886 886 AbstractBarChartItem *bar = static_cast<AbstractBarChartItem *>(m_item.data());
887 887 Q_ASSERT(bar);
888 888 if (options.testFlag(QChart::SeriesAnimations)) {
889 889 bar->setAnimation(new BarAnimation(bar));
890 890 }else{
891 891 bar->setAnimation(0);
892 892 }
893 893 QAbstractSeriesPrivate::initializeAnimations(options);
894 894 }
895 895
896 896 #include "moc_qabstractbarseries.cpp"
897 897 #include "moc_qabstractbarseries_p.cpp"
898 898
899 899 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,83 +1,84
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 #ifndef QABSTRACTBARSERIES_H
22 22 #define QABSTRACTBARSERIES_H
23 23
24 24 #include <qabstractseries.h>
25 25 #include <QStringList>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 class QBarSet;
30 30 class QAbstractBarSeriesPrivate;
31 31
32 32 // Container for series
33 33 class QTCOMMERCIALCHART_EXPORT QAbstractBarSeries : public QAbstractSeries
34 34 {
35 35 Q_OBJECT
36 36 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth)
37 37 Q_PROPERTY(int count READ count NOTIFY countChanged)
38 38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
39 39
40 40 public:
41 41 virtual ~QAbstractBarSeries();
42 42
43 43 void setBarWidth(qreal width);
44 44 qreal barWidth() const;
45 45
46 46 bool append(QBarSet *set);
47 47 bool remove(QBarSet *set);
48 48 bool take(QBarSet *set);
49 49 bool append(QList<QBarSet *> sets);
50 50 bool insert(int index, QBarSet *set);
51 51 int count() const;
52 52 QList<QBarSet *> barSets() const;
53 53 void clear();
54 54
55 55 void setLabelsVisible(bool visible = true);
56 56 bool isLabelsVisible() const;
57 57
58 58 protected:
59 59 explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent = 0);
60 60
61 61 Q_SIGNALS:
62 62 void clicked(int index, QBarSet *barset);
63 63 void hovered(bool status, QBarSet *barset);
64 64 void countChanged();
65 65 void labelsVisibleChanged();
66 66
67 67 void barsetsAdded(QList<QBarSet *> sets);
68 68 void barsetsRemoved(QList<QBarSet *> sets);
69 69
70 70 protected:
71 71 Q_DECLARE_PRIVATE(QAbstractBarSeries)
72 72 friend class AbstractBarChartItem;
73 73 friend class PercentBarChartItem;
74 74 friend class StackedBarChartItem;
75 75 friend class BarChartItem;
76 76 friend class HorizontalBarChartItem;
77 77 friend class HorizontalStackedBarChartItem;
78 78 friend class HorizontalPercentBarChartItem;
79 friend class BarSet;
79 80 };
80 81
81 82 QTCOMMERCIALCHART_END_NAMESPACE
82 83
83 84 #endif // QABSTRACTBARSERIES_H
@@ -1,649 +1,658
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 "qbarset.h"
22 22 #include "qbarset_p.h"
23 23 #include "charthelpers_p.h"
24 24 #include "qchart_p.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 /*!
29 29 \class QBarSet
30 30 \brief Building block for different bar charts
31 31
32 32 QBarSet represents one set of bars. Set of bars contains one data value for each category.
33 33 First value of set is assumed to belong to first category, second to second category and so on.
34 34 If set has fewer values than there are categories, then the missing values are assumed to be
35 35 at the end of set. For missing values in middle of a set, numerical value of zero is used.
36 36
37 37 \mainclass
38 38
39 39 \sa QAbstractBarSeries, QBarSeries, QStackedBarSeries, QPercentBarSeries
40 40 */
41 41 /*!
42 42 \qmlclass BarSet QBarSet
43 43
44 44 BarSet represents one set of bars. Set of bars contains one data value for each category.
45 45 First value of set is assumed to belong to first category, second to second category and so on.
46 46 If set has fewer values than there are categories, then the missing values are assumed to be
47 47 at the end of set. For missing values in middle of a set, numerical value of zero is used.
48 48 \sa AbstractBarSeries, BarSeries, StackedBarSeries, PercentBarSeries
49 49 */
50 50
51 51 /*!
52 52 \property QBarSet::label
53 53 Defines the label of the barSet.
54 54 */
55 55 /*!
56 56 \qmlproperty string BarSet::label
57 57 Defines the label of the barSet.
58 58 */
59 59
60 60 /*!
61 61 \property QBarSet::pen
62 62 \brief Defines the pen used by the barSet.
63 63 */
64 64
65 65 /*!
66 66 \property QBarSet::brush
67 67 \brief Defines the brush used by the barSet.
68 68 */
69 69
70 70 /*!
71 71 \property QBarSet::labelBrush
72 72 \brief Defines the brush used by the barSet's label.
73 73 */
74 74
75 75 /*!
76 76 \property QBarSet::labelFont
77 77 \brief Defines the font used by the barSet's label.
78 78 */
79 79
80 80 /*!
81 81 \qmlproperty Font BarSet::labelFont
82 82 Defines the font used by the barSet's label.
83 83
84 84 See the \l {Font} {QML Font Element} for detailed documentation.
85 85 */
86 86
87 87 /*!
88 88 \property QBarSet::color
89 89 The fill (brush) color of the bar set.
90 90 */
91 91 /*!
92 92 \qmlproperty color BarSet::color
93 93 The fill (brush) color of the bar set.
94 94 */
95 95
96 96 /*!
97 97 \property QBarSet::borderColor
98 98 The line (pen) color of the bar set.
99 99 */
100 100 /*!
101 101 \qmlproperty color BarSet::borderColor
102 102 The line (pen) color of the bar set.
103 103 */
104 104
105 105 /*!
106 106 \qmlproperty real BarSet::borderWidth
107 107 The width of the border line. By default the width is 2.0.
108 108 */
109 109
110 110 /*!
111 111 \property QBarSet::labelColor
112 112 The text (label) color of the bar set.
113 113 */
114 114 /*!
115 115 \qmlproperty color BarSet::labelColor
116 116 The text (label) color of the bar set.
117 117 */
118 118
119 119 /*!
120 120 \fn void QBarSet::clicked(int index)
121 121
122 122 The signal is emitted if the user clicks with a mouse on top of barset.
123 123 Clicked bar inside set is indexed by \a index
124 124 */
125 125
126 126 /*!
127 127 \fn void QBarSet::hovered(bool status)
128 128
129 129 The signal is emitted if mouse is hovered on top of barset.
130 130 Parameter \a status is true, if mouse entered on top of barset, false if mouse left from top of barset.
131 131 */
132 132
133 133
134 134 /*!
135 135 \fn void QBarSet::labelChanged()
136 136 This signal is emitted when the label of the barSet has changed.
137 137 \sa label
138 138 */
139 139 /*!
140 140 \qmlsignal BarSet::onLabelChanged()
141 141 This signal is emitted when the label of the barSet has changed.
142 142 */
143 143
144 144 /*!
145 145 \fn void QBarSet::penChanged()
146 146 This signal is emitted when the pen of the barSet has changed.
147 147 \sa pen
148 148 */
149 149
150 150 /*!
151 151 \fn void QBarSet::brushChanged()
152 152 This signal is emitted when the brush of the barSet has changed.
153 153 \sa brush
154 154 */
155 155
156 156 /*!
157 157 \fn void QBarSet::labelBrushChanged()
158 158 This signal is emitted when the brush of the barSet's label has changed.
159 159 \sa labelBrush
160 160 */
161 161
162 162 /*!
163 163 \fn void QBarSet::labelFontChanged()
164 164 This signal is emitted when the font of the barSet's label has changed.
165 165 \sa labelBrush
166 166 */
167 167
168 168 /*!
169 169 \fn void QBarSet::colorChanged(QColor)
170 170 This signal is emitted when the fill (brush) color of the set has changed to \a color.
171 171 */
172 172 /*!
173 173 \qmlsignal BarSet::onColorChanged(color color)
174 174 This signal is emitted when the fill (brush) color of the set has changed to \a color.
175 175 */
176 176
177 177 /*!
178 178 \fn void QBarSet::borderColorChanged(QColor)
179 179 This signal is emitted when the line (pen) color of the set has changed to \a color.
180 180 */
181 181 /*!
182 182 \qmlsignal BarSet::onBorderColorChanged(color color)
183 183 This signal is emitted when the line (pen) color of the set has changed to \a color.
184 184 */
185 185
186 186 /*!
187 187 \fn void QBarSet::labelColorChanged(QColor)
188 188 This signal is emitted when the text (label) color of the set has changed to \a color.
189 189 */
190 190 /*!
191 191 \qmlsignal BarSet::onLabelColorChanged(color color)
192 192 This signal is emitted when the text (label) color of the set has changed to \a color.
193 193 */
194 194
195 195 /*!
196 196 \fn void QBarSet::valuesAdded(int index, int count)
197 197 This signal is emitted when new values have been added to the set.
198 198 Parameter \a index indicates the position of the first inserted value.
199 199 Parameter \a count is the number of inserted values.
200 200 \sa append(), insert()
201 201 */
202 202 /*!
203 203 \qmlsignal BarSet::onValuesAdded(int index, int count)
204 204 This signal is emitted when new values have been added to the set.
205 205 Parameter \a index indicates the position of the first inserted value.
206 206 Parameter \a count is the number of inserted values.
207 207 */
208 208
209 209 /*!
210 210 \fn void QBarSet::valuesRemoved(int index, int count)
211 211 This signal is emitted values have been removed from the set.
212 212 Parameter \a index indicates the position of the first removed value.
213 213 Parameter \a count is the number of removed values.
214 214 \sa remove()
215 215 */
216 216 /*!
217 217 \qmlsignal BarSet::onValuesRemoved(int index, int count)
218 218 This signal is emitted values have been removed from the set.
219 219 Parameter \a index indicates the position of the first removed value.
220 220 Parameter \a count is the number of removed values.
221 221 */
222 222
223 223 /*!
224 224 \fn void QBarSet::valueChanged(int index)
225 225 This signal is emitted values the value in the set has been modified.
226 226 Parameter \a index indicates the position of the modified value.
227 227 \sa at()
228 228 */
229 229 /*!
230 230 \qmlsignal BarSet::onValueChanged(int index)
231 231 This signal is emitted values the value in the set has been modified.
232 232 Parameter \a index indicates the position of the modified value.
233 233 */
234 234
235 235 /*!
236 236 \qmlproperty int BarSet::count
237 237 The count of values on the barset
238 238 */
239 239
240 240 /*!
241 241 \qmlproperty QVariantList BarSet::values
242 242 The values of the barset. You can set either a list of reals or a list of points as values. If you set a list of
243 243 reals as values, the values are automatically completed to points by using the index of a value as it's
244 244 x-coordinate. For example:
245 245 \code
246 246 myBarSet1.values = [0, 5, 1, 5];
247 247 myBarSet2.values = [Qt.point(0, 1), Qt.point(1, 5), Qt.point(2.2, 4.3)];
248 248 \endcode
249 249 */
250 250
251 251 /*!
252 252 Constructs QBarSet with a label of \a label and with parent of \a parent.
253 253 */
254 254 QBarSet::QBarSet(const QString label, QObject *parent)
255 255 : QObject(parent),
256 256 d_ptr(new QBarSetPrivate(label, this))
257 257 {
258 258 }
259 259
260 260 /*!
261 261 Destroys the barset.
262 262 */
263 263 QBarSet::~QBarSet()
264 264 {
265 265 // NOTE: d_ptr destroyed by QObject
266 266 }
267 267
268 268 /*!
269 269 Sets new \a label for set.
270 270 */
271 271 void QBarSet::setLabel(const QString label)
272 272 {
273 273 d_ptr->m_label = label;
274 274 emit labelChanged();
275 275 }
276 276
277 277 /*!
278 278 Returns label of the set.
279 279 */
280 280 QString QBarSet::label() const
281 281 {
282 282 return d_ptr->m_label;
283 283 }
284 284
285 285 /*!
286 286 Appends new value \a value to the end of set.
287 287 */
288 288 void QBarSet::append(const qreal value)
289 289 {
290 290 // Convert to QPointF
291 291 int index = d_ptr->m_values.count();
292 292 d_ptr->append(QPointF(d_ptr->m_values.count(), value));
293 293 emit valuesAdded(index, 1);
294 294 }
295 295
296 296 /*!
297 297 Appends a list of reals to set. Works like append with single real value. The \a values in list
298 298 are appended to end of barset
299 299 \sa append()
300 300 */
301 301 void QBarSet::append(const QList<qreal> &values)
302 302 {
303 303 int index = d_ptr->m_values.count();
304 304 d_ptr->append(values);
305 305 emit valuesAdded(index, values.count());
306 306 }
307 307
308 308 /*!
309 309 Convenience operator. Same as append, with real \a value.
310 310 \sa append()
311 311 */
312 312 QBarSet &QBarSet::operator << (const qreal &value)
313 313 {
314 314 append(value);
315 315 return *this;
316 316 }
317 317
318 318 /*!
319 319 Inserts new \a value on the \a index position.
320 320 The value that is currently at this postion is moved to position index + 1
321 321 \sa remove()
322 322 */
323 323 void QBarSet::insert(const int index, const qreal value)
324 324 {
325 325 d_ptr->insert(index, value);
326 326 emit valuesAdded(index, 1);
327 327 }
328 328
329 329 /*!
330 330 Removes \a count number of values from the set starting at \a index.
331 331 \sa insert()
332 332 */
333 333 void QBarSet::remove(const int index, const int count)
334 334 {
335 335 int removedCount = d_ptr->remove(index, count);
336 336 if (removedCount > 0)
337 337 emit valuesRemoved(index, removedCount);
338 338 return;
339 339 }
340 340
341 341 /*!
342 342 Sets a new value \a value to set, indexed by \a index.
343 343 */
344 344 void QBarSet::replace(const int index, const qreal value)
345 345 {
346 346 if (index >= 0 && index < d_ptr->m_values.count()) {
347 347 d_ptr->replace(index, value);
348 348 emit valueChanged(index);
349 349 }
350 350 }
351 351
352 352
353 353 /*!
354 354 Returns value of set indexed by \a index.
355 355 If the index is out of bounds 0.0 is returned.
356 356 */
357 357 qreal QBarSet::at(const int index) const
358 358 {
359 359 if (index < 0 || index >= d_ptr->m_values.count())
360 360 return 0;
361 361 return d_ptr->m_values.at(index).y();
362 362 }
363 363
364 364 /*!
365 365 Returns value of set indexed by \a index.
366 366 If the index is out of bounds 0.0 is returned.
367 367 */
368 368 qreal QBarSet::operator [](const int index) const
369 369 {
370 370 return at(index);
371 371 }
372 372
373 373 /*!
374 374 Returns count of values in set.
375 375 */
376 376 int QBarSet::count() const
377 377 {
378 378 return d_ptr->m_values.count();
379 379 }
380 380
381 381 /*!
382 382 Returns sum of all values in barset.
383 383 */
384 384 qreal QBarSet::sum() const
385 385 {
386 386 qreal total(0);
387 387 for (int i = 0; i < d_ptr->m_values.count(); i++)
388 388 total += d_ptr->m_values.at(i).y();
389 389 return total;
390 390 }
391 391
392 392 /*!
393 393 Sets pen for set. Bars of this set are drawn using \a pen
394 394 */
395 395 void QBarSet::setPen(const QPen &pen)
396 396 {
397 397 if (d_ptr->m_pen != pen) {
398 398 d_ptr->m_pen = pen;
399 399 emit d_ptr->updatedBars();
400 400 emit penChanged();
401 401 }
402 402 }
403 403
404 404 /*!
405 405 Returns pen of the set.
406 406 */
407 407 QPen QBarSet::pen() const
408 408 {
409 return d_ptr->m_pen;
409 if (d_ptr->m_pen == QChartPrivate::defaultPen())
410 return QPen();
411 else
412 return d_ptr->m_pen;
410 413 }
411 414
412 415 /*!
413 416 Sets brush for the set. Bars of this set are drawn using \a brush
414 417 */
415 418 void QBarSet::setBrush(const QBrush &brush)
416 419 {
417 420 if (d_ptr->m_brush != brush) {
418 421 d_ptr->m_brush = brush;
419 422 emit d_ptr->updatedBars();
420 423 emit brushChanged();
421 424 }
422 425 }
423 426
424 427 /*!
425 428 Returns brush of the set.
426 429 */
427 430 QBrush QBarSet::brush() const
428 431 {
429 return d_ptr->m_brush;
432 if (d_ptr->m_brush == QChartPrivate::defaultBrush())
433 return QBrush();
434 else
435 return d_ptr->m_brush;
430 436 }
431 437
432 438 /*!
433 439 Sets \a brush of the values that are drawn on top of this barset
434 440 */
435 441 void QBarSet::setLabelBrush(const QBrush &brush)
436 442 {
437 443 if (d_ptr->m_labelBrush != brush) {
438 444 d_ptr->m_labelBrush = brush;
439 445 emit d_ptr->updatedBars();
440 446 emit labelBrushChanged();
441 447 }
442 448 }
443 449
444 450 /*!
445 451 Returns brush of the values that are drawn on top of this barset
446 452 */
447 453 QBrush QBarSet::labelBrush() const
448 454 {
449 return d_ptr->m_labelBrush;
455 if (d_ptr->m_labelBrush == QChartPrivate::defaultBrush())
456 return QBrush();
457 else
458 return d_ptr->m_labelBrush;
450 459 }
451 460
452 461 /*!
453 462 Sets the \a font for values that are drawn on top of this barset
454 463 */
455 464 void QBarSet::setLabelFont(const QFont &font)
456 465 {
457 466 if (d_ptr->m_labelFont != font) {
458 467 d_ptr->m_labelFont = font;
459 468 emit d_ptr->updatedBars();
460 469 emit labelFontChanged();
461 470 }
462 471
463 472 }
464 473
465 474 /*!
466 475 Returns the pen for values that are drawn on top of this barset
467 476 */
468 477 QFont QBarSet::labelFont() const
469 478 {
470 479 return d_ptr->m_labelFont;
471 480 }
472 481
473 482 /*!
474 483 Returns the color of the brush of barset.
475 484 */
476 485 QColor QBarSet::color()
477 486 {
478 487 return brush().color();
479 488 }
480 489
481 490 /*!
482 491 Sets the \a color of brush for this barset
483 492 */
484 493 void QBarSet::setColor(QColor color)
485 494 {
486 495 QBrush b = brush();
487 496 if ((b.color() != color) || (b.style() == Qt::NoBrush)) {
488 497 b.setColor(color);
489 498 if (b.style() == Qt::NoBrush) {
490 499 // Set tyle to Qt::SolidPattern. (Default is Qt::NoBrush)
491 500 // This prevents theme to override color defined in QML side:
492 501 // BarSet { label: "Bob"; color:"red"; values: [1,2,3] }
493 502 // The color must be obeyed, since user wanted it.
494 503 b.setStyle(Qt::SolidPattern);
495 504 }
496 505 setBrush(b);
497 506 emit colorChanged(color);
498 507 }
499 508 }
500 509
501 510 /*!
502 511 Returns the color of pen of this barset
503 512 */
504 513 QColor QBarSet::borderColor()
505 514 {
506 515 return pen().color();
507 516 }
508 517
509 518 /*!
510 519 Sets the color of pen for this barset
511 520 */
512 521 void QBarSet::setBorderColor(QColor color)
513 522 {
514 523 QPen p = pen();
515 524 if (p.color() != color) {
516 525 p.setColor(color);
517 526 setPen(p);
518 527 emit borderColorChanged(color);
519 528 }
520 529 }
521 530
522 531 /*!
523 532 Returns the color of labels of this barset
524 533 */
525 534 QColor QBarSet::labelColor()
526 535 {
527 536 return labelBrush().color();
528 537 }
529 538
530 539 /*!
531 540 Sets the color of labels for this barset
532 541 */
533 542 void QBarSet::setLabelColor(QColor color)
534 543 {
535 544 QBrush b = labelBrush();
536 545 if (b == QBrush())
537 546 b.setStyle(Qt::SolidPattern);
538 547
539 548 if (b.color() != color) {
540 549 b.setColor(color);
541 550 setLabelBrush(b);
542 551 emit labelColorChanged(color);
543 552 }
544 553 }
545 554
546 555 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
547 556
548 557 QBarSetPrivate::QBarSetPrivate(const QString label, QBarSet *parent) : QObject(parent),
549 558 q_ptr(parent),
550 559 m_label(label),
551 560 m_pen(QChartPrivate::defaultPen()),
552 561 m_brush(QChartPrivate::defaultBrush()),
553 562 m_labelBrush(QChartPrivate::defaultBrush())
554 563 {
555 564 }
556 565
557 566 QBarSetPrivate::~QBarSetPrivate()
558 567 {
559 568 }
560 569
561 570 void QBarSetPrivate::append(QPointF value)
562 571 {
563 572 if (isValidValue(value)) {
564 573 m_values.append(value);
565 574 emit restructuredBars();
566 575 }
567 576 }
568 577
569 578 void QBarSetPrivate::append(QList<QPointF> values)
570 579 {
571 580 for (int i = 0; i < values.count(); i++) {
572 581 if (isValidValue(values.at(i)))
573 582 m_values.append(values.at(i));
574 583 }
575 584 emit restructuredBars();
576 585 }
577 586
578 587 void QBarSetPrivate::append(QList<qreal> values)
579 588 {
580 589 int index = m_values.count();
581 590 for (int i = 0; i < values.count(); i++) {
582 591 if (isValidValue(values.at(i))) {
583 592 m_values.append(QPointF(index, values.at(i)));
584 593 index++;
585 594 }
586 595 }
587 596 emit restructuredBars();
588 597 }
589 598
590 599 void QBarSetPrivate::insert(const int index, const qreal value)
591 600 {
592 601 m_values.insert(index, QPointF(index, value));
593 602 emit restructuredBars();
594 603 }
595 604
596 605 void QBarSetPrivate::insert(const int index, const QPointF value)
597 606 {
598 607 m_values.insert(index, value);
599 608 emit restructuredBars();
600 609 }
601 610
602 611 int QBarSetPrivate::remove(const int index, const int count)
603 612 {
604 613 int removeCount = count;
605 614
606 615 if ((index < 0) || (m_values.count() == 0))
607 616 return 0; // Invalid index or not values in list, remove nothing.
608 617 else if ((index + count) > m_values.count())
609 618 removeCount = m_values.count() - index; // Trying to remove more items than list has. Limit amount to be removed.
610 619
611 620 int c = 0;
612 621 while (c < removeCount) {
613 622 m_values.removeAt(index);
614 623 c++;
615 624 }
616 625 emit restructuredBars();
617 626 return removeCount;
618 627 }
619 628
620 629 void QBarSetPrivate::replace(const int index, const qreal value)
621 630 {
622 631 m_values.replace(index, QPointF(index, value));
623 632 emit updatedLayout();
624 633 }
625 634
626 635 void QBarSetPrivate::replace(const int index, const QPointF value)
627 636 {
628 637 m_values.replace(index, value);
629 638 emit updatedLayout();
630 639 }
631 640
632 641 qreal QBarSetPrivate::pos(const int index)
633 642 {
634 643 if (index < 0 || index >= m_values.count())
635 644 return 0;
636 645 return m_values.at(index).x();
637 646 }
638 647
639 648 qreal QBarSetPrivate::value(const int index)
640 649 {
641 650 if (index < 0 || index >= m_values.count())
642 651 return 0;
643 652 return m_values.at(index).y();
644 653 }
645 654
646 655 #include "moc_qbarset.cpp"
647 656 #include "moc_qbarset_p.cpp"
648 657
649 658 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,590 +1,596
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 "abstractdomain_p.h"
24 24 #include "qvalueaxis.h"
25 25 #include "xychart_p.h"
26 26 #include "qxylegendmarker.h"
27 27 #include "charthelpers_p.h"
28 28 #include "qchart_p.h"
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 /*!
33 33 \class QXYSeries
34 34 \brief The QXYSeries class is a base class for line, spline and scatter series.
35 35 */
36 36 /*!
37 37 \qmlclass XYSeries
38 38 \inherits AbstractSeries
39 39 The XYSeries class is a base class for line, spline and scatter series.
40 40
41 41 The class cannot be instantiated directly.
42 42 */
43 43
44 44 /*!
45 45 \qmlproperty AbstractAxis XYSeries::axisX
46 46 The x axis used for the series. If you leave both axisX and axisXTop undefined, a ValueAxis is created for
47 47 the series.
48 48 \sa axisXTop
49 49 */
50 50
51 51 /*!
52 52 \qmlproperty AbstractAxis XYSeries::axisY
53 53 The y axis used for the series. If you leave both axisY and axisYRight undefined, a ValueAxis is created for
54 54 the series.
55 55 \sa axisYRight
56 56 */
57 57
58 58 /*!
59 59 \qmlproperty AbstractAxis XYSeries::axisXTop
60 60 The x axis used for the series, drawn on top of the chart view. Note that you can only provide either axisX or
61 61 axisXTop, but not both.
62 62 \sa axisX
63 63 */
64 64
65 65 /*!
66 66 \qmlproperty AbstractAxis XYSeries::axisYRight
67 67 The y axis used for the series, drawn to the right on the chart view. Note that you can only provide either axisY
68 68 or axisYRight, but not both.
69 69 \sa axisY
70 70 */
71 71
72 72 /*!
73 73 \qmlproperty AbstractAxis XYSeries::axisAngular
74 74 The angular axis used for the series, drawn around the polar chart view.
75 75 \sa axisX
76 76 */
77 77
78 78 /*!
79 79 \qmlproperty AbstractAxis XYSeries::axisRadial
80 80 The radial axis used for the series, drawn inside the polar chart view.
81 81 \sa axisY
82 82 */
83 83
84 84 /*!
85 85 \property QXYSeries::pointsVisible
86 86 Controls if the data points are visible and should be drawn.
87 87 */
88 88 /*!
89 89 \qmlproperty bool XYSeries::pointsVisible
90 90 Controls if the data points are visible and should be drawn.
91 91 */
92 92
93 93 /*!
94 94 \fn QPen QXYSeries::pen() const
95 95 \brief Returns pen used to draw points for series.
96 96 \sa setPen()
97 97 */
98 98
99 99 /*!
100 100 \fn QBrush QXYSeries::brush() const
101 101 \brief Returns brush used to draw points for series.
102 102 \sa setBrush()
103 103 */
104 104
105 105 /*!
106 106 \property QXYSeries::color
107 107 The color of the series. This is line (pen) color in case of QLineSeries or QSplineSeries and
108 108 fill (brush) color in case of QScatterSeries or QAreaSeries.
109 109 \sa QXYSeries::pen(), QXYSeries::brush()
110 110 */
111 111 /*!
112 112 \qmlproperty color XYSeries::color
113 113 The color of the series. This is line (pen) color in case of LineSeries or SplineSeries and
114 114 fill (brush) color in case of ScatterSeries or AreaSeries.
115 115 */
116 116
117 117 /*!
118 118 \fn void QXYSeries::clicked(const QPointF& point)
119 119 \brief Signal is emitted when user clicks the \a point on chart.
120 120 */
121 121 /*!
122 122 \qmlsignal XYSeries::onClicked(QPointF point)
123 123 Signal is emitted when user clicks the \a point on chart. For example:
124 124 \code
125 125 LineSeries {
126 126 XYPoint { x: 0; y: 0 }
127 127 XYPoint { x: 1.1; y: 2.1 }
128 128 onClicked: console.log("onClicked: " + point.x + ", " + point.y);
129 129 }
130 130 \endcode
131 131 */
132 132
133 133 /*!
134 134 \fn void QXYSeries::hovered(const QPointF &point, bool state)
135 135 This signal is emitted when user has hovered over or away from the series. \a point shows the origin (coordinate)
136 136 of the hover event. \a state is true when user has hovered over the series and false when hover has moved away from
137 137 the series.
138 138 */
139 139 /*!
140 140 \qmlsignal XYSeries::onHovered(point point, bool state)
141 141 This signal is emitted when user has hovered over or away from the series. \a point shows the origin (coordinate)
142 142 of the hover event. \a state is true when user has hovered over the series and false when hover has moved away from
143 143 the series.
144 144 */
145 145
146 146 /*!
147 147 \fn void QXYSeries::pointReplaced(int index)
148 148 Signal is emitted when a point has been replaced at \a index.
149 149 \sa replace()
150 150 */
151 151 /*!
152 152 \qmlsignal XYSeries::onPointReplaced(int index)
153 153 Signal is emitted when a point has been replaced at \a index.
154 154 */
155 155
156 156 /*!
157 157 \fn void QXYSeries::pointsReplaced()
158 158 Signal is emitted when all points have been replaced with other points.
159 159 \sa replace()
160 160 */
161 161 /*!
162 162 \qmlsignal XYSeries::onPointsReplaced()
163 163 */
164 164
165 165 /*!
166 166 \fn void QXYSeries::pointAdded(int index)
167 167 Signal is emitted when a point has been added at \a index.
168 168 \sa append(), insert()
169 169 */
170 170 /*!
171 171 \qmlsignal XYSeries::onPointAdded(int index)
172 172 Signal is emitted when a point has been added at \a index.
173 173 */
174 174
175 175 /*!
176 176 \fn void QXYSeries::pointRemoved(int index)
177 177 Signal is emitted when a point has been removed from \a index.
178 178 \sa remove()
179 179 */
180 180 /*!
181 181 \qmlsignal XYSeries::onPointRemoved(int index)
182 182 Signal is emitted when a point has been removed from \a index.
183 183 */
184 184
185 185 /*!
186 186 \fn void QXYSeries::colorChanged(QColor color)
187 187 \brief Signal is emitted when the line (pen) color has changed to \a color.
188 188 */
189 189 /*!
190 190 \qmlsignal XYSeries::onColorChanged(color color)
191 191 Signal is emitted when the line (pen) color has changed to \a color.
192 192 */
193 193
194 194 /*!
195 195 \fn void QXYSeriesPrivate::updated()
196 196 \brief \internal
197 197 */
198 198
199 199 /*!
200 200 \qmlmethod XYSeries::append(real x, real y)
201 201 Append point (\a x, \a y) to the series
202 202 */
203 203
204 204 /*!
205 205 \qmlmethod XYSeries::replace(real oldX, real oldY, real newX, real newY)
206 206 Replaces point (\a oldX, \a oldY) with point (\a newX, \a newY). Does nothing, if point (oldX, oldY) does not
207 207 exist.
208 208 */
209 209
210 210 /*!
211 211 \qmlmethod XYSeries::remove(real x, real y)
212 212 Removes point (\a x, \a y) from the series. Does nothing, if point (x, y) does not exist.
213 213 */
214 214
215 215 /*!
216 216 \qmlmethod XYSeries::insert(int index, real x, real y)
217 217 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
218 218 points. If index is the same as or bigger than count, the point is appended to the list of points.
219 219 */
220 220
221 221 /*!
222 222 \qmlmethod QPointF XYSeries::at(int index)
223 223 Returns point at \a index. Returns (0, 0) if the index is not valid.
224 224 */
225 225
226 226 /*!
227 227 \internal
228 228
229 229 Constructs empty series object which is a child of \a parent.
230 230 When series object is added to QChart instance ownerships is transferred.
231 231 */
232 232 QXYSeries::QXYSeries(QXYSeriesPrivate &d, QObject *parent)
233 233 : QAbstractSeries(d, parent)
234 234 {
235 235 }
236 236
237 237 /*!
238 238 Destroys the object. Series added to QChart instances are owned by those,
239 239 and are destroyed when QChart instances are destroyed.
240 240 */
241 241 QXYSeries::~QXYSeries()
242 242 {
243 243 }
244 244
245 245 /*!
246 246 Adds data point (\a x, \a y) to the series.
247 247 */
248 248 void QXYSeries::append(qreal x, qreal y)
249 249 {
250 250 append(QPointF(x, y));
251 251 }
252 252
253 253 /*!
254 254 This is an overloaded function.
255 255 Adds data \a point to the series.
256 256 */
257 257 void QXYSeries::append(const QPointF &point)
258 258 {
259 259 Q_D(QXYSeries);
260 260
261 261 if (isValidValue(point)) {
262 262 d->m_points << point;
263 263 emit pointAdded(d->m_points.count() - 1);
264 264 }
265 265 }
266 266
267 267 /*!
268 268 This is an overloaded function.
269 269 Adds list of data \a points to the series.
270 270 */
271 271 void QXYSeries::append(const QList<QPointF> &points)
272 272 {
273 273 foreach (const QPointF &point , points)
274 274 append(point);
275 275 }
276 276
277 277 /*!
278 278 Replaces data point (\a oldX, \a oldY) with data point (\a newX, \a newY).
279 279 \sa QXYSeries::pointReplaced()
280 280 */
281 281 void QXYSeries::replace(qreal oldX, qreal oldY, qreal newX, qreal newY)
282 282 {
283 283 replace(QPointF(oldX, oldY), QPointF(newX, newY));
284 284 }
285 285
286 286 /*!
287 287 Replaces \a oldPoint with \a newPoint.
288 288 \sa QXYSeries::pointReplaced()
289 289 */
290 290 void QXYSeries::replace(const QPointF &oldPoint, const QPointF &newPoint)
291 291 {
292 292 Q_D(QXYSeries);
293 293 int index = d->m_points.indexOf(oldPoint);
294 294 if (index == -1)
295 295 return;
296 296 replace(index, newPoint);
297 297 }
298 298
299 299 /*!
300 300 Replaces the point at \a index with data point (\a newX, \a newY).
301 301 \sa QXYSeries::pointReplaced()
302 302 */
303 303 void QXYSeries::replace(int index, qreal newX, qreal newY)
304 304 {
305 305 replace(index, QPointF(newX, newY));
306 306 }
307 307
308 308 /*!
309 309 Replaces the point at \a index with \a newPoint.
310 310 \sa QXYSeries::pointReplaced()
311 311 */
312 312 void QXYSeries::replace(int index, const QPointF &newPoint)
313 313 {
314 314 Q_D(QXYSeries);
315 315 if (isValidValue(newPoint)) {
316 316 d->m_points[index] = newPoint;
317 317 emit pointReplaced(index);
318 318 }
319 319 }
320 320
321 321 /*!
322 322 Replaces the current points with \a points.
323 323 \note This is much faster than replacing data points one by one,
324 324 or first clearing all data, and then appending the new data. Emits QXYSeries::pointsReplaced()
325 325 when the points have been replaced.
326 326 \sa QXYSeries::pointsReplaced()
327 327 */
328 328 void QXYSeries::replace(QList<QPointF> points)
329 329 {
330 330 Q_D(QXYSeries);
331 331 d->m_points = points.toVector();
332 332 emit pointsReplaced();
333 333 }
334 334
335 335 /*!
336 336 Removes the point (\a x, \a y) from the series.
337 337 */
338 338 void QXYSeries::remove(qreal x, qreal y)
339 339 {
340 340 remove(QPointF(x, y));
341 341 }
342 342
343 343 /*!
344 344 Removes the \a point from the series.
345 345 */
346 346 void QXYSeries::remove(const QPointF &point)
347 347 {
348 348 Q_D(QXYSeries);
349 349 int index = d->m_points.indexOf(point);
350 350 if (index == -1)
351 351 return;
352 352 remove(index);
353 353 }
354 354
355 355 /*!
356 356 Removes the point at \a index from the series.
357 357 */
358 358 void QXYSeries::remove(int index)
359 359 {
360 360 Q_D(QXYSeries);
361 361 d->m_points.remove(index);
362 362 emit pointRemoved(index);
363 363 }
364 364
365 365 /*!
366 366 Inserts a \a point in the series at \a index position.
367 367 */
368 368 void QXYSeries::insert(int index, const QPointF &point)
369 369 {
370 370 Q_D(QXYSeries);
371 371 if (isValidValue(point)) {
372 372 d->m_points.insert(index, point);
373 373 emit pointAdded(index);
374 374 }
375 375 }
376 376
377 377 /*!
378 378 Removes all points from the series.
379 379 */
380 380 void QXYSeries::clear()
381 381 {
382 382 Q_D(QXYSeries);
383 383 for (int i = d->m_points.size() - 1; i >= 0; i--)
384 384 remove(d->m_points.at(i));
385 385 }
386 386
387 387 /*!
388 388 Returns list of points in the series.
389 389 */
390 390 QList<QPointF> QXYSeries::points() const
391 391 {
392 392 Q_D(const QXYSeries);
393 393 return d->m_points.toList();
394 394 }
395 395
396 396 /*!
397 397 Returns point at \a index in internal points vector.
398 398 */
399 399 const QPointF &QXYSeries::at(int index) const
400 400 {
401 401 Q_D(const QXYSeries);
402 402 return d->m_points.at(index);
403 403 }
404 404
405 405 /*!
406 406 Returns number of data points within series.
407 407 */
408 408 int QXYSeries::count() const
409 409 {
410 410 Q_D(const QXYSeries);
411 411 return d->m_points.count();
412 412 }
413 413
414 414
415 415 /*!
416 416 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
417 417 pen from chart theme is used.
418 418 \sa QChart::setTheme()
419 419 */
420 420 void QXYSeries::setPen(const QPen &pen)
421 421 {
422 422 Q_D(QXYSeries);
423 423 if (d->m_pen != pen) {
424 424 bool emitColorChanged = d->m_pen.color() != pen.color();
425 425 d->m_pen = pen;
426 426 emit d->updated();
427 427 if (emitColorChanged)
428 428 emit colorChanged(pen.color());
429 429 }
430 430 }
431 431
432 432 QPen QXYSeries::pen() const
433 433 {
434 434 Q_D(const QXYSeries);
435 return d->m_pen;
435 if (d->m_pen == QChartPrivate::defaultPen())
436 return QPen();
437 else
438 return d->m_pen;
436 439 }
437 440
438 441 /*!
439 442 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
440 443 from chart theme setting is used.
441 444 \sa QChart::setTheme()
442 445 */
443 446 void QXYSeries::setBrush(const QBrush &brush)
444 447 {
445 448 Q_D(QXYSeries);
446 449 if (d->m_brush != brush) {
447 450 d->m_brush = brush;
448 451 emit d->updated();
449 452 }
450 453 }
451 454
452 455 QBrush QXYSeries::brush() const
453 456 {
454 457 Q_D(const QXYSeries);
455 return d->m_brush;
458 if (d->m_brush == QChartPrivate::defaultBrush())
459 return QBrush();
460 else
461 return d->m_brush;
456 462 }
457 463
458 464 void QXYSeries::setColor(const QColor &color)
459 465 {
460 466 QPen p = pen();
461 467 if (p.color() != color) {
462 468 p.setColor(color);
463 469 setPen(p);
464 470 }
465 471 }
466 472
467 473 QColor QXYSeries::color() const
468 474 {
469 475 return pen().color();
470 476 }
471 477
472 478 void QXYSeries::setPointsVisible(bool visible)
473 479 {
474 480 Q_D(QXYSeries);
475 481 if (d->m_pointsVisible != visible) {
476 482 d->m_pointsVisible = visible;
477 483 emit d->updated();
478 484 }
479 485 }
480 486
481 487 bool QXYSeries::pointsVisible() const
482 488 {
483 489 Q_D(const QXYSeries);
484 490 return d->m_pointsVisible;
485 491 }
486 492
487 493
488 494 /*!
489 495 Stream operator for adding a data \a point to the series.
490 496 \sa append()
491 497 */
492 498 QXYSeries &QXYSeries::operator<< (const QPointF &point)
493 499 {
494 500 append(point);
495 501 return *this;
496 502 }
497 503
498 504
499 505 /*!
500 506 Stream operator for adding a list of \a points to the series.
501 507 \sa append()
502 508 */
503 509
504 510 QXYSeries &QXYSeries::operator<< (const QList<QPointF>& points)
505 511 {
506 512 append(points);
507 513 return *this;
508 514 }
509 515
510 516 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
511 517
512 518
513 519 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q)
514 520 : QAbstractSeriesPrivate(q),
515 521 m_pen(QChartPrivate::defaultPen()),
516 522 m_brush(QChartPrivate::defaultBrush()),
517 523 m_pointsVisible(false)
518 524 {
519 525 }
520 526
521 527 void QXYSeriesPrivate::initializeDomain()
522 528 {
523 529 qreal minX(0);
524 530 qreal minY(0);
525 531 qreal maxX(1);
526 532 qreal maxY(1);
527 533
528 534 Q_Q(QXYSeries);
529 535
530 536 const QList<QPointF>& points = q->points();
531 537
532 538 if (!points.isEmpty()) {
533 539 minX = points[0].x();
534 540 minY = points[0].y();
535 541 maxX = minX;
536 542 maxY = minY;
537 543
538 544 for (int i = 0; i < points.count(); i++) {
539 545 qreal x = points[i].x();
540 546 qreal y = points[i].y();
541 547 minX = qMin(minX, x);
542 548 minY = qMin(minY, y);
543 549 maxX = qMax(maxX, x);
544 550 maxY = qMax(maxY, y);
545 551 }
546 552 }
547 553
548 554 domain()->setRange(minX, maxX, minY, maxY);
549 555 }
550 556
551 557 QList<QLegendMarker*> QXYSeriesPrivate::createLegendMarkers(QLegend* legend)
552 558 {
553 559 Q_Q(QXYSeries);
554 560 QList<QLegendMarker*> list;
555 561 return list << new QXYLegendMarker(q,legend);
556 562 }
557 563
558 564 void QXYSeriesPrivate::initializeAxes()
559 565 {
560 566
561 567 }
562 568
563 569 QAbstractAxis::AxisType QXYSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
564 570 {
565 571 Q_UNUSED(orientation);
566 572 return QAbstractAxis::AxisTypeValue;
567 573 }
568 574
569 575 QAbstractAxis* QXYSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
570 576 {
571 577 Q_UNUSED(orientation);
572 578 return 0;
573 579 }
574 580
575 581 void QXYSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options)
576 582 {
577 583 XYChart *item = static_cast<XYChart *>(m_item.data());
578 584 Q_ASSERT(item);
579 585 if (options.testFlag(QChart::SeriesAnimations)) {
580 586 item->setAnimation(new XYAnimation(item));
581 587 }else{
582 588 item->setAnimation(0);
583 589 }
584 590 QAbstractSeriesPrivate::initializeAnimations(options);
585 591 }
586 592
587 593 #include "moc_qxyseries.cpp"
588 594 #include "moc_qxyseries_p.cpp"
589 595
590 596 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,520 +1,520
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 <QtTest/QtTest>
22 22 #include <qbarset.h>
23 23 #include <qbarseries.h>
24 24 #include <qchartview.h>
25 25 #include "tst_definitions.h"
26 26
27 27 QTCOMMERCIALCHART_USE_NAMESPACE
28 28
29 29 class tst_QBarSet : public QObject
30 30 {
31 31 Q_OBJECT
32 32
33 33 public slots:
34 34 void initTestCase();
35 35 void cleanupTestCase();
36 36 void init();
37 37 void cleanup();
38 38
39 39 private slots:
40 40 void qbarset_data();
41 41 void qbarset();
42 42 void label_data();
43 43 void label();
44 44 void append_data();
45 45 void append();
46 46 void appendOperator_data();
47 47 void appendOperator();
48 48 void insert_data();
49 49 void insert();
50 50 void remove_data();
51 51 void remove();
52 52 void replace_data();
53 53 void replace();
54 54 void at_data();
55 55 void at();
56 56 void atOperator_data();
57 57 void atOperator();
58 58 void count_data();
59 59 void count();
60 60 void sum_data();
61 61 void sum();
62 62 void customize();
63 63
64 64 private:
65 65 QBarSet* m_barset;
66 66 };
67 67
68 68 void tst_QBarSet::initTestCase()
69 69 {
70 70 }
71 71
72 72 void tst_QBarSet::cleanupTestCase()
73 73 {
74 74 }
75 75
76 76 void tst_QBarSet::init()
77 77 {
78 78 m_barset = new QBarSet(QString("label"));
79 79 }
80 80
81 81 void tst_QBarSet::cleanup()
82 82 {
83 83 delete m_barset;
84 84 m_barset = 0;
85 85 }
86 86
87 87 void tst_QBarSet::qbarset_data()
88 88 {
89 89 }
90 90
91 91 void tst_QBarSet::qbarset()
92 92 {
93 93 QBarSet barset(QString("label"));
94 94 QCOMPARE(barset.label(), QString("label"));
95 95 QCOMPARE(barset.count(), 0);
96 96 QVERIFY(qFuzzyCompare(barset.sum(), 0));
97 97 }
98 98
99 99 void tst_QBarSet::label_data()
100 100 {
101 101 QTest::addColumn<QString> ("label");
102 102 QTest::addColumn<QString> ("result");
103 103 QTest::newRow("label0") << QString("label0") << QString("label0");
104 104 QTest::newRow("label1") << QString("label1") << QString("label1");
105 105 }
106 106
107 107 void tst_QBarSet::label()
108 108 {
109 109 QFETCH(QString, label);
110 110 QFETCH(QString, result);
111 111
112 112 QSignalSpy labelSpy(m_barset,SIGNAL(labelChanged()));
113 113 m_barset->setLabel(label);
114 114 QCOMPARE(m_barset->label(), result);
115 115 QVERIFY(labelSpy.count() == 1);
116 116 }
117 117
118 118 void tst_QBarSet::append_data()
119 119 {
120 120 QTest::addColumn<int> ("count");
121 121 QTest::newRow("0") << 0;
122 122 QTest::newRow("5") << 5;
123 123 QTest::newRow("100") << 100;
124 124 QTest::newRow("1000") << 1000;
125 125 }
126 126
127 127 void tst_QBarSet::append()
128 128 {
129 129 QFETCH(int, count);
130 130
131 131 QCOMPARE(m_barset->count(), 0);
132 132 QVERIFY(qFuzzyCompare(m_barset->sum(), 0));
133 133
134 134 QSignalSpy valueSpy(m_barset, SIGNAL(valuesAdded(int,int)));
135 135
136 136 qreal sum(0.0);
137 137 qreal value(0.0);
138 138
139 139 for (int i=0; i<count; i++) {
140 140 m_barset->append(value);
141 141 QCOMPARE(m_barset->at(i), value);
142 142 sum += value;
143 143 value += 1.0;
144 144 }
145 145
146 146 QCOMPARE(m_barset->count(), count);
147 147 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
148 148
149 149 QCOMPARE(valueSpy.count(), count);
150 150 }
151 151
152 152 void tst_QBarSet::appendOperator_data()
153 153 {
154 154 append_data();
155 155 }
156 156
157 157 void tst_QBarSet::appendOperator()
158 158 {
159 159 QFETCH(int, count);
160 160
161 161 QCOMPARE(m_barset->count(), 0);
162 162 QVERIFY(qFuzzyCompare(m_barset->sum(), 0));
163 163
164 164 QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int)));
165 165
166 166 qreal sum(0.0);
167 167 qreal value(0.0);
168 168
169 169 for (int i=0; i<count; i++) {
170 170 *m_barset << value;
171 171 QCOMPARE(m_barset->at(i), value);
172 172 sum += value;
173 173 value += 1.0;
174 174 }
175 175
176 176 QCOMPARE(m_barset->count(), count);
177 177 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
178 178 QCOMPARE(valueSpy.count(), count);
179 179 }
180 180
181 181 void tst_QBarSet::insert_data()
182 182 {
183 183 }
184 184
185 185 void tst_QBarSet::insert()
186 186 {
187 187 QCOMPARE(m_barset->count(), 0);
188 188 QVERIFY(qFuzzyCompare(m_barset->sum(), 0));
189 189 QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int)));
190 190
191 191 m_barset->insert(0, 1.0); // 1.0
192 192 QCOMPARE(m_barset->at(0), 1.0);
193 193 QCOMPARE(m_barset->count(), 1);
194 194 QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)1.0));
195 195
196 196 m_barset->insert(0, 2.0); // 2.0 1.0
197 197 QCOMPARE(m_barset->at(0), 2.0);
198 198 QCOMPARE(m_barset->at(1), 1.0);
199 199 QCOMPARE(m_barset->count(), 2);
200 200 QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)3.0));
201 201
202 202 m_barset->insert(1, 3.0); // 2.0 3.0 1.0
203 203 QCOMPARE(m_barset->at(1), 3.0);
204 204 QCOMPARE(m_barset->at(0), 2.0);
205 205 QCOMPARE(m_barset->at(2), 1.0);
206 206 QCOMPARE(m_barset->count(), 3);
207 207 QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)6.0));
208 208 QCOMPARE(valueSpy.count(), 3);
209 209 }
210 210
211 211 void tst_QBarSet::remove_data()
212 212 {
213 213 }
214 214
215 215 void tst_QBarSet::remove()
216 216 {
217 217 QCOMPARE(m_barset->count(), 0);
218 218 QVERIFY(qFuzzyCompare(m_barset->sum(), 0));
219 219
220 220 QSignalSpy valueSpy(m_barset,SIGNAL(valuesRemoved(int,int)));
221 221
222 222 m_barset->append(1.0);
223 223 m_barset->append(2.0);
224 224 m_barset->append(3.0);
225 225 m_barset->append(4.0);
226 226
227 227 QCOMPARE(m_barset->count(), 4);
228 228 QCOMPARE(m_barset->sum(), 10.0);
229 229
230 230 // Remove middle
231 231 m_barset->remove(2); // 1.0 2.0 4.0
232 232 QCOMPARE(m_barset->at(0), 1.0);
233 233 QCOMPARE(m_barset->at(1), 2.0);
234 234 QCOMPARE(m_barset->at(2), 4.0);
235 235 QCOMPARE(m_barset->count(), 3);
236 236 QCOMPARE(m_barset->sum(), 7.0);
237 237 QCOMPARE(valueSpy.count(), 1);
238 238
239 239 QList<QVariant> valueSpyArg = valueSpy.takeFirst();
240 240 // Verify index of removed signal
241 241 QVERIFY(valueSpyArg.at(0).type() == QVariant::Int);
242 242 QVERIFY(valueSpyArg.at(0).toInt() == 2);
243 243 // Verify count of removed signal
244 244 QVERIFY(valueSpyArg.at(1).type() == QVariant::Int);
245 245 QVERIFY(valueSpyArg.at(1).toInt() == 1);
246 246
247 247 // Remove first
248 248 m_barset->remove(0); // 2.0 4.0
249 249 QCOMPARE(m_barset->at(0), 2.0);
250 250 QCOMPARE(m_barset->at(1), 4.0);
251 251 QCOMPARE(m_barset->count(), 2);
252 252 QCOMPARE(m_barset->sum(), 6.0);
253 253
254 254 QCOMPARE(valueSpy.count(), 1);
255 255 valueSpyArg = valueSpy.takeFirst();
256 256 // Verify index of removed signal
257 257 QVERIFY(valueSpyArg.at(0).type() == QVariant::Int);
258 258 QVERIFY(valueSpyArg.at(0).toInt() == 0);
259 259 // Verify count of removed signal
260 260 QVERIFY(valueSpyArg.at(1).type() == QVariant::Int);
261 261 QVERIFY(valueSpyArg.at(1).toInt() == 1);
262 262
263 263
264 264 // Illegal indexes
265 265 m_barset->remove(4);
266 266 QCOMPARE(m_barset->count(), 2);
267 267 QCOMPARE(m_barset->sum(), 6.0);
268 268 m_barset->remove(-1);
269 269 QCOMPARE(m_barset->count(), 2);
270 270 QCOMPARE(m_barset->sum(), 6.0);
271 271
272 272 // nothing removed, no signals should be emitted
273 273 QCOMPARE(valueSpy.count(), 0);
274 274
275 275 // Remove more items than list has
276 276 m_barset->remove(0,312);
277 277 QCOMPARE(m_barset->count(), 0);
278 278 QVERIFY(qFuzzyCompare(m_barset->sum(), 0));
279 279
280 280 QCOMPARE(valueSpy.count(), 1);
281 281 valueSpyArg = valueSpy.takeFirst();
282 282
283 283 // Verify index of removed signal
284 284 QVERIFY(valueSpyArg.at(0).type() == QVariant::Int);
285 285 QVERIFY(valueSpyArg.at(0).toInt() == 0);
286 286 // Verify count of removed signal (expect 2 values removed, because list had only 2 items)
287 287 QVERIFY(valueSpyArg.at(1).type() == QVariant::Int);
288 288 QVERIFY(valueSpyArg.at(1).toInt() == 2);
289 289 }
290 290
291 291 void tst_QBarSet::replace_data()
292 292 {
293 293
294 294 }
295 295
296 296 void tst_QBarSet::replace()
297 297 {
298 298 QCOMPARE(m_barset->count(), 0);
299 299 QVERIFY(qFuzzyCompare(m_barset->sum(), 0));
300 300 QSignalSpy valueSpy(m_barset,SIGNAL(valueChanged(int)));
301 301
302 302 m_barset->append(1.0);
303 303 m_barset->append(2.0);
304 304 m_barset->append(3.0);
305 305 m_barset->append(4.0);
306 306
307 307 QCOMPARE(m_barset->count(), 4);
308 308 QCOMPARE(m_barset->sum(), 10.0);
309 309
310 310 // Replace first
311 311 m_barset->replace(0, 5.0); // 5.0 2.0 3.0 4.0
312 312 QCOMPARE(m_barset->count(), 4);
313 313 QCOMPARE(m_barset->sum(), 14.0);
314 314 QCOMPARE(m_barset->at(0), 5.0);
315 315
316 316 // Replace last
317 317 m_barset->replace(3, 6.0);
318 318 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
319 319 QCOMPARE(m_barset->sum(), 16.0);
320 320 QCOMPARE(m_barset->at(0), 5.0);
321 321 QCOMPARE(m_barset->at(1), 2.0);
322 322 QCOMPARE(m_barset->at(2), 3.0);
323 323 QCOMPARE(m_barset->at(3), 6.0);
324 324
325 325 // Illegal indexes
326 326 m_barset->replace(4, 6.0);
327 327 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
328 328 QCOMPARE(m_barset->sum(), 16.0);
329 329 m_barset->replace(-1, 6.0);
330 330 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
331 331 QCOMPARE(m_barset->sum(), 16.0);
332 332 m_barset->replace(4, 1.0);
333 333 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
334 334 QCOMPARE(m_barset->sum(), 16.0);
335 335 m_barset->replace(-1, 1.0);
336 336 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
337 337 QCOMPARE(m_barset->sum(), 16.0);
338 338
339 339 QVERIFY(valueSpy.count() == 2);
340 340 }
341 341
342 342 void tst_QBarSet::at_data()
343 343 {
344 344
345 345 }
346 346
347 347 void tst_QBarSet::at()
348 348 {
349 349 QCOMPARE(m_barset->count(), 0);
350 350 QVERIFY(qFuzzyCompare(m_barset->sum(), 0));
351 351
352 352 m_barset->append(1.0);
353 353 m_barset->append(2.0);
354 354 m_barset->append(3.0);
355 355 m_barset->append(4.0);
356 356
357 357 QCOMPARE(m_barset->at(0), 1.0);
358 358 QCOMPARE(m_barset->at(1), 2.0);
359 359 QCOMPARE(m_barset->at(2), 3.0);
360 360 QCOMPARE(m_barset->at(3), 4.0);
361 361 }
362 362
363 363 void tst_QBarSet::atOperator_data()
364 364 {
365 365
366 366 }
367 367
368 368 void tst_QBarSet::atOperator()
369 369 {
370 370 QCOMPARE(m_barset->count(), 0);
371 371 QVERIFY(qFuzzyCompare(m_barset->sum(), 0));
372 372
373 373 m_barset->append(1.0);
374 374 m_barset->append(2.0);
375 375 m_barset->append(3.0);
376 376 m_barset->append(4.0);
377 377
378 378 QCOMPARE(m_barset->operator [](0), 1.0);
379 379 QCOMPARE(m_barset->operator [](1), 2.0);
380 380 QCOMPARE(m_barset->operator [](2), 3.0);
381 381 QCOMPARE(m_barset->operator [](3), 4.0);
382 382 }
383 383
384 384 void tst_QBarSet::count_data()
385 385 {
386 386
387 387 }
388 388
389 389 void tst_QBarSet::count()
390 390 {
391 391 QCOMPARE(m_barset->count(), 0);
392 392 QVERIFY(qFuzzyCompare(m_barset->sum(), 0));
393 393
394 394 m_barset->append(1.0);
395 395 QCOMPARE(m_barset->count(),1);
396 396 m_barset->append(2.0);
397 397 QCOMPARE(m_barset->count(),2);
398 398 m_barset->append(3.0);
399 399 QCOMPARE(m_barset->count(),3);
400 400 m_barset->append(4.0);
401 401 QCOMPARE(m_barset->count(),4);
402 402 }
403 403
404 404 void tst_QBarSet::sum_data()
405 405 {
406 406
407 407 }
408 408
409 409 void tst_QBarSet::sum()
410 410 {
411 411 QCOMPARE(m_barset->count(), 0);
412 412 QVERIFY(qFuzzyCompare(m_barset->sum(), 0));
413 413
414 414 m_barset->append(1.0);
415 415 QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)1.0));
416 416 m_barset->append(2.0);
417 417 QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)3.0));
418 418 m_barset->append(3.0);
419 419 QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)6.0));
420 420 m_barset->append(4.0);
421 421 QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)10.0));
422 422 }
423 423
424 424 void tst_QBarSet::customize()
425 425 {
426 426 // Create sets
427 427 QBarSet *set1 = new QBarSet("set1");
428 428 QBarSet *set2 = new QBarSet("set2");
429 429
430 430 // Append set1 to series
431 431 QBarSeries *series = new QBarSeries();
432 432 bool success = series->append(set1);
433 433 QVERIFY(success);
434 434
435 435 // Add series to the chart
436 436 QChartView view(new QChart());
437 437 view.resize(200, 200);
438 438 view.chart()->addSeries(series);
439 439 view.show();
440 440 QTest::qWaitForWindowShown(&view);
441 441
442 442 // Test adding data to the sets
443 443 *set1 << 1 << 2 << 1 << 3;
444 444 *set2 << 2 << 1 << 3 << 1;
445 445
446 446 // Remove sets from series
447 447 series->take(set1);
448 448 series->take(set2);
449 449
450 450 // Test pen
451 451 QVERIFY(set1->pen() != QPen());
452 452 QVERIFY(set2->pen() == QPen());
453 453 QPen pen(QColor(128,128,128,128));
454 454 set1->setPen(pen);
455 455
456 456 // Add sets back to series
457 457 series->append(set1);
458 458 series->append(set2);
459 459
460 460 QVERIFY(set1->pen() == pen); // Should be customized
461 461 QVERIFY(set2->pen() != QPen()); // Should be decorated by theme
462 462
463 463 // Remove sets from series
464 464 series->take(set1);
465 465 series->take(set2);
466 466
467 467 // Test brush
468 468 set2->setBrush(QBrush());
469 469 QVERIFY(set1->brush() != QBrush());
470 470 QVERIFY(set2->brush() == QBrush());
471 471 QBrush brush(QColor(128,128,128,128));
472 472 set1->setBrush(brush);
473 473
474 474 // Add sets back to series
475 475 series->append(set1);
476 476 series->append(set2);
477 477
478 478 QVERIFY(set1->brush() == brush); // Should be customized
479 QVERIFY(set2->brush() != QBrush()); // Should be decorated by theme
479 QVERIFY(set2->brush() == QBrush()); // Setting empty brush doesn't reset to theme brush
480 480
481 481 // Remove sets from series
482 482 series->take(set1);
483 483 series->take(set2);
484 484
485 485 // Test label brush
486 486 set2->setLabelBrush(QBrush());
487 487 QVERIFY(set1->labelBrush() != QBrush());
488 488 QVERIFY(set2->labelBrush() == QBrush());
489 489 set1->setLabelBrush(brush);
490 490
491 491 series->append(set1);
492 492 series->append(set2);
493 QVERIFY(set1->labelBrush() == brush); // Should be customized
494 QVERIFY(set2->labelBrush() != QBrush()); // Should be decorated by theme
493 QVERIFY(set1->labelBrush() == brush); // Should be customized
494 QVERIFY(set2->labelBrush() == QBrush()); // Setting empty brush doesn't reset to theme brush
495 495
496 496 // Test label font
497 497 // Note: QFont empty constructor creates font with application's default font, so the font may or may not be the
498 498 // same for the set added to the series (depending on the QChart's theme configuration)
499 499 QVERIFY(set1->labelFont() != QFont() || set1->labelFont() == QFont());
500 500 QVERIFY(set2->labelFont() == QFont());
501 501 QFont font;
502 502 font.setBold(true);
503 503 font.setItalic(true);
504 504 set1->setLabelFont(font);
505 505 QVERIFY(set1->labelFont() == font);
506 506 QVERIFY(set2->labelFont() == QFont());
507 507
508 508 // Test adding data to the sets
509 509 *set1 << 1 << 2 << 1 << 3;
510 510 *set2 << 2 << 1 << 3 << 1;
511 511
512 512 }
513 513
514 514
515 515
516 516
517 517 QTEST_MAIN(tst_QBarSet)
518 518
519 519 #include "tst_qbarset.moc"
520 520
General Comments 0
You need to be logged in to leave comments. Login now