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