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