##// END OF EJS Templates
WS changes...
Mika Salmela -
r2474:3080953542fc
parent child
Show More
@@ -1,903 +1,903
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
34 34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 35
36 36 /*!
37 37 \class QAbstractBarSeries
38 38 \brief Series for creating a bar chart
39 39 \mainclass
40 40
41 41 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
42 42 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
43 43 and y-value is the height of the bar. The category names are ignored with this series and x-axis
44 44 shows the x-values.
45 45
46 46 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
47 47 \image examples_barchart.png
48 48
49 49 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
50 50 */
51 51 /*!
52 52 \qmlclass AbstractBarSeries QAbstractBarSeries
53 53 \inherits QAbstractSeries
54 54
55 55 The following QML shows how to create a simple bar chart:
56 56 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
57 57
58 58 \beginfloatleft
59 59 \image demos_qmlchart6.png
60 60 \endfloat
61 61 \clearfloat
62 62 */
63 63
64 64 /*!
65 65 \qmlproperty AbstractAxis AbstractBarSeries::axisX
66 66 The x axis used for the series. If you leave both axisX and axisXTop undefined, a BarCategoriesAxis is created for
67 67 the series.
68 68 \sa axisXTop
69 69 */
70 70
71 71 /*!
72 72 \qmlproperty AbstractAxis AbstractBarSeries::axisY
73 73 The y axis used for the series. If you leave both axisY and axisYRight undefined, a ValueAxis is created for
74 74 the series.
75 75 \sa axisYRight
76 76 */
77 77
78 78 /*!
79 79 \qmlproperty AbstractAxis AbstractBarSeries::axisXTop
80 80 The x axis used for the series, drawn on top of the chart view. Note that you can only provide either axisX or
81 81 axisXTop, but not both.
82 82 \sa axisX
83 83 */
84 84
85 85 /*!
86 86 \qmlproperty AbstractAxis AbstractBarSeries::axisYRight
87 87 The y axis used for the series, drawn to the right on the chart view. Note that you can only provide either axisY
88 88 or axisYRight, but not both.
89 89 \sa axisY
90 90 */
91 91
92 92 /*!
93 93 \property QAbstractBarSeries::barWidth
94 94 The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
95 95 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
96 96 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
97 97 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
98 98 \sa QBarSeries
99 99 */
100 100 /*!
101 101 \qmlproperty real AbstractBarSeries::barWidth
102 102 The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars
103 103 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
104 104 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
105 105 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
106 106 */
107 107
108 108 /*!
109 109 \property QAbstractBarSeries::count
110 110 Holds the number of sets in series.
111 111 */
112 112 /*!
113 113 \qmlproperty int AbstractBarSeries::count
114 114 Holds the number of sets in series.
115 115 */
116 116
117 117 /*!
118 118 \property QAbstractBarSeries::labelsVisible
119 119 Defines the visibility of the labels in series
120 120 */
121 121 /*!
122 122 \qmlproperty bool AbstractBarSeries::labelsVisible
123 123 Defines the visibility of the labels in series
124 124 */
125 125
126 126 /*!
127 127 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
128 128 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
129 129 Clicked bar inside set is indexed by \a index
130 130 */
131 131 /*!
132 132 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
133 133 The signal is emitted if the user clicks with a mouse on top of BarSet.
134 134 Clicked bar inside set is indexed by \a index
135 135 */
136 136
137 137 /*!
138 138 \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset)
139 139
140 140 The signal is emitted if mouse is hovered on top of series.
141 141 Parameter \a barset is the pointer of barset, where hover happened.
142 142 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
143 143 */
144 144 /*!
145 145 \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset)
146 146
147 147 The signal is emitted if mouse is hovered on top of series.
148 148 Parameter \a barset is the pointer of barset, where hover happened.
149 149 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
150 150 */
151 151
152 152 /*!
153 153 \fn void QAbstractBarSeries::countChanged()
154 154 This signal is emitted when barset count has been changed, for example by append or remove.
155 155 */
156 156 /*!
157 157 \qmlsignal AbstractBarSeries::onCountChanged()
158 158 This signal is emitted when barset count has been changed, for example by append or remove.
159 159 */
160 160
161 161 /*!
162 162 \fn void QAbstractBarSeries::labelsVisibleChanged()
163 163 This signal is emitted when labels visibility have changed.
164 164 \sa isLabelsVisible(), setLabelsVisible()
165 165 */
166 166
167 167 /*!
168 168 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
169 169 This signal is emitted when \a sets have been added to the series.
170 170 \sa append(), insert()
171 171 */
172 172 /*!
173 173 \qmlsignal AbstractBarSeries::onBarsetsAdded(BarSet barset)
174 174 Emitted when \a barset has been added to the series.
175 175 */
176 176
177 177 /*!
178 178 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
179 179 This signal is emitted when \a sets have been removed from the series.
180 180 \sa remove()
181 181 */
182 182 /*!
183 183 \qmlsignal AbstractBarSeries::onBarsetsRemoved(BarSet barset)
184 184 Emitted when \a barset has been removed from the series.
185 185 */
186 186
187 187 /*!
188 188 \qmlmethod BarSet AbstractBarSeries::at(int index)
189 189 Returns bar set at \a index. Returns null if the index is not valid.
190 190 */
191 191
192 192 /*!
193 193 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
194 194 Adds a new bar set with \a label and \a values to \a index. Values is a list of reals.
195 195 For example:
196 196 \code
197 197 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
198 198 \endcode
199 199 */
200 200
201 201 /*!
202 202 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
203 203 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.
204 204 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
205 205 appended.
206 206 \sa AbstractBarSeries::append()
207 207 */
208 208
209 209 /*!
210 210 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
211 211 Removes the barset from the series. Returns true if successful, false otherwise.
212 212 */
213 213
214 214 /*!
215 215 \qmlmethod AbstractBarSeries::clear()
216 216 Removes all barsets from the series.
217 217 */
218 218
219 219 /*!
220 220 Destructs abstractbarseries and owned barsets.
221 221 */
222 222 QAbstractBarSeries::~QAbstractBarSeries()
223 223 {
224 224
225 225 }
226 226
227 227 /*!
228 228 \internal
229 229 */
230 230 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent)
231 231 : QAbstractSeries(o, parent)
232 232 {
233 233 Q_D(QAbstractSeries);
234 234 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
235 235 }
236 236
237 237 /*!
238 238 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
239 239 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
240 240 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
241 241 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
242 242 */
243 243 void QAbstractBarSeries::setBarWidth(qreal width)
244 244 {
245 245 Q_D(QAbstractBarSeries);
246 246 d->setBarWidth(width);
247 247 }
248 248
249 249 /*!
250 250 Returns the width of the bars of the series.
251 251 \sa setBarWidth()
252 252 */
253 253 qreal QAbstractBarSeries::barWidth() const
254 254 {
255 255 Q_D(const QAbstractBarSeries);
256 256 return d->barWidth();
257 257 }
258 258
259 259 /*!
260 260 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.
261 261 Returns true, if appending succeeded.
262 262 */
263 263 bool QAbstractBarSeries::append(QBarSet *set)
264 264 {
265 265 Q_D(QAbstractBarSeries);
266 266 bool success = d->append(set);
267 267 if (success) {
268 268 QList<QBarSet *> sets;
269 269 sets.append(set);
270 270 set->setParent(this);
271 271 emit barsetsAdded(sets);
272 272 emit countChanged();
273 273 }
274 274 return success;
275 275 }
276 276
277 277 /*!
278 278 Removes barset from series. Releases ownership of \a set. Deletes the set, if remove
279 279 was successful.
280 280 Returns true, if set was removed.
281 281 */
282 282 bool QAbstractBarSeries::remove(QBarSet *set)
283 283 {
284 284 Q_D(QAbstractBarSeries);
285 285 bool success = d->remove(set);
286 286 if (success) {
287 287 QList<QBarSet *> sets;
288 288 sets.append(set);
289 289 set->setParent(0);
290 290 emit barsetsRemoved(sets);
291 291 emit countChanged();
292 292 delete set;
293 293 set = 0;
294 294 }
295 295 return success;
296 296 }
297 297
298 298 /*!
299 299 Takes a single \a set from the series. Does not delete the barset object.
300 300
301 301 NOTE: The series remains as the barset's parent object. You must set the
302 302 parent object to take full ownership.
303 303
304 304 Returns true if take was successful.
305 305 */
306 306 bool QAbstractBarSeries::take(QBarSet *set)
307 307 {
308 308 Q_D(QAbstractBarSeries);
309 309 bool success = d->remove(set);
310 310 if (success) {
311 311 QList<QBarSet *> sets;
312 312 sets.append(set);
313 313 emit barsetsRemoved(sets);
314 314 emit countChanged();
315 315 }
316 316 return success;
317 317 }
318 318
319 319 /*!
320 320 Adds a list of barsets to series. Takes ownership of \a sets.
321 321 Returns true, if all sets were appended successfully. If any of the sets is null or is already appended to series,
322 322 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
323 323 and function returns false.
324 324 */
325 325 bool QAbstractBarSeries::append(QList<QBarSet *> sets)
326 326 {
327 327 Q_D(QAbstractBarSeries);
328 328 bool success = d->append(sets);
329 329 if (success) {
330 330 emit barsetsAdded(sets);
331 331 emit countChanged();
332 332 }
333 333 return success;
334 334 }
335 335
336 336 /*!
337 337 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.
338 338 Returns true, if inserting succeeded.
339 339
340 340 */
341 341 bool QAbstractBarSeries::insert(int index, QBarSet *set)
342 342 {
343 343 Q_D(QAbstractBarSeries);
344 344 bool success = d->insert(index, set);
345 345 if (success) {
346 346 QList<QBarSet *> sets;
347 347 sets.append(set);
348 348 emit barsetsAdded(sets);
349 349 emit countChanged();
350 350 }
351 351 return success;
352 352 }
353 353
354 354 /*!
355 355 Removes all barsets from the series. Deletes removed sets.
356 356 */
357 357 void QAbstractBarSeries::clear()
358 358 {
359 359 Q_D(QAbstractBarSeries);
360 360 QList<QBarSet *> sets = barSets();
361 361 bool success = d->remove(sets);
362 362 if (success) {
363 363 emit barsetsRemoved(sets);
364 364 emit countChanged();
365 365 foreach (QBarSet *set, sets)
366 366 delete set;
367 367 }
368 368 }
369 369
370 370 /*!
371 371 Returns number of sets in series.
372 372 */
373 373 int QAbstractBarSeries::count() const
374 374 {
375 375 Q_D(const QAbstractBarSeries);
376 376 return d->m_barSets.count();
377 377 }
378 378
379 379 /*!
380 380 Returns a list of sets in series. Keeps ownership of sets.
381 381 */
382 382 QList<QBarSet *> QAbstractBarSeries::barSets() const
383 383 {
384 384 Q_D(const QAbstractBarSeries);
385 385 return d->m_barSets;
386 386 }
387 387
388 388 /*!
389 389 Sets the visibility of labels in series to \a visible
390 390 */
391 391 void QAbstractBarSeries::setLabelsVisible(bool visible)
392 392 {
393 393 Q_D(QAbstractBarSeries);
394 394 if (d->m_labelsVisible != visible) {
395 395 d->setLabelsVisible(visible);
396 396 emit labelsVisibleChanged();
397 397 }
398 398 }
399 399
400 400 /*!
401 401 Returns the visibility of labels
402 402 */
403 403 bool QAbstractBarSeries::isLabelsVisible() const
404 404 {
405 405 Q_D(const QAbstractBarSeries);
406 406 return d->m_labelsVisible;
407 407 }
408 408
409 409 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
410 410
411 411 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
412 412 QAbstractSeriesPrivate(q),
413 413 m_barWidth(0.5), // Default value is 50% of category width
414 414 m_labelsVisible(false),
415 415 m_visible(true)
416 416 {
417 417 }
418 418
419 419 int QAbstractBarSeriesPrivate::categoryCount() const
420 420 {
421 421 // No categories defined. return count of longest set.
422 422 int count = 0;
423 423 for (int i = 0; i < m_barSets.count(); i++) {
424 424 if (m_barSets.at(i)->count() > count)
425 425 count = m_barSets.at(i)->count();
426 426 }
427 427
428 428 return count;
429 429 }
430 430
431 431 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
432 432 {
433 433 if (width < 0.0)
434 434 width = 0.0;
435 435 m_barWidth = width;
436 436 emit updatedLayout();
437 437 }
438 438
439 439 qreal QAbstractBarSeriesPrivate::barWidth() const
440 440 {
441 441 return m_barWidth;
442 442 }
443 443
444 444 QBarSet *QAbstractBarSeriesPrivate::barsetAt(int index)
445 445 {
446 446 return m_barSets.at(index);
447 447 }
448 448
449 449 void QAbstractBarSeriesPrivate::setVisible(bool visible)
450 450 {
451 451 m_visible = visible;
452 452 emit visibleChanged();
453 453 }
454 454
455 455 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
456 456 {
457 457 m_labelsVisible = visible;
458 458 emit labelsVisibleChanged(visible);
459 459 }
460 460
461 461 qreal QAbstractBarSeriesPrivate::min()
462 462 {
463 463 if (m_barSets.count() <= 0)
464 464 return 0;
465 465
466 466 qreal min = INT_MAX;
467 467
468 468 for (int i = 0; i < m_barSets.count(); i++) {
469 469 int categoryCount = m_barSets.at(i)->count();
470 470 for (int j = 0; j < categoryCount; j++) {
471 471 qreal temp = m_barSets.at(i)->at(j);
472 472 if (temp < min)
473 473 min = temp;
474 474 }
475 475 }
476 476 return min;
477 477 }
478 478
479 479 qreal QAbstractBarSeriesPrivate::max()
480 480 {
481 481 if (m_barSets.count() <= 0)
482 482 return 0;
483 483
484 484 qreal max = INT_MIN;
485 485
486 486 for (int i = 0; i < m_barSets.count(); i++) {
487 487 int categoryCount = m_barSets.at(i)->count();
488 488 for (int j = 0; j < categoryCount; j++) {
489 489 qreal temp = m_barSets.at(i)->at(j);
490 490 if (temp > max)
491 491 max = temp;
492 492 }
493 493 }
494 494
495 495 return max;
496 496 }
497 497
498 498 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
499 499 {
500 500 if ((set < 0) || (set >= m_barSets.count()))
501 501 return 0; // No set, no value.
502 502 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
503 503 return 0; // No category, no value.
504 504
505 505 return m_barSets.at(set)->at(category);
506 506 }
507 507
508 508 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
509 509 {
510 510 if ((set < 0) || (set >= m_barSets.count()))
511 511 return 0; // No set, no value.
512 512 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
513 513 return 0; // No category, no value.
514 514
515 515 qreal value = m_barSets.at(set)->at(category);
516 516 qreal sum = categorySum(category);
517 517 if (qFuzzyCompare(sum, 0))
518 518 return 0;
519 519
520 520 return value / sum;
521 521 }
522 522
523 523 qreal QAbstractBarSeriesPrivate::categorySum(int category)
524 524 {
525 525 qreal sum(0);
526 526 int count = m_barSets.count(); // Count sets
527 527 for (int set = 0; set < count; set++) {
528 528 if (category < m_barSets.at(set)->count())
529 529 sum += m_barSets.at(set)->at(category);
530 530 }
531 531 return sum;
532 532 }
533 533
534 534 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
535 535 {
536 536 qreal sum(0);
537 537 int count = m_barSets.count(); // Count sets
538 538 for (int set = 0; set < count; set++) {
539 539 if (category < m_barSets.at(set)->count())
540 540 sum += qAbs(m_barSets.at(set)->at(category));
541 541 }
542 542 return sum;
543 543 }
544 544
545 545 qreal QAbstractBarSeriesPrivate::maxCategorySum()
546 546 {
547 547 qreal max = INT_MIN;
548 548 int count = categoryCount();
549 549 for (int i = 0; i < count; i++) {
550 550 qreal sum = categorySum(i);
551 551 if (sum > max)
552 552 max = sum;
553 553 }
554 554 return max;
555 555 }
556 556
557 557 qreal QAbstractBarSeriesPrivate::minX()
558 558 {
559 559 if (m_barSets.count() <= 0)
560 560 return 0;
561 561
562 562 qreal min = INT_MAX;
563 563
564 564 for (int i = 0; i < m_barSets.count(); i++) {
565 565 int categoryCount = m_barSets.at(i)->count();
566 566 for (int j = 0; j < categoryCount; j++) {
567 567 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
568 568 if (temp < min)
569 569 min = temp;
570 570 }
571 571 }
572 572 return min;
573 573 }
574 574
575 575 qreal QAbstractBarSeriesPrivate::maxX()
576 576 {
577 577 if (m_barSets.count() <= 0)
578 578 return 0;
579 579
580 580 qreal max = INT_MIN;
581 581
582 582 for (int i = 0; i < m_barSets.count(); i++) {
583 583 int categoryCount = m_barSets.at(i)->count();
584 584 for (int j = 0; j < categoryCount; j++) {
585 585 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
586 586 if (temp > max)
587 587 max = temp;
588 588 }
589 589 }
590 590
591 591 return max;
592 592 }
593 593
594 594 qreal QAbstractBarSeriesPrivate::categoryTop(int category)
595 595 {
596 596 // Returns top (sum of all positive values) of category.
597 597 // Returns 0, if all values are negative
598 598 qreal top(0);
599 599 int count = m_barSets.count();
600 600 for (int set = 0; set < count; set++) {
601 601 if (category < m_barSets.at(set)->count()) {
602 602 qreal temp = m_barSets.at(set)->at(category);
603 603 if (temp > 0) {
604 604 top += temp;
605 605 }
606 606 }
607 607 }
608 608 return top;
609 609 }
610 610
611 611 qreal QAbstractBarSeriesPrivate::categoryBottom(int category)
612 612 {
613 613 // Returns bottom (sum of all negative values) of category
614 614 // Returns 0, if all values are positive
615 615 qreal bottom(0);
616 616 int count = m_barSets.count();
617 617 for (int set = 0; set < count; set++) {
618 618 if (category < m_barSets.at(set)->count()) {
619 619 qreal temp = m_barSets.at(set)->at(category);
620 620 if (temp < 0) {
621 621 bottom += temp;
622 622 }
623 623 }
624 624 }
625 625 return bottom;
626 626 }
627 627
628 628 qreal QAbstractBarSeriesPrivate::top()
629 629 {
630 630 // Returns top of all categories
631 631 qreal top(0);
632 632 int count = categoryCount();
633 633 for (int i = 0; i < count; i++) {
634 634 qreal temp = categoryTop(i);
635 635 if (temp > top)
636 636 top = temp;
637 637 }
638 638 return top;
639 639 }
640 640
641 641 qreal QAbstractBarSeriesPrivate::bottom()
642 642 {
643 643 // Returns bottom of all categories
644 644 qreal bottom(0);
645 645 int count = categoryCount();
646 646 for (int i = 0; i < count; i++) {
647 647 qreal temp = categoryBottom(i);
648 648 if (temp < bottom)
649 649 bottom = temp;
650 650 }
651 651 return bottom;
652 652 }
653 653
654 654
655 655 void QAbstractBarSeriesPrivate::initializeDomain()
656 656 {
657 657 qreal minX(domain()->minX());
658 658 qreal minY(domain()->minY());
659 659 qreal maxX(domain()->maxX());
660 660 qreal maxY(domain()->maxY());
661 661
662 662 qreal seriesMinX = this->minX();
663 663 qreal seriesMaxX = this->maxX();
664 664 qreal y = max();
665 665 minX = qMin(minX, seriesMinX - (qreal)0.5);
666 666 minY = qMin(minY, y);
667 667 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
668 668 maxY = qMax(maxY, y);
669 669
670 670 domain()->setRange(minX, maxX, minY, maxY);
671 671 }
672 672
673 673 QList<QLegendMarker*> QAbstractBarSeriesPrivate::createLegendMarkers(QLegend* legend)
674 674 {
675 675 Q_Q(QAbstractBarSeries);
676 676 QList<QLegendMarker*> markers;
677 677
678 678 foreach(QBarSet* set, q->barSets()) {
679 679 QBarLegendMarker* marker = new QBarLegendMarker(q,set,legend);
680 680 markers << marker;
681 681 }
682 682 return markers;
683 683 }
684 684
685 685
686 686 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
687 687 {
688 688 if ((m_barSets.contains(set)) || (set == 0))
689 689 return false; // Fail if set is already in list or set is null.
690 690
691 691 m_barSets.append(set);
692 692 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
693 693 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
694 694 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
695 695
696 696 emit restructuredBars(); // this notifies barchartitem
697 697 return true;
698 698 }
699 699
700 700 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
701 701 {
702 702 if (!m_barSets.contains(set))
703 703 return false; // Fail if set is not in list
704 704
705 705 m_barSets.removeOne(set);
706 706 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
707 707 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
708 708 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
709 709
710 710 emit restructuredBars(); // this notifies barchartitem
711 711 return true;
712 712 }
713 713
714 714 bool QAbstractBarSeriesPrivate::append(QList<QBarSet * > sets)
715 715 {
716 716 foreach (QBarSet *set, sets) {
717 717 if ((set == 0) || (m_barSets.contains(set)))
718 718 return false; // Fail if any of the sets is null or is already appended.
719 719 if (sets.count(set) != 1)
720 720 return false; // Also fail if same set is more than once in given list.
721 721 }
722 722
723 723 foreach (QBarSet *set, sets) {
724 724 m_barSets.append(set);
725 725 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
726 726 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
727 727 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
728 728 }
729 729
730 730 emit restructuredBars(); // this notifies barchartitem
731 731 return true;
732 732 }
733 733
734 734 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet * > sets)
735 735 {
736 736 if (sets.count() == 0)
737 737 return false;
738 738
739 739 foreach (QBarSet *set, sets) {
740 740 if ((set == 0) || (!m_barSets.contains(set)))
741 741 return false; // Fail if any of the sets is null or is not in series
742 742 if (sets.count(set) != 1)
743 743 return false; // Also fail if same set is more than once in given list.
744 744 }
745 745
746 746 foreach (QBarSet *set, sets) {
747 747 m_barSets.removeOne(set);
748 748 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
749 749 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
750 750 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
751 751 }
752 752
753 753 emit restructuredBars(); // this notifies barchartitem
754 754
755 755 return true;
756 756 }
757 757
758 758 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
759 759 {
760 760 if ((m_barSets.contains(set)) || (set == 0))
761 761 return false; // Fail if set is already in list or set is null.
762 762
763 763 m_barSets.insert(index, set);
764 764 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
765 765 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
766 766 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
767 767
768 768 emit restructuredBars(); // this notifies barchartitem
769 769 return true;
770 770 }
771 771
772 772 void QAbstractBarSeriesPrivate::initializeAxes()
773 773 {
774 774 Q_Q(QAbstractBarSeries);
775 775
776 776 foreach(QAbstractAxis* axis, m_axes) {
777 777
778 778 if (axis->type() == QAbstractAxis::AxisTypeBarCategory) {
779 779 switch (q->type()) {
780 case QAbstractSeries::SeriesTypeHorizontalBar:
781 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
782 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
780 case QAbstractSeries::SeriesTypeHorizontalBar:
781 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
782 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
783 783 if (axis->orientation() == Qt::Vertical)
784 784 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
785 break;
786 case QAbstractSeries::SeriesTypeBar:
787 case QAbstractSeries::SeriesTypePercentBar:
788 case QAbstractSeries::SeriesTypeStackedBar:
789 case QAbstractSeries::SeriesTypeBoxPlot:
785 break;
786 case QAbstractSeries::SeriesTypeBar:
787 case QAbstractSeries::SeriesTypePercentBar:
788 case QAbstractSeries::SeriesTypeStackedBar:
789 case QAbstractSeries::SeriesTypeBoxPlot:
790 790 if (axis->orientation() == Qt::Horizontal)
791 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
792 break;
793 default:
791 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
792 break;
793 default:
794 794 qWarning() << "Unexpected series type";
795 break;
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 case QAbstractSeries::SeriesTypeBoxPlot:
816 816 if (orientation == Qt::Horizontal)
817 817 return QAbstractAxis::AxisTypeBarCategory;
818 818 break;
819 819 default:
820 820 qWarning() << "Unexpected series type";
821 821 break;
822 822 }
823 823 return QAbstractAxis::AxisTypeValue;
824 824
825 825 }
826 826
827 827 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis *axis)
828 828 {
829 829 QStringList categories;
830 830 if (axis->categories().isEmpty()) {
831 831 for (int i(1); i < categoryCount() + 1; i++)
832 832 categories << QString::number(i);
833 833 axis->append(categories);
834 834 }
835 835 }
836 836
837 837 QAbstractAxis* QAbstractBarSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
838 838 {
839 839 Q_UNUSED(orientation);
840 840 return 0;
841 841 }
842 842
843 843 void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
844 844 {
845 845 const QList<QGradient> gradients = theme->seriesGradients();
846 846
847 847 QBrush brush;
848 848 QPen pen;
849 849
850 850 qreal takeAtPos = 0.5;
851 851 qreal step = 0.2;
852 852 if (m_barSets.count() > 1) {
853 853 step = 1.0 / (qreal) m_barSets.count();
854 854 if (m_barSets.count() % gradients.count())
855 855 step *= gradients.count();
856 856 else
857 857 step *= (gradients.count() - 1);
858 858 }
859 859
860 860 for (int i(0); i < m_barSets.count(); i++) {
861 861 int colorIndex = (index + i) % gradients.count();
862 862 if (i > 0 && i %gradients.count() == 0) {
863 863 // There is no dedicated base color for each sets, generate more colors
864 864 takeAtPos += step;
865 865 if (takeAtPos == 1.0)
866 866 takeAtPos += step;
867 867 takeAtPos -= (int) takeAtPos;
868 868 }
869 869 if (forced || brush == m_barSets.at(i)->brush())
870 870 m_barSets.at(i)->setBrush(ChartThemeManager::colorAt(gradients.at(colorIndex), takeAtPos));
871 871
872 872 // Pick label color from the opposite end of the gradient.
873 873 // 0.3 as a boundary seems to work well.
874 874 if (forced || brush == m_barSets.at(i)->labelBrush()) {
875 875 if (takeAtPos < 0.3)
876 876 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1));
877 877 else
878 878 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0));
879 879 }
880 880
881 881 if (forced || pen == m_barSets.at(i)->pen()) {
882 882 QColor c = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0);
883 883 m_barSets.at(i)->setPen(c);
884 884 }
885 885 }
886 886 }
887 887
888 888 void QAbstractBarSeriesPrivate::initializeAnimations(QChart::AnimationOptions options)
889 889 {
890 890 AbstractBarChartItem *bar = static_cast<AbstractBarChartItem *>(m_item.data());
891 891 Q_ASSERT(bar);
892 892 if (options.testFlag(QChart::SeriesAnimations)) {
893 893 bar->setAnimation(new BarAnimation(bar));
894 894 }else{
895 895 bar->setAnimation(0);
896 896 }
897 897 QAbstractSeriesPrivate::initializeAnimations(options);
898 898 }
899 899
900 900 #include "moc_qabstractbarseries.cpp"
901 901 #include "moc_qabstractbarseries_p.cpp"
902 902
903 903 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now