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