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