##// END OF EJS Templates
scaledomain fix for barchart
sauimone -
r1423:4a12a8646078
parent child
Show More
@@ -1,640 +1,679
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarseries.h"
21 #include "qbarseries.h"
22 #include "qbarseries_p.h"
22 #include "qbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include "legendmarker_p.h"
26 #include "legendmarker_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "charttheme_p.h"
28 #include "charttheme_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 /*!
33 /*!
34 \class QBarSeries
34 \class QBarSeries
35 \brief part of QtCommercial chart API.
35 \brief part of QtCommercial chart API.
36 \mainclass
36 \mainclass
37
37
38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
41 shows the x-values.
41 shows the x-values.
42
42
43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
44 \image examples_barchart.png
44 \image examples_barchart.png
45
45
46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
47 */
47 */
48
48
49 /*!
49 /*!
50 \property QBarSeries::barMargin
50 \property QBarSeries::barMargin
51 \brief Defines the margin around bars.
51 \brief Defines the margin around bars.
52 */
52 */
53
53
54 /*!
54 /*!
55 \property QBarSeries::count
55 \property QBarSeries::count
56 \brief Holds the number of sets in series.
56 \brief Holds the number of sets in series.
57 */
57 */
58
58
59 /*!
59 /*!
60 \property QBarSeries::labelsVisible
60 \property QBarSeries::labelsVisible
61 \brief Defines the visibility of the labels in series
61 \brief Defines the visibility of the labels in series
62 */
62 */
63
63
64 /*!
64 /*!
65 \fn void QBarSeries::clicked(QBarSet *barset, int index)
65 \fn void QBarSeries::clicked(QBarSet *barset, int index)
66
66
67 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
67 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
68 Clicked bar inside set is indexed by \a index
68 Clicked bar inside set is indexed by \a index
69 */
69 */
70
70
71 /*!
71 /*!
72 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
72 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
73
73
74 The signal is emitted if mouse is hovered on top of series.
74 The signal is emitted if mouse is hovered on top of series.
75 Parameter \a barset is the pointer of barset, where hover happened.
75 Parameter \a barset is the pointer of barset, where hover happened.
76 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
76 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
77 */
77 */
78
78
79 /*!
79 /*!
80 \fn void QBarSeries::labelsVisibleChanged()
80 \fn void QBarSeries::labelsVisibleChanged()
81
81
82 This signal is emitted when labels visibility have changed.
82 This signal is emitted when labels visibility have changed.
83
83
84 \sa isLabelsVisible(), setLabelsVisible()
84 \sa isLabelsVisible(), setLabelsVisible()
85 */
85 */
86
86
87 /*!
87 /*!
88 \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets)
88 \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets)
89
89
90 This signal is emitted when \a sets have been added to the series.
90 This signal is emitted when \a sets have been added to the series.
91
91
92 \sa append(), insert()
92 \sa append(), insert()
93 */
93 */
94
94
95 /*!
95 /*!
96 \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets)
96 \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets)
97
97
98 This signal is emitted when \a sets have been removed from the series.
98 This signal is emitted when \a sets have been removed from the series.
99
99
100 \sa remove()
100 \sa remove()
101 */
101 */
102
102
103 /*!
103 /*!
104 Constructs empty QBarSeries.
104 Constructs empty QBarSeries.
105 QBarSeries is QObject which is a child of a \a parent.
105 QBarSeries is QObject which is a child of a \a parent.
106 */
106 */
107 QBarSeries::QBarSeries(QObject *parent) :
107 QBarSeries::QBarSeries(QObject *parent) :
108 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
108 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
109 {
109 {
110 }
110 }
111
111
112 /*!
112 /*!
113 Destructs barseries and owned barsets.
113 Destructs barseries and owned barsets.
114 */
114 */
115 QBarSeries::~QBarSeries()
115 QBarSeries::~QBarSeries()
116 {
116 {
117 Q_D(QBarSeries);
117 Q_D(QBarSeries);
118 if(d->m_dataset){
118 if(d->m_dataset){
119 d->m_dataset->removeSeries(this);
119 d->m_dataset->removeSeries(this);
120 }
120 }
121 }
121 }
122
122
123 /*!
123 /*!
124 \internal
124 \internal
125 */
125 */
126 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
126 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
127 QAbstractSeries(d,parent)
127 QAbstractSeries(d,parent)
128 {
128 {
129 }
129 }
130
130
131 /*!
131 /*!
132 Returns the type of series. Derived classes override this.
132 Returns the type of series. Derived classes override this.
133 */
133 */
134 QAbstractSeries::SeriesType QBarSeries::type() const
134 QAbstractSeries::SeriesType QBarSeries::type() const
135 {
135 {
136 return QAbstractSeries::SeriesTypeBar;
136 return QAbstractSeries::SeriesTypeBar;
137 }
137 }
138
138
139 /*!
139 /*!
140 Sets the margin of the bars of the series. The unit of \a margin is the unit of x-axis. Setting the margin to 0.0
140 Sets the margin of the bars of the series. The unit of \a margin is the unit of x-axis. Setting the margin to 0.0
141 means there is no margin around the bars, making a single bar or bargroup to take one x-axis unit on the screen.
141 means there is no margin around the bars, making a single bar or bargroup to take one x-axis unit on the screen.
142 Setting margin to maximum value of 1.0 makes the bar width to exactly 1 pixel on the screen. Bars cannot be zero
142 Setting margin to maximum value of 1.0 makes the bar width to exactly 1 pixel on the screen. Bars cannot be zero
143 width, otherwise they would not be visible at all. If you want to hide bars, use visible property of the series
143 width, otherwise they would not be visible at all. If you want to hide bars, use visible property of the series
144 instead.
144 instead.
145 */
145 */
146 void QBarSeries::setBarMargin(qreal margin)
146 void QBarSeries::setBarMargin(qreal margin)
147 {
147 {
148 Q_D(QBarSeries);
148 Q_D(QBarSeries);
149 d->setBarMargin(margin);
149 d->setBarMargin(margin);
150 }
150 }
151
151
152 /*!
152 /*!
153 Returns the margin around bars
153 Returns the margin around bars
154 */
154 */
155 qreal QBarSeries::barMargin() const
155 qreal QBarSeries::barMargin() const
156 {
156 {
157 Q_D(const QBarSeries);
157 Q_D(const QBarSeries);
158 return d->barMargin();
158 return d->barMargin();
159 }
159 }
160
160
161 /*!
161 /*!
162 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
162 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
163 Returns true, if appending succeeded.
163 Returns true, if appending succeeded.
164
164
165 */
165 */
166 bool QBarSeries::append(QBarSet *set)
166 bool QBarSeries::append(QBarSet *set)
167 {
167 {
168 Q_D(QBarSeries);
168 Q_D(QBarSeries);
169 bool success = d->append(set);
169 bool success = d->append(set);
170 if (success) {
170 if (success) {
171 QList<QBarSet*> sets;
171 QList<QBarSet*> sets;
172 sets.append(set);
172 sets.append(set);
173 emit barsetsAdded(sets);
173 emit barsetsAdded(sets);
174 }
174 }
175 return success;
175 return success;
176 }
176 }
177
177
178 /*!
178 /*!
179 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
179 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
180 Returns true, if set was removed.
180 Returns true, if set was removed.
181 */
181 */
182 bool QBarSeries::remove(QBarSet *set)
182 bool QBarSeries::remove(QBarSet *set)
183 {
183 {
184 Q_D(QBarSeries);
184 Q_D(QBarSeries);
185 bool success = d->remove(set);
185 bool success = d->remove(set);
186 if (success) {
186 if (success) {
187 QList<QBarSet*> sets;
187 QList<QBarSet*> sets;
188 sets.append(set);
188 sets.append(set);
189 emit barsetsRemoved(sets);
189 emit barsetsRemoved(sets);
190 }
190 }
191 return success;
191 return success;
192 }
192 }
193
193
194 /*!
194 /*!
195 Adds a list of barsets to series. Takes ownership of \a sets.
195 Adds a list of barsets to series. Takes ownership of \a sets.
196 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
196 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
197 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
197 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
198 and function returns false.
198 and function returns false.
199 */
199 */
200 bool QBarSeries::append(QList<QBarSet* > sets)
200 bool QBarSeries::append(QList<QBarSet* > sets)
201 {
201 {
202 Q_D(QBarSeries);
202 Q_D(QBarSeries);
203 bool success = d->append(sets);
203 bool success = d->append(sets);
204 if (success) {
204 if (success) {
205 emit barsetsAdded(sets);
205 emit barsetsAdded(sets);
206 }
206 }
207 return success;
207 return success;
208 }
208 }
209
209
210 /*!
210 /*!
211 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
211 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
212 Returns true, if inserting succeeded.
212 Returns true, if inserting succeeded.
213
213
214 */
214 */
215 bool QBarSeries::insert(int index, QBarSet *set)
215 bool QBarSeries::insert(int index, QBarSet *set)
216 {
216 {
217 Q_D(QBarSeries);
217 Q_D(QBarSeries);
218 bool success = d->insert(index, set);
218 bool success = d->insert(index, set);
219 if (success) {
219 if (success) {
220 QList<QBarSet*> sets;
220 QList<QBarSet*> sets;
221 sets.append(set);
221 sets.append(set);
222 emit barsetsAdded(sets);
222 emit barsetsAdded(sets);
223 }
223 }
224 return success;
224 return success;
225 }
225 }
226
226
227 /*!
227 /*!
228 Removes all of the bar sets from the series
228 Removes all of the bar sets from the series
229 */
229 */
230 void QBarSeries::clear()
230 void QBarSeries::clear()
231 {
231 {
232 Q_D(QBarSeries);
232 Q_D(QBarSeries);
233 QList<QBarSet *> sets = barSets();
233 QList<QBarSet *> sets = barSets();
234 bool success = d->remove(sets);
234 bool success = d->remove(sets);
235 if (success) {
235 if (success) {
236 emit barsetsRemoved(sets);
236 emit barsetsRemoved(sets);
237 }
237 }
238 }
238 }
239
239
240 /*!
240 /*!
241 Returns number of sets in series.
241 Returns number of sets in series.
242 */
242 */
243 int QBarSeries::barsetCount() const
243 int QBarSeries::barsetCount() const
244 {
244 {
245 Q_D(const QBarSeries);
245 Q_D(const QBarSeries);
246 return d->m_barSets.count();
246 return d->m_barSets.count();
247 }
247 }
248
248
249 /*!
249 /*!
250 Returns a list of sets in series. Keeps ownership of sets.
250 Returns a list of sets in series. Keeps ownership of sets.
251 */
251 */
252 QList<QBarSet*> QBarSeries::barSets() const
252 QList<QBarSet*> QBarSeries::barSets() const
253 {
253 {
254 Q_D(const QBarSeries);
254 Q_D(const QBarSeries);
255 return d->m_barSets;
255 return d->m_barSets;
256 }
256 }
257
257
258 /*!
258 /*!
259 Sets the visibility of labels in series to \a visible
259 Sets the visibility of labels in series to \a visible
260 */
260 */
261 void QBarSeries::setLabelsVisible(bool visible)
261 void QBarSeries::setLabelsVisible(bool visible)
262 {
262 {
263 Q_D(QBarSeries);
263 Q_D(QBarSeries);
264 if (d->m_labelsVisible != visible) {
264 if (d->m_labelsVisible != visible) {
265 d->setLabelsVisible(visible);
265 d->setLabelsVisible(visible);
266 emit labelsVisibleChanged();
266 emit labelsVisibleChanged();
267 }
267 }
268 }
268 }
269
269
270 /*!
270 /*!
271 Returns the visibility of labels
271 Returns the visibility of labels
272 */
272 */
273 bool QBarSeries::isLabelsVisible() const
273 bool QBarSeries::isLabelsVisible() const
274 {
274 {
275 Q_D(const QBarSeries);
275 Q_D(const QBarSeries);
276 return d->m_labelsVisible;
276 return d->m_labelsVisible;
277 }
277 }
278
278
279 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
279 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
280
280
281 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
281 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
282 QAbstractSeriesPrivate(q),
282 QAbstractSeriesPrivate(q),
283 m_barMargin(0.5), // Default value is 50% of category width
283 m_barMargin(0.5), // Default value is 50% of category width
284 m_labelsVisible(false),
284 m_labelsVisible(false),
285 m_visible(true)
285 m_visible(true)
286 {
286 {
287 }
287 }
288
288
289 void QBarSeriesPrivate::setCategories(QStringList categories)
289 void QBarSeriesPrivate::setCategories(QStringList categories)
290 {
290 {
291 m_categories = categories;
291 m_categories = categories;
292 }
292 }
293
293
294 void QBarSeriesPrivate::insertCategory(int index, const QString category)
294 void QBarSeriesPrivate::insertCategory(int index, const QString category)
295 {
295 {
296 m_categories.insert(index, category);
296 m_categories.insert(index, category);
297 emit categoriesUpdated();
297 emit categoriesUpdated();
298 }
298 }
299
299
300 void QBarSeriesPrivate::removeCategory(int index)
300 void QBarSeriesPrivate::removeCategory(int index)
301 {
301 {
302 m_categories.removeAt(index);
302 m_categories.removeAt(index);
303 emit categoriesUpdated();
303 emit categoriesUpdated();
304 }
304 }
305
305
306 int QBarSeriesPrivate::categoryCount() const
306 int QBarSeriesPrivate::categoryCount() const
307 {
307 {
308 if (m_categories.count() > 0) {
308 if (m_categories.count() > 0) {
309 return m_categories.count();
309 return m_categories.count();
310 }
310 }
311
311
312 // No categories defined. return count of longest set.
312 // No categories defined. return count of longest set.
313 int count = 0;
313 int count = 0;
314 for (int i=0; i<m_barSets.count(); i++) {
314 for (int i=0; i<m_barSets.count(); i++) {
315 if (m_barSets.at(i)->count() > count) {
315 if (m_barSets.at(i)->count() > count) {
316 count = m_barSets.at(i)->count();
316 count = m_barSets.at(i)->count();
317 }
317 }
318 }
318 }
319
319
320 return count;
320 return count;
321 }
321 }
322
322
323 QStringList QBarSeriesPrivate::categories() const
323 QStringList QBarSeriesPrivate::categories() const
324 {
324 {
325 if (m_categories.count() > 0) {
325 if (m_categories.count() > 0) {
326 return m_categories;
326 return m_categories;
327 }
327 }
328
328
329 // No categories defined. retun list of indices.
329 // No categories defined. retun list of indices.
330 QStringList categories;
330 QStringList categories;
331
331
332 int count = categoryCount();
332 int count = categoryCount();
333 for (int i = 0; i < count; i++) {
333 for (int i = 0; i < count; i++) {
334 categories.append(QString::number(i));
334 categories.append(QString::number(i));
335 }
335 }
336 return categories;
336 return categories;
337 }
337 }
338
338
339 void QBarSeriesPrivate::setBarMargin(qreal margin)
339 void QBarSeriesPrivate::setBarMargin(qreal margin)
340 {
340 {
341 if (margin > 1.0) {
341 if (margin > 1.0) {
342 margin = 1.0;
342 margin = 1.0;
343 } else if (margin < 0.0) {
343 } else if (margin < 0.0) {
344 margin = 0.0;
344 margin = 0.0;
345 }
345 }
346
346
347 m_barMargin = margin;
347 m_barMargin = margin;
348 emit updatedBars();
348 emit updatedBars();
349 }
349 }
350
350
351 qreal QBarSeriesPrivate::barMargin() const
351 qreal QBarSeriesPrivate::barMargin() const
352 {
352 {
353 return m_barMargin;
353 return m_barMargin;
354 }
354 }
355
355
356 QBarSet* QBarSeriesPrivate::barsetAt(int index)
356 QBarSet* QBarSeriesPrivate::barsetAt(int index)
357 {
357 {
358 return m_barSets.at(index);
358 return m_barSets.at(index);
359 }
359 }
360
360
361 void QBarSeriesPrivate::setVisible(bool visible)
361 void QBarSeriesPrivate::setVisible(bool visible)
362 {
362 {
363 m_visible = visible;
363 m_visible = visible;
364 emit updatedBars();
364 emit updatedBars();
365 }
365 }
366
366
367 void QBarSeriesPrivate::setLabelsVisible(bool visible)
367 void QBarSeriesPrivate::setLabelsVisible(bool visible)
368 {
368 {
369 m_labelsVisible = visible;
369 m_labelsVisible = visible;
370 emit labelsVisibleChanged(visible);
370 emit labelsVisibleChanged(visible);
371 }
371 }
372
372
373 QString QBarSeriesPrivate::categoryName(int category)
373 QString QBarSeriesPrivate::categoryName(int category)
374 {
374 {
375 if ((category >= 0) && (category < m_categories.count())) {
375 if ((category >= 0) && (category < m_categories.count())) {
376 return m_categories.at(category);
376 return m_categories.at(category);
377 }
377 }
378
378
379 return QString::number(category);
379 return QString::number(category);
380 }
380 }
381
381
382 qreal QBarSeriesPrivate::min()
382 qreal QBarSeriesPrivate::min()
383 {
383 {
384 if (m_barSets.count() <= 0) {
384 if (m_barSets.count() <= 0) {
385 return 0;
385 return 0;
386 }
386 }
387 qreal min = INT_MAX;
387 qreal min = INT_MAX;
388
388
389 for (int i = 0; i < m_barSets.count(); i++) {
389 for (int i = 0; i < m_barSets.count(); i++) {
390 int categoryCount = m_barSets.at(i)->count();
390 int categoryCount = m_barSets.at(i)->count();
391 for (int j = 0; j < categoryCount; j++) {
391 for (int j = 0; j < categoryCount; j++) {
392 qreal temp = m_barSets.at(i)->at(j).y();
392 qreal temp = m_barSets.at(i)->at(j).y();
393 if (temp < min)
393 if (temp < min)
394 min = temp;
394 min = temp;
395 }
395 }
396 }
396 }
397 return min;
397 return min;
398 }
398 }
399
399
400 qreal QBarSeriesPrivate::max()
400 qreal QBarSeriesPrivate::max()
401 {
401 {
402 if (m_barSets.count() <= 0) {
402 if (m_barSets.count() <= 0) {
403 return 0;
403 return 0;
404 }
404 }
405 qreal max = INT_MIN;
405 qreal max = INT_MIN;
406
406
407 for (int i = 0; i < m_barSets.count(); i++) {
407 for (int i = 0; i < m_barSets.count(); i++) {
408 int categoryCount = m_barSets.at(i)->count();
408 int categoryCount = m_barSets.at(i)->count();
409 for (int j = 0; j < categoryCount; j++) {
409 for (int j = 0; j < categoryCount; j++) {
410 qreal temp = m_barSets.at(i)->at(j).y();
410 qreal temp = m_barSets.at(i)->at(j).y();
411 if (temp > max)
411 if (temp > max)
412 max = temp;
412 max = temp;
413 }
413 }
414 }
414 }
415
415
416 return max;
416 return max;
417 }
417 }
418
418
419 qreal QBarSeriesPrivate::valueAt(int set, int category)
419 qreal QBarSeriesPrivate::valueAt(int set, int category)
420 {
420 {
421 if ((set < 0) || (set >= m_barSets.count())) {
421 if ((set < 0) || (set >= m_barSets.count())) {
422 // No set, no value.
422 // No set, no value.
423 return 0;
423 return 0;
424 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
424 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
425 // No category, no value.
425 // No category, no value.
426 return 0;
426 return 0;
427 }
427 }
428
428
429 return m_barSets.at(set)->at(category).y();
429 return m_barSets.at(set)->at(category).y();
430 }
430 }
431
431
432 qreal QBarSeriesPrivate::percentageAt(int set, int category)
432 qreal QBarSeriesPrivate::percentageAt(int set, int category)
433 {
433 {
434 if ((set < 0) || (set >= m_barSets.count())) {
434 if ((set < 0) || (set >= m_barSets.count())) {
435 // No set, no value.
435 // No set, no value.
436 return 0;
436 return 0;
437 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
437 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
438 // No category, no value.
438 // No category, no value.
439 return 0;
439 return 0;
440 }
440 }
441
441
442 qreal value = m_barSets.at(set)->at(category).y();
442 qreal value = m_barSets.at(set)->at(category).y();
443 qreal sum = categorySum(category);
443 qreal sum = categorySum(category);
444 if ( qFuzzyIsNull(sum) ) {
444 if ( qFuzzyIsNull(sum) ) {
445 return 0;
445 return 0;
446 }
446 }
447
447
448 return value / sum;
448 return value / sum;
449 }
449 }
450
450
451 qreal QBarSeriesPrivate::categorySum(int category)
451 qreal QBarSeriesPrivate::categorySum(int category)
452 {
452 {
453 qreal sum(0);
453 qreal sum(0);
454 int count = m_barSets.count(); // Count sets
454 int count = m_barSets.count(); // Count sets
455 for (int set = 0; set < count; set++) {
455 for (int set = 0; set < count; set++) {
456 if (category < m_barSets.at(set)->count())
456 if (category < m_barSets.at(set)->count())
457 sum += m_barSets.at(set)->at(category).y();
457 sum += m_barSets.at(set)->at(category).y();
458 }
458 }
459 return sum;
459 return sum;
460 }
460 }
461
461
462 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
462 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
463 {
463 {
464 qreal sum(0);
464 qreal sum(0);
465 int count = m_barSets.count(); // Count sets
465 int count = m_barSets.count(); // Count sets
466 for (int set = 0; set < count; set++) {
466 for (int set = 0; set < count; set++) {
467 if (category < m_barSets.at(set)->count())
467 if (category < m_barSets.at(set)->count())
468 sum += qAbs(m_barSets.at(set)->at(category).y());
468 sum += qAbs(m_barSets.at(set)->at(category).y());
469 }
469 }
470 return sum;
470 return sum;
471 }
471 }
472
472
473 qreal QBarSeriesPrivate::maxCategorySum()
473 qreal QBarSeriesPrivate::maxCategorySum()
474 {
474 {
475 qreal max = INT_MIN;
475 qreal max = INT_MIN;
476 int count = categoryCount();
476 int count = categoryCount();
477 for (int i = 0; i < count; i++) {
477 for (int i = 0; i < count; i++) {
478 qreal sum = categorySum(i);
478 qreal sum = categorySum(i);
479 if (sum > max)
479 if (sum > max)
480 max = sum;
480 max = sum;
481 }
481 }
482 return max;
482 return max;
483 }
483 }
484
484
485 qreal QBarSeriesPrivate::minX()
486 {
487 if (m_barSets.count() <= 0) {
488 return 0;
489 }
490 qreal min = INT_MAX;
491
492 for (int i = 0; i < m_barSets.count(); i++) {
493 int categoryCount = m_barSets.at(i)->count();
494 for (int j = 0; j < categoryCount; j++) {
495 qreal temp = m_barSets.at(i)->at(j).x();
496 if (temp < min)
497 min = temp;
498 }
499 }
500 return min;
501 }
502
503 qreal QBarSeriesPrivate::maxX()
504 {
505 if (m_barSets.count() <= 0) {
506 return 0;
507 }
508 qreal max = INT_MIN;
509
510 for (int i = 0; i < m_barSets.count(); i++) {
511 int categoryCount = m_barSets.at(i)->count();
512 for (int j = 0; j < categoryCount; j++) {
513 qreal temp = m_barSets.at(i)->at(j).x();
514 if (temp > max)
515 max = temp;
516 }
517 }
518
519 return max;
520 }
521
522
485 void QBarSeriesPrivate::scaleDomain(Domain& domain)
523 void QBarSeriesPrivate::scaleDomain(Domain& domain)
486 {
524 {
487 qreal minX(domain.minX());
525 qreal minX(domain.minX());
488 qreal minY(domain.minY());
526 qreal minY(domain.minY());
489 qreal maxX(domain.maxX());
527 qreal maxX(domain.maxX());
490 qreal maxY(domain.maxY());
528 qreal maxY(domain.maxY());
491 int tickXCount(domain.tickXCount());
529 int tickXCount(domain.tickXCount());
492 int tickYCount(domain.tickYCount());
530 int tickYCount(domain.tickYCount());
493
531
494 qreal x = categoryCount();
532 qreal seriesMinX = this->minX();
533 qreal seriesMaxX = this->maxX();
495 qreal y = max();
534 qreal y = max();
496 minX = qMin(minX, x) - 0.5;
535 minX = qMin(minX, seriesMinX - 0.5);
497 minY = qMin(minY, y);
536 minY = qMin(minY, y);
498 maxX = qMax(maxX, x) + 0.5;
537 maxX = qMax(maxX, seriesMaxX + 0.5);
499 maxY = qMax(maxY, y);
538 maxY = qMax(maxY, y);
500 tickXCount = x+1;
539 tickXCount = categoryCount()+1;
501
540
502 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
541 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
503 }
542 }
504
543
505 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
544 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
506 {
545 {
507 Q_Q(QBarSeries);
546 Q_Q(QBarSeries);
508
547
509 BarChartItem* bar = new BarChartItem(q,presenter);
548 BarChartItem* bar = new BarChartItem(q,presenter);
510 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
549 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
511 presenter->animator()->addAnimation(bar);
550 presenter->animator()->addAnimation(bar);
512 }
551 }
513 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
552 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
514 return bar;
553 return bar;
515
554
516 }
555 }
517
556
518 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
557 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
519 {
558 {
520 Q_Q(QBarSeries);
559 Q_Q(QBarSeries);
521 QList<LegendMarker*> markers;
560 QList<LegendMarker*> markers;
522 foreach(QBarSet* set, q->barSets()) {
561 foreach(QBarSet* set, q->barSets()) {
523 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
562 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
524 markers << marker;
563 markers << marker;
525 }
564 }
526
565
527 return markers;
566 return markers;
528 }
567 }
529
568
530 bool QBarSeriesPrivate::append(QBarSet *set)
569 bool QBarSeriesPrivate::append(QBarSet *set)
531 {
570 {
532 Q_Q(QBarSeries);
571 Q_Q(QBarSeries);
533 if ((m_barSets.contains(set)) || (set == 0)) {
572 if ((m_barSets.contains(set)) || (set == 0)) {
534 // Fail if set is already in list or set is null.
573 // Fail if set is already in list or set is null.
535 return false;
574 return false;
536 }
575 }
537 m_barSets.append(set);
576 m_barSets.append(set);
538 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
577 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
539 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
578 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
540 if (m_dataset) {
579 if (m_dataset) {
541 m_dataset->updateSeries(q); // this notifies legend
580 m_dataset->updateSeries(q); // this notifies legend
542 }
581 }
543 emit restructuredBars(); // this notifies barchartitem
582 emit restructuredBars(); // this notifies barchartitem
544 return true;
583 return true;
545 }
584 }
546
585
547 bool QBarSeriesPrivate::remove(QBarSet *set)
586 bool QBarSeriesPrivate::remove(QBarSet *set)
548 {
587 {
549 Q_Q(QBarSeries);
588 Q_Q(QBarSeries);
550 if (!m_barSets.contains(set)) {
589 if (!m_barSets.contains(set)) {
551 // Fail if set is not in list
590 // Fail if set is not in list
552 return false;
591 return false;
553 }
592 }
554 m_barSets.removeOne(set);
593 m_barSets.removeOne(set);
555 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
594 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
556 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
595 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
557 if (m_dataset) {
596 if (m_dataset) {
558 m_dataset->updateSeries(q); // this notifies legend
597 m_dataset->updateSeries(q); // this notifies legend
559 }
598 }
560 emit restructuredBars(); // this notifies barchartitem
599 emit restructuredBars(); // this notifies barchartitem
561 return true;
600 return true;
562 }
601 }
563
602
564 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
603 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
565 {
604 {
566 Q_Q(QBarSeries);
605 Q_Q(QBarSeries);
567 foreach (QBarSet* set, sets) {
606 foreach (QBarSet* set, sets) {
568 if ((set == 0) || (m_barSets.contains(set))) {
607 if ((set == 0) || (m_barSets.contains(set))) {
569 // Fail if any of the sets is null or is already appended.
608 // Fail if any of the sets is null or is already appended.
570 return false;
609 return false;
571 }
610 }
572 if (sets.count(set) != 1) {
611 if (sets.count(set) != 1) {
573 // Also fail if same set is more than once in given list.
612 // Also fail if same set is more than once in given list.
574 return false;
613 return false;
575 }
614 }
576 }
615 }
577
616
578 foreach (QBarSet* set, sets) {
617 foreach (QBarSet* set, sets) {
579 m_barSets.append(set);
618 m_barSets.append(set);
580 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
619 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
581 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
620 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
582 }
621 }
583 if (m_dataset) {
622 if (m_dataset) {
584 m_dataset->updateSeries(q); // this notifies legend
623 m_dataset->updateSeries(q); // this notifies legend
585 }
624 }
586 emit restructuredBars(); // this notifies barchartitem
625 emit restructuredBars(); // this notifies barchartitem
587 return true;
626 return true;
588 }
627 }
589
628
590 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
629 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
591 {
630 {
592 Q_Q(QBarSeries);
631 Q_Q(QBarSeries);
593 if (sets.count() == 0) {
632 if (sets.count() == 0) {
594 return false;
633 return false;
595 }
634 }
596 foreach (QBarSet* set, sets) {
635 foreach (QBarSet* set, sets) {
597 if ((set == 0) || (!m_barSets.contains(set))) {
636 if ((set == 0) || (!m_barSets.contains(set))) {
598 // Fail if any of the sets is null or is not in series
637 // Fail if any of the sets is null or is not in series
599 return false;
638 return false;
600 }
639 }
601 if (sets.count(set) != 1) {
640 if (sets.count(set) != 1) {
602 // Also fail if same set is more than once in given list.
641 // Also fail if same set is more than once in given list.
603 return false;
642 return false;
604 }
643 }
605 }
644 }
606
645
607 foreach (QBarSet* set, sets) {
646 foreach (QBarSet* set, sets) {
608 m_barSets.removeOne(set);
647 m_barSets.removeOne(set);
609 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
648 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
610 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
649 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
611 }
650 }
612
651
613 if (m_dataset) {
652 if (m_dataset) {
614 m_dataset->updateSeries(q); // this notifies legend
653 m_dataset->updateSeries(q); // this notifies legend
615 }
654 }
616 emit restructuredBars(); // this notifies barchartitem
655 emit restructuredBars(); // this notifies barchartitem
617 return true;
656 return true;
618 }
657 }
619
658
620 bool QBarSeriesPrivate::insert(int index, QBarSet *set)
659 bool QBarSeriesPrivate::insert(int index, QBarSet *set)
621 {
660 {
622 Q_Q(QBarSeries);
661 Q_Q(QBarSeries);
623 if ((m_barSets.contains(set)) || (set == 0)) {
662 if ((m_barSets.contains(set)) || (set == 0)) {
624 // Fail if set is already in list or set is null.
663 // Fail if set is already in list or set is null.
625 return false;
664 return false;
626 }
665 }
627 m_barSets.insert(index, set);
666 m_barSets.insert(index, set);
628 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
667 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
629 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
668 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
630 if (m_dataset) {
669 if (m_dataset) {
631 m_dataset->updateSeries(q); // this notifies legend
670 m_dataset->updateSeries(q); // this notifies legend
632 }
671 }
633 emit restructuredBars(); // this notifies barchartitem
672 emit restructuredBars(); // this notifies barchartitem
634 return true;
673 return true;
635 }
674 }
636
675
637 #include "moc_qbarseries.cpp"
676 #include "moc_qbarseries.cpp"
638 #include "moc_qbarseries_p.cpp"
677 #include "moc_qbarseries_p.cpp"
639
678
640 QTCOMMERCIALCHART_END_NAMESPACE
679 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,100 +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);
48 void setCategories(QStringList categories);
49 void insertCategory(int index, const QString category);
49 void insertCategory(int index, const QString category);
50 void removeCategory(int index);
50 void removeCategory(int index);
51 int categoryCount() const;
51 int categoryCount() const;
52 QStringList categories() const;
52 QStringList categories() const;
53
53
54 void setBarMargin(qreal margin);
54 void setBarMargin(qreal margin);
55 qreal barMargin() const;
55 qreal barMargin() const;
56
56
57 void setVisible(bool visible);
57 void setVisible(bool visible);
58 void setLabelsVisible(bool visible);
58 void setLabelsVisible(bool visible);
59
59
60 void scaleDomain(Domain& domain);
60 void scaleDomain(Domain& domain);
61 Chart* createGraphics(ChartPresenter* presenter);
61 Chart* createGraphics(ChartPresenter* presenter);
62 QList<LegendMarker*> createLegendMarker(QLegend* legend);
62 QList<LegendMarker*> createLegendMarker(QLegend* legend);
63
63
64 bool append(QBarSet *set);
64 bool append(QBarSet *set);
65 bool remove(QBarSet *set);
65 bool remove(QBarSet *set);
66 bool append(QList<QBarSet* > sets);
66 bool append(QList<QBarSet* > sets);
67 bool remove(QList<QBarSet* > sets);
67 bool remove(QList<QBarSet* > sets);
68 bool insert(int index, QBarSet *set);
68 bool insert(int index, QBarSet *set);
69
69
70 QBarSet* barsetAt(int index);
70 QBarSet* barsetAt(int index);
71 QString categoryName(int category);
71 QString categoryName(int category);
72 qreal min();
72 qreal min();
73 qreal max();
73 qreal max();
74 qreal valueAt(int set, int category);
74 qreal valueAt(int set, int category);
75 qreal percentageAt(int set, int category);
75 qreal percentageAt(int set, int category);
76 qreal categorySum(int category);
76 qreal categorySum(int category);
77 qreal absoluteCategorySum(int category);
77 qreal absoluteCategorySum(int category);
78 qreal maxCategorySum();
78 qreal maxCategorySum();
79 qreal minX();
80 qreal maxX();
79
81
80 Q_SIGNALS:
82 Q_SIGNALS:
81 void clicked(QBarSet *barset, int index);
83 void clicked(QBarSet *barset, int index);
82 void updatedBars();
84 void updatedBars();
83 void restructuredBars();
85 void restructuredBars();
84 void categoriesUpdated();
86 void categoriesUpdated();
85 void labelsVisibleChanged(bool visible);
87 void labelsVisibleChanged(bool visible);
86
88
87 protected:
89 protected:
88 QList<QBarSet *> m_barSets;
90 QList<QBarSet *> m_barSets;
89 QStringList m_categories;
91 QStringList m_categories;
90 qreal m_barMargin;
92 qreal m_barMargin;
91 bool m_labelsVisible;
93 bool m_labelsVisible;
92 bool m_visible;
94 bool m_visible;
93
95
94 private:
96 private:
95 Q_DECLARE_PUBLIC(QBarSeries)
97 Q_DECLARE_PUBLIC(QBarSeries)
96 };
98 };
97
99
98 QTCOMMERCIALCHART_END_NAMESPACE
100 QTCOMMERCIALCHART_END_NAMESPACE
99
101
100 #endif // QBARSERIESPRIVATE_P_H
102 #endif // QBARSERIESPRIVATE_P_H
@@ -1,105 +1,105
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 "qgroupedbarseries.h"
21 #include "qgroupedbarseries.h"
22 #include "qgroupedbarseries_p.h"
22 #include "qgroupedbarseries_p.h"
23 #include "groupedbarchartitem_p.h"
23 #include "groupedbarchartitem_p.h"
24 #include "chartdataset_p.h"
24 #include "chartdataset_p.h"
25 #include "charttheme_p.h"
25 #include "charttheme_p.h"
26 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 /*!
30 /*!
31 \class QGroupedBarSeries
31 \class QGroupedBarSeries
32 \brief part of QtCommercial chart API.
32 \brief part of QtCommercial chart API.
33 \mainclass
33 \mainclass
34
34
35 QGroupedBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
35 QGroupedBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
36 as groups, where bars in same category are grouped next to each other. QGroupedBarSeries groups the data
36 as groups, where bars in same category are grouped next to each other. QGroupedBarSeries groups the data
37 from sets to categories, which are defined by a QStringList.
37 from sets to categories, which are defined by a QStringList.
38
38
39 See the \l {GroupedbarChart Example} {grouped bar chart example} to learn how to create a grouped bar chart.
39 See the \l {GroupedbarChart Example} {grouped bar chart example} to learn how to create a grouped bar chart.
40 \image examples_groupedbarchart.png
40 \image examples_groupedbarchart.png
41
41
42 \sa QBarSet, QPercentBarSeries, QBarSeries, QStackedBarSeries
42 \sa QBarSet, QPercentBarSeries, QBarSeries, QStackedBarSeries
43 */
43 */
44
44
45 /*!
45 /*!
46 Constructs empty QGroupedBarSeries.
46 Constructs empty QGroupedBarSeries.
47 QGroupedBarSeries is QObject which is a child of a \a parent.
47 QGroupedBarSeries is QObject which is a child of a \a parent.
48 */
48 */
49 QGroupedBarSeries::QGroupedBarSeries(QObject *parent)
49 QGroupedBarSeries::QGroupedBarSeries(QObject *parent)
50 : QBarSeries(*new QGroupedBarSeriesPrivate(this), parent)
50 : QBarSeries(*new QGroupedBarSeriesPrivate(this), parent)
51 {
51 {
52 }
52 }
53
53
54 /*!
54 /*!
55 Returns QChartSeries::SeriesTypeGroupedBar.
55 Returns QChartSeries::SeriesTypeGroupedBar.
56 */
56 */
57 QAbstractSeries::SeriesType QGroupedBarSeries::type() const
57 QAbstractSeries::SeriesType QGroupedBarSeries::type() const
58 {
58 {
59 return QAbstractSeries::SeriesTypeGroupedBar;
59 return QAbstractSeries::SeriesTypeGroupedBar;
60 }
60 }
61
61
62 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
62 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63
63
64 QGroupedBarSeriesPrivate::QGroupedBarSeriesPrivate(QGroupedBarSeries *q) : QBarSeriesPrivate(q)
64 QGroupedBarSeriesPrivate::QGroupedBarSeriesPrivate(QGroupedBarSeries *q) : QBarSeriesPrivate(q)
65 {
65 {
66
66
67 }
67 }
68
68
69 void QGroupedBarSeriesPrivate::scaleDomain(Domain& domain)
69 void QGroupedBarSeriesPrivate::scaleDomain(Domain& domain)
70 {
70 {
71 qreal minX(domain.minX());
71 qreal minX(domain.minX());
72 qreal minY(domain.minY());
72 qreal minY(domain.minY());
73 qreal maxX(domain.maxX());
73 qreal maxX(domain.maxX());
74 qreal maxY(domain.maxY());
74 qreal maxY(domain.maxY());
75 int tickXCount(domain.tickXCount());
75 int tickXCount(domain.tickXCount());
76 int tickYCount(domain.tickYCount());
76 int tickYCount(domain.tickYCount());
77
77
78 qreal x = categoryCount();
78 qreal x = categoryCount();
79 qreal y = max();
79 qreal y = max();
80 minX = qMin(minX, x) - 0.5;
80 minX = qMin(minX, -0.5);
81 minY = qMin(minY, y);
81 minY = qMin(minY, y);
82 maxX = qMax(maxX, x) - 0.5;
82 maxX = qMax(maxX, x - 0.5);
83 maxY = qMax(maxY, y);
83 maxY = qMax(maxY, y);
84 tickXCount = x+1;
84 tickXCount = x+1;
85
85
86 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
86 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
87 }
87 }
88
88
89
89
90 Chart* QGroupedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
90 Chart* QGroupedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
91 {
91 {
92 Q_Q(QGroupedBarSeries);
92 Q_Q(QGroupedBarSeries);
93
93
94 GroupedBarChartItem* bar = new GroupedBarChartItem(q,presenter);
94 GroupedBarChartItem* bar = new GroupedBarChartItem(q,presenter);
95 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
95 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
96 presenter->animator()->addAnimation(bar);
96 presenter->animator()->addAnimation(bar);
97 }
97 }
98 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
98 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
99 return bar;
99 return bar;
100 }
100 }
101
101
102 #include "moc_qgroupedbarseries.cpp"
102 #include "moc_qgroupedbarseries.cpp"
103
103
104 QTCOMMERCIALCHART_END_NAMESPACE
104 QTCOMMERCIALCHART_END_NAMESPACE
105
105
@@ -1,104 +1,104
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 "qpercentbarseries.h"
21 #include "qpercentbarseries.h"
22 #include "qpercentbarseries_p.h"
22 #include "qpercentbarseries_p.h"
23 #include "percentbarchartitem_p.h"
23 #include "percentbarchartitem_p.h"
24 #include "chartdataset_p.h"
24 #include "chartdataset_p.h"
25 #include "charttheme_p.h"
25 #include "charttheme_p.h"
26 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 /*!
30 /*!
31 \class QPercentBarSeries
31 \class QPercentBarSeries
32 \brief part of QtCommercial chart API.
32 \brief part of QtCommercial chart API.
33 \mainclass
33 \mainclass
34
34
35 QPercentBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
35 QPercentBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
36 as stacks, where each bar is shown as percentage of all bars in that category.
36 as stacks, where each bar is shown as percentage of all bars in that category.
37 QPercentBarSeries groups the data from sets to categories, which are defined by a QStringList.
37 QPercentBarSeries groups the data from sets to categories, which are defined by a QStringList.
38
38
39 See the \l {PercentbarChart Example} {percent bar chart example} to learn how to create a percent bar chart.
39 See the \l {PercentbarChart Example} {percent bar chart example} to learn how to create a percent bar chart.
40 \image examples_percentbarchart.png
40 \image examples_percentbarchart.png
41
41
42 \sa QBarSet, QStackedBarSeries, QBarSeries
42 \sa QBarSet, QStackedBarSeries, QBarSeries
43 */
43 */
44
44
45 /*!
45 /*!
46 Constructs empty QPercentBarSeries.
46 Constructs empty QPercentBarSeries.
47 QPercentBarSeries is QObject which is a child of a \a parent.
47 QPercentBarSeries is QObject which is a child of a \a parent.
48 */
48 */
49 QPercentBarSeries::QPercentBarSeries(QObject *parent)
49 QPercentBarSeries::QPercentBarSeries(QObject *parent)
50 : QBarSeries(*new QPercentBarSeriesPrivate(this), parent)
50 : QBarSeries(*new QPercentBarSeriesPrivate(this), parent)
51 {
51 {
52 }
52 }
53
53
54 /*!
54 /*!
55 Returns QChartSeries::SeriesTypePercentBar.
55 Returns QChartSeries::SeriesTypePercentBar.
56 */
56 */
57 QAbstractSeries::SeriesType QPercentBarSeries::type() const
57 QAbstractSeries::SeriesType QPercentBarSeries::type() const
58 {
58 {
59 return QAbstractSeries::SeriesTypePercentBar;
59 return QAbstractSeries::SeriesTypePercentBar;
60 }
60 }
61
61
62 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
62 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63
63
64 QPercentBarSeriesPrivate::QPercentBarSeriesPrivate(QPercentBarSeries *q) : QBarSeriesPrivate(q)
64 QPercentBarSeriesPrivate::QPercentBarSeriesPrivate(QPercentBarSeries *q) : QBarSeriesPrivate(q)
65 {
65 {
66
66
67 }
67 }
68
68
69 void QPercentBarSeriesPrivate::scaleDomain(Domain& domain)
69 void QPercentBarSeriesPrivate::scaleDomain(Domain& domain)
70 {
70 {
71 qreal minX(domain.minX());
71 qreal minX(domain.minX());
72 qreal minY(domain.minY());
72 qreal minY(domain.minY());
73 qreal maxX(domain.maxX());
73 qreal maxX(domain.maxX());
74 qreal maxY(domain.maxY());
74 qreal maxY(domain.maxY());
75 int tickXCount(domain.tickXCount());
75 int tickXCount(domain.tickXCount());
76 int tickYCount(domain.tickYCount());
76 int tickYCount(domain.tickYCount());
77
77
78 qreal x = categoryCount();
78 qreal x = categoryCount();
79 minX = qMin(minX, x) - 0.5;
79 minX = qMin(minX, -0.5);
80 maxX = qMax(maxX, x) - 0.5;
80 maxX = qMax(maxX, x - 0.5);
81 minY = 0;
81 minY = 0;
82 maxY = 100;
82 maxY = 100;
83 tickXCount = x+1;
83 tickXCount = x+1;
84
84
85 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
85 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
86 }
86 }
87
87
88
88
89 Chart* QPercentBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
89 Chart* QPercentBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
90 {
90 {
91 Q_Q(QPercentBarSeries);
91 Q_Q(QPercentBarSeries);
92
92
93 PercentBarChartItem* bar = new PercentBarChartItem(q,presenter);
93 PercentBarChartItem* bar = new PercentBarChartItem(q,presenter);
94 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
94 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
95 presenter->animator()->addAnimation(bar);
95 presenter->animator()->addAnimation(bar);
96 }
96 }
97 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
97 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
98 return bar;
98 return bar;
99 }
99 }
100
100
101 #include "moc_qpercentbarseries.cpp"
101 #include "moc_qpercentbarseries.cpp"
102
102
103 QTCOMMERCIALCHART_END_NAMESPACE
103 QTCOMMERCIALCHART_END_NAMESPACE
104
104
@@ -1,105 +1,105
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 "qstackedbarseries.h"
21 #include "qstackedbarseries.h"
22 #include "qstackedbarseries_p.h"
22 #include "qstackedbarseries_p.h"
23 #include "stackedbarchartitem_p.h"
23 #include "stackedbarchartitem_p.h"
24 #include "chartdataset_p.h"
24 #include "chartdataset_p.h"
25 #include "charttheme_p.h"
25 #include "charttheme_p.h"
26 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 /*!
30 /*!
31 \class QStackedBarSeries
31 \class QStackedBarSeries
32 \brief part of QtCommercial chart API.
32 \brief part of QtCommercial chart API.
33 \mainclass
33 \mainclass
34
34
35 QStackedBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
35 QStackedBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
36 as stacks, where bars in same category are stacked on top of each other.
36 as stacks, where bars in same category are stacked on top of each other.
37 QStackedBarSeries groups the data from sets to categories, which are defined by QStringList.
37 QStackedBarSeries groups the data from sets to categories, which are defined by QStringList.
38
38
39 See the \l {StackedbarChart Example} {stacked bar chart example} to learn how to create a stacked bar chart.
39 See the \l {StackedbarChart Example} {stacked bar chart example} to learn how to create a stacked bar chart.
40 \image examples_stackedbarchart.png
40 \image examples_stackedbarchart.png
41
41
42 \sa QBarSet, QPercentBarSeries, QBarSeries
42 \sa QBarSet, QPercentBarSeries, QBarSeries
43 */
43 */
44
44
45 /*!
45 /*!
46 Constructs empty QStackedBarSeries.
46 Constructs empty QStackedBarSeries.
47 QStackedBarSeries is QObject which is a child of a \a parent.
47 QStackedBarSeries is QObject which is a child of a \a parent.
48 */
48 */
49 QStackedBarSeries::QStackedBarSeries(QObject *parent)
49 QStackedBarSeries::QStackedBarSeries(QObject *parent)
50 : QBarSeries(*new QStackedBarSeriesPrivate(this), parent)
50 : QBarSeries(*new QStackedBarSeriesPrivate(this), parent)
51 {
51 {
52 }
52 }
53
53
54 /*!
54 /*!
55 Returns QChartSeries::SeriesTypeStackedBar.
55 Returns QChartSeries::SeriesTypeStackedBar.
56 */
56 */
57 QAbstractSeries::SeriesType QStackedBarSeries::type() const
57 QAbstractSeries::SeriesType QStackedBarSeries::type() const
58 {
58 {
59 return QAbstractSeries::SeriesTypeStackedBar;
59 return QAbstractSeries::SeriesTypeStackedBar;
60 }
60 }
61
61
62 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
62 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63
63
64 QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(QStackedBarSeries *q) : QBarSeriesPrivate(q)
64 QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(QStackedBarSeries *q) : QBarSeriesPrivate(q)
65 {
65 {
66
66
67 }
67 }
68
68
69 void QStackedBarSeriesPrivate::scaleDomain(Domain& domain)
69 void QStackedBarSeriesPrivate::scaleDomain(Domain& domain)
70 {
70 {
71 qreal minX(domain.minX());
71 qreal minX(domain.minX());
72 qreal minY(domain.minY());
72 qreal minY(domain.minY());
73 qreal maxX(domain.maxX());
73 qreal maxX(domain.maxX());
74 qreal maxY(domain.maxY());
74 qreal maxY(domain.maxY());
75 int tickXCount(domain.tickXCount());
75 int tickXCount(domain.tickXCount());
76 int tickYCount(domain.tickYCount());
76 int tickYCount(domain.tickYCount());
77
77
78 qreal x = categoryCount();
78 qreal x = categoryCount();
79 qreal y = maxCategorySum();
79 qreal y = maxCategorySum();
80 minX = qMin(minX, x) - 0.5;
80 minX = qMin(minX, -0.5);
81 minY = qMin(minY, y);
81 minY = qMin(minY, y);
82 maxX = qMax(maxX, x) - 0.5;
82 maxX = qMax(maxX, x - 0.5);
83 maxY = qMax(maxY, y);
83 maxY = qMax(maxY, y);
84 tickXCount = x+1;
84 tickXCount = x+1;
85
85
86 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
86 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
87 }
87 }
88
88
89
89
90 Chart* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
90 Chart* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
91 {
91 {
92 Q_Q(QStackedBarSeries);
92 Q_Q(QStackedBarSeries);
93
93
94 StackedBarChartItem* bar = new StackedBarChartItem(q,presenter);
94 StackedBarChartItem* bar = new StackedBarChartItem(q,presenter);
95 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
95 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
96 presenter->animator()->addAnimation(bar);
96 presenter->animator()->addAnimation(bar);
97 }
97 }
98 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
98 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
99 return bar;
99 return bar;
100 }
100 }
101
101
102 #include "moc_qstackedbarseries.cpp"
102 #include "moc_qstackedbarseries.cpp"
103
103
104 QTCOMMERCIALCHART_END_NAMESPACE
104 QTCOMMERCIALCHART_END_NAMESPACE
105
105
General Comments 0
You need to be logged in to leave comments. Login now