##// END OF EJS Templates
minor doc update
sauimone -
r1299:60e2b55c1831
parent child
Show More
@@ -1,575 +1,591
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 \fn void QBarSeries::clicked(QBarSet *barset, QString category)
51 51
52 52 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset of category \a category
53 53 contained by the series.
54 54 */
55 55
56 56 /*!
57 57 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
58 58
59 59 The signal is emitted if mouse is hovered on top of series.
60 60 Parameter \a barset is the pointer of barset, where hover happened.
61 61 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
62 62 */
63 63
64 64 /*!
65 65 Constructs empty QBarSeries.
66 66 QBarSeries is QObject which is a child of a \a parent.
67 67 */
68 68 QBarSeries::QBarSeries(QObject *parent) :
69 69 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
70 70 {
71 71 }
72 72
73 73 /*!
74 74 Destructs barseries and owned barsets.
75 75 */
76 76 QBarSeries::~QBarSeries()
77 77 {
78 78 Q_D(QBarSeries);
79 79 if(d->m_dataset){
80 80 d->m_dataset->removeSeries(this);
81 81 }
82 82 }
83 83
84 84 /*!
85 85 \internal
86 86 */
87 87 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
88 88 QAbstractSeries(d,parent)
89 89 {
90 90 }
91 91
92 92 /*!
93 93 Returns the type of series. Derived classes override this.
94 94 */
95 95 QAbstractSeries::SeriesType QBarSeries::type() const
96 96 {
97 97 return QAbstractSeries::SeriesTypeBar;
98 98 }
99 99
100 100 /*!
101 101 Sets the \a categories, which are used to to group the data.
102 102 */
103 103 void QBarSeries::setCategories(QBarCategories categories)
104 104 {
105 105 Q_D(QBarSeries);
106 106 d->setCategories(categories);
107 107 emit d->categoriesUpdated();
108 108 }
109 109
110 /*!
111 Sets the margin around bars. Parameter \a margin is from 0 to 1 and represents
112 percentage of margin compared to bars
113 */
110 114 void QBarSeries::setBarMargin(qreal margin)
111 115 {
112 116 Q_D(QBarSeries);
113 117 d->setBarMargin(margin);
114 118 }
115 119
120 /*!
121 Returns the margin around bars
122 */
116 123 qreal QBarSeries::barMargin() const
117 124 {
118 125 Q_D(const QBarSeries);
119 126 return d->barMargin();
120 127 }
121 128
122 129 /*!
123 130 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.
124 131 Returns true, if appending succeeded.
125 132
126 133 */
127 134 bool QBarSeries::append(QBarSet *set)
128 135 {
129 136 Q_D(QBarSeries);
130 137 return d->append(set);
131 138 }
132 139
133 140 /*!
134 141 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
135 142 Returns true, if set was removed.
136 143 */
137 144 bool QBarSeries::remove(QBarSet *set)
138 145 {
139 146 Q_D(QBarSeries);
140 147 return d->remove(set);
141 148 }
142 149
143 150 /*!
144 151 Adds a list of barsets to series. Takes ownership of \a sets.
145 152 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
146 153 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
147 154 and function returns false.
148 155 */
149 156 bool QBarSeries::append(QList<QBarSet* > sets)
150 157 {
151 158 Q_D(QBarSeries);
152 159 return d->append(sets);
153 160 }
154 161
155 162 /*!
156 163 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
157 164 */
158 165 bool QBarSeries::remove(QList<QBarSet* > sets)
159 166 {
160 167 Q_D(QBarSeries);
161 168 return d->remove(sets);
162 169 }
163 170
164 171 void QBarSeries::clear()
165 172 {
166 173 Q_D(QBarSeries);
167 174 d->m_barSets.clear();
168 175 d->m_categories.clear();
169 176 }
170 177
171 178 /*!
172 179 Returns number of sets in series.
173 180 */
174 181 int QBarSeries::barsetCount() const
175 182 {
176 183 Q_D(const QBarSeries);
177 184 return d->m_barSets.count();
178 185 }
179 186
180 187 /*!
181 188 Returns number of categories in series
182 189 */
183 190 int QBarSeries::categoryCount() const
184 191 {
185 192 Q_D(const QBarSeries);
186 193 return d->categoryCount();
187 194 }
188 195
189 196 /*!
190 197 Returns a list of sets in series. Keeps ownership of sets.
191 198 */
192 199 QList<QBarSet*> QBarSeries::barSets() const
193 200 {
194 201 Q_D(const QBarSeries);
195 202 return d->m_barSets;
196 203 }
197 204
198 205 /*!
199 206 Returns the bar categories of the series.
200 207 */
201 208 QBarCategories QBarSeries::categories() const
202 209 {
203 210 Q_D(const QBarSeries);
204 211 return d->categories();
205 212 }
206 213
214 /*!
215 Sets the visibility of series to \a visible
216 */
207 217 void QBarSeries::setVisible(bool visible)
208 218 {
209 219 Q_D(QBarSeries);
210 220 d->setVisible(visible);
211 221 }
212 222
223 /*!
224 Returns the visibility of series
225 */
213 226 bool QBarSeries::isVisible() const
214 227 {
215 228 Q_D(const QBarSeries);
216 229 return d->isVisible();
217 230 }
218 231
219 232 /*!
220 233 Sets the visibility of labels in series to \a visible
221 234 */
222 235 void QBarSeries::setLabelsVisible(bool visible)
223 236 {
224 237 Q_D(QBarSeries);
225 238 if (d->m_labelsVisible != visible) {
226 239 d->m_labelsVisible = visible;
227 240 emit d->updatedBars();
228 241 }
229 242 }
230 243
244 /*!
245 Returns the visibility of labels
246 */
231 247 bool QBarSeries::isLabelsVisible() const
232 248 {
233 249 Q_D(const QBarSeries);
234 250 return d->m_labelsVisible;
235 251 }
236 252
237 253 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
238 254
239 255 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
240 256 QAbstractSeriesPrivate(q),
241 257 m_barMargin(0.5), // Default value is 50% of category width
242 258 m_labelsVisible(false),
243 259 m_visible(true)
244 260 {
245 261 }
246 262
247 263 void QBarSeriesPrivate::setCategories(QBarCategories categories)
248 264 {
249 265 m_categories = categories;
250 266 }
251 267
252 268 void QBarSeriesPrivate::insertCategory(int index, const QString category)
253 269 {
254 270 m_categories.insert(index, category);
255 271 emit categoriesUpdated();
256 272 }
257 273
258 274 void QBarSeriesPrivate::removeCategory(int index)
259 275 {
260 276 m_categories.removeAt(index);
261 277 emit categoriesUpdated();
262 278 }
263 279
264 280 int QBarSeriesPrivate::categoryCount() const
265 281 {
266 282 if (m_categories.count() > 0) {
267 283 return m_categories.count();
268 284 }
269 285
270 286 // No categories defined. return count of longest set.
271 287 int count = 0;
272 288 for (int i=0; i<m_barSets.count(); i++) {
273 289 if (m_barSets.at(i)->count() > count) {
274 290 count = m_barSets.at(i)->count();
275 291 }
276 292 }
277 293
278 294 return count;
279 295 }
280 296
281 297 QBarCategories QBarSeriesPrivate::categories() const
282 298 {
283 299 if (m_categories.count() > 0) {
284 300 return m_categories;
285 301 }
286 302
287 303 // No categories defined. retun list of indices.
288 304 QBarCategories categories;
289 305
290 306 int count = categoryCount();
291 307 for (int i = 0; i < count; i++) {
292 308 categories.append(QString::number(i));
293 309 }
294 310 return categories;
295 311 }
296 312
297 313 void QBarSeriesPrivate::setBarMargin(qreal margin)
298 314 {
299 315 if (margin > 1.0) {
300 316 margin = 1.0;
301 317 } else if (margin < 0.0) {
302 318 margin = 0.0;
303 319 }
304 320
305 321 m_barMargin = margin;
306 322 emit updatedBars();
307 323 }
308 324
309 325 qreal QBarSeriesPrivate::barMargin() const
310 326 {
311 327 return m_barMargin;
312 328 }
313 329
314 330 QBarSet* QBarSeriesPrivate::barsetAt(int index)
315 331 {
316 332 return m_barSets.at(index);
317 333 }
318 334
319 335 void QBarSeriesPrivate::setVisible(bool visible)
320 336 {
321 337 if (m_visible != visible) {
322 338 m_visible = visible;
323 339 emit updatedBars();
324 340 }
325 341 }
326 342
327 343 bool QBarSeriesPrivate::isVisible() const
328 344 {
329 345 return m_visible;
330 346 }
331 347
332 348 QString QBarSeriesPrivate::categoryName(int category)
333 349 {
334 350 if ((category >= 0) && (category < m_categories.count())) {
335 351 return m_categories.at(category);
336 352 }
337 353
338 354 return QString::number(category);
339 355 }
340 356
341 357 qreal QBarSeriesPrivate::min()
342 358 {
343 359 if (m_barSets.count() <= 0) {
344 360 return 0;
345 361 }
346 362 qreal min = INT_MAX;
347 363
348 364 for (int i = 0; i < m_barSets.count(); i++) {
349 365 int categoryCount = m_barSets.at(i)->count();
350 366 for (int j = 0; j < categoryCount; j++) {
351 367 qreal temp = m_barSets.at(i)->at(j).y();
352 368 if (temp < min)
353 369 min = temp;
354 370 }
355 371 }
356 372 return min;
357 373 }
358 374
359 375 qreal QBarSeriesPrivate::max()
360 376 {
361 377 if (m_barSets.count() <= 0) {
362 378 return 0;
363 379 }
364 380 qreal max = INT_MIN;
365 381
366 382 for (int i = 0; i < m_barSets.count(); i++) {
367 383 int categoryCount = m_barSets.at(i)->count();
368 384 for (int j = 0; j < categoryCount; j++) {
369 385 qreal temp = m_barSets.at(i)->at(j).y();
370 386 if (temp > max)
371 387 max = temp;
372 388 }
373 389 }
374 390
375 391 return max;
376 392 }
377 393
378 394 qreal QBarSeriesPrivate::valueAt(int set, int category)
379 395 {
380 396 if ((set < 0) || (set >= m_barSets.count())) {
381 397 // No set, no value.
382 398 return 0;
383 399 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
384 400 // No category, no value.
385 401 return 0;
386 402 }
387 403
388 404 return m_barSets.at(set)->at(category).y();
389 405 }
390 406
391 407 qreal QBarSeriesPrivate::percentageAt(int set, int category)
392 408 {
393 409 if ((set < 0) || (set >= m_barSets.count())) {
394 410 // No set, no value.
395 411 return 0;
396 412 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
397 413 // No category, no value.
398 414 return 0;
399 415 }
400 416
401 417 qreal value = m_barSets.at(set)->at(category).y();
402 418 qreal sum = categorySum(category);
403 419 if ( qFuzzyIsNull(sum) ) {
404 420 return 0;
405 421 }
406 422
407 423 return value / sum;
408 424 }
409 425
410 426 qreal QBarSeriesPrivate::categorySum(int category)
411 427 {
412 428 qreal sum(0);
413 429 int count = m_barSets.count(); // Count sets
414 430 for (int set = 0; set < count; set++) {
415 431 if (category < m_barSets.at(set)->count())
416 432 sum += m_barSets.at(set)->at(category).y();
417 433 }
418 434 return sum;
419 435 }
420 436
421 437 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
422 438 {
423 439 qreal sum(0);
424 440 int count = m_barSets.count(); // Count sets
425 441 for (int set = 0; set < count; set++) {
426 442 if (category < m_barSets.at(set)->count())
427 443 sum += qAbs(m_barSets.at(set)->at(category).y());
428 444 }
429 445 return sum;
430 446 }
431 447
432 448 qreal QBarSeriesPrivate::maxCategorySum()
433 449 {
434 450 qreal max = INT_MIN;
435 451 int count = categoryCount();
436 452 for (int i = 0; i < count; i++) {
437 453 qreal sum = categorySum(i);
438 454 if (sum > max)
439 455 max = sum;
440 456 }
441 457 return max;
442 458 }
443 459
444 460 void QBarSeriesPrivate::barsetChanged()
445 461 {
446 462 emit updatedBars();
447 463 }
448 464
449 465 void QBarSeriesPrivate::scaleDomain(Domain& domain)
450 466 {
451 467 qreal minX(domain.minX());
452 468 qreal minY(domain.minY());
453 469 qreal maxX(domain.maxX());
454 470 qreal maxY(domain.maxY());
455 471 int tickXCount(domain.tickXCount());
456 472 int tickYCount(domain.tickYCount());
457 473
458 474 qreal x = categoryCount();
459 475 qreal y = max();
460 476 minX = qMin(minX, x) - 0.5;
461 477 minY = qMin(minY, y);
462 478 maxX = qMax(maxX, x) - 0.5;
463 479 maxY = qMax(maxY, y);
464 480 tickXCount = x+1;
465 481
466 482 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
467 483 }
468 484
469 485 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
470 486 {
471 487 Q_Q(QBarSeries);
472 488
473 489 BarChartItem* bar = new BarChartItem(q,presenter);
474 490 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
475 491 presenter->animator()->addAnimation(bar);
476 492 }
477 493 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
478 494 return bar;
479 495
480 496 }
481 497
482 498 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
483 499 {
484 500 Q_Q(QBarSeries);
485 501 QList<LegendMarker*> markers;
486 502 foreach(QBarSet* set, q->barSets()) {
487 503 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
488 504 markers << marker;
489 505 }
490 506
491 507 return markers;
492 508 }
493 509
494 510 bool QBarSeriesPrivate::append(QBarSet *set)
495 511 {
496 512 Q_Q(QBarSeries);
497 513 if ((m_barSets.contains(set)) || (set == 0)) {
498 514 // Fail if set is already in list or set is null.
499 515 return false;
500 516 }
501 517 m_barSets.append(set);
502 518 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
503 519 if (m_dataset) {
504 520 m_dataset->updateSeries(q); // this notifies legend
505 521 }
506 522 emit restructuredBars(); // this notifies barchartitem
507 523 return true;
508 524 }
509 525
510 526 bool QBarSeriesPrivate::remove(QBarSet *set)
511 527 {
512 528 Q_Q(QBarSeries);
513 529 if (!m_barSets.contains(set)) {
514 530 // Fail if set is not in list
515 531 return false;
516 532 }
517 533 m_barSets.removeOne(set);
518 534 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
519 535 if (m_dataset) {
520 536 m_dataset->updateSeries(q); // this notifies legend
521 537 }
522 538 emit restructuredBars(); // this notifies barchartitem
523 539 return true;
524 540 }
525 541
526 542 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
527 543 {
528 544 Q_Q(QBarSeries);
529 545 foreach (QBarSet* set, sets) {
530 546 if ((set == 0) || (m_barSets.contains(set))) {
531 547 // Fail if any of the sets is null or is already appended.
532 548 return false;
533 549 }
534 550 if (sets.count(set) != 1) {
535 551 // Also fail if same set is more than once in given list.
536 552 return false;
537 553 }
538 554 }
539 555
540 556 foreach (QBarSet* set, sets) {
541 557 m_barSets.append(set);
542 558 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
543 559 }
544 560 if (m_dataset) {
545 561 m_dataset->updateSeries(q); // this notifies legend
546 562 }
547 563 emit restructuredBars(); // this notifies barchartitem
548 564 return true;
549 565 }
550 566
551 567 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
552 568 {
553 569 Q_Q(QBarSeries);
554 570 bool setsRemoved = false;
555 571 foreach (QBarSet* set, sets) {
556 572 if (m_barSets.contains(set)) {
557 573 m_barSets.removeOne(set);
558 574 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
559 575 setsRemoved = true;
560 576 }
561 577 }
562 578
563 579 if (setsRemoved) {
564 580 if (m_dataset) {
565 581 m_dataset->updateSeries(q); // this notifies legend
566 582 }
567 583 emit restructuredBars(); // this notifies barchartitem
568 584 }
569 585 return setsRemoved;
570 586 }
571 587
572 588 #include "moc_qbarseries.cpp"
573 589 #include "moc_qbarseries_p.cpp"
574 590
575 591 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now