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