##// END OF EJS Templates
barchart signals for properties and changes
sauimone -
r1353:e34fe2671696
parent child
Show More
@@ -1,543 +1,567
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 bool success = d->append(set);
128 if (success) {
129 QList<QBarSet*> sets;
130 sets.append(set);
131 emit barsetsAdded(sets);
132 }
133 return success;
128 }
134 }
129
135
130 /*!
136 /*!
131 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
137 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
132 Returns true, if set was removed.
138 Returns true, if set was removed.
133 */
139 */
134 bool QBarSeries::remove(QBarSet *set)
140 bool QBarSeries::remove(QBarSet *set)
135 {
141 {
136 Q_D(QBarSeries);
142 Q_D(QBarSeries);
137 return d->remove(set);
143 bool success = d->remove(set);
144 if (success) {
145 QList<QBarSet*> sets;
146 sets.append(set);
147 emit barsetsRemoved(sets);
148 }
149 return success;
138 }
150 }
139
151
140 /*!
152 /*!
141 Adds a list of barsets to series. Takes ownership of \a sets.
153 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,
154 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
155 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.
156 and function returns false.
145 */
157 */
146 bool QBarSeries::append(QList<QBarSet* > sets)
158 bool QBarSeries::append(QList<QBarSet* > sets)
147 {
159 {
148 Q_D(QBarSeries);
160 Q_D(QBarSeries);
149 return d->append(sets);
161 bool success = d->append(sets);
162 if (success) {
163 emit barsetsAdded(sets);
164 }
165 return success;
150 }
166 }
151
167
152 /*!
168 /*!
153 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
169 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
154 */
170 */
155 bool QBarSeries::remove(QList<QBarSet* > sets)
171 bool QBarSeries::remove(QList<QBarSet* > sets)
156 {
172 {
157 Q_D(QBarSeries);
173 Q_D(QBarSeries);
158 return d->remove(sets);
174 bool success = d->remove(sets);
175 if (success) {
176 emit barsetsRemoved(sets);
177 }
178 return success;
159 }
179 }
160
180
161 void QBarSeries::clear()
181 void QBarSeries::clear()
162 {
182 {
163 Q_D(QBarSeries);
183 Q_D(QBarSeries);
164 d->m_barSets.clear();
184 d->m_barSets.clear();
165 }
185 }
166
186
167 /*!
187 /*!
168 Returns number of sets in series.
188 Returns number of sets in series.
169 */
189 */
170 int QBarSeries::barsetCount() const
190 int QBarSeries::barsetCount() const
171 {
191 {
172 Q_D(const QBarSeries);
192 Q_D(const QBarSeries);
173 return d->m_barSets.count();
193 return d->m_barSets.count();
174 }
194 }
175
195
176 /*!
196 /*!
177 Returns a list of sets in series. Keeps ownership of sets.
197 Returns a list of sets in series. Keeps ownership of sets.
178 */
198 */
179 QList<QBarSet*> QBarSeries::barSets() const
199 QList<QBarSet*> QBarSeries::barSets() const
180 {
200 {
181 Q_D(const QBarSeries);
201 Q_D(const QBarSeries);
182 return d->m_barSets;
202 return d->m_barSets;
183 }
203 }
184
204
185 /*!
205 /*!
186 Sets the visibility of labels in series to \a visible
206 Sets the visibility of labels in series to \a visible
187 */
207 */
188 void QBarSeries::setLabelsVisible(bool visible)
208 void QBarSeries::setLabelsVisible(bool visible)
189 {
209 {
190 Q_D(QBarSeries);
210 Q_D(QBarSeries);
191 if (d->m_labelsVisible != visible) {
211 if (d->m_labelsVisible != visible) {
192 d->m_labelsVisible = visible;
212 d->setLabelsVisible(visible);
193 emit d->labelsVisibleChanged(visible);
213 emit labelsVisibleChanged();
194 }
214 }
195 }
215 }
196
216
197 /*!
217 /*!
198 Returns the visibility of labels
218 Returns the visibility of labels
199 */
219 */
200 bool QBarSeries::isLabelsVisible() const
220 bool QBarSeries::isLabelsVisible() const
201 {
221 {
202 Q_D(const QBarSeries);
222 Q_D(const QBarSeries);
203 return d->m_labelsVisible;
223 return d->m_labelsVisible;
204 }
224 }
205
225
206 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
226 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
207
227
208 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
228 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
209 QAbstractSeriesPrivate(q),
229 QAbstractSeriesPrivate(q),
210 m_barMargin(0.5), // Default value is 50% of category width
230 m_barMargin(0.5), // Default value is 50% of category width
211 m_labelsVisible(false),
231 m_labelsVisible(false),
212 m_visible(true)
232 m_visible(true)
213 {
233 {
214 }
234 }
215
235
216 void QBarSeriesPrivate::setCategories(QStringList categories)
236 void QBarSeriesPrivate::setCategories(QStringList categories)
217 {
237 {
218 m_categories = categories;
238 m_categories = categories;
219 }
239 }
220
240
221 void QBarSeriesPrivate::insertCategory(int index, const QString category)
241 void QBarSeriesPrivate::insertCategory(int index, const QString category)
222 {
242 {
223 m_categories.insert(index, category);
243 m_categories.insert(index, category);
224 emit categoriesUpdated();
244 emit categoriesUpdated();
225 }
245 }
226
246
227 void QBarSeriesPrivate::removeCategory(int index)
247 void QBarSeriesPrivate::removeCategory(int index)
228 {
248 {
229 m_categories.removeAt(index);
249 m_categories.removeAt(index);
230 emit categoriesUpdated();
250 emit categoriesUpdated();
231 }
251 }
232
252
233 int QBarSeriesPrivate::categoryCount() const
253 int QBarSeriesPrivate::categoryCount() const
234 {
254 {
235 if (m_categories.count() > 0) {
255 if (m_categories.count() > 0) {
236 return m_categories.count();
256 return m_categories.count();
237 }
257 }
238
258
239 // No categories defined. return count of longest set.
259 // No categories defined. return count of longest set.
240 int count = 0;
260 int count = 0;
241 for (int i=0; i<m_barSets.count(); i++) {
261 for (int i=0; i<m_barSets.count(); i++) {
242 if (m_barSets.at(i)->count() > count) {
262 if (m_barSets.at(i)->count() > count) {
243 count = m_barSets.at(i)->count();
263 count = m_barSets.at(i)->count();
244 }
264 }
245 }
265 }
246
266
247 return count;
267 return count;
248 }
268 }
249
269
250 QStringList QBarSeriesPrivate::categories() const
270 QStringList QBarSeriesPrivate::categories() const
251 {
271 {
252 if (m_categories.count() > 0) {
272 if (m_categories.count() > 0) {
253 return m_categories;
273 return m_categories;
254 }
274 }
255
275
256 // No categories defined. retun list of indices.
276 // No categories defined. retun list of indices.
257 QStringList categories;
277 QStringList categories;
258
278
259 int count = categoryCount();
279 int count = categoryCount();
260 for (int i = 0; i < count; i++) {
280 for (int i = 0; i < count; i++) {
261 categories.append(QString::number(i));
281 categories.append(QString::number(i));
262 }
282 }
263 return categories;
283 return categories;
264 }
284 }
265
285
266 void QBarSeriesPrivate::setBarMargin(qreal margin)
286 void QBarSeriesPrivate::setBarMargin(qreal margin)
267 {
287 {
268 if (margin > 1.0) {
288 if (margin > 1.0) {
269 margin = 1.0;
289 margin = 1.0;
270 } else if (margin < 0.0) {
290 } else if (margin < 0.0) {
271 margin = 0.0;
291 margin = 0.0;
272 }
292 }
273
293
274 m_barMargin = margin;
294 m_barMargin = margin;
275 emit updatedBars();
295 emit updatedBars();
276 }
296 }
277
297
278 qreal QBarSeriesPrivate::barMargin() const
298 qreal QBarSeriesPrivate::barMargin() const
279 {
299 {
280 return m_barMargin;
300 return m_barMargin;
281 }
301 }
282
302
283 QBarSet* QBarSeriesPrivate::barsetAt(int index)
303 QBarSet* QBarSeriesPrivate::barsetAt(int index)
284 {
304 {
285 return m_barSets.at(index);
305 return m_barSets.at(index);
286 }
306 }
287
307
288 void QBarSeriesPrivate::setVisible(bool visible)
308 void QBarSeriesPrivate::setVisible(bool visible)
289 {
309 {
290 if (m_visible != visible) {
291 m_visible = visible;
310 m_visible = visible;
292 emit updatedBars();
311 emit updatedBars();
293 }
312 }
294 }
295
313
296 bool QBarSeriesPrivate::isVisible() const
314 void QBarSeriesPrivate::setLabelsVisible(bool visible)
297 {
315 {
298 return m_visible;
316 m_labelsVisible = visible;
317 emit labelsVisibleChanged(visible);
299 }
318 }
300
319
301 QString QBarSeriesPrivate::categoryName(int category)
320 QString QBarSeriesPrivate::categoryName(int category)
302 {
321 {
303 if ((category >= 0) && (category < m_categories.count())) {
322 if ((category >= 0) && (category < m_categories.count())) {
304 return m_categories.at(category);
323 return m_categories.at(category);
305 }
324 }
306
325
307 return QString::number(category);
326 return QString::number(category);
308 }
327 }
309
328
310 qreal QBarSeriesPrivate::min()
329 qreal QBarSeriesPrivate::min()
311 {
330 {
312 if (m_barSets.count() <= 0) {
331 if (m_barSets.count() <= 0) {
313 return 0;
332 return 0;
314 }
333 }
315 qreal min = INT_MAX;
334 qreal min = INT_MAX;
316
335
317 for (int i = 0; i < m_barSets.count(); i++) {
336 for (int i = 0; i < m_barSets.count(); i++) {
318 int categoryCount = m_barSets.at(i)->count();
337 int categoryCount = m_barSets.at(i)->count();
319 for (int j = 0; j < categoryCount; j++) {
338 for (int j = 0; j < categoryCount; j++) {
320 qreal temp = m_barSets.at(i)->at(j).y();
339 qreal temp = m_barSets.at(i)->at(j).y();
321 if (temp < min)
340 if (temp < min)
322 min = temp;
341 min = temp;
323 }
342 }
324 }
343 }
325 return min;
344 return min;
326 }
345 }
327
346
328 qreal QBarSeriesPrivate::max()
347 qreal QBarSeriesPrivate::max()
329 {
348 {
330 if (m_barSets.count() <= 0) {
349 if (m_barSets.count() <= 0) {
331 return 0;
350 return 0;
332 }
351 }
333 qreal max = INT_MIN;
352 qreal max = INT_MIN;
334
353
335 for (int i = 0; i < m_barSets.count(); i++) {
354 for (int i = 0; i < m_barSets.count(); i++) {
336 int categoryCount = m_barSets.at(i)->count();
355 int categoryCount = m_barSets.at(i)->count();
337 for (int j = 0; j < categoryCount; j++) {
356 for (int j = 0; j < categoryCount; j++) {
338 qreal temp = m_barSets.at(i)->at(j).y();
357 qreal temp = m_barSets.at(i)->at(j).y();
339 if (temp > max)
358 if (temp > max)
340 max = temp;
359 max = temp;
341 }
360 }
342 }
361 }
343
362
344 return max;
363 return max;
345 }
364 }
346
365
347 qreal QBarSeriesPrivate::valueAt(int set, int category)
366 qreal QBarSeriesPrivate::valueAt(int set, int category)
348 {
367 {
349 if ((set < 0) || (set >= m_barSets.count())) {
368 if ((set < 0) || (set >= m_barSets.count())) {
350 // No set, no value.
369 // No set, no value.
351 return 0;
370 return 0;
352 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
371 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
353 // No category, no value.
372 // No category, no value.
354 return 0;
373 return 0;
355 }
374 }
356
375
357 return m_barSets.at(set)->at(category).y();
376 return m_barSets.at(set)->at(category).y();
358 }
377 }
359
378
360 qreal QBarSeriesPrivate::percentageAt(int set, int category)
379 qreal QBarSeriesPrivate::percentageAt(int set, int category)
361 {
380 {
362 if ((set < 0) || (set >= m_barSets.count())) {
381 if ((set < 0) || (set >= m_barSets.count())) {
363 // No set, no value.
382 // No set, no value.
364 return 0;
383 return 0;
365 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
384 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
366 // No category, no value.
385 // No category, no value.
367 return 0;
386 return 0;
368 }
387 }
369
388
370 qreal value = m_barSets.at(set)->at(category).y();
389 qreal value = m_barSets.at(set)->at(category).y();
371 qreal sum = categorySum(category);
390 qreal sum = categorySum(category);
372 if ( qFuzzyIsNull(sum) ) {
391 if ( qFuzzyIsNull(sum) ) {
373 return 0;
392 return 0;
374 }
393 }
375
394
376 return value / sum;
395 return value / sum;
377 }
396 }
378
397
379 qreal QBarSeriesPrivate::categorySum(int category)
398 qreal QBarSeriesPrivate::categorySum(int category)
380 {
399 {
381 qreal sum(0);
400 qreal sum(0);
382 int count = m_barSets.count(); // Count sets
401 int count = m_barSets.count(); // Count sets
383 for (int set = 0; set < count; set++) {
402 for (int set = 0; set < count; set++) {
384 if (category < m_barSets.at(set)->count())
403 if (category < m_barSets.at(set)->count())
385 sum += m_barSets.at(set)->at(category).y();
404 sum += m_barSets.at(set)->at(category).y();
386 }
405 }
387 return sum;
406 return sum;
388 }
407 }
389
408
390 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
409 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
391 {
410 {
392 qreal sum(0);
411 qreal sum(0);
393 int count = m_barSets.count(); // Count sets
412 int count = m_barSets.count(); // Count sets
394 for (int set = 0; set < count; set++) {
413 for (int set = 0; set < count; set++) {
395 if (category < m_barSets.at(set)->count())
414 if (category < m_barSets.at(set)->count())
396 sum += qAbs(m_barSets.at(set)->at(category).y());
415 sum += qAbs(m_barSets.at(set)->at(category).y());
397 }
416 }
398 return sum;
417 return sum;
399 }
418 }
400
419
401 qreal QBarSeriesPrivate::maxCategorySum()
420 qreal QBarSeriesPrivate::maxCategorySum()
402 {
421 {
403 qreal max = INT_MIN;
422 qreal max = INT_MIN;
404 int count = categoryCount();
423 int count = categoryCount();
405 for (int i = 0; i < count; i++) {
424 for (int i = 0; i < count; i++) {
406 qreal sum = categorySum(i);
425 qreal sum = categorySum(i);
407 if (sum > max)
426 if (sum > max)
408 max = sum;
427 max = sum;
409 }
428 }
410 return max;
429 return max;
411 }
430 }
412
431
413 void QBarSeriesPrivate::scaleDomain(Domain& domain)
432 void QBarSeriesPrivate::scaleDomain(Domain& domain)
414 {
433 {
415 qreal minX(domain.minX());
434 qreal minX(domain.minX());
416 qreal minY(domain.minY());
435 qreal minY(domain.minY());
417 qreal maxX(domain.maxX());
436 qreal maxX(domain.maxX());
418 qreal maxY(domain.maxY());
437 qreal maxY(domain.maxY());
419 int tickXCount(domain.tickXCount());
438 int tickXCount(domain.tickXCount());
420 int tickYCount(domain.tickYCount());
439 int tickYCount(domain.tickYCount());
421
440
422 qreal x = categoryCount();
441 qreal x = categoryCount();
423 qreal y = max();
442 qreal y = max();
424 minX = qMin(minX, x) - 0.5;
443 minX = qMin(minX, x) - 0.5;
425 minY = qMin(minY, y);
444 minY = qMin(minY, y);
426 maxX = qMax(maxX, x) - 0.5;
445 maxX = qMax(maxX, x) - 0.5;
427 maxY = qMax(maxY, y);
446 maxY = qMax(maxY, y);
428 tickXCount = x+1;
447 tickXCount = x+1;
429
448
430 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
449 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
431 }
450 }
432
451
433 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
452 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
434 {
453 {
435 Q_Q(QBarSeries);
454 Q_Q(QBarSeries);
436
455
437 BarChartItem* bar = new BarChartItem(q,presenter);
456 BarChartItem* bar = new BarChartItem(q,presenter);
438 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
457 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
439 presenter->animator()->addAnimation(bar);
458 presenter->animator()->addAnimation(bar);
440 }
459 }
441 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
460 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
442 return bar;
461 return bar;
443
462
444 }
463 }
445
464
446 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
465 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
447 {
466 {
448 Q_Q(QBarSeries);
467 Q_Q(QBarSeries);
449 QList<LegendMarker*> markers;
468 QList<LegendMarker*> markers;
450 foreach(QBarSet* set, q->barSets()) {
469 foreach(QBarSet* set, q->barSets()) {
451 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
470 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
452 markers << marker;
471 markers << marker;
453 }
472 }
454
473
455 return markers;
474 return markers;
456 }
475 }
457
476
458 bool QBarSeriesPrivate::append(QBarSet *set)
477 bool QBarSeriesPrivate::append(QBarSet *set)
459 {
478 {
460 Q_Q(QBarSeries);
479 Q_Q(QBarSeries);
461 if ((m_barSets.contains(set)) || (set == 0)) {
480 if ((m_barSets.contains(set)) || (set == 0)) {
462 // Fail if set is already in list or set is null.
481 // Fail if set is already in list or set is null.
463 return false;
482 return false;
464 }
483 }
465 m_barSets.append(set);
484 m_barSets.append(set);
466 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
485 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
467 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
486 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
468 if (m_dataset) {
487 if (m_dataset) {
469 m_dataset->updateSeries(q); // this notifies legend
488 m_dataset->updateSeries(q); // this notifies legend
470 }
489 }
471 emit restructuredBars(); // this notifies barchartitem
490 emit restructuredBars(); // this notifies barchartitem
472 return true;
491 return true;
473 }
492 }
474
493
475 bool QBarSeriesPrivate::remove(QBarSet *set)
494 bool QBarSeriesPrivate::remove(QBarSet *set)
476 {
495 {
477 Q_Q(QBarSeries);
496 Q_Q(QBarSeries);
478 if (!m_barSets.contains(set)) {
497 if (!m_barSets.contains(set)) {
479 // Fail if set is not in list
498 // Fail if set is not in list
480 return false;
499 return false;
481 }
500 }
482 m_barSets.removeOne(set);
501 m_barSets.removeOne(set);
483 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
502 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
484 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
503 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
485 if (m_dataset) {
504 if (m_dataset) {
486 m_dataset->updateSeries(q); // this notifies legend
505 m_dataset->updateSeries(q); // this notifies legend
487 }
506 }
488 emit restructuredBars(); // this notifies barchartitem
507 emit restructuredBars(); // this notifies barchartitem
489 return true;
508 return true;
490 }
509 }
491
510
492 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
511 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
493 {
512 {
494 Q_Q(QBarSeries);
513 Q_Q(QBarSeries);
495 foreach (QBarSet* set, sets) {
514 foreach (QBarSet* set, sets) {
496 if ((set == 0) || (m_barSets.contains(set))) {
515 if ((set == 0) || (m_barSets.contains(set))) {
497 // Fail if any of the sets is null or is already appended.
516 // Fail if any of the sets is null or is already appended.
498 return false;
517 return false;
499 }
518 }
500 if (sets.count(set) != 1) {
519 if (sets.count(set) != 1) {
501 // Also fail if same set is more than once in given list.
520 // Also fail if same set is more than once in given list.
502 return false;
521 return false;
503 }
522 }
504 }
523 }
505
524
506 foreach (QBarSet* set, sets) {
525 foreach (QBarSet* set, sets) {
507 m_barSets.append(set);
526 m_barSets.append(set);
508 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
527 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
509 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
528 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
510 }
529 }
511 if (m_dataset) {
530 if (m_dataset) {
512 m_dataset->updateSeries(q); // this notifies legend
531 m_dataset->updateSeries(q); // this notifies legend
513 }
532 }
514 emit restructuredBars(); // this notifies barchartitem
533 emit restructuredBars(); // this notifies barchartitem
515 return true;
534 return true;
516 }
535 }
517
536
518 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
537 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
519 {
538 {
520 Q_Q(QBarSeries);
539 Q_Q(QBarSeries);
521 bool setsRemoved = false;
522 foreach (QBarSet* set, sets) {
540 foreach (QBarSet* set, sets) {
523 if (m_barSets.contains(set)) {
541 if ((set == 0) || (!m_barSets.contains(set))) {
542 // Fail if any of the sets is null or is not in series
543 return false;
544 }
545 if (sets.count(set) != 1) {
546 // Also fail if same set is more than once in given list.
547 return false;
548 }
549 }
550
551 foreach (QBarSet* set, sets) {
524 m_barSets.removeOne(set);
552 m_barSets.removeOne(set);
525 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
553 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
526 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
554 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
527 setsRemoved = true;
528 }
529 }
555 }
530
556
531 if (setsRemoved) {
532 if (m_dataset) {
557 if (m_dataset) {
533 m_dataset->updateSeries(q); // this notifies legend
558 m_dataset->updateSeries(q); // this notifies legend
534 }
559 }
535 emit restructuredBars(); // this notifies barchartitem
560 emit restructuredBars(); // this notifies barchartitem
536 }
561 return true;
537 return setsRemoved;
538 }
562 }
539
563
540 #include "moc_qbarseries.cpp"
564 #include "moc_qbarseries.cpp"
541 #include "moc_qbarseries_p.cpp"
565 #include "moc_qbarseries_p.cpp"
542
566
543 QTCOMMERCIALCHART_END_NAMESPACE
567 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,77 +1,82
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 #ifndef BARSERIES_H
21 #ifndef BARSERIES_H
22 #define BARSERIES_H
22 #define BARSERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25 #include <QStringList>
25 #include <QStringList>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 class QBarSet;
29 class QBarSet;
30 class QBarSeriesPrivate;
30 class QBarSeriesPrivate;
31
31
32 // Container for series
32 // Container for series
33 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
33 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(qreal barMargin READ barMargin WRITE setBarMargin)
36 Q_PROPERTY(qreal barMargin READ barMargin WRITE setBarMargin)
37 Q_PROPERTY(int count READ barsetCount)
37 Q_PROPERTY(int count READ barsetCount)
38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible)
38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible)
39
39
40 public:
40 public:
41 explicit QBarSeries(QObject *parent = 0);
41 explicit QBarSeries(QObject *parent = 0);
42 virtual ~QBarSeries();
42 virtual ~QBarSeries();
43
43
44 QAbstractSeries::SeriesType type() const;
44 QAbstractSeries::SeriesType type() const;
45
45
46 void setBarMargin(qreal margin);
46 void setBarMargin(qreal margin);
47 qreal barMargin() const;
47 qreal barMargin() const;
48
48
49 bool append(QBarSet *set);
49 bool append(QBarSet *set);
50 bool remove(QBarSet *set);
50 bool remove(QBarSet *set);
51 bool append(QList<QBarSet* > sets);
51 bool append(QList<QBarSet* > sets);
52 bool remove(QList<QBarSet* > sets);
52 bool remove(QList<QBarSet* > sets);
53 int barsetCount() const;
53 int barsetCount() const;
54 QList<QBarSet*> barSets() const;
54 QList<QBarSet*> barSets() const;
55 void clear();
55 void clear();
56
56
57 void setLabelsVisible(bool visible = true);
57 void setLabelsVisible(bool visible = true);
58 bool isLabelsVisible() const;
58 bool isLabelsVisible() const;
59
59
60 protected:
60 protected:
61 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
61 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
62
62
63 Q_SIGNALS:
63 Q_SIGNALS:
64 void clicked(QBarSet *barset, int index);
64 void clicked(QBarSet *barset, int index);
65 void hovered(QBarSet* barset, bool status);
65 void hovered(QBarSet* barset, bool status);
66 void visibleChanged();
67 void labelsVisibleChanged();
68
69 void barsetsAdded(QList<QBarSet*> sets);
70 void barsetsRemoved(QList<QBarSet*> sets);
66
71
67 protected:
72 protected:
68 Q_DECLARE_PRIVATE(QBarSeries)
73 Q_DECLARE_PRIVATE(QBarSeries)
69 friend class BarChartItem;
74 friend class BarChartItem;
70 friend class PercentBarChartItem;
75 friend class PercentBarChartItem;
71 friend class StackedBarChartItem;
76 friend class StackedBarChartItem;
72 friend class GroupedBarChartItem;
77 friend class GroupedBarChartItem;
73 };
78 };
74
79
75 QTCOMMERCIALCHART_END_NAMESPACE
80 QTCOMMERCIALCHART_END_NAMESPACE
76
81
77 #endif // BARSERIES_H
82 #endif // BARSERIES_H
@@ -1,99 +1,99
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 bool isVisible() const;
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
68
69 QBarSet* barsetAt(int index);
69 QBarSet* barsetAt(int index);
70 QString categoryName(int category);
70 QString categoryName(int category);
71 qreal min();
71 qreal min();
72 qreal max();
72 qreal max();
73 qreal valueAt(int set, int category);
73 qreal valueAt(int set, int category);
74 qreal percentageAt(int set, int category);
74 qreal percentageAt(int set, int category);
75 qreal categorySum(int category);
75 qreal categorySum(int category);
76 qreal absoluteCategorySum(int category);
76 qreal absoluteCategorySum(int category);
77 qreal maxCategorySum();
77 qreal maxCategorySum();
78
78
79 Q_SIGNALS:
79 Q_SIGNALS:
80 void clicked(QBarSet *barset, int index);
80 void clicked(QBarSet *barset, int index);
81 void updatedBars();
81 void updatedBars();
82 void restructuredBars();
82 void restructuredBars();
83 void categoriesUpdated();
83 void categoriesUpdated();
84 void labelsVisibleChanged(bool visible);
84 void labelsVisibleChanged(bool visible);
85
85
86 protected:
86 protected:
87 QList<QBarSet *> m_barSets;
87 QList<QBarSet *> m_barSets;
88 QStringList m_categories;
88 QStringList m_categories;
89 qreal m_barMargin;
89 qreal m_barMargin;
90 bool m_labelsVisible;
90 bool m_labelsVisible;
91 bool m_visible;
91 bool m_visible;
92
92
93 private:
93 private:
94 Q_DECLARE_PUBLIC(QBarSeries)
94 Q_DECLARE_PUBLIC(QBarSeries)
95 };
95 };
96
96
97 QTCOMMERCIALCHART_END_NAMESPACE
97 QTCOMMERCIALCHART_END_NAMESPACE
98
98
99 #endif // QBARSERIESPRIVATE_P_H
99 #endif // QBARSERIESPRIVATE_P_H
@@ -1,309 +1,398
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarset.h"
21 #include "qbarset.h"
22 #include "qbarset_p.h"
22 #include "qbarset_p.h"
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 /*!
26 /*!
27 \class QBarSet
27 \class QBarSet
28 \brief part of QtCommercial chart API.
28 \brief part of QtCommercial chart API.
29
29
30 QBarSet represents one set of bars. Set of bars contains one data value for each category.
30 QBarSet represents one set of bars. Set of bars contains one data value for each category.
31 First value of set is assumed to belong to first category, second to second category and so on.
31 First value of set is assumed to belong to first category, second to second category and so on.
32 If set has fewer values than there are categories, then the missing values are assumed to be
32 If set has fewer values than there are categories, then the missing values are assumed to be
33 at the end of set. For missing values in middle of a set, numerical value of zero is used.
33 at the end of set. For missing values in middle of a set, numerical value of zero is used.
34
34
35 \mainclass
35 \mainclass
36
36
37 \sa QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries
37 \sa QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries
38 */
38 */
39
39
40 /*!
40 /*!
41 Constructs QBarSet with a name of \a name and with parent of \a parent
41 Constructs QBarSet with a name of \a name and with parent of \a parent
42 */
42 */
43 QBarSet::QBarSet(const QString name, QObject *parent)
43 QBarSet::QBarSet(const QString name, QObject *parent)
44 : QObject(parent)
44 : QObject(parent)
45 ,d_ptr(new QBarSetPrivate(name,this))
45 ,d_ptr(new QBarSetPrivate(name,this))
46 {
46 {
47 }
47 }
48
48
49 /*!
49 /*!
50 Destroys the barset
50 Destroys the barset
51 */
51 */
52 QBarSet::~QBarSet()
52 QBarSet::~QBarSet()
53 {
53 {
54 // NOTE: d_ptr destroyed by QObject
54 // NOTE: d_ptr destroyed by QObject
55 }
55 }
56
56
57 /*!
57 /*!
58 Sets new \a name for set.
58 Sets new \a name for set.
59 */
59 */
60 void QBarSet::setName(const QString name)
60 void QBarSet::setName(const QString name)
61 {
61 {
62 d_ptr->m_name = name;
62 d_ptr->m_name = name;
63 emit nameChanged();
63 }
64 }
64
65
65 /*!
66 /*!
66 Returns name of the set.
67 Returns name of the set.
67 */
68 */
68 QString QBarSet::name() const
69 QString QBarSet::name() const
69 {
70 {
70 return d_ptr->m_name;
71 return d_ptr->m_name;
71 }
72 }
72
73
73 /*!
74 /*!
74 Appends a point to set. Parameter \a value x coordinate defines the
75 Appends a point to set. Parameter \a value x coordinate defines the
75 position in x-axis and y coordinate defines the height of bar.
76 position in x-axis and y coordinate defines the height of bar.
76 Depending on presentation (QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries)
77 Depending on presentation (QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries)
77 the x values are used or ignored.
78 the x values are used or ignored.
78 */
79 */
79 void QBarSet::append(const QPointF value)
80 void QBarSet::append(const QPointF value)
80 {
81 {
81 d_ptr->m_values.append(value);
82 int index = d_ptr->m_values.count();
82 emit d_ptr->restructuredBars();
83 d_ptr->append(value);
84 emit valuesAdded(index, 1);
83 }
85 }
84
86
85 /*!
87 /*!
86 Appends a list of \a values to set. Works like append with single point.
88 Appends a list of \a values to set. Works like append with single point.
87 \sa append()
89 \sa append()
88 */
90 */
89 void QBarSet::append(const QList<QPointF> values)
91 void QBarSet::append(const QList<QPointF> values)
90 {
92 {
91 for (int i=0; i<values.count(); i++) {
93 int index = d_ptr->m_values.count();
92 d_ptr->m_values.append(values.at(i));
94 d_ptr->append(values);
93 }
95 emit valuesAdded(index, values.count());
94 emit d_ptr->restructuredBars();
95 }
96 }
96
97
97 /*!
98 /*!
98 Appends new value \a value to the end of set. Internally the value is converted to QPointF,
99 Appends new value \a value to the end of set. Internally the value is converted to QPointF,
99 with x coordinate being the index of appended value and y coordinate is the value.
100 with x coordinate being the index of appended value and y coordinate is the value.
100 */
101 */
101 void QBarSet::append(const qreal value)
102 void QBarSet::append(const qreal value)
102 {
103 {
104 // Convert to QPointF and use other append(QPointF) method.
103 append(QPointF(d_ptr->m_values.count(), value));
105 append(QPointF(d_ptr->m_values.count(), value));
104 }
106 }
105
107
106 /*!
108 /*!
107 Appends a list of reals to set. Works like append with single real value. The values in list
109 Appends a list of reals to set. Works like append with single real value. The values in list
108 are converted to QPointF, where x coordinate is the index of point and y coordinate is the value.
110 are converted to QPointF, where x coordinate is the index of point and y coordinate is the value.
109 \sa append()
111 \sa append()
110 */
112 */
111 void QBarSet::append(const QList<qreal> values)
113 void QBarSet::append(const QList<qreal> values)
112 {
114 {
113 int index = d_ptr->m_values.count();
115 int index = d_ptr->m_values.count();
114 for (int i=0; i<values.count(); i++) {
116 d_ptr->append(values);
115 d_ptr->m_values.append(QPointF(index,values.at(i)));
117 emit valuesAdded(index, values.count());
116 index++;
117 }
118 emit d_ptr->restructuredBars();
119 }
118 }
120
119
121 /*!
120 /*!
122 Convinience operator. Same as append, with real \a value.
121 Convinience operator. Same as append, with real \a value.
123 \sa append()
122 \sa append()
124 */
123 */
125 QBarSet& QBarSet::operator << (const qreal &value)
124 QBarSet& QBarSet::operator << (const qreal &value)
126 {
125 {
127 append(value);
126 append(value);
128 return *this;
127 return *this;
129 }
128 }
130
129
131 /*!
130 /*!
132 Convinience operator. Same as append, with QPointF \a value.
131 Convinience operator. Same as append, with QPointF \a value.
133 \sa append()
132 \sa append()
134 */
133 */
135 QBarSet& QBarSet::operator << (const QPointF &value)
134 QBarSet& QBarSet::operator << (const QPointF &value)
136 {
135 {
137 append(value);
136 append(value);
138 return *this;
137 return *this;
139 }
138 }
140
139
141 /*!
140 /*!
142 Inserts new \a value on the \a index position.
141 Inserts new \a value on the \a index position.
143 The value that is currently at this postion is moved to postion index + 1
142 The value that is currently at this postion is moved to postion index + 1
144 \sa remove()
143 \sa remove()
145 */
144 */
146 void QBarSet::insert(const int index, const qreal value)
145 void QBarSet::insert(const int index, const qreal value)
147 {
146 {
148 d_ptr->m_values.insert(index, QPointF(index, value));
147 d_ptr->insert(index, value);
149 // emit d_ptr->updatedBars();
148 emit valuesAdded(index,1);
149 }
150
151 /*!
152 Inserts new \a value on the \a index position.
153 The value that is currently at this postion is moved to postion index + 1
154 \sa remove()
155 */
156 void QBarSet::insert(const int index, const QPointF value)
157 {
158 d_ptr->insert(index,value);
159 emit valuesAdded(index,1);
150 }
160 }
151
161
152 /*!
162 /*!
153 Removes the value specified by \a index
163 Removes the value specified by \a index
154 \sa insert()
164 \sa insert()
155 */
165 */
156 void QBarSet::remove(const int index)
166 bool QBarSet::remove(const int index, const int count)
157 {
167 {
158 d_ptr->m_values.removeAt(index);
168 bool success = d_ptr->remove(index,count);
159 // emit d_ptr->updatedBars();
169 if (success) {
170 emit valuesRemoved(index,count);
171 }
172 return success;
160 }
173 }
161
174
162 /*!
175 /*!
163 Sets a new value \a value to set, indexed by \a index
176 Sets a new value \a value to set, indexed by \a index
164 */
177 */
165 void QBarSet::replace(const int index, const qreal value)
178 void QBarSet::replace(const int index, const qreal value)
166 {
179 {
167 d_ptr->m_values.replace(index,QPointF(index,value));
180 d_ptr->replace(index,value);
168 emit d_ptr->updatedBars();
181 emit valueChanged(index);
182 }
183
184 /*!
185 Sets a new value \a value to set, indexed by \a index
186 */
187 void QBarSet::replace(const int index, const QPointF value)
188 {
189 d_ptr->replace(index,value);
190 emit valueChanged(index);
169 }
191 }
170
192
171 /*!
193 /*!
172 Returns value of set indexed by \a index. Note that all appended values are stored internally as QPointF.
194 Returns value of set indexed by \a index. Note that all appended values are stored internally as QPointF.
173 The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value
195 The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value
174 of the QPointF (if appended with QPointF append).
196 of the QPointF (if appended with QPointF append).
175 If the index is out of bounds QPointF(0, 0.0) is returned.
197 If the index is out of bounds QPointF(0, 0.0) is returned.
176 */
198 */
177 QPointF QBarSet::at(const int index) const
199 QPointF QBarSet::at(const int index) const
178 {
200 {
179 if (index < 0 || index >= d_ptr->m_values.count()) {
201 if (index < 0 || index >= d_ptr->m_values.count()) {
180 return QPointF(index, 0.0);
202 return QPointF(index, 0.0);
181 }
203 }
182
204
183 return d_ptr->m_values.at(index);
205 return d_ptr->m_values.at(index);
184 }
206 }
185
207
186 /*!
208 /*!
187 Returns value of set indexed by \a index. ote that all appended values are stored internally as QPointF.
209 Returns value of set indexed by \a index. ote that all appended values are stored internally as QPointF.
188 The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value
210 The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value
189 of the QPointF (if appended with QPointF append).
211 of the QPointF (if appended with QPointF append).
190 */
212 */
191 QPointF QBarSet::operator [](const int index) const
213 QPointF QBarSet::operator [](const int index) const
192 {
214 {
193 return d_ptr->m_values.at(index);
215 return d_ptr->m_values.at(index);
194 }
216 }
195
217
196 /*!
218 /*!
197 Returns count of values in set.
219 Returns count of values in set.
198 */
220 */
199 int QBarSet::count() const
221 int QBarSet::count() const
200 {
222 {
201 return d_ptr->m_values.count();
223 return d_ptr->m_values.count();
202 }
224 }
203
225
204 /*!
226 /*!
205 Returns sum of all values in barset. The sum is sum of y coordinates in the QPointF representation.
227 Returns sum of all values in barset. The sum is sum of y coordinates in the QPointF representation.
206 */
228 */
207 qreal QBarSet::sum() const
229 qreal QBarSet::sum() const
208 {
230 {
209 qreal total(0);
231 qreal total(0);
210 for (int i=0; i < d_ptr->m_values.count(); i++) {
232 for (int i=0; i < d_ptr->m_values.count(); i++) {
211 //total += d_ptr->m_values.at(i);
233 //total += d_ptr->m_values.at(i);
212 total += d_ptr->m_values.at(i).y();
234 total += d_ptr->m_values.at(i).y();
213 }
235 }
214 return total;
236 return total;
215 }
237 }
216
238
217 /*!
239 /*!
218 Sets pen for set. Bars of this set are drawn using \a pen
240 Sets pen for set. Bars of this set are drawn using \a pen
219 */
241 */
220 void QBarSet::setPen(const QPen &pen)
242 void QBarSet::setPen(const QPen &pen)
221 {
243 {
222 if(d_ptr->m_pen!=pen){
244 if(d_ptr->m_pen!=pen){
223 d_ptr->m_pen = pen;
245 d_ptr->m_pen = pen;
224 emit d_ptr->updatedBars();
246 emit d_ptr->updatedBars();
247 emit penChanged();
225 }
248 }
226 }
249 }
227
250
228 /*!
251 /*!
229 Returns pen of the set.
252 Returns pen of the set.
230 */
253 */
231 QPen QBarSet::pen() const
254 QPen QBarSet::pen() const
232 {
255 {
233 return d_ptr->m_pen;
256 return d_ptr->m_pen;
234 }
257 }
235
258
236 /*!
259 /*!
237 Sets brush for the set. Bars of this set are drawn using \a brush
260 Sets brush for the set. Bars of this set are drawn using \a brush
238 */
261 */
239 void QBarSet::setBrush(const QBrush &brush)
262 void QBarSet::setBrush(const QBrush &brush)
240 {
263 {
241 if(d_ptr->m_brush!=brush){
264 if(d_ptr->m_brush!=brush){
242 d_ptr->m_brush = brush;
265 d_ptr->m_brush = brush;
243 emit d_ptr->updatedBars();
266 emit d_ptr->updatedBars();
267 emit brushChanged();
244 }
268 }
245 }
269 }
246
270
247 /*!
271 /*!
248 Returns brush of the set.
272 Returns brush of the set.
249 */
273 */
250 QBrush QBarSet::brush() const
274 QBrush QBarSet::brush() const
251 {
275 {
252 return d_ptr->m_brush;
276 return d_ptr->m_brush;
253 }
277 }
254
278
255 /*!
279 /*!
256 Sets \a brush of the values that are drawn on top of this barset
280 Sets \a brush of the values that are drawn on top of this barset
257 */
281 */
258 void QBarSet::setLabelBrush(const QBrush &brush)
282 void QBarSet::setLabelBrush(const QBrush &brush)
259 {
283 {
260 if(d_ptr->m_labelBrush!=brush){
284 if(d_ptr->m_labelBrush!=brush){
261 d_ptr->m_labelBrush = brush;
285 d_ptr->m_labelBrush = brush;
262 emit d_ptr->updatedBars();
286 emit d_ptr->updatedBars();
287 emit labelBrushChanged();
263 }
288 }
264 }
289 }
265
290
266 /*!
291 /*!
267 Returns brush of the values that are drawn on top of this barset
292 Returns brush of the values that are drawn on top of this barset
268 */
293 */
269 QBrush QBarSet::labelBrush() const
294 QBrush QBarSet::labelBrush() const
270 {
295 {
271 return d_ptr->m_labelBrush;
296 return d_ptr->m_labelBrush;
272 }
297 }
273
298
274 /*!
299 /*!
275 Sets the \a font for values that are drawn on top of this barset
300 Sets the \a font for values that are drawn on top of this barset
276 */
301 */
277 void QBarSet::setLabelFont(const QFont &font)
302 void QBarSet::setLabelFont(const QFont &font)
278 {
303 {
279 if(d_ptr->m_labelFont!=font) {
304 if(d_ptr->m_labelFont!=font) {
280 d_ptr->m_labelFont = font;
305 d_ptr->m_labelFont = font;
281 emit d_ptr->updatedBars();
306 emit d_ptr->updatedBars();
307 emit labelFontChanged();
282 }
308 }
283
309
284 }
310 }
285
311
286 /*!
312 /*!
287 Returns the pen for values that are drawn on top of this set
313 Returns the pen for values that are drawn on top of this set
288 */
314 */
289 QFont QBarSet::labelFont() const
315 QFont QBarSet::labelFont() const
290 {
316 {
291 return d_ptr->m_labelFont;
317 return d_ptr->m_labelFont;
292 }
318 }
293
319
294 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
320 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
295
321
296 QBarSetPrivate::QBarSetPrivate(const QString name, QBarSet *parent) : QObject(parent),
322 QBarSetPrivate::QBarSetPrivate(const QString name, QBarSet *parent) : QObject(parent),
297 q_ptr(parent),
323 q_ptr(parent),
298 m_name(name)
324 m_name(name)
299 {
325 {
300 }
326 }
301
327
302 QBarSetPrivate::~QBarSetPrivate()
328 QBarSetPrivate::~QBarSetPrivate()
303 {
329 {
304 }
330 }
305
331
332 void QBarSetPrivate::append(QPointF value)
333 {
334 m_values.append(value);
335 emit restructuredBars();
336 }
337
338 void QBarSetPrivate::append(QList<QPointF> values)
339 {
340 for (int i=0; i<values.count(); i++) {
341 m_values.append(values.at(i));
342 }
343 emit restructuredBars();
344 }
345
346 void QBarSetPrivate::append(QList<qreal> values)
347 {
348 int index = m_values.count();
349 for (int i=0; i<values.count(); i++) {
350 m_values.append(QPointF(index,values.at(i)));
351 index++;
352 }
353 emit restructuredBars();
354 }
355
356 void QBarSetPrivate::insert(const int index, const qreal value)
357 {
358 m_values.insert(index, QPointF(index, value));
359 emit restructuredBars();
360 }
361
362 void QBarSetPrivate::insert(const int index, const QPointF value)
363 {
364 m_values.insert(index, value);
365 emit restructuredBars();
366 }
367
368 bool QBarSetPrivate::remove(const int index, const int count)
369 {
370 if ((index + count) > m_values.count()) {
371 // cant remove more values than there are
372 return false;
373 }
374 int c = count;
375 while (c > 0) {
376 m_values.removeAt(index);
377 c--;
378 }
379 emit restructuredBars();
380 return true;
381 }
382
383 void QBarSetPrivate::replace(const int index, const qreal value)
384 {
385 m_values.replace(index,QPointF(index,value));
386 emit updatedBars();
387 }
388
389 void QBarSetPrivate::replace(const int index, const QPointF value)
390 {
391 m_values.replace(index,value);
392 emit updatedBars();
393 }
394
306 #include "moc_qbarset.cpp"
395 #include "moc_qbarset.cpp"
307 #include "moc_qbarset_p.cpp"
396 #include "moc_qbarset_p.cpp"
308
397
309 QTCOMMERCIALCHART_END_NAMESPACE
398 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,87 +1,101
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 #ifndef QBARSET_H
21 #ifndef QBARSET_H
22 #define QBARSET_H
22 #define QBARSET_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <QPen>
25 #include <QPen>
26 #include <QBrush>
26 #include <QBrush>
27 #include <QFont>
27 #include <QFont>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QBarSetPrivate;
30 class QBarSetPrivate;
31
31
32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
33 {
33 {
34 Q_OBJECT
34 Q_OBJECT
35 Q_PROPERTY(QString name READ name WRITE setName)
35 Q_PROPERTY(QString name READ name WRITE setName)
36 Q_PROPERTY(QPen pen READ pen WRITE setPen)
36 Q_PROPERTY(QPen pen READ pen WRITE setPen)
37 Q_PROPERTY(QBrush brush READ brush WRITE setBrush)
37 Q_PROPERTY(QBrush brush READ brush WRITE setBrush)
38 Q_PROPERTY(QBrush labelBrush READ labelBrush WRITE setLabelBrush)
38 Q_PROPERTY(QBrush labelBrush READ labelBrush WRITE setLabelBrush)
39 Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont)
39 Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont)
40
40
41 public:
41 public:
42 explicit QBarSet(const QString name, QObject *parent = 0);
42 explicit QBarSet(const QString name, QObject *parent = 0);
43 virtual ~QBarSet();
43 virtual ~QBarSet();
44
44
45 void setName(const QString name);
45 void setName(const QString name);
46 QString name() const;
46 QString name() const;
47
47
48 void append(const QPointF value);
48 void append(const QPointF value);
49 void append(const QList<QPointF> values);
49 void append(const QList<QPointF> values);
50 void append(const qreal value);
50 void append(const qreal value);
51 void append(const QList<qreal> values);
51 void append(const QList<qreal> values);
52
52
53 QBarSet& operator << (const qreal &value);
53 QBarSet& operator << (const qreal &value);
54 QBarSet& operator << (const QPointF &value);
54 QBarSet& operator << (const QPointF &value);
55
55
56 void insert(const int index, const qreal value);
56 void insert(const int index, const qreal value);
57 void remove(const int index);
57 void insert(const int index, const QPointF value);
58 bool remove(const int index, const int count = 1);
58 void replace(const int index, const qreal value);
59 void replace(const int index, const qreal value);
60 void replace(const int index, const QPointF value);
59 QPointF at(const int index) const;
61 QPointF at(const int index) const;
60 QPointF operator [] (const int index) const;
62 QPointF operator [] (const int index) const;
61 int count() const;
63 int count() const;
62 qreal sum() const;
64 qreal sum() const;
63
65
64 void setPen(const QPen &pen);
66 void setPen(const QPen &pen);
65 QPen pen() const;
67 QPen pen() const;
66
68
67 void setBrush(const QBrush &brush);
69 void setBrush(const QBrush &brush);
68 QBrush brush() const;
70 QBrush brush() const;
69
71
70 void setLabelBrush(const QBrush &brush);
72 void setLabelBrush(const QBrush &brush);
71 QBrush labelBrush() const;
73 QBrush labelBrush() const;
72
74
73 void setLabelFont(const QFont &font);
75 void setLabelFont(const QFont &font);
74 QFont labelFont() const;
76 QFont labelFont() const;
75
77
78 Q_SIGNALS:
79 void nameChanged();
80 void penChanged();
81 void brushChanged();
82 void labelChanged();
83 void labelBrushChanged();
84 void labelFontChanged();
85
86 void valuesAdded(int index, int count);
87 void valuesRemoved(int index, int count);
88 void valueChanged(int index);
89
76 private:
90 private:
77 QScopedPointer<QBarSetPrivate> d_ptr;
91 QScopedPointer<QBarSetPrivate> d_ptr;
78 Q_DISABLE_COPY(QBarSet)
92 Q_DISABLE_COPY(QBarSet)
79 friend class QBarSeries;
93 friend class QBarSeries;
80 friend class BarLegendMarker;
94 friend class BarLegendMarker;
81 friend class BarChartItem;
95 friend class BarChartItem;
82 friend class QBarSeriesPrivate;
96 friend class QBarSeriesPrivate;
83 };
97 };
84
98
85 QTCOMMERCIALCHART_END_NAMESPACE
99 QTCOMMERCIALCHART_END_NAMESPACE
86
100
87 #endif // QBARSET_H
101 #endif // QBARSET_H
@@ -1,67 +1,78
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 QBARSET_P_H
30 #ifndef QBARSET_P_H
31 #define QBARSET_P_H
31 #define QBARSET_P_H
32
32
33 #include "qbarset.h"
33 #include "qbarset.h"
34 #include <QMap>
34 #include <QMap>
35 #include <QPen>
35 #include <QPen>
36 #include <QBrush>
36 #include <QBrush>
37 #include <QFont>
37 #include <QFont>
38
38
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40
40
41 class QBarSetPrivate : public QObject
41 class QBarSetPrivate : public QObject
42 {
42 {
43 Q_OBJECT
43 Q_OBJECT
44
44
45 public:
45 public:
46 QBarSetPrivate(const QString name, QBarSet *parent);
46 QBarSetPrivate(const QString name, QBarSet *parent);
47 ~QBarSetPrivate();
47 ~QBarSetPrivate();
48
48
49 void append(QPointF value);
50 void append(QList<QPointF> values);
51 void append(QList<qreal> values);
52
53 void insert(const int index, const qreal value);
54 void insert(const int index, const QPointF value);
55 bool remove(const int index, const int count);
56
57 void replace(const int index, const qreal value);
58 void replace(const int index, const QPointF value);
59
49 Q_SIGNALS:
60 Q_SIGNALS:
50 void restructuredBars();
61 void restructuredBars();
51 void updatedBars();
62 void updatedBars();
52
63
53 public:
64 public:
54 QBarSet * const q_ptr;
65 QBarSet * const q_ptr;
55 QString m_name;
66 QString m_name;
56 QList<QPointF> m_values;
67 QList<QPointF> m_values;
57 QPen m_pen;
68 QPen m_pen;
58 QBrush m_brush;
69 QBrush m_brush;
59 QBrush m_labelBrush;
70 QBrush m_labelBrush;
60 QFont m_labelFont;
71 QFont m_labelFont;
61
72
62 friend class QBarSet;
73 friend class QBarSet;
63 };
74 };
64
75
65 QTCOMMERCIALCHART_END_NAMESPACE
76 QTCOMMERCIALCHART_END_NAMESPACE
66
77
67 #endif // QBARSETPRIVATE_P_H
78 #endif // QBARSETPRIVATE_P_H
@@ -1,409 +1,428
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 <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <qbarset.h>
22 #include <qbarset.h>
23 #include <qgroupedbarseries.h>
23 #include <qgroupedbarseries.h>
24 #include <qchartview.h>
24 #include <qchartview.h>
25
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
27
28 class tst_QBarSet : public QObject
28 class tst_QBarSet : public QObject
29 {
29 {
30 Q_OBJECT
30 Q_OBJECT
31
31
32 public slots:
32 public slots:
33 void initTestCase();
33 void initTestCase();
34 void cleanupTestCase();
34 void cleanupTestCase();
35 void init();
35 void init();
36 void cleanup();
36 void cleanup();
37
37
38 private slots:
38 private slots:
39 void qbarset_data();
39 void qbarset_data();
40 void qbarset();
40 void qbarset();
41 void name_data();
41 void name_data();
42 void name();
42 void name();
43 void append_data();
43 void append_data();
44 void append();
44 void append();
45 void appendOperator_data();
45 void appendOperator_data();
46 void appendOperator();
46 void appendOperator();
47 void insert_data();
47 void insert_data();
48 void insert();
48 void insert();
49 void remove_data();
49 void remove_data();
50 void remove();
50 void remove();
51 void replace_data();
51 void replace_data();
52 void replace();
52 void replace();
53 void at_data();
53 void at_data();
54 void at();
54 void at();
55 void atOperator_data();
55 void atOperator_data();
56 void atOperator();
56 void atOperator();
57 void count_data();
57 void count_data();
58 void count();
58 void count();
59 void sum_data();
59 void sum_data();
60 void sum();
60 void sum();
61 void customize();
61 void customize();
62
62
63 private:
63 private:
64 QBarSet* m_barset;
64 QBarSet* m_barset;
65 };
65 };
66
66
67 void tst_QBarSet::initTestCase()
67 void tst_QBarSet::initTestCase()
68 {
68 {
69 }
69 }
70
70
71 void tst_QBarSet::cleanupTestCase()
71 void tst_QBarSet::cleanupTestCase()
72 {
72 {
73 }
73 }
74
74
75 void tst_QBarSet::init()
75 void tst_QBarSet::init()
76 {
76 {
77 m_barset = new QBarSet(QString("Name"));
77 m_barset = new QBarSet(QString("Name"));
78 }
78 }
79
79
80 void tst_QBarSet::cleanup()
80 void tst_QBarSet::cleanup()
81 {
81 {
82 delete m_barset;
82 delete m_barset;
83 m_barset = 0;
83 m_barset = 0;
84 }
84 }
85
85
86 void tst_QBarSet::qbarset_data()
86 void tst_QBarSet::qbarset_data()
87 {
87 {
88 }
88 }
89
89
90 void tst_QBarSet::qbarset()
90 void tst_QBarSet::qbarset()
91 {
91 {
92 QBarSet barset(QString("Name"));
92 QBarSet barset(QString("Name"));
93 QCOMPARE(barset.name(), QString("Name"));
93 QCOMPARE(barset.name(), QString("Name"));
94 QCOMPARE(barset.count(), 0);
94 QCOMPARE(barset.count(), 0);
95 QVERIFY(qFuzzyIsNull(barset.sum()));
95 QVERIFY(qFuzzyIsNull(barset.sum()));
96 }
96 }
97
97
98 void tst_QBarSet::name_data()
98 void tst_QBarSet::name_data()
99 {
99 {
100 QTest::addColumn<QString> ("name");
100 QTest::addColumn<QString> ("name");
101 QTest::addColumn<QString> ("result");
101 QTest::addColumn<QString> ("result");
102 QTest::newRow("name0") << QString("name0") << QString("name0");
102 QTest::newRow("name0") << QString("name0") << QString("name0");
103 QTest::newRow("name1") << QString("name1") << QString("name1");
103 QTest::newRow("name1") << QString("name1") << QString("name1");
104 }
104 }
105
105
106 void tst_QBarSet::name()
106 void tst_QBarSet::name()
107 {
107 {
108 QFETCH(QString, name);
108 QFETCH(QString, name);
109 QFETCH(QString, result);
109 QFETCH(QString, result);
110
110
111 QSignalSpy nameSpy(m_barset,SIGNAL(nameChanged()));
111 m_barset->setName(name);
112 m_barset->setName(name);
112 QCOMPARE(m_barset->name(), result);
113 QCOMPARE(m_barset->name(), result);
114 QVERIFY(nameSpy.count() == 1);
113 }
115 }
114
116
115 void tst_QBarSet::append_data()
117 void tst_QBarSet::append_data()
116 {
118 {
117 QTest::addColumn<int> ("count");
119 QTest::addColumn<int> ("count");
118 QTest::newRow("0") << 0;
120 QTest::newRow("0") << 0;
119 QTest::newRow("5") << 5;
121 QTest::newRow("5") << 5;
120 QTest::newRow("100") << 100;
122 QTest::newRow("100") << 100;
121 QTest::newRow("1000") << 1000;
123 QTest::newRow("1000") << 1000;
122 }
124 }
123
125
124 void tst_QBarSet::append()
126 void tst_QBarSet::append()
125 {
127 {
126 QFETCH(int, count);
128 QFETCH(int, count);
127
129
128 QCOMPARE(m_barset->count(), 0);
130 QCOMPARE(m_barset->count(), 0);
129 QVERIFY(qFuzzyIsNull(m_barset->sum()));
131 QVERIFY(qFuzzyIsNull(m_barset->sum()));
130
132
133 QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int)));
134
131 qreal sum(0.0);
135 qreal sum(0.0);
132 qreal value(0.0);
136 qreal value(0.0);
133
137
134 for (int i=0; i<count; i++) {
138 for (int i=0; i<count; i++) {
135 m_barset->append(value);
139 m_barset->append(value);
136 QCOMPARE(m_barset->at(i).y(), value);
140 QCOMPARE(m_barset->at(i).y(), value);
137 sum += value;
141 sum += value;
138 value += 1.0;
142 value += 1.0;
139 }
143 }
140
144
141 QCOMPARE(m_barset->count(), count);
145 QCOMPARE(m_barset->count(), count);
142 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
146 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
147
148 QVERIFY(valueSpy.count() == count);
143 }
149 }
144
150
145 void tst_QBarSet::appendOperator_data()
151 void tst_QBarSet::appendOperator_data()
146 {
152 {
147 append_data();
153 append_data();
148 }
154 }
149
155
150 void tst_QBarSet::appendOperator()
156 void tst_QBarSet::appendOperator()
151 {
157 {
152 QFETCH(int, count);
158 QFETCH(int, count);
153
159
154 QCOMPARE(m_barset->count(), 0);
160 QCOMPARE(m_barset->count(), 0);
155 QVERIFY(qFuzzyIsNull(m_barset->sum()));
161 QVERIFY(qFuzzyIsNull(m_barset->sum()));
156
162
163 QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int)));
164
157 qreal sum(0.0);
165 qreal sum(0.0);
158 qreal value(0.0);
166 qreal value(0.0);
159
167
160 for (int i=0; i<count; i++) {
168 for (int i=0; i<count; i++) {
161 *m_barset << value;
169 *m_barset << value;
162 QCOMPARE(m_barset->at(i).y(), value);
170 QCOMPARE(m_barset->at(i).y(), value);
163 sum += value;
171 sum += value;
164 value += 1.0;
172 value += 1.0;
165 }
173 }
166
174
167 QCOMPARE(m_barset->count(), count);
175 QCOMPARE(m_barset->count(), count);
168 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
176 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
177 QVERIFY(valueSpy.count() == count);
178
169 }
179 }
170
180
171 void tst_QBarSet::insert_data()
181 void tst_QBarSet::insert_data()
172 {
182 {
173 }
183 }
174
184
175 void tst_QBarSet::insert()
185 void tst_QBarSet::insert()
176 {
186 {
177 QCOMPARE(m_barset->count(), 0);
187 QCOMPARE(m_barset->count(), 0);
178 QVERIFY(qFuzzyIsNull(m_barset->sum()));
188 QVERIFY(qFuzzyIsNull(m_barset->sum()));
189 QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int)));
179
190
180 m_barset->insert(0, 1.0); // 1.0
191 m_barset->insert(0, 1.0); // 1.0
181 QCOMPARE(m_barset->at(0).y(), 1.0);
192 QCOMPARE(m_barset->at(0).y(), 1.0);
182 QCOMPARE(m_barset->count(), 1);
193 QCOMPARE(m_barset->count(), 1);
183 QVERIFY(qFuzzyCompare(m_barset->sum(), 1.0));
194 QVERIFY(qFuzzyCompare(m_barset->sum(), 1.0));
184
195
185 m_barset->insert(0, 2.0); // 2.0 1.0
196 m_barset->insert(0, 2.0); // 2.0 1.0
186 QCOMPARE(m_barset->at(0).y(), 2.0);
197 QCOMPARE(m_barset->at(0).y(), 2.0);
187 QCOMPARE(m_barset->at(1).y(), 1.0);
198 QCOMPARE(m_barset->at(1).y(), 1.0);
188 QCOMPARE(m_barset->count(), 2);
199 QCOMPARE(m_barset->count(), 2);
189 QVERIFY(qFuzzyCompare(m_barset->sum(), 3.0));
200 QVERIFY(qFuzzyCompare(m_barset->sum(), 3.0));
190
201
191 m_barset->insert(1, 3.0); // 2.0 3.0 1.0
202 m_barset->insert(1, 3.0); // 2.0 3.0 1.0
192 QCOMPARE(m_barset->at(1).y(), 3.0);
203 QCOMPARE(m_barset->at(1).y(), 3.0);
193 QCOMPARE(m_barset->at(0).y(), 2.0);
204 QCOMPARE(m_barset->at(0).y(), 2.0);
194 QCOMPARE(m_barset->at(2).y(), 1.0);
205 QCOMPARE(m_barset->at(2).y(), 1.0);
195 QCOMPARE(m_barset->count(), 3);
206 QCOMPARE(m_barset->count(), 3);
196 QVERIFY(qFuzzyCompare(m_barset->sum(), 6.0));
207 QVERIFY(qFuzzyCompare(m_barset->sum(), 6.0));
208 QVERIFY(valueSpy.count() == 3);
197 }
209 }
198
210
199 void tst_QBarSet::remove_data()
211 void tst_QBarSet::remove_data()
200 {
212 {
201 }
213 }
202
214
203 void tst_QBarSet::remove()
215 void tst_QBarSet::remove()
204 {
216 {
205 QCOMPARE(m_barset->count(), 0);
217 QCOMPARE(m_barset->count(), 0);
206 QVERIFY(qFuzzyIsNull(m_barset->sum()));
218 QVERIFY(qFuzzyIsNull(m_barset->sum()));
207
219
220 QSignalSpy valueSpy(m_barset,SIGNAL(valuesRemoved(int,int)));
221
208 m_barset->append(1.0);
222 m_barset->append(1.0);
209 m_barset->append(2.0);
223 m_barset->append(2.0);
210 m_barset->append(3.0);
224 m_barset->append(3.0);
211 m_barset->append(4.0);
225 m_barset->append(4.0);
212
226
213 QCOMPARE(m_barset->count(), 4);
227 QCOMPARE(m_barset->count(), 4);
214 QCOMPARE(m_barset->sum(), 10.0);
228 QCOMPARE(m_barset->sum(), 10.0);
215
229
216 m_barset->remove(2); // 1.0 2.0 4.0
230 m_barset->remove(2); // 1.0 2.0 4.0
217 QCOMPARE(m_barset->at(0).y(), 1.0);
231 QCOMPARE(m_barset->at(0).y(), 1.0);
218 QCOMPARE(m_barset->at(1).y(), 2.0);
232 QCOMPARE(m_barset->at(1).y(), 2.0);
219 QCOMPARE(m_barset->at(2).y(), 4.0);
233 QCOMPARE(m_barset->at(2).y(), 4.0);
220 QCOMPARE(m_barset->count(), 3);
234 QCOMPARE(m_barset->count(), 3);
221 QCOMPARE(m_barset->sum(), 7.0);
235 QCOMPARE(m_barset->sum(), 7.0);
222
236
223 m_barset->remove(0); // 2.0 4.0
237 m_barset->remove(0); // 2.0 4.0
224 QCOMPARE(m_barset->at(0).y(), 2.0);
238 QCOMPARE(m_barset->at(0).y(), 2.0);
225 QCOMPARE(m_barset->at(1).y(), 4.0);
239 QCOMPARE(m_barset->at(1).y(), 4.0);
226 QCOMPARE(m_barset->count(), 2);
240 QCOMPARE(m_barset->count(), 2);
227 QCOMPARE(m_barset->sum(), 6.0);
241 QCOMPARE(m_barset->sum(), 6.0);
242
243 QVERIFY(valueSpy.count() == 2);
228 }
244 }
229
245
230 void tst_QBarSet::replace_data()
246 void tst_QBarSet::replace_data()
231 {
247 {
232
248
233 }
249 }
234
250
235 void tst_QBarSet::replace()
251 void tst_QBarSet::replace()
236 {
252 {
237 QCOMPARE(m_barset->count(), 0);
253 QCOMPARE(m_barset->count(), 0);
238 QVERIFY(qFuzzyIsNull(m_barset->sum()));
254 QVERIFY(qFuzzyIsNull(m_barset->sum()));
255 QSignalSpy valueSpy(m_barset,SIGNAL(valueChanged(int)));
239
256
240 m_barset->append(1.0);
257 m_barset->append(1.0);
241 m_barset->append(2.0);
258 m_barset->append(2.0);
242 m_barset->append(3.0);
259 m_barset->append(3.0);
243 m_barset->append(4.0);
260 m_barset->append(4.0);
244
261
245 QCOMPARE(m_barset->count(), 4);
262 QCOMPARE(m_barset->count(), 4);
246 QCOMPARE(m_barset->sum(), 10.0);
263 QCOMPARE(m_barset->sum(), 10.0);
247
264
248 m_barset->replace(0, 5.0); // 5.0 2.0 3.0 4.0
265 m_barset->replace(0, 5.0); // 5.0 2.0 3.0 4.0
249 QCOMPARE(m_barset->count(), 4);
266 QCOMPARE(m_barset->count(), 4);
250 QCOMPARE(m_barset->sum(), 14.0);
267 QCOMPARE(m_barset->sum(), 14.0);
251 QCOMPARE(m_barset->at(0).y(), 5.0);
268 QCOMPARE(m_barset->at(0).y(), 5.0);
252
269
253 m_barset->replace(3, 6.0);
270 m_barset->replace(3, 6.0);
254 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
271 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
255 QCOMPARE(m_barset->sum(), 16.0);
272 QCOMPARE(m_barset->sum(), 16.0);
256 QCOMPARE(m_barset->at(0).y(), 5.0);
273 QCOMPARE(m_barset->at(0).y(), 5.0);
257 QCOMPARE(m_barset->at(1).y(), 2.0);
274 QCOMPARE(m_barset->at(1).y(), 2.0);
258 QCOMPARE(m_barset->at(2).y(), 3.0);
275 QCOMPARE(m_barset->at(2).y(), 3.0);
259 QCOMPARE(m_barset->at(3).y(), 6.0);
276 QCOMPARE(m_barset->at(3).y(), 6.0);
277
278 QVERIFY(valueSpy.count() == 2);
260 }
279 }
261
280
262 void tst_QBarSet::at_data()
281 void tst_QBarSet::at_data()
263 {
282 {
264
283
265 }
284 }
266
285
267 void tst_QBarSet::at()
286 void tst_QBarSet::at()
268 {
287 {
269 QCOMPARE(m_barset->count(), 0);
288 QCOMPARE(m_barset->count(), 0);
270 QVERIFY(qFuzzyIsNull(m_barset->sum()));
289 QVERIFY(qFuzzyIsNull(m_barset->sum()));
271
290
272 m_barset->append(1.0);
291 m_barset->append(1.0);
273 m_barset->append(2.0);
292 m_barset->append(2.0);
274 m_barset->append(3.0);
293 m_barset->append(3.0);
275 m_barset->append(4.0);
294 m_barset->append(4.0);
276
295
277 QCOMPARE(m_barset->at(0).y(), 1.0);
296 QCOMPARE(m_barset->at(0).y(), 1.0);
278 QCOMPARE(m_barset->at(1).y(), 2.0);
297 QCOMPARE(m_barset->at(1).y(), 2.0);
279 QCOMPARE(m_barset->at(2).y(), 3.0);
298 QCOMPARE(m_barset->at(2).y(), 3.0);
280 QCOMPARE(m_barset->at(3).y(), 4.0);
299 QCOMPARE(m_barset->at(3).y(), 4.0);
281 }
300 }
282
301
283 void tst_QBarSet::atOperator_data()
302 void tst_QBarSet::atOperator_data()
284 {
303 {
285
304
286 }
305 }
287
306
288 void tst_QBarSet::atOperator()
307 void tst_QBarSet::atOperator()
289 {
308 {
290 QCOMPARE(m_barset->count(), 0);
309 QCOMPARE(m_barset->count(), 0);
291 QVERIFY(qFuzzyIsNull(m_barset->sum()));
310 QVERIFY(qFuzzyIsNull(m_barset->sum()));
292
311
293 m_barset->append(1.0);
312 m_barset->append(1.0);
294 m_barset->append(2.0);
313 m_barset->append(2.0);
295 m_barset->append(3.0);
314 m_barset->append(3.0);
296 m_barset->append(4.0);
315 m_barset->append(4.0);
297
316
298 QCOMPARE(m_barset->operator [](0).y(), 1.0);
317 QCOMPARE(m_barset->operator [](0).y(), 1.0);
299 QCOMPARE(m_barset->operator [](1).y(), 2.0);
318 QCOMPARE(m_barset->operator [](1).y(), 2.0);
300 QCOMPARE(m_barset->operator [](2).y(), 3.0);
319 QCOMPARE(m_barset->operator [](2).y(), 3.0);
301 QCOMPARE(m_barset->operator [](3).y(), 4.0);
320 QCOMPARE(m_barset->operator [](3).y(), 4.0);
302 }
321 }
303
322
304 void tst_QBarSet::count_data()
323 void tst_QBarSet::count_data()
305 {
324 {
306
325
307 }
326 }
308
327
309 void tst_QBarSet::count()
328 void tst_QBarSet::count()
310 {
329 {
311 QCOMPARE(m_barset->count(), 0);
330 QCOMPARE(m_barset->count(), 0);
312 QVERIFY(qFuzzyIsNull(m_barset->sum()));
331 QVERIFY(qFuzzyIsNull(m_barset->sum()));
313
332
314 m_barset->append(1.0);
333 m_barset->append(1.0);
315 QCOMPARE(m_barset->count(),1);
334 QCOMPARE(m_barset->count(),1);
316 m_barset->append(2.0);
335 m_barset->append(2.0);
317 QCOMPARE(m_barset->count(),2);
336 QCOMPARE(m_barset->count(),2);
318 m_barset->append(3.0);
337 m_barset->append(3.0);
319 QCOMPARE(m_barset->count(),3);
338 QCOMPARE(m_barset->count(),3);
320 m_barset->append(4.0);
339 m_barset->append(4.0);
321 QCOMPARE(m_barset->count(),4);
340 QCOMPARE(m_barset->count(),4);
322 }
341 }
323
342
324 void tst_QBarSet::sum_data()
343 void tst_QBarSet::sum_data()
325 {
344 {
326
345
327 }
346 }
328
347
329 void tst_QBarSet::sum()
348 void tst_QBarSet::sum()
330 {
349 {
331 QCOMPARE(m_barset->count(), 0);
350 QCOMPARE(m_barset->count(), 0);
332 QVERIFY(qFuzzyIsNull(m_barset->sum()));
351 QVERIFY(qFuzzyIsNull(m_barset->sum()));
333
352
334 m_barset->append(1.0);
353 m_barset->append(1.0);
335 QVERIFY(qFuzzyCompare(m_barset->sum(),1.0));
354 QVERIFY(qFuzzyCompare(m_barset->sum(),1.0));
336 m_barset->append(2.0);
355 m_barset->append(2.0);
337 QVERIFY(qFuzzyCompare(m_barset->sum(),3.0));
356 QVERIFY(qFuzzyCompare(m_barset->sum(),3.0));
338 m_barset->append(3.0);
357 m_barset->append(3.0);
339 QVERIFY(qFuzzyCompare(m_barset->sum(),6.0));
358 QVERIFY(qFuzzyCompare(m_barset->sum(),6.0));
340 m_barset->append(4.0);
359 m_barset->append(4.0);
341 QVERIFY(qFuzzyCompare(m_barset->sum(),10.0));
360 QVERIFY(qFuzzyCompare(m_barset->sum(),10.0));
342 }
361 }
343
362
344 void tst_QBarSet::customize()
363 void tst_QBarSet::customize()
345 {
364 {
346 // Create sets
365 // Create sets
347 QBarSet *set1 = new QBarSet("set1");
366 QBarSet *set1 = new QBarSet("set1");
348 QBarSet *set2 = new QBarSet("set2");
367 QBarSet *set2 = new QBarSet("set2");
349
368
350 // Append set1 to series
369 // Append set1 to series
351 QGroupedBarSeries *series = new QGroupedBarSeries();
370 QGroupedBarSeries *series = new QGroupedBarSeries();
352 bool success = series->append(set1);
371 bool success = series->append(set1);
353 QVERIFY(success);
372 QVERIFY(success);
354
373
355 // Add series to the chart
374 // Add series to the chart
356 QChartView view(new QChart());
375 QChartView view(new QChart());
357 view.resize(200, 200);
376 view.resize(200, 200);
358 view.chart()->addSeries(series);
377 view.chart()->addSeries(series);
359 view.show();
378 view.show();
360 QTest::qWaitForWindowShown(&view);
379 QTest::qWaitForWindowShown(&view);
361
380
362 // Test adding data to the sets
381 // Test adding data to the sets
363 *set1 << 1 << 2 << 1 << 3;
382 *set1 << 1 << 2 << 1 << 3;
364 *set2 << 2 << 1 << 3 << 1;
383 *set2 << 2 << 1 << 3 << 1;
365
384
366 // Test pen
385 // Test pen
367 QVERIFY(set1->pen() != QPen());
386 QVERIFY(set1->pen() != QPen());
368 QVERIFY(set2->pen() == QPen());
387 QVERIFY(set2->pen() == QPen());
369 QPen pen(QColor(128,128,128,128));
388 QPen pen(QColor(128,128,128,128));
370 set1->setPen(pen);
389 set1->setPen(pen);
371 QVERIFY(set1->pen() == pen);
390 QVERIFY(set1->pen() == pen);
372 QVERIFY(set2->pen() == QPen());
391 QVERIFY(set2->pen() == QPen());
373
392
374 // Test brush
393 // Test brush
375 QVERIFY(set1->brush() != QBrush());
394 QVERIFY(set1->brush() != QBrush());
376 QVERIFY(set2->brush() == QBrush());
395 QVERIFY(set2->brush() == QBrush());
377 QBrush brush(QColor(128,128,128,128));
396 QBrush brush(QColor(128,128,128,128));
378 set1->setBrush(brush);
397 set1->setBrush(brush);
379 QVERIFY(set1->brush() == brush);
398 QVERIFY(set1->brush() == brush);
380 QVERIFY(set2->brush() == QBrush());
399 QVERIFY(set2->brush() == QBrush());
381
400
382 // Test label brush
401 // Test label brush
383 QVERIFY(set1->labelBrush() != QBrush());
402 QVERIFY(set1->labelBrush() != QBrush());
384 QVERIFY(set2->labelBrush() == QBrush());
403 QVERIFY(set2->labelBrush() == QBrush());
385 set1->setLabelBrush(brush);
404 set1->setLabelBrush(brush);
386 QVERIFY(set1->labelBrush() == brush);
405 QVERIFY(set1->labelBrush() == brush);
387 QVERIFY(set2->labelBrush() == QBrush());
406 QVERIFY(set2->labelBrush() == QBrush());
388
407
389 // Test label font
408 // Test label font
390 // Note: QFont empty constructor creates font with application's default font, so the font may or may not be the
409 // Note: QFont empty constructor creates font with application's default font, so the font may or may not be the
391 // same for the set added to the series (depending on the QChart's theme configuration)
410 // same for the set added to the series (depending on the QChart's theme configuration)
392 QVERIFY(set1->labelFont() != QFont() || set1->labelFont() == QFont());
411 QVERIFY(set1->labelFont() != QFont() || set1->labelFont() == QFont());
393 QVERIFY(set2->labelFont() == QFont());
412 QVERIFY(set2->labelFont() == QFont());
394 QFont font;
413 QFont font;
395 font.setBold(true);
414 font.setBold(true);
396 font.setItalic(true);
415 font.setItalic(true);
397 set1->setLabelFont(font);
416 set1->setLabelFont(font);
398 QVERIFY(set1->labelFont() == font);
417 QVERIFY(set1->labelFont() == font);
399 QVERIFY(set2->labelFont() == QFont());
418 QVERIFY(set2->labelFont() == QFont());
400
419
401 // Test adding data to the sets
420 // Test adding data to the sets
402 *set1 << 1 << 2 << 1 << 3;
421 *set1 << 1 << 2 << 1 << 3;
403 *set2 << 2 << 1 << 3 << 1;
422 *set2 << 2 << 1 << 3 << 1;
404 }
423 }
405
424
406 QTEST_MAIN(tst_QBarSet)
425 QTEST_MAIN(tst_QBarSet)
407
426
408 #include "tst_qbarset.moc"
427 #include "tst_qbarset.moc"
409
428
General Comments 0
You need to be logged in to leave comments. Login now