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