@@ -1,782 +1,784 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "qabstractbarseries.h" |
|
22 | 22 | #include "qabstractbarseries_p.h" |
|
23 | 23 | #include "qbarset.h" |
|
24 | 24 | #include "qbarset_p.h" |
|
25 | 25 | #include "domain_p.h" |
|
26 | 26 | #include "legendmarker_p.h" |
|
27 | 27 | #include "chartdataset_p.h" |
|
28 | 28 | #include "charttheme_p.h" |
|
29 | 29 | #include "qvaluesaxis.h" |
|
30 | 30 | #include "qbarcategoriesaxis.h" |
|
31 | 31 | |
|
32 | 32 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
33 | 33 | |
|
34 | 34 | /*! |
|
35 | 35 | \class QAbstractBarSeries |
|
36 | 36 | \brief Series for creating a bar chart |
|
37 | 37 | \mainclass |
|
38 | 38 | |
|
39 | 39 | QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to |
|
40 | 40 | the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar |
|
41 | 41 | and y-value is the height of the bar. The category names are ignored with this series and x-axis |
|
42 | 42 | shows the x-values. |
|
43 | 43 | |
|
44 | 44 | See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart. |
|
45 | 45 | \image examples_barchart.png |
|
46 | 46 | |
|
47 | 47 | \sa QBarSet, QStackedBarSeries, QPercentBarSeries |
|
48 | 48 | */ |
|
49 | 49 | /*! |
|
50 | 50 | \qmlclass AbstractBarSeries QAbstractBarSeries |
|
51 | 51 | \inherits QAbstractSeries |
|
52 | 52 | |
|
53 | 53 | The following QML shows how to create a simple bar chart: |
|
54 | 54 | \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1 |
|
55 | 55 | |
|
56 | 56 | \beginfloatleft |
|
57 | 57 | \image demos_qmlchart6.png |
|
58 | 58 | \endfloat |
|
59 | 59 | \clearfloat |
|
60 | 60 | */ |
|
61 | 61 | |
|
62 | 62 | /*! |
|
63 | 63 | \property QAbstractBarSeries::barWidth |
|
64 | 64 | The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars |
|
65 | 65 | is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen |
|
66 | 66 | is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. |
|
67 | 67 | Note that with QBarSeries this value means the width of one group of bars instead of just one bar. |
|
68 | 68 | \sa QBarSeries |
|
69 | 69 | */ |
|
70 | 70 | /*! |
|
71 | 71 | \qmlproperty real AbstractBarSeries::barWidth |
|
72 | 72 | The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars |
|
73 | 73 | is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen |
|
74 | 74 | is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. |
|
75 | 75 | Note that with QBarSeries this value means the width of one group of bars instead of just one bar. |
|
76 | 76 | */ |
|
77 | 77 | |
|
78 | 78 | /*! |
|
79 | 79 | \property QAbstractBarSeries::count |
|
80 | 80 | Holds the number of sets in series. |
|
81 | 81 | */ |
|
82 | 82 | /*! |
|
83 | 83 | \qmlproperty int AbstractBarSeries::count |
|
84 | 84 | Holds the number of sets in series. |
|
85 | 85 | */ |
|
86 | 86 | |
|
87 | 87 | /*! |
|
88 | 88 | \property QAbstractBarSeries::labelsVisible |
|
89 | 89 | Defines the visibility of the labels in series |
|
90 | 90 | */ |
|
91 | 91 | /*! |
|
92 | 92 | \qmlproperty bool AbstractBarSeries::labelsVisible |
|
93 | 93 | Defines the visibility of the labels in series |
|
94 | 94 | */ |
|
95 | 95 | |
|
96 | 96 | /*! |
|
97 | 97 | \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset) |
|
98 | 98 | The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset. |
|
99 | 99 | Clicked bar inside set is indexed by \a index |
|
100 | 100 | */ |
|
101 | 101 | /*! |
|
102 | 102 | \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset) |
|
103 | 103 | The signal is emitted if the user clicks with a mouse on top of BarSet. |
|
104 | 104 | Clicked bar inside set is indexed by \a index |
|
105 | 105 | */ |
|
106 | 106 | |
|
107 | 107 | /*! |
|
108 | 108 | \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset) |
|
109 | 109 | |
|
110 | 110 | The signal is emitted if mouse is hovered on top of series. |
|
111 | 111 | Parameter \a barset is the pointer of barset, where hover happened. |
|
112 | 112 | Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series. |
|
113 | 113 | */ |
|
114 | 114 | /*! |
|
115 | 115 | \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset) |
|
116 | 116 | |
|
117 | 117 | The signal is emitted if mouse is hovered on top of series. |
|
118 | 118 | Parameter \a barset is the pointer of barset, where hover happened. |
|
119 | 119 | Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series. |
|
120 | 120 | */ |
|
121 | 121 | |
|
122 | 122 | /*! |
|
123 | 123 | \fn void QAbstractBarSeries::countChanged() |
|
124 | 124 | This signal is emitted when barset count has been changed, for example by append or remove. |
|
125 | 125 | */ |
|
126 | 126 | /*! |
|
127 | 127 | \qmlsignal AbstractBarSeries::onCountChanged() |
|
128 | 128 | This signal is emitted when barset count has been changed, for example by append or remove. |
|
129 | 129 | */ |
|
130 | 130 | |
|
131 | 131 | /*! |
|
132 | 132 | \fn void QAbstractBarSeries::labelsVisibleChanged() |
|
133 | 133 | This signal is emitted when labels visibility have changed. |
|
134 | 134 | \sa isLabelsVisible(), setLabelsVisible() |
|
135 | 135 | */ |
|
136 | 136 | |
|
137 | 137 | /*! |
|
138 | 138 | \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets) |
|
139 | 139 | This signal is emitted when \a sets have been added to the series. |
|
140 | 140 | \sa append(), insert() |
|
141 | 141 | */ |
|
142 | 142 | /*! |
|
143 | 143 | \qmlsignal AbstractBarSeries::onAdded(BarSet barset) |
|
144 | 144 | Emitted when \a barset has been added to the series. |
|
145 | 145 | */ |
|
146 | 146 | |
|
147 | 147 | /*! |
|
148 | 148 | \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets) |
|
149 | 149 | This signal is emitted when \a sets have been removed from the series. |
|
150 | 150 | \sa remove() |
|
151 | 151 | */ |
|
152 | 152 | /*! |
|
153 | 153 | \qmlsignal AbstractBarSeries::onRemoved(BarSet barset) |
|
154 | 154 | Emitted when \a barset has been removed from the series. |
|
155 | 155 | */ |
|
156 | 156 | |
|
157 | 157 | /*! |
|
158 | 158 | \qmlmethod BarSet AbstractBarSeries::at(int index) |
|
159 | 159 | Returns bar set at \a index. Returns null if the index is not valid. |
|
160 | 160 | */ |
|
161 | 161 | |
|
162 | 162 | /*! |
|
163 | 163 | \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values) |
|
164 | 164 | Adds 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. |
|
165 | 165 | For example: |
|
166 | 166 | \code |
|
167 | 167 | myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]); |
|
168 | 168 | myBarSeries.append("set 2", [Qt.point(0, 1), Qt.point(2, 2.5), Qt.point(3.5, 2.2)]); |
|
169 | 169 | \endcode |
|
170 | 170 | */ |
|
171 | 171 | |
|
172 | 172 | /*! |
|
173 | 173 | \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values) |
|
174 | 174 | 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. |
|
175 | 175 | If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is |
|
176 | 176 | appended. |
|
177 | 177 | \sa AbstractBarSeries::append() |
|
178 | 178 | */ |
|
179 | 179 | |
|
180 | 180 | /*! |
|
181 | 181 | \qmlmethod bool AbstractBarSeries::remove(BarSet barset) |
|
182 | 182 | Removes the barset from the series. Returns true if successfull, false otherwise. |
|
183 | 183 | */ |
|
184 | 184 | |
|
185 | 185 | /*! |
|
186 | 186 | \qmlmethod AbstractBarSeries::clear() |
|
187 | 187 | Removes all barsets from the series. |
|
188 | 188 | */ |
|
189 | 189 | |
|
190 | 190 | /*! |
|
191 | 191 | This is depreciated constructor. |
|
192 | 192 | */ |
|
193 | 193 | QAbstractBarSeries::QAbstractBarSeries(QObject *parent) : |
|
194 | 194 | QAbstractSeries(*(QAbstractBarSeriesPrivate*)(0),parent) |
|
195 | 195 | { |
|
196 | 196 | } |
|
197 | 197 | |
|
198 | 198 | /*! |
|
199 | 199 | Destructs abstractbarseries and owned barsets. |
|
200 | 200 | */ |
|
201 | 201 | QAbstractBarSeries::~QAbstractBarSeries() |
|
202 | 202 | { |
|
203 | 203 | |
|
204 | 204 | } |
|
205 | 205 | |
|
206 | 206 | /*! |
|
207 | 207 | \internal |
|
208 | 208 | */ |
|
209 | 209 | QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent) : |
|
210 | 210 | QAbstractSeries(d,parent) |
|
211 | 211 | { |
|
212 | 212 | } |
|
213 | 213 | |
|
214 | 214 | /*! |
|
215 | 215 | 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 |
|
216 | 216 | is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen |
|
217 | 217 | is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. |
|
218 | 218 | Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar. |
|
219 | 219 | */ |
|
220 | 220 | void QAbstractBarSeries::setBarWidth(qreal width) |
|
221 | 221 | { |
|
222 | 222 | Q_D(QAbstractBarSeries); |
|
223 | 223 | d->setBarWidth(width); |
|
224 | 224 | } |
|
225 | 225 | |
|
226 | 226 | /*! |
|
227 | 227 | Returns the width of the bars of the series. |
|
228 | 228 | \sa setBarWidth() |
|
229 | 229 | */ |
|
230 | 230 | qreal QAbstractBarSeries::barWidth() const |
|
231 | 231 | { |
|
232 | 232 | Q_D(const QAbstractBarSeries); |
|
233 | 233 | return d->barWidth(); |
|
234 | 234 | } |
|
235 | 235 | |
|
236 | 236 | /*! |
|
237 | 237 | 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. |
|
238 | 238 | Returns true, if appending succeeded. |
|
239 | 239 | */ |
|
240 | 240 | bool QAbstractBarSeries::append(QBarSet *set) |
|
241 | 241 | { |
|
242 | 242 | Q_D(QAbstractBarSeries); |
|
243 | 243 | bool success = d->append(set); |
|
244 | 244 | if (success) { |
|
245 | 245 | QList<QBarSet*> sets; |
|
246 | 246 | sets.append(set); |
|
247 | set->setParent(this); | |
|
247 | 248 | emit barsetsAdded(sets); |
|
248 | 249 | emit countChanged(); |
|
249 | 250 | } |
|
250 | 251 | return success; |
|
251 | 252 | } |
|
252 | 253 | |
|
253 | 254 | /*! |
|
254 | 255 | Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set. |
|
255 | 256 | Returns true, if set was removed. |
|
256 | 257 | */ |
|
257 | 258 | bool QAbstractBarSeries::remove(QBarSet *set) |
|
258 | 259 | { |
|
259 | 260 | Q_D(QAbstractBarSeries); |
|
260 | 261 | bool success = d->remove(set); |
|
261 | 262 | if (success) { |
|
262 | 263 | QList<QBarSet*> sets; |
|
263 | 264 | sets.append(set); |
|
265 | set->setParent(0); | |
|
264 | 266 | emit barsetsRemoved(sets); |
|
265 | 267 | emit countChanged(); |
|
266 | 268 | } |
|
267 | 269 | return success; |
|
268 | 270 | } |
|
269 | 271 | |
|
270 | 272 | /*! |
|
271 | 273 | Adds a list of barsets to series. Takes ownership of \a sets. |
|
272 | 274 | Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series, |
|
273 | 275 | nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended |
|
274 | 276 | and function returns false. |
|
275 | 277 | */ |
|
276 | 278 | bool QAbstractBarSeries::append(QList<QBarSet* > sets) |
|
277 | 279 | { |
|
278 | 280 | Q_D(QAbstractBarSeries); |
|
279 | 281 | bool success = d->append(sets); |
|
280 | 282 | if (success) { |
|
281 | 283 | emit barsetsAdded(sets); |
|
282 | 284 | emit countChanged(); |
|
283 | 285 | } |
|
284 | 286 | return success; |
|
285 | 287 | } |
|
286 | 288 | |
|
287 | 289 | /*! |
|
288 | 290 | 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. |
|
289 | 291 | Returns true, if inserting succeeded. |
|
290 | 292 | |
|
291 | 293 | */ |
|
292 | 294 | bool QAbstractBarSeries::insert(int index, QBarSet *set) |
|
293 | 295 | { |
|
294 | 296 | Q_D(QAbstractBarSeries); |
|
295 | 297 | bool success = d->insert(index, set); |
|
296 | 298 | if (success) { |
|
297 | 299 | QList<QBarSet*> sets; |
|
298 | 300 | sets.append(set); |
|
299 | 301 | emit barsetsAdded(sets); |
|
300 | 302 | emit countChanged(); |
|
301 | 303 | } |
|
302 | 304 | return success; |
|
303 | 305 | } |
|
304 | 306 | |
|
305 | 307 | /*! |
|
306 | 308 | Removes all of the bar sets from the series |
|
307 | 309 | */ |
|
308 | 310 | void QAbstractBarSeries::clear() |
|
309 | 311 | { |
|
310 | 312 | Q_D(QAbstractBarSeries); |
|
311 | 313 | QList<QBarSet *> sets = barSets(); |
|
312 | 314 | bool success = d->remove(sets); |
|
313 | 315 | if (success) { |
|
314 | 316 | emit barsetsRemoved(sets); |
|
315 | 317 | emit countChanged(); |
|
316 | 318 | } |
|
317 | 319 | } |
|
318 | 320 | |
|
319 | 321 | /*! |
|
320 | 322 | Returns number of sets in series. |
|
321 | 323 | */ |
|
322 | 324 | int QAbstractBarSeries::count() const |
|
323 | 325 | { |
|
324 | 326 | Q_D(const QAbstractBarSeries); |
|
325 | 327 | return d->m_barSets.count(); |
|
326 | 328 | } |
|
327 | 329 | |
|
328 | 330 | /*! |
|
329 | 331 | Returns a list of sets in series. Keeps ownership of sets. |
|
330 | 332 | */ |
|
331 | 333 | QList<QBarSet*> QAbstractBarSeries::barSets() const |
|
332 | 334 | { |
|
333 | 335 | Q_D(const QAbstractBarSeries); |
|
334 | 336 | return d->m_barSets; |
|
335 | 337 | } |
|
336 | 338 | |
|
337 | 339 | /*! |
|
338 | 340 | Sets the visibility of labels in series to \a visible |
|
339 | 341 | */ |
|
340 | 342 | void QAbstractBarSeries::setLabelsVisible(bool visible) |
|
341 | 343 | { |
|
342 | 344 | Q_D(QAbstractBarSeries); |
|
343 | 345 | if (d->m_labelsVisible != visible) { |
|
344 | 346 | d->setLabelsVisible(visible); |
|
345 | 347 | emit labelsVisibleChanged(); |
|
346 | 348 | } |
|
347 | 349 | } |
|
348 | 350 | |
|
349 | 351 | /*! |
|
350 | 352 | Returns the visibility of labels |
|
351 | 353 | */ |
|
352 | 354 | bool QAbstractBarSeries::isLabelsVisible() const |
|
353 | 355 | { |
|
354 | 356 | Q_D(const QAbstractBarSeries); |
|
355 | 357 | return d->m_labelsVisible; |
|
356 | 358 | } |
|
357 | 359 | |
|
358 | 360 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
359 | 361 | |
|
360 | 362 | QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) : |
|
361 | 363 | QAbstractSeriesPrivate(q), |
|
362 | 364 | m_barWidth(0.5), // Default value is 50% of category width |
|
363 | 365 | m_labelsVisible(false), |
|
364 | 366 | m_visible(true) |
|
365 | 367 | { |
|
366 | 368 | } |
|
367 | 369 | |
|
368 | 370 | int QAbstractBarSeriesPrivate::categoryCount() const |
|
369 | 371 | { |
|
370 | 372 | // No categories defined. return count of longest set. |
|
371 | 373 | int count = 0; |
|
372 | 374 | for (int i=0; i<m_barSets.count(); i++) { |
|
373 | 375 | if (m_barSets.at(i)->count() > count) { |
|
374 | 376 | count = m_barSets.at(i)->count(); |
|
375 | 377 | } |
|
376 | 378 | } |
|
377 | 379 | |
|
378 | 380 | return count; |
|
379 | 381 | } |
|
380 | 382 | |
|
381 | 383 | void QAbstractBarSeriesPrivate::setBarWidth(qreal width) |
|
382 | 384 | { |
|
383 | 385 | if (width < 0.0) { |
|
384 | 386 | width = 0.0; |
|
385 | 387 | } |
|
386 | 388 | m_barWidth = width; |
|
387 | 389 | emit updatedBars(); |
|
388 | 390 | } |
|
389 | 391 | |
|
390 | 392 | qreal QAbstractBarSeriesPrivate::barWidth() const |
|
391 | 393 | { |
|
392 | 394 | return m_barWidth; |
|
393 | 395 | } |
|
394 | 396 | |
|
395 | 397 | QBarSet* QAbstractBarSeriesPrivate::barsetAt(int index) |
|
396 | 398 | { |
|
397 | 399 | return m_barSets.at(index); |
|
398 | 400 | } |
|
399 | 401 | |
|
400 | 402 | void QAbstractBarSeriesPrivate::setVisible(bool visible) |
|
401 | 403 | { |
|
402 | 404 | m_visible = visible; |
|
403 | 405 | emit updatedBars(); |
|
404 | 406 | } |
|
405 | 407 | |
|
406 | 408 | void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible) |
|
407 | 409 | { |
|
408 | 410 | m_labelsVisible = visible; |
|
409 | 411 | emit labelsVisibleChanged(visible); |
|
410 | 412 | } |
|
411 | 413 | |
|
412 | 414 | qreal QAbstractBarSeriesPrivate::min() |
|
413 | 415 | { |
|
414 | 416 | if (m_barSets.count() <= 0) { |
|
415 | 417 | return 0; |
|
416 | 418 | } |
|
417 | 419 | qreal min = INT_MAX; |
|
418 | 420 | |
|
419 | 421 | for (int i = 0; i < m_barSets.count(); i++) { |
|
420 | 422 | int categoryCount = m_barSets.at(i)->count(); |
|
421 | 423 | for (int j = 0; j < categoryCount; j++) { |
|
422 | 424 | qreal temp = m_barSets.at(i)->at(j); |
|
423 | 425 | if (temp < min) |
|
424 | 426 | min = temp; |
|
425 | 427 | } |
|
426 | 428 | } |
|
427 | 429 | return min; |
|
428 | 430 | } |
|
429 | 431 | |
|
430 | 432 | qreal QAbstractBarSeriesPrivate::max() |
|
431 | 433 | { |
|
432 | 434 | if (m_barSets.count() <= 0) { |
|
433 | 435 | return 0; |
|
434 | 436 | } |
|
435 | 437 | qreal max = INT_MIN; |
|
436 | 438 | |
|
437 | 439 | for (int i = 0; i < m_barSets.count(); i++) { |
|
438 | 440 | int categoryCount = m_barSets.at(i)->count(); |
|
439 | 441 | for (int j = 0; j < categoryCount; j++) { |
|
440 | 442 | qreal temp = m_barSets.at(i)->at(j); |
|
441 | 443 | if (temp > max) |
|
442 | 444 | max = temp; |
|
443 | 445 | } |
|
444 | 446 | } |
|
445 | 447 | |
|
446 | 448 | return max; |
|
447 | 449 | } |
|
448 | 450 | |
|
449 | 451 | qreal QAbstractBarSeriesPrivate::valueAt(int set, int category) |
|
450 | 452 | { |
|
451 | 453 | if ((set < 0) || (set >= m_barSets.count())) { |
|
452 | 454 | // No set, no value. |
|
453 | 455 | return 0; |
|
454 | 456 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
455 | 457 | // No category, no value. |
|
456 | 458 | return 0; |
|
457 | 459 | } |
|
458 | 460 | |
|
459 | 461 | return m_barSets.at(set)->at(category); |
|
460 | 462 | } |
|
461 | 463 | |
|
462 | 464 | qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category) |
|
463 | 465 | { |
|
464 | 466 | if ((set < 0) || (set >= m_barSets.count())) { |
|
465 | 467 | // No set, no value. |
|
466 | 468 | return 0; |
|
467 | 469 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
468 | 470 | // No category, no value. |
|
469 | 471 | return 0; |
|
470 | 472 | } |
|
471 | 473 | |
|
472 | 474 | qreal value = m_barSets.at(set)->at(category); |
|
473 | 475 | qreal sum = categorySum(category); |
|
474 | 476 | if ( qFuzzyIsNull(sum) ) { |
|
475 | 477 | return 0; |
|
476 | 478 | } |
|
477 | 479 | |
|
478 | 480 | return value / sum; |
|
479 | 481 | } |
|
480 | 482 | |
|
481 | 483 | qreal QAbstractBarSeriesPrivate::categorySum(int category) |
|
482 | 484 | { |
|
483 | 485 | qreal sum(0); |
|
484 | 486 | int count = m_barSets.count(); // Count sets |
|
485 | 487 | for (int set = 0; set < count; set++) { |
|
486 | 488 | if (category < m_barSets.at(set)->count()) |
|
487 | 489 | sum += m_barSets.at(set)->at(category); |
|
488 | 490 | } |
|
489 | 491 | return sum; |
|
490 | 492 | } |
|
491 | 493 | |
|
492 | 494 | qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category) |
|
493 | 495 | { |
|
494 | 496 | qreal sum(0); |
|
495 | 497 | int count = m_barSets.count(); // Count sets |
|
496 | 498 | for (int set = 0; set < count; set++) { |
|
497 | 499 | if (category < m_barSets.at(set)->count()) |
|
498 | 500 | sum += qAbs(m_barSets.at(set)->at(category)); |
|
499 | 501 | } |
|
500 | 502 | return sum; |
|
501 | 503 | } |
|
502 | 504 | |
|
503 | 505 | qreal QAbstractBarSeriesPrivate::maxCategorySum() |
|
504 | 506 | { |
|
505 | 507 | qreal max = INT_MIN; |
|
506 | 508 | int count = categoryCount(); |
|
507 | 509 | for (int i = 0; i < count; i++) { |
|
508 | 510 | qreal sum = categorySum(i); |
|
509 | 511 | if (sum > max) |
|
510 | 512 | max = sum; |
|
511 | 513 | } |
|
512 | 514 | return max; |
|
513 | 515 | } |
|
514 | 516 | |
|
515 | 517 | qreal QAbstractBarSeriesPrivate::minX() |
|
516 | 518 | { |
|
517 | 519 | if (m_barSets.count() <= 0) { |
|
518 | 520 | return 0; |
|
519 | 521 | } |
|
520 | 522 | qreal min = INT_MAX; |
|
521 | 523 | |
|
522 | 524 | for (int i = 0; i < m_barSets.count(); i++) { |
|
523 | 525 | int categoryCount = m_barSets.at(i)->count(); |
|
524 | 526 | for (int j = 0; j < categoryCount; j++) { |
|
525 | 527 | qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x(); |
|
526 | 528 | if (temp < min) |
|
527 | 529 | min = temp; |
|
528 | 530 | } |
|
529 | 531 | } |
|
530 | 532 | return min; |
|
531 | 533 | } |
|
532 | 534 | |
|
533 | 535 | qreal QAbstractBarSeriesPrivate::maxX() |
|
534 | 536 | { |
|
535 | 537 | if (m_barSets.count() <= 0) { |
|
536 | 538 | return 0; |
|
537 | 539 | } |
|
538 | 540 | qreal max = INT_MIN; |
|
539 | 541 | |
|
540 | 542 | for (int i = 0; i < m_barSets.count(); i++) { |
|
541 | 543 | int categoryCount = m_barSets.at(i)->count(); |
|
542 | 544 | for (int j = 0; j < categoryCount; j++) { |
|
543 | 545 | qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x(); |
|
544 | 546 | if (temp > max) |
|
545 | 547 | max = temp; |
|
546 | 548 | } |
|
547 | 549 | } |
|
548 | 550 | |
|
549 | 551 | return max; |
|
550 | 552 | } |
|
551 | 553 | |
|
552 | 554 | |
|
553 | 555 | void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain) |
|
554 | 556 | { |
|
555 | 557 | qreal minX(domain.minX()); |
|
556 | 558 | qreal minY(domain.minY()); |
|
557 | 559 | qreal maxX(domain.maxX()); |
|
558 | 560 | qreal maxY(domain.maxY()); |
|
559 | 561 | |
|
560 | 562 | qreal seriesMinX = this->minX(); |
|
561 | 563 | qreal seriesMaxX = this->maxX(); |
|
562 | 564 | qreal y = max(); |
|
563 | 565 | minX = qMin(minX, seriesMinX - (qreal)0.5); |
|
564 | 566 | minY = qMin(minY, y); |
|
565 | 567 | maxX = qMax(maxX, seriesMaxX + (qreal)0.5); |
|
566 | 568 | maxY = qMax(maxY, y); |
|
567 | 569 | |
|
568 | 570 | domain.setRange(minX,maxX,minY,maxY); |
|
569 | 571 | } |
|
570 | 572 | |
|
571 | 573 | ChartElement* QAbstractBarSeriesPrivate::createGraphics(ChartPresenter* presenter) |
|
572 | 574 | { |
|
573 | 575 | Q_UNUSED(presenter); |
|
574 | 576 | qWarning() << "QAbstractBarSeriesPrivate::createGraphics called"; |
|
575 | 577 | return 0; |
|
576 | 578 | } |
|
577 | 579 | |
|
578 | 580 | QList<LegendMarker*> QAbstractBarSeriesPrivate::createLegendMarker(QLegend* legend) |
|
579 | 581 | { |
|
580 | 582 | Q_Q(QAbstractBarSeries); |
|
581 | 583 | QList<LegendMarker*> markers; |
|
582 | 584 | foreach(QBarSet* set, q->barSets()) { |
|
583 | 585 | BarLegendMarker* marker = new BarLegendMarker(q,set,legend); |
|
584 | 586 | markers << marker; |
|
585 | 587 | } |
|
586 | 588 | |
|
587 | 589 | return markers; |
|
588 | 590 | } |
|
589 | 591 | |
|
590 | 592 | bool QAbstractBarSeriesPrivate::append(QBarSet *set) |
|
591 | 593 | { |
|
592 | 594 | Q_Q(QAbstractBarSeries); |
|
593 | 595 | if ((m_barSets.contains(set)) || (set == 0)) { |
|
594 | 596 | // Fail if set is already in list or set is null. |
|
595 | 597 | return false; |
|
596 | 598 | } |
|
597 | 599 | m_barSets.append(set); |
|
598 | 600 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
599 | 601 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
600 | 602 | emit restructuredBars(); // this notifies barchartitem |
|
601 | 603 | if (m_dataset) { |
|
602 | 604 | m_dataset->updateSeries(q); // this notifies legend |
|
603 | 605 | } |
|
604 | 606 | return true; |
|
605 | 607 | } |
|
606 | 608 | |
|
607 | 609 | bool QAbstractBarSeriesPrivate::remove(QBarSet *set) |
|
608 | 610 | { |
|
609 | 611 | Q_Q(QAbstractBarSeries); |
|
610 | 612 | if (!m_barSets.contains(set)) { |
|
611 | 613 | // Fail if set is not in list |
|
612 | 614 | return false; |
|
613 | 615 | } |
|
614 | 616 | m_barSets.removeOne(set); |
|
615 | 617 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
616 | 618 | QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
617 | 619 | emit restructuredBars(); // this notifies barchartitem |
|
618 | 620 | if (m_dataset) { |
|
619 | 621 | m_dataset->updateSeries(q); // this notifies legend |
|
620 | 622 | } |
|
621 | 623 | return true; |
|
622 | 624 | } |
|
623 | 625 | |
|
624 | 626 | bool QAbstractBarSeriesPrivate::append(QList<QBarSet* > sets) |
|
625 | 627 | { |
|
626 | 628 | Q_Q(QAbstractBarSeries); |
|
627 | 629 | foreach (QBarSet* set, sets) { |
|
628 | 630 | if ((set == 0) || (m_barSets.contains(set))) { |
|
629 | 631 | // Fail if any of the sets is null or is already appended. |
|
630 | 632 | return false; |
|
631 | 633 | } |
|
632 | 634 | if (sets.count(set) != 1) { |
|
633 | 635 | // Also fail if same set is more than once in given list. |
|
634 | 636 | return false; |
|
635 | 637 | } |
|
636 | 638 | } |
|
637 | 639 | |
|
638 | 640 | foreach (QBarSet* set, sets) { |
|
639 | 641 | m_barSets.append(set); |
|
640 | 642 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
641 | 643 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
642 | 644 | } |
|
643 | 645 | emit restructuredBars(); // this notifies barchartitem |
|
644 | 646 | if (m_dataset) { |
|
645 | 647 | m_dataset->updateSeries(q); // this notifies legend |
|
646 | 648 | } |
|
647 | 649 | return true; |
|
648 | 650 | } |
|
649 | 651 | |
|
650 | 652 | bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets) |
|
651 | 653 | { |
|
652 | 654 | Q_Q(QAbstractBarSeries); |
|
653 | 655 | if (sets.count() == 0) { |
|
654 | 656 | return false; |
|
655 | 657 | } |
|
656 | 658 | foreach (QBarSet* set, sets) { |
|
657 | 659 | if ((set == 0) || (!m_barSets.contains(set))) { |
|
658 | 660 | // Fail if any of the sets is null or is not in series |
|
659 | 661 | return false; |
|
660 | 662 | } |
|
661 | 663 | if (sets.count(set) != 1) { |
|
662 | 664 | // Also fail if same set is more than once in given list. |
|
663 | 665 | return false; |
|
664 | 666 | } |
|
665 | 667 | } |
|
666 | 668 | |
|
667 | 669 | foreach (QBarSet* set, sets) { |
|
668 | 670 | m_barSets.removeOne(set); |
|
669 | 671 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
670 | 672 | QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
671 | 673 | } |
|
672 | 674 | |
|
673 | 675 | emit restructuredBars(); // this notifies barchartitem |
|
674 | 676 | if (m_dataset) { |
|
675 | 677 | m_dataset->updateSeries(q); // this notifies legend |
|
676 | 678 | } |
|
677 | 679 | return true; |
|
678 | 680 | } |
|
679 | 681 | |
|
680 | 682 | bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set) |
|
681 | 683 | { |
|
682 | 684 | Q_Q(QAbstractBarSeries); |
|
683 | 685 | if ((m_barSets.contains(set)) || (set == 0)) { |
|
684 | 686 | // Fail if set is already in list or set is null. |
|
685 | 687 | return false; |
|
686 | 688 | } |
|
687 | 689 | m_barSets.insert(index, set); |
|
688 | 690 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
689 | 691 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
690 | 692 | emit restructuredBars(); // this notifies barchartitem |
|
691 | 693 | if (m_dataset) { |
|
692 | 694 | m_dataset->updateSeries(q); // this notifies legend |
|
693 | 695 | } |
|
694 | 696 | return true; |
|
695 | 697 | } |
|
696 | 698 | |
|
697 | 699 | void QAbstractBarSeriesPrivate::initializeAxis(QAbstractAxis* axis) |
|
698 | 700 | { |
|
699 | 701 | Q_Q(QAbstractBarSeries); |
|
700 | 702 | |
|
701 | 703 | if(axis->type()==QAbstractAxis::AxisTypeCategories) { |
|
702 | 704 | |
|
703 | 705 | switch(q->type()) { |
|
704 | 706 | |
|
705 | 707 | case QAbstractSeries::SeriesTypeHorizontalBar: |
|
706 | 708 | case QAbstractSeries::SeriesTypeHorizontalPercentBar: |
|
707 | 709 | case QAbstractSeries::SeriesTypeHorizontalStackedBar: { |
|
708 | 710 | |
|
709 | 711 | if(axis->orientation()==Qt::Vertical) |
|
710 | 712 | { |
|
711 | 713 | populateCategories(qobject_cast<QBarCategoriesAxis*>(axis)); |
|
712 | 714 | } |
|
713 | 715 | break; |
|
714 | 716 | } |
|
715 | 717 | case QAbstractSeries::SeriesTypeBar: |
|
716 | 718 | case QAbstractSeries::SeriesTypePercentBar: |
|
717 | 719 | case QAbstractSeries::SeriesTypeStackedBar: { |
|
718 | 720 | |
|
719 | 721 | if(axis->orientation()==Qt::Horizontal) |
|
720 | 722 | { |
|
721 | 723 | populateCategories(qobject_cast<QBarCategoriesAxis*>(axis)); |
|
722 | 724 | } |
|
723 | 725 | break; |
|
724 | 726 | } |
|
725 | 727 | default: |
|
726 | 728 | qWarning()<<"Unexpected series type"; |
|
727 | 729 | break; |
|
728 | 730 | |
|
729 | 731 | } |
|
730 | 732 | } |
|
731 | 733 | } |
|
732 | 734 | |
|
733 | 735 | QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const |
|
734 | 736 | { |
|
735 | 737 | Q_Q(const QAbstractBarSeries); |
|
736 | 738 | |
|
737 | 739 | switch(q->type()) { |
|
738 | 740 | |
|
739 | 741 | case QAbstractSeries::SeriesTypeHorizontalBar: |
|
740 | 742 | case QAbstractSeries::SeriesTypeHorizontalPercentBar: |
|
741 | 743 | case QAbstractSeries::SeriesTypeHorizontalStackedBar: { |
|
742 | 744 | |
|
743 | 745 | if(orientation==Qt::Vertical) |
|
744 | 746 | { |
|
745 | 747 | return QAbstractAxis::AxisTypeCategories; |
|
746 | 748 | } |
|
747 | 749 | break; |
|
748 | 750 | } |
|
749 | 751 | case QAbstractSeries::SeriesTypeBar: |
|
750 | 752 | case QAbstractSeries::SeriesTypePercentBar: |
|
751 | 753 | case QAbstractSeries::SeriesTypeStackedBar: { |
|
752 | 754 | |
|
753 | 755 | if(orientation==Qt::Horizontal) |
|
754 | 756 | { |
|
755 | 757 | return QAbstractAxis::AxisTypeCategories; |
|
756 | 758 | } |
|
757 | 759 | break; |
|
758 | 760 | } |
|
759 | 761 | default: |
|
760 | 762 | qWarning()<<"Unexpected series type"; |
|
761 | 763 | break; |
|
762 | 764 | |
|
763 | 765 | } |
|
764 | 766 | return QAbstractAxis::AxisTypeValues; |
|
765 | 767 | |
|
766 | 768 | } |
|
767 | 769 | |
|
768 | 770 | void QAbstractBarSeriesPrivate::populateCategories(QBarCategoriesAxis* axis) |
|
769 | 771 | { |
|
770 | 772 | QStringList categories; |
|
771 | 773 | if(axis->categories().isEmpty()) { |
|
772 | 774 | for (int i(1); i < categoryCount()+1; i++) |
|
773 | 775 | categories << QString::number(i); |
|
774 | 776 | axis->append(categories); |
|
775 | 777 | } |
|
776 | 778 | } |
|
777 | 779 | |
|
778 | 780 | #include "moc_qabstractbarseries.cpp" |
|
779 | 781 | #include "moc_qabstractbarseries_p.cpp" |
|
780 | 782 | |
|
781 | 783 | |
|
782 | 784 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now