@@ -1,690 +1,701 | |||
|
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 "qbarseries.h" |
|
22 | 22 | #include "qbarseries_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 "chartanimator_p.h" |
|
30 | 30 | |
|
31 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
32 | 32 | |
|
33 | 33 | /*! |
|
34 | 34 | \class QBarSeries |
|
35 | 35 | \brief part of QtCommercial chart API. |
|
36 | 36 | \mainclass |
|
37 | 37 | |
|
38 | 38 | QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to |
|
39 | 39 | the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar |
|
40 | 40 | and y-value is the height of the bar. The category names are ignored with this series and x-axis |
|
41 | 41 | shows the x-values. |
|
42 | 42 | |
|
43 | 43 | See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart. |
|
44 | 44 | \image examples_barchart.png |
|
45 | 45 | |
|
46 | 46 | \sa QBarSet, QStackedBarSeries, QPercentBarSeries |
|
47 | 47 | */ |
|
48 | 48 | |
|
49 | 49 | /*! |
|
50 | 50 | \property QBarSeries::barWidth |
|
51 | 51 | \brief 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 |
|
52 | 52 | is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen |
|
53 | 53 | is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. |
|
54 | 54 | Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. This is |
|
55 | 55 | because with grouped series it is more logical to set width of whole group and let the chart calculate correct |
|
56 | 56 | width for bar. |
|
57 | 57 | \sa QGroupedBarSeries |
|
58 | 58 | */ |
|
59 | 59 | |
|
60 | 60 | /*! |
|
61 | 61 | \property QBarSeries::count |
|
62 | 62 | \brief Holds the number of sets in series. |
|
63 | 63 | */ |
|
64 | 64 | |
|
65 | 65 | /*! |
|
66 | 66 | \property QBarSeries::labelsVisible |
|
67 | 67 | \brief Defines the visibility of the labels in series |
|
68 | 68 | */ |
|
69 | 69 | |
|
70 | 70 | /*! |
|
71 | 71 | \fn void QBarSeries::clicked(QBarSet *barset, int index) |
|
72 | 72 | |
|
73 | 73 | The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset. |
|
74 | 74 | Clicked bar inside set is indexed by \a index |
|
75 | 75 | */ |
|
76 | 76 | |
|
77 | 77 | /*! |
|
78 | 78 | \fn void QBarSeries::hovered(QBarSet* barset, bool status) |
|
79 | 79 | |
|
80 | 80 | The signal is emitted if mouse is hovered on top of series. |
|
81 | 81 | Parameter \a barset is the pointer of barset, where hover happened. |
|
82 | 82 | Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series. |
|
83 | 83 | */ |
|
84 | /*! | |
|
85 | \fn void QBarSeries::barWidthChanged() | |
|
86 | ||
|
87 | This signal is emitted when bar width has been changed. | |
|
88 | */ | |
|
89 | ||
|
90 | /*! | |
|
91 | \fn void QBarSeries::barsetCountChanged() | |
|
92 | ||
|
93 | This signal is emitted when barset count has been changed, for example by append or remove. | |
|
94 | */ | |
|
84 | 95 | |
|
85 | 96 | /*! |
|
86 | 97 | \fn void QBarSeries::labelsVisibleChanged() |
|
87 | 98 | |
|
88 | 99 | This signal is emitted when labels visibility have changed. |
|
89 | 100 | |
|
90 | 101 | \sa isLabelsVisible(), setLabelsVisible() |
|
91 | 102 | */ |
|
92 | 103 | |
|
93 | 104 | /*! |
|
94 | 105 | \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets) |
|
95 | 106 | |
|
96 | 107 | This signal is emitted when \a sets have been added to the series. |
|
97 | 108 | |
|
98 | 109 | \sa append(), insert() |
|
99 | 110 | */ |
|
100 | 111 | |
|
101 | 112 | /*! |
|
102 | 113 | \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets) |
|
103 | 114 | |
|
104 | 115 | This signal is emitted when \a sets have been removed from the series. |
|
105 | 116 | |
|
106 | 117 | \sa remove() |
|
107 | 118 | */ |
|
108 | 119 | |
|
109 | 120 | /*! |
|
110 | 121 | Constructs empty QBarSeries. |
|
111 | 122 | QBarSeries is QObject which is a child of a \a parent. |
|
112 | 123 | */ |
|
113 | 124 | QBarSeries::QBarSeries(QObject *parent) : |
|
114 | 125 | QAbstractSeries(*new QBarSeriesPrivate(this),parent) |
|
115 | 126 | { |
|
116 | 127 | } |
|
117 | 128 | |
|
118 | 129 | /*! |
|
119 | 130 | Destructs barseries and owned barsets. |
|
120 | 131 | */ |
|
121 | 132 | QBarSeries::~QBarSeries() |
|
122 | 133 | { |
|
123 | 134 | Q_D(QBarSeries); |
|
124 | 135 | if(d->m_dataset){ |
|
125 | 136 | d->m_dataset->removeSeries(this); |
|
126 | 137 | } |
|
127 | 138 | } |
|
128 | 139 | |
|
129 | 140 | /*! |
|
130 | 141 | \internal |
|
131 | 142 | */ |
|
132 | 143 | QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) : |
|
133 | 144 | QAbstractSeries(d,parent) |
|
134 | 145 | { |
|
135 | 146 | } |
|
136 | 147 | |
|
137 | 148 | /*! |
|
138 | 149 | Returns the type of series. Derived classes override this. |
|
139 | 150 | */ |
|
140 | 151 | QAbstractSeries::SeriesType QBarSeries::type() const |
|
141 | 152 | { |
|
142 | 153 | return QAbstractSeries::SeriesTypeBar; |
|
143 | 154 | } |
|
144 | 155 | |
|
145 | 156 | /*! |
|
146 | 157 | 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 |
|
147 | 158 | is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen |
|
148 | 159 | is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis. |
|
149 | 160 | Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. This is |
|
150 | 161 | because with grouped series it is more logical to set widht of whole group and let the chart calculate correct |
|
151 | 162 | width for bar. |
|
152 | 163 | \sa QGroupedBarSeries |
|
153 | 164 | */ |
|
154 | 165 | void QBarSeries::setBarWidth(qreal width) |
|
155 | 166 | { |
|
156 | 167 | Q_D(QBarSeries); |
|
157 | 168 | d->setBarWidth(width); |
|
158 | 169 | emit barWidthChanged(); |
|
159 | 170 | } |
|
160 | 171 | |
|
161 | 172 | /*! |
|
162 | 173 | Returns the width of bars. |
|
163 | 174 | */ |
|
164 | 175 | qreal QBarSeries::barWidth() const |
|
165 | 176 | { |
|
166 | 177 | Q_D(const QBarSeries); |
|
167 | 178 | return d->barWidth(); |
|
168 | 179 | } |
|
169 | 180 | |
|
170 | 181 | /*! |
|
171 | 182 | 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. |
|
172 | 183 | Returns true, if appending succeeded. |
|
173 | 184 | |
|
174 | 185 | */ |
|
175 | 186 | bool QBarSeries::append(QBarSet *set) |
|
176 | 187 | { |
|
177 | 188 | Q_D(QBarSeries); |
|
178 | 189 | bool success = d->append(set); |
|
179 | 190 | if (success) { |
|
180 | 191 | QList<QBarSet*> sets; |
|
181 | 192 | sets.append(set); |
|
182 | 193 | emit barsetsAdded(sets); |
|
183 | 194 | emit barsetCountChanged(); |
|
184 | 195 | } |
|
185 | 196 | return success; |
|
186 | 197 | } |
|
187 | 198 | |
|
188 | 199 | /*! |
|
189 | 200 | Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set. |
|
190 | 201 | Returns true, if set was removed. |
|
191 | 202 | */ |
|
192 | 203 | bool QBarSeries::remove(QBarSet *set) |
|
193 | 204 | { |
|
194 | 205 | Q_D(QBarSeries); |
|
195 | 206 | bool success = d->remove(set); |
|
196 | 207 | if (success) { |
|
197 | 208 | QList<QBarSet*> sets; |
|
198 | 209 | sets.append(set); |
|
199 | 210 | emit barsetsRemoved(sets); |
|
200 | 211 | emit barsetCountChanged(); |
|
201 | 212 | } |
|
202 | 213 | return success; |
|
203 | 214 | } |
|
204 | 215 | |
|
205 | 216 | /*! |
|
206 | 217 | Adds a list of barsets to series. Takes ownership of \a sets. |
|
207 | 218 | Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series, |
|
208 | 219 | nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended |
|
209 | 220 | and function returns false. |
|
210 | 221 | */ |
|
211 | 222 | bool QBarSeries::append(QList<QBarSet* > sets) |
|
212 | 223 | { |
|
213 | 224 | Q_D(QBarSeries); |
|
214 | 225 | bool success = d->append(sets); |
|
215 | 226 | if (success) { |
|
216 | 227 | emit barsetsAdded(sets); |
|
217 | 228 | emit barsetCountChanged(); |
|
218 | 229 | } |
|
219 | 230 | return success; |
|
220 | 231 | } |
|
221 | 232 | |
|
222 | 233 | /*! |
|
223 | 234 | 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. |
|
224 | 235 | Returns true, if inserting succeeded. |
|
225 | 236 | |
|
226 | 237 | */ |
|
227 | 238 | bool QBarSeries::insert(int index, QBarSet *set) |
|
228 | 239 | { |
|
229 | 240 | Q_D(QBarSeries); |
|
230 | 241 | bool success = d->insert(index, set); |
|
231 | 242 | if (success) { |
|
232 | 243 | QList<QBarSet*> sets; |
|
233 | 244 | sets.append(set); |
|
234 | 245 | emit barsetsAdded(sets); |
|
235 | 246 | emit barsetCountChanged(); |
|
236 | 247 | } |
|
237 | 248 | return success; |
|
238 | 249 | } |
|
239 | 250 | |
|
240 | 251 | /*! |
|
241 | 252 | Removes all of the bar sets from the series |
|
242 | 253 | */ |
|
243 | 254 | void QBarSeries::clear() |
|
244 | 255 | { |
|
245 | 256 | Q_D(QBarSeries); |
|
246 | 257 | QList<QBarSet *> sets = barSets(); |
|
247 | 258 | bool success = d->remove(sets); |
|
248 | 259 | if (success) { |
|
249 | 260 | emit barsetsRemoved(sets); |
|
250 | 261 | emit barsetCountChanged(); |
|
251 | 262 | } |
|
252 | 263 | } |
|
253 | 264 | |
|
254 | 265 | /*! |
|
255 | 266 | Returns number of sets in series. |
|
256 | 267 | */ |
|
257 | 268 | int QBarSeries::barsetCount() const |
|
258 | 269 | { |
|
259 | 270 | Q_D(const QBarSeries); |
|
260 | 271 | return d->m_barSets.count(); |
|
261 | 272 | } |
|
262 | 273 | |
|
263 | 274 | /*! |
|
264 | 275 | Returns a list of sets in series. Keeps ownership of sets. |
|
265 | 276 | */ |
|
266 | 277 | QList<QBarSet*> QBarSeries::barSets() const |
|
267 | 278 | { |
|
268 | 279 | Q_D(const QBarSeries); |
|
269 | 280 | return d->m_barSets; |
|
270 | 281 | } |
|
271 | 282 | |
|
272 | 283 | /*! |
|
273 | 284 | Sets the visibility of labels in series to \a visible |
|
274 | 285 | */ |
|
275 | 286 | void QBarSeries::setLabelsVisible(bool visible) |
|
276 | 287 | { |
|
277 | 288 | Q_D(QBarSeries); |
|
278 | 289 | if (d->m_labelsVisible != visible) { |
|
279 | 290 | d->setLabelsVisible(visible); |
|
280 | 291 | emit labelsVisibleChanged(); |
|
281 | 292 | } |
|
282 | 293 | } |
|
283 | 294 | |
|
284 | 295 | /*! |
|
285 | 296 | Returns the visibility of labels |
|
286 | 297 | */ |
|
287 | 298 | bool QBarSeries::isLabelsVisible() const |
|
288 | 299 | { |
|
289 | 300 | Q_D(const QBarSeries); |
|
290 | 301 | return d->m_labelsVisible; |
|
291 | 302 | } |
|
292 | 303 | |
|
293 | 304 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
294 | 305 | |
|
295 | 306 | QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) : |
|
296 | 307 | QAbstractSeriesPrivate(q), |
|
297 | 308 | m_barWidth(0.5), // Default value is 50% of category width |
|
298 | 309 | m_labelsVisible(false), |
|
299 | 310 | m_visible(true) |
|
300 | 311 | { |
|
301 | 312 | } |
|
302 | 313 | |
|
303 | 314 | void QBarSeriesPrivate::setCategories(QStringList categories) |
|
304 | 315 | { |
|
305 | 316 | m_categories = categories; |
|
306 | 317 | } |
|
307 | 318 | |
|
308 | 319 | void QBarSeriesPrivate::insertCategory(int index, const QString category) |
|
309 | 320 | { |
|
310 | 321 | m_categories.insert(index, category); |
|
311 | 322 | emit categoriesUpdated(); |
|
312 | 323 | } |
|
313 | 324 | |
|
314 | 325 | void QBarSeriesPrivate::removeCategory(int index) |
|
315 | 326 | { |
|
316 | 327 | m_categories.removeAt(index); |
|
317 | 328 | emit categoriesUpdated(); |
|
318 | 329 | } |
|
319 | 330 | |
|
320 | 331 | int QBarSeriesPrivate::categoryCount() const |
|
321 | 332 | { |
|
322 | 333 | if (m_categories.count() > 0) { |
|
323 | 334 | return m_categories.count(); |
|
324 | 335 | } |
|
325 | 336 | |
|
326 | 337 | // No categories defined. return count of longest set. |
|
327 | 338 | int count = 0; |
|
328 | 339 | for (int i=0; i<m_barSets.count(); i++) { |
|
329 | 340 | if (m_barSets.at(i)->count() > count) { |
|
330 | 341 | count = m_barSets.at(i)->count(); |
|
331 | 342 | } |
|
332 | 343 | } |
|
333 | 344 | |
|
334 | 345 | return count; |
|
335 | 346 | } |
|
336 | 347 | |
|
337 | 348 | QStringList QBarSeriesPrivate::categories() const |
|
338 | 349 | { |
|
339 | 350 | if (m_categories.count() > 0) { |
|
340 | 351 | return m_categories; |
|
341 | 352 | } |
|
342 | 353 | |
|
343 | 354 | // No categories defined. retun list of indices. |
|
344 | 355 | QStringList categories; |
|
345 | 356 | |
|
346 | 357 | int count = categoryCount(); |
|
347 | 358 | for (int i = 0; i < count; i++) { |
|
348 | 359 | categories.append(QString::number(i)); |
|
349 | 360 | } |
|
350 | 361 | return categories; |
|
351 | 362 | } |
|
352 | 363 | |
|
353 | 364 | void QBarSeriesPrivate::setBarWidth(qreal width) |
|
354 | 365 | { |
|
355 | 366 | if (width < 0.0) { |
|
356 | 367 | width = 0.0; |
|
357 | 368 | } |
|
358 | 369 | m_barWidth = width; |
|
359 | 370 | emit updatedBars(); |
|
360 | 371 | } |
|
361 | 372 | |
|
362 | 373 | qreal QBarSeriesPrivate::barWidth() const |
|
363 | 374 | { |
|
364 | 375 | return m_barWidth; |
|
365 | 376 | } |
|
366 | 377 | |
|
367 | 378 | QBarSet* QBarSeriesPrivate::barsetAt(int index) |
|
368 | 379 | { |
|
369 | 380 | return m_barSets.at(index); |
|
370 | 381 | } |
|
371 | 382 | |
|
372 | 383 | void QBarSeriesPrivate::setVisible(bool visible) |
|
373 | 384 | { |
|
374 | 385 | m_visible = visible; |
|
375 | 386 | emit updatedBars(); |
|
376 | 387 | } |
|
377 | 388 | |
|
378 | 389 | void QBarSeriesPrivate::setLabelsVisible(bool visible) |
|
379 | 390 | { |
|
380 | 391 | m_labelsVisible = visible; |
|
381 | 392 | emit labelsVisibleChanged(visible); |
|
382 | 393 | } |
|
383 | 394 | |
|
384 | 395 | QString QBarSeriesPrivate::categoryName(int category) |
|
385 | 396 | { |
|
386 | 397 | if ((category >= 0) && (category < m_categories.count())) { |
|
387 | 398 | return m_categories.at(category); |
|
388 | 399 | } |
|
389 | 400 | |
|
390 | 401 | return QString::number(category); |
|
391 | 402 | } |
|
392 | 403 | |
|
393 | 404 | qreal QBarSeriesPrivate::min() |
|
394 | 405 | { |
|
395 | 406 | if (m_barSets.count() <= 0) { |
|
396 | 407 | return 0; |
|
397 | 408 | } |
|
398 | 409 | qreal min = INT_MAX; |
|
399 | 410 | |
|
400 | 411 | for (int i = 0; i < m_barSets.count(); i++) { |
|
401 | 412 | int categoryCount = m_barSets.at(i)->count(); |
|
402 | 413 | for (int j = 0; j < categoryCount; j++) { |
|
403 | 414 | qreal temp = m_barSets.at(i)->at(j).y(); |
|
404 | 415 | if (temp < min) |
|
405 | 416 | min = temp; |
|
406 | 417 | } |
|
407 | 418 | } |
|
408 | 419 | return min; |
|
409 | 420 | } |
|
410 | 421 | |
|
411 | 422 | qreal QBarSeriesPrivate::max() |
|
412 | 423 | { |
|
413 | 424 | if (m_barSets.count() <= 0) { |
|
414 | 425 | return 0; |
|
415 | 426 | } |
|
416 | 427 | qreal max = INT_MIN; |
|
417 | 428 | |
|
418 | 429 | for (int i = 0; i < m_barSets.count(); i++) { |
|
419 | 430 | int categoryCount = m_barSets.at(i)->count(); |
|
420 | 431 | for (int j = 0; j < categoryCount; j++) { |
|
421 | 432 | qreal temp = m_barSets.at(i)->at(j).y(); |
|
422 | 433 | if (temp > max) |
|
423 | 434 | max = temp; |
|
424 | 435 | } |
|
425 | 436 | } |
|
426 | 437 | |
|
427 | 438 | return max; |
|
428 | 439 | } |
|
429 | 440 | |
|
430 | 441 | qreal QBarSeriesPrivate::valueAt(int set, int category) |
|
431 | 442 | { |
|
432 | 443 | if ((set < 0) || (set >= m_barSets.count())) { |
|
433 | 444 | // No set, no value. |
|
434 | 445 | return 0; |
|
435 | 446 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
436 | 447 | // No category, no value. |
|
437 | 448 | return 0; |
|
438 | 449 | } |
|
439 | 450 | |
|
440 | 451 | return m_barSets.at(set)->at(category).y(); |
|
441 | 452 | } |
|
442 | 453 | |
|
443 | 454 | qreal QBarSeriesPrivate::percentageAt(int set, int category) |
|
444 | 455 | { |
|
445 | 456 | if ((set < 0) || (set >= m_barSets.count())) { |
|
446 | 457 | // No set, no value. |
|
447 | 458 | return 0; |
|
448 | 459 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
449 | 460 | // No category, no value. |
|
450 | 461 | return 0; |
|
451 | 462 | } |
|
452 | 463 | |
|
453 | 464 | qreal value = m_barSets.at(set)->at(category).y(); |
|
454 | 465 | qreal sum = categorySum(category); |
|
455 | 466 | if ( qFuzzyIsNull(sum) ) { |
|
456 | 467 | return 0; |
|
457 | 468 | } |
|
458 | 469 | |
|
459 | 470 | return value / sum; |
|
460 | 471 | } |
|
461 | 472 | |
|
462 | 473 | qreal QBarSeriesPrivate::categorySum(int category) |
|
463 | 474 | { |
|
464 | 475 | qreal sum(0); |
|
465 | 476 | int count = m_barSets.count(); // Count sets |
|
466 | 477 | for (int set = 0; set < count; set++) { |
|
467 | 478 | if (category < m_barSets.at(set)->count()) |
|
468 | 479 | sum += m_barSets.at(set)->at(category).y(); |
|
469 | 480 | } |
|
470 | 481 | return sum; |
|
471 | 482 | } |
|
472 | 483 | |
|
473 | 484 | qreal QBarSeriesPrivate::absoluteCategorySum(int category) |
|
474 | 485 | { |
|
475 | 486 | qreal sum(0); |
|
476 | 487 | int count = m_barSets.count(); // Count sets |
|
477 | 488 | for (int set = 0; set < count; set++) { |
|
478 | 489 | if (category < m_barSets.at(set)->count()) |
|
479 | 490 | sum += qAbs(m_barSets.at(set)->at(category).y()); |
|
480 | 491 | } |
|
481 | 492 | return sum; |
|
482 | 493 | } |
|
483 | 494 | |
|
484 | 495 | qreal QBarSeriesPrivate::maxCategorySum() |
|
485 | 496 | { |
|
486 | 497 | qreal max = INT_MIN; |
|
487 | 498 | int count = categoryCount(); |
|
488 | 499 | for (int i = 0; i < count; i++) { |
|
489 | 500 | qreal sum = categorySum(i); |
|
490 | 501 | if (sum > max) |
|
491 | 502 | max = sum; |
|
492 | 503 | } |
|
493 | 504 | return max; |
|
494 | 505 | } |
|
495 | 506 | |
|
496 | 507 | qreal QBarSeriesPrivate::minX() |
|
497 | 508 | { |
|
498 | 509 | if (m_barSets.count() <= 0) { |
|
499 | 510 | return 0; |
|
500 | 511 | } |
|
501 | 512 | qreal min = INT_MAX; |
|
502 | 513 | |
|
503 | 514 | for (int i = 0; i < m_barSets.count(); i++) { |
|
504 | 515 | int categoryCount = m_barSets.at(i)->count(); |
|
505 | 516 | for (int j = 0; j < categoryCount; j++) { |
|
506 | 517 | qreal temp = m_barSets.at(i)->at(j).x(); |
|
507 | 518 | if (temp < min) |
|
508 | 519 | min = temp; |
|
509 | 520 | } |
|
510 | 521 | } |
|
511 | 522 | return min; |
|
512 | 523 | } |
|
513 | 524 | |
|
514 | 525 | qreal QBarSeriesPrivate::maxX() |
|
515 | 526 | { |
|
516 | 527 | if (m_barSets.count() <= 0) { |
|
517 | 528 | return 0; |
|
518 | 529 | } |
|
519 | 530 | qreal max = INT_MIN; |
|
520 | 531 | |
|
521 | 532 | for (int i = 0; i < m_barSets.count(); i++) { |
|
522 | 533 | int categoryCount = m_barSets.at(i)->count(); |
|
523 | 534 | for (int j = 0; j < categoryCount; j++) { |
|
524 | 535 | qreal temp = m_barSets.at(i)->at(j).x(); |
|
525 | 536 | if (temp > max) |
|
526 | 537 | max = temp; |
|
527 | 538 | } |
|
528 | 539 | } |
|
529 | 540 | |
|
530 | 541 | return max; |
|
531 | 542 | } |
|
532 | 543 | |
|
533 | 544 | |
|
534 | 545 | void QBarSeriesPrivate::scaleDomain(Domain& domain) |
|
535 | 546 | { |
|
536 | 547 | qreal minX(domain.minX()); |
|
537 | 548 | qreal minY(domain.minY()); |
|
538 | 549 | qreal maxX(domain.maxX()); |
|
539 | 550 | qreal maxY(domain.maxY()); |
|
540 | 551 | int tickXCount(domain.tickXCount()); |
|
541 | 552 | int tickYCount(domain.tickYCount()); |
|
542 | 553 | |
|
543 | 554 | qreal seriesMinX = this->minX(); |
|
544 | 555 | qreal seriesMaxX = this->maxX(); |
|
545 | 556 | qreal y = max(); |
|
546 | 557 | minX = qMin(minX, seriesMinX - 0.5); |
|
547 | 558 | minY = qMin(minY, y); |
|
548 | 559 | maxX = qMax(maxX, seriesMaxX + 0.5); |
|
549 | 560 | maxY = qMax(maxY, y); |
|
550 | 561 | tickXCount = categoryCount()+1; |
|
551 | 562 | |
|
552 | 563 | domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount); |
|
553 | 564 | } |
|
554 | 565 | |
|
555 | 566 | Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter) |
|
556 | 567 | { |
|
557 | 568 | Q_Q(QBarSeries); |
|
558 | 569 | |
|
559 | 570 | BarChartItem* bar = new BarChartItem(q,presenter); |
|
560 | 571 | if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { |
|
561 | 572 | presenter->animator()->addAnimation(bar); |
|
562 | 573 | } |
|
563 | 574 | presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); |
|
564 | 575 | return bar; |
|
565 | 576 | |
|
566 | 577 | } |
|
567 | 578 | |
|
568 | 579 | QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend) |
|
569 | 580 | { |
|
570 | 581 | Q_Q(QBarSeries); |
|
571 | 582 | QList<LegendMarker*> markers; |
|
572 | 583 | foreach(QBarSet* set, q->barSets()) { |
|
573 | 584 | BarLegendMarker* marker = new BarLegendMarker(q,set,legend); |
|
574 | 585 | markers << marker; |
|
575 | 586 | } |
|
576 | 587 | |
|
577 | 588 | return markers; |
|
578 | 589 | } |
|
579 | 590 | |
|
580 | 591 | bool QBarSeriesPrivate::append(QBarSet *set) |
|
581 | 592 | { |
|
582 | 593 | Q_Q(QBarSeries); |
|
583 | 594 | if ((m_barSets.contains(set)) || (set == 0)) { |
|
584 | 595 | // Fail if set is already in list or set is null. |
|
585 | 596 | return false; |
|
586 | 597 | } |
|
587 | 598 | m_barSets.append(set); |
|
588 | 599 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
589 | 600 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
590 | 601 | emit restructuredBars(); // this notifies barchartitem |
|
591 | 602 | if (m_dataset) { |
|
592 | 603 | m_dataset->updateSeries(q); // this notifies legend |
|
593 | 604 | } |
|
594 | 605 | return true; |
|
595 | 606 | } |
|
596 | 607 | |
|
597 | 608 | bool QBarSeriesPrivate::remove(QBarSet *set) |
|
598 | 609 | { |
|
599 | 610 | Q_Q(QBarSeries); |
|
600 | 611 | if (!m_barSets.contains(set)) { |
|
601 | 612 | // Fail if set is not in list |
|
602 | 613 | return false; |
|
603 | 614 | } |
|
604 | 615 | m_barSets.removeOne(set); |
|
605 | 616 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
606 | 617 | QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
607 | 618 | emit restructuredBars(); // this notifies barchartitem |
|
608 | 619 | if (m_dataset) { |
|
609 | 620 | m_dataset->updateSeries(q); // this notifies legend |
|
610 | 621 | } |
|
611 | 622 | return true; |
|
612 | 623 | } |
|
613 | 624 | |
|
614 | 625 | bool QBarSeriesPrivate::append(QList<QBarSet* > sets) |
|
615 | 626 | { |
|
616 | 627 | Q_Q(QBarSeries); |
|
617 | 628 | foreach (QBarSet* set, sets) { |
|
618 | 629 | if ((set == 0) || (m_barSets.contains(set))) { |
|
619 | 630 | // Fail if any of the sets is null or is already appended. |
|
620 | 631 | return false; |
|
621 | 632 | } |
|
622 | 633 | if (sets.count(set) != 1) { |
|
623 | 634 | // Also fail if same set is more than once in given list. |
|
624 | 635 | return false; |
|
625 | 636 | } |
|
626 | 637 | } |
|
627 | 638 | |
|
628 | 639 | foreach (QBarSet* set, sets) { |
|
629 | 640 | m_barSets.append(set); |
|
630 | 641 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
631 | 642 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
632 | 643 | } |
|
633 | 644 | emit restructuredBars(); // this notifies barchartitem |
|
634 | 645 | if (m_dataset) { |
|
635 | 646 | m_dataset->updateSeries(q); // this notifies legend |
|
636 | 647 | } |
|
637 | 648 | return true; |
|
638 | 649 | } |
|
639 | 650 | |
|
640 | 651 | bool QBarSeriesPrivate::remove(QList<QBarSet* > sets) |
|
641 | 652 | { |
|
642 | 653 | Q_Q(QBarSeries); |
|
643 | 654 | if (sets.count() == 0) { |
|
644 | 655 | return false; |
|
645 | 656 | } |
|
646 | 657 | foreach (QBarSet* set, sets) { |
|
647 | 658 | if ((set == 0) || (!m_barSets.contains(set))) { |
|
648 | 659 | // Fail if any of the sets is null or is not in series |
|
649 | 660 | return false; |
|
650 | 661 | } |
|
651 | 662 | if (sets.count(set) != 1) { |
|
652 | 663 | // Also fail if same set is more than once in given list. |
|
653 | 664 | return false; |
|
654 | 665 | } |
|
655 | 666 | } |
|
656 | 667 | |
|
657 | 668 | foreach (QBarSet* set, sets) { |
|
658 | 669 | m_barSets.removeOne(set); |
|
659 | 670 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
660 | 671 | QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
661 | 672 | } |
|
662 | 673 | |
|
663 | 674 | emit restructuredBars(); // this notifies barchartitem |
|
664 | 675 | if (m_dataset) { |
|
665 | 676 | m_dataset->updateSeries(q); // this notifies legend |
|
666 | 677 | } |
|
667 | 678 | return true; |
|
668 | 679 | } |
|
669 | 680 | |
|
670 | 681 | bool QBarSeriesPrivate::insert(int index, QBarSet *set) |
|
671 | 682 | { |
|
672 | 683 | Q_Q(QBarSeries); |
|
673 | 684 | if ((m_barSets.contains(set)) || (set == 0)) { |
|
674 | 685 | // Fail if set is already in list or set is null. |
|
675 | 686 | return false; |
|
676 | 687 | } |
|
677 | 688 | m_barSets.insert(index, set); |
|
678 | 689 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); |
|
679 | 690 | QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); |
|
680 | 691 | emit restructuredBars(); // this notifies barchartitem |
|
681 | 692 | if (m_dataset) { |
|
682 | 693 | m_dataset->updateSeries(q); // this notifies legend |
|
683 | 694 | } |
|
684 | 695 | return true; |
|
685 | 696 | } |
|
686 | 697 | |
|
687 | 698 | #include "moc_qbarseries.cpp" |
|
688 | 699 | #include "moc_qbarseries_p.cpp" |
|
689 | 700 | |
|
690 | 701 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now