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