##// END OF EJS Templates
bar margin setter to series
sauimone -
r1283:a3587e7f5c5f
parent child
Show More
@@ -1,530 +1,536
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 void QBarSeries::setBarMargin(qreal margin)
111 {
112 Q_D(QBarSeries);
113 d->setBarMargin(margin);
114 }
115
110 116 /*!
111 117 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.
112 118 Returns true, if appending succeeded.
113 119
114 120 */
115 121 bool QBarSeries::append(QBarSet *set)
116 122 {
117 123 Q_D(QBarSeries);
118 124 return d->append(set);
119 125 }
120 126
121 127 /*!
122 128 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
123 129 Returns true, if set was removed.
124 130 */
125 131 bool QBarSeries::remove(QBarSet *set)
126 132 {
127 133 Q_D(QBarSeries);
128 134 return d->remove(set);
129 135 }
130 136
131 137 /*!
132 138 Adds a list of barsets to series. Takes ownership of \a sets.
133 139 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
134 140 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
135 141 and function returns false.
136 142 */
137 143 bool QBarSeries::append(QList<QBarSet* > sets)
138 144 {
139 145 Q_D(QBarSeries);
140 146 return d->append(sets);
141 147 }
142 148
143 149 /*!
144 150 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
145 151 */
146 152 bool QBarSeries::remove(QList<QBarSet* > sets)
147 153 {
148 154 Q_D(QBarSeries);
149 155 return d->remove(sets);
150 156 }
151 157
152 158 /*!
153 159 Returns number of sets in series.
154 160 */
155 161 int QBarSeries::barsetCount() const
156 162 {
157 163 Q_D(const QBarSeries);
158 164 return d->m_barSets.count();
159 165 }
160 166
161 167 /*!
162 168 Returns number of categories in series
163 169 */
164 170 int QBarSeries::categoryCount() const
165 171 {
166 172 Q_D(const QBarSeries);
167 173 return d->categoryCount();
168 174 }
169 175
170 176 /*!
171 177 Returns a list of sets in series. Keeps ownership of sets.
172 178 */
173 179 QList<QBarSet*> QBarSeries::barSets() const
174 180 {
175 181 Q_D(const QBarSeries);
176 182 return d->m_barSets;
177 183 }
178 184
179 185 /*!
180 186 Returns the bar categories of the series.
181 187 */
182 188 QBarCategories QBarSeries::categories() const
183 189 {
184 190 Q_D(const QBarSeries);
185 191 return d->categories();
186 192 }
187 193
188 194 /*!
189 195 Sets the visibility of labels in series to \a visible
190 196 */
191 197 void QBarSeries::setLabelsVisible(bool visible)
192 198 {
193 199 Q_D(QBarSeries);
194 200 if (d->m_labelsVisible != visible) {
195 201 d->m_labelsVisible = visible;
196 202 emit d->updatedBars();
197 203 }
198 204 }
199 205
200 206 bool QBarSeries::isLabelsVisible() const
201 207 {
202 208 Q_D(const QBarSeries);
203 209 return d->m_labelsVisible;
204 210 }
205 211
206 212 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
207 213
208 214 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
209 215 QAbstractSeriesPrivate(q),
210 216 m_barMargin(0.05), // Default value is 5% of category width
211 217 m_labelsVisible(false)
212 218 {
213 219 }
214 220
215 221 void QBarSeriesPrivate::setCategories(QBarCategories categories)
216 222 {
217 223 m_categories = categories;
218 224 }
219 225
220 226 void QBarSeriesPrivate::insertCategory(int index, const QString category)
221 227 {
222 228 m_categories.insert(index, category);
223 229 emit categoriesUpdated();
224 230 }
225 231
226 232 void QBarSeriesPrivate::removeCategory(int index)
227 233 {
228 234 m_categories.removeAt(index);
229 235 emit categoriesUpdated();
230 236 }
231 237
232 238 int QBarSeriesPrivate::categoryCount() const
233 239 {
234 240 if (m_categories.count() > 0) {
235 241 return m_categories.count();
236 242 }
237 243
238 244 // No categories defined. return count of longest set.
239 245 int count = 0;
240 246 for (int i=0; i<m_barSets.count(); i++) {
241 247 if (m_barSets.at(i)->count() > count) {
242 248 count = m_barSets.at(i)->count();
243 249 }
244 250 }
245 251
246 252 return count;
247 253 }
248 254
249 255 QBarCategories QBarSeriesPrivate::categories() const
250 256 {
251 257 if (m_categories.count() > 0) {
252 258 return m_categories;
253 259 }
254 260
255 261 // No categories defined. retun list of indices.
256 262 QBarCategories categories;
257 263
258 264 int count = categoryCount();
259 265 for (int i = 0; i < count; i++) {
260 266 categories.append(QString::number(i));
261 267 }
262 268 return categories;
263 269 }
264 270
265 271 void QBarSeriesPrivate::setBarMargin(qreal margin)
266 272 {
267 273 if (margin > 1.0) {
268 274 margin = 1.0;
269 275 } else if (margin < 0.0) {
270 276 margin = 0.0;
271 277 }
272 278
273 279 m_barMargin = margin;
274 280 emit updatedBars();
275 281 }
276 282
277 283 qreal QBarSeriesPrivate::barMargin()
278 284 {
279 285 return m_barMargin;
280 286 }
281 287
282 288 QBarSet* QBarSeriesPrivate::barsetAt(int index)
283 289 {
284 290 return m_barSets.at(index);
285 291 }
286 292
287 293 QString QBarSeriesPrivate::categoryName(int category)
288 294 {
289 295 if ((category >= 0) && (category < m_categories.count())) {
290 296 return m_categories.at(category);
291 297 }
292 298
293 299 return QString::number(category);
294 300 }
295 301
296 302 qreal QBarSeriesPrivate::min()
297 303 {
298 304 if (m_barSets.count() <= 0) {
299 305 return 0;
300 306 }
301 307 qreal min = INT_MAX;
302 308
303 309 for (int i = 0; i < m_barSets.count(); i++) {
304 310 int categoryCount = m_barSets.at(i)->count();
305 311 for (int j = 0; j < categoryCount; j++) {
306 312 qreal temp = m_barSets.at(i)->at(j).y();
307 313 if (temp < min)
308 314 min = temp;
309 315 }
310 316 }
311 317 return min;
312 318 }
313 319
314 320 qreal QBarSeriesPrivate::max()
315 321 {
316 322 if (m_barSets.count() <= 0) {
317 323 return 0;
318 324 }
319 325 qreal max = INT_MIN;
320 326
321 327 for (int i = 0; i < m_barSets.count(); i++) {
322 328 int categoryCount = m_barSets.at(i)->count();
323 329 for (int j = 0; j < categoryCount; j++) {
324 330 qreal temp = m_barSets.at(i)->at(j).y();
325 331 if (temp > max)
326 332 max = temp;
327 333 }
328 334 }
329 335
330 336 return max;
331 337 }
332 338
333 339 qreal QBarSeriesPrivate::valueAt(int set, int category)
334 340 {
335 341 if ((set < 0) || (set >= m_barSets.count())) {
336 342 // No set, no value.
337 343 return 0;
338 344 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
339 345 // No category, no value.
340 346 return 0;
341 347 }
342 348
343 349 return m_barSets.at(set)->at(category).y();
344 350 }
345 351
346 352 qreal QBarSeriesPrivate::percentageAt(int set, int category)
347 353 {
348 354 if ((set < 0) || (set >= m_barSets.count())) {
349 355 // No set, no value.
350 356 return 0;
351 357 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
352 358 // No category, no value.
353 359 return 0;
354 360 }
355 361
356 362 qreal value = m_barSets.at(set)->at(category).y();
357 363 qreal sum = categorySum(category);
358 364 if ( qFuzzyIsNull(sum) ) {
359 365 return 0;
360 366 }
361 367
362 368 return value / sum;
363 369 }
364 370
365 371 qreal QBarSeriesPrivate::categorySum(int category)
366 372 {
367 373 qreal sum(0);
368 374 int count = m_barSets.count(); // Count sets
369 375 for (int set = 0; set < count; set++) {
370 376 if (category < m_barSets.at(set)->count())
371 377 sum += m_barSets.at(set)->at(category).y();
372 378 }
373 379 return sum;
374 380 }
375 381
376 382 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
377 383 {
378 384 qreal sum(0);
379 385 int count = m_barSets.count(); // Count sets
380 386 for (int set = 0; set < count; set++) {
381 387 if (category < m_barSets.at(set)->count())
382 388 sum += qAbs(m_barSets.at(set)->at(category).y());
383 389 }
384 390 return sum;
385 391 }
386 392
387 393 qreal QBarSeriesPrivate::maxCategorySum()
388 394 {
389 395 qreal max = INT_MIN;
390 396 int count = categoryCount();
391 397 for (int i = 0; i < count; i++) {
392 398 qreal sum = categorySum(i);
393 399 if (sum > max)
394 400 max = sum;
395 401 }
396 402 return max;
397 403 }
398 404
399 405 void QBarSeriesPrivate::barsetChanged()
400 406 {
401 407 emit updatedBars();
402 408 }
403 409
404 410 void QBarSeriesPrivate::scaleDomain(Domain& domain)
405 411 {
406 412 qreal minX(domain.minX());
407 413 qreal minY(domain.minY());
408 414 qreal maxX(domain.maxX());
409 415 qreal maxY(domain.maxY());
410 416 int tickXCount(domain.tickXCount());
411 417 int tickYCount(domain.tickYCount());
412 418
413 419 qreal x = categoryCount();
414 420 qreal y = max();
415 421 minX = qMin(minX, x) - 0.5;
416 422 minY = qMin(minY, y);
417 423 maxX = qMax(maxX, x) - 0.5;
418 424 maxY = qMax(maxY, y);
419 425 tickXCount = x+1;
420 426
421 427 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
422 428 }
423 429
424 430 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
425 431 {
426 432 Q_Q(QBarSeries);
427 433
428 434 BarChartItem* bar = new BarChartItem(q,presenter);
429 435 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
430 436 presenter->animator()->addAnimation(bar);
431 437 }
432 438 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
433 439 return bar;
434 440
435 441 }
436 442
437 443 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
438 444 {
439 445 Q_Q(QBarSeries);
440 446 QList<LegendMarker*> markers;
441 447 foreach(QBarSet* set, q->barSets()) {
442 448 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
443 449 markers << marker;
444 450 }
445 451
446 452 return markers;
447 453 }
448 454
449 455 bool QBarSeriesPrivate::append(QBarSet *set)
450 456 {
451 457 Q_Q(QBarSeries);
452 458 if ((m_barSets.contains(set)) || (set == 0)) {
453 459 // Fail if set is already in list or set is null.
454 460 return false;
455 461 }
456 462 m_barSets.append(set);
457 463 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
458 464 if (m_dataset) {
459 465 m_dataset->updateSeries(q); // this notifies legend
460 466 }
461 467 emit restructuredBars(); // this notifies barchartitem
462 468 return true;
463 469 }
464 470
465 471 bool QBarSeriesPrivate::remove(QBarSet *set)
466 472 {
467 473 Q_Q(QBarSeries);
468 474 if (!m_barSets.contains(set)) {
469 475 // Fail if set is not in list
470 476 return false;
471 477 }
472 478 m_barSets.removeOne(set);
473 479 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
474 480 if (m_dataset) {
475 481 m_dataset->updateSeries(q); // this notifies legend
476 482 }
477 483 emit restructuredBars(); // this notifies barchartitem
478 484 return true;
479 485 }
480 486
481 487 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
482 488 {
483 489 Q_Q(QBarSeries);
484 490 foreach (QBarSet* set, sets) {
485 491 if ((set == 0) || (m_barSets.contains(set))) {
486 492 // Fail if any of the sets is null or is already appended.
487 493 return false;
488 494 }
489 495 if (sets.count(set) != 1) {
490 496 // Also fail if same set is more than once in given list.
491 497 return false;
492 498 }
493 499 }
494 500
495 501 foreach (QBarSet* set, sets) {
496 502 m_barSets.append(set);
497 503 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
498 504 }
499 505 if (m_dataset) {
500 506 m_dataset->updateSeries(q); // this notifies legend
501 507 }
502 508 emit restructuredBars(); // this notifies barchartitem
503 509 return true;
504 510 }
505 511
506 512 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
507 513 {
508 514 Q_Q(QBarSeries);
509 515 bool setsRemoved = false;
510 516 foreach (QBarSet* set, sets) {
511 517 if (m_barSets.contains(set)) {
512 518 m_barSets.removeOne(set);
513 519 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
514 520 setsRemoved = true;
515 521 }
516 522 }
517 523
518 524 if (setsRemoved) {
519 525 if (m_dataset) {
520 526 m_dataset->updateSeries(q); // this notifies legend
521 527 }
522 528 emit restructuredBars(); // this notifies barchartitem
523 529 }
524 530 return setsRemoved;
525 531 }
526 532
527 533 #include "moc_qbarseries.cpp"
528 534 #include "moc_qbarseries_p.cpp"
529 535
530 536 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,75 +1,77
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 #ifndef BARSERIES_H
22 22 #define BARSERIES_H
23 23
24 24 #include <qabstractseries.h>
25 25 #include <QStringList>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 typedef QStringList QBarCategories;
30 30
31 31 class QBarSet;
32 32 class BarCategory;
33 33 class QBarSeriesPrivate;
34 34
35 35 // Container for series
36 36 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
37 37 {
38 38 Q_OBJECT
39 39 public:
40 40 explicit QBarSeries(QObject *parent = 0);
41 41 virtual ~QBarSeries();
42 42
43 43 QAbstractSeries::SeriesType type() const;
44 44 void setCategories(QBarCategories categories);
45 45
46 void setBarMargin(qreal margin);
47
46 48 bool append(QBarSet *set);
47 49 bool remove(QBarSet *set);
48 50 bool append(QList<QBarSet* > sets);
49 51 bool remove(QList<QBarSet* > sets);
50 52 int barsetCount() const;
51 53 int categoryCount() const;
52 54 QList<QBarSet*> barSets() const;
53 55 QBarCategories categories() const;
54 56
55 57 void setLabelsVisible(bool visible = true);
56 58 bool isLabelsVisible() const;
57 59
58 60 protected:
59 61 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
60 62
61 63 Q_SIGNALS:
62 64 void clicked(QBarSet *barset, QString category);
63 65 void hovered(QBarSet* barset, bool status);
64 66
65 67 protected:
66 68 Q_DECLARE_PRIVATE(QBarSeries)
67 69 friend class BarChartItem;
68 70 friend class PercentBarChartItem;
69 71 friend class StackedBarChartItem;
70 72 friend class GroupedBarChartItem;
71 73 };
72 74
73 75 QTCOMMERCIALCHART_END_NAMESPACE
74 76
75 77 #endif // BARSERIES_H
General Comments 0
You need to be logged in to leave comments. Login now