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