##// END OF EJS Templates
Inner pie (donut) size cannot be smaller than the outer pie size
Marek Rosa -
r1741:5e93b5bfe418
parent child
Show More
@@ -1,82 +1,76
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 <QApplication>
22 22 #include <QMainWindow>
23 23 #include <QChartView>
24 24 #include <QLineSeries>
25 25 #include <QDateTime>
26 26 #include <QDateTimeAxis>
27 27
28 28 QTCOMMERCIALCHART_USE_NAMESPACE
29 29
30 30 int main(int argc, char *argv[])
31 31 {
32 32 QApplication a(argc, argv);
33 33 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
34 34
35 35 //![1]
36 36 QLineSeries* series = new QLineSeries();
37 37 //![1]
38 38
39 39 //![2]
40 40 QDateTime momentInTime;
41 for (int i = 0; i < 100; i++) {
41 for (int i = 0; i < 200; i++) {
42 42 momentInTime.setDate(QDate(2012, 1 , (1 + i / 9) % 28));
43 43 momentInTime.setTime(QTime(9 + i % 9, 0));
44 44 if (i > 0)
45 series->append(momentInTime.toMSecsSinceEpoch(), series->points().at(i - 1).y() * (0.95 + (qrand() % 11) / 100.0));
45 series->append(momentInTime.toMSecsSinceEpoch(), series->points().at(i - 1).y() * (0.99 + (qrand() % 3) / 100.0));
46 46 else
47 47 series->append(momentInTime.toMSecsSinceEpoch(), 45);
48 48 }
49 49 //![2]
50 50
51 51 //![3]
52 52 QChart* chart = new QChart();
53 53 chart->legend()->hide();
54 54 chart->addSeries(series);
55 55 chart->createDefaultAxes();
56 56 QDateTimeAxis *axisX = new QDateTimeAxis;
57 axisX->setTicksCount(20);
58 momentInTime.setDate(QDate(2012,1,1));
59 momentInTime.setTime(QTime(6, 0));
60 axisX->setMin(momentInTime);
61 momentInTime.setDate(QDate(2012,1,7));
62 momentInTime.setTime(QTime(18, 0));
63 axisX->setMax(momentInTime);
57 // axisX->setTicksCount(10);
64 58 chart->setAxisX(axisX, series);
65 59 chart->setTitle("Date and Time axis chart example");
66 60 //![3]
67 61
68 62 //![4]
69 63 QChartView* chartView = new QChartView(chart);
70 64 chartView->setRenderHint(QPainter::Antialiasing);
71 65 //![4]
72 66
73 67
74 68 //![5]
75 69 QMainWindow window;
76 70 window.setCentralWidget(chartView);
77 71 window.resize(800, 600);
78 72 window.show();
79 73 //![5]
80 74
81 75 return a.exec();
82 76 }
@@ -1,857 +1,864
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 "qpieseries.h"
22 22 #include "qpieseries_p.h"
23 23 #include "qpieslice.h"
24 24 #include "qpieslice_p.h"
25 25 #include "pieslicedata_p.h"
26 26 #include "chartdataset_p.h"
27 27 #include "charttheme_p.h"
28 28 #include "legendmarker_p.h"
29 29 #include "qabstractaxis.h"
30 30 #include "pieanimation_p.h"
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 /*!
35 35 \class QPieSeries
36 36 \brief Pie series API for QtCommercial Charts
37 37
38 38 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
39 39 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
40 40 The actual slice size is determined by that relative value.
41 41
42 42 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
43 43 These relate to the actual chart rectangle.
44 44
45 45 By default the pie is defined as a full pie but it can also be a partial pie.
46 46 This can be done by setting a starting angle and angle span to the series.
47 47 Full pie is 360 degrees where 0 is at 12 a'clock.
48 48
49 49 See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart.
50 50 \image examples_piechart.png
51 51 */
52 52 /*!
53 53 \qmlclass PieSeries QPieSeries
54 54 \inherits AbstractSeries
55 55
56 56 The following QML shows how to create a simple pie chart.
57 57
58 58 \snippet ../demos/qmlchart/qml/qmlchart/View1.qml 1
59 59
60 60 \beginfloatleft
61 61 \image demos_qmlchart1.png
62 62 \endfloat
63 63 \clearfloat
64 64 */
65 65
66 66 /*!
67 67 \property QPieSeries::horizontalPosition
68 68 \brief Defines the horizontal position of the pie.
69 69
70 70 The value is a relative value to the chart rectangle where:
71 71
72 72 \list
73 73 \o 0.0 is the absolute left.
74 74 \o 1.0 is the absolute right.
75 75 \endlist
76 76 Default value is 0.5 (center).
77 77 \sa verticalPosition
78 78 */
79 79
80 80 /*!
81 81 \qmlproperty real PieSeries::horizontalPosition
82 82
83 83 Defines the horizontal position of the pie.
84 84
85 85 The value is a relative value to the chart rectangle where:
86 86
87 87 \list
88 88 \o 0.0 is the absolute left.
89 89 \o 1.0 is the absolute right.
90 90 \endlist
91 91 Default value is 0.5 (center).
92 92 \sa verticalPosition
93 93 */
94 94
95 95 /*!
96 96 \property QPieSeries::verticalPosition
97 97 \brief Defines the vertical position of the pie.
98 98
99 99 The value is a relative value to the chart rectangle where:
100 100
101 101 \list
102 102 \o 0.0 is the absolute top.
103 103 \o 1.0 is the absolute bottom.
104 104 \endlist
105 105 Default value is 0.5 (center).
106 106 \sa horizontalPosition
107 107 */
108 108
109 109 /*!
110 110 \qmlproperty real PieSeries::verticalPosition
111 111
112 112 Defines the vertical position of the pie.
113 113
114 114 The value is a relative value to the chart rectangle where:
115 115
116 116 \list
117 117 \o 0.0 is the absolute top.
118 118 \o 1.0 is the absolute bottom.
119 119 \endlist
120 120 Default value is 0.5 (center).
121 121 \sa horizontalPosition
122 122 */
123 123
124 124 /*!
125 125 \property QPieSeries::size
126 126 \brief Defines the pie size.
127 127
128 128 The value is a relative value to the chart rectangle where:
129 129
130 130 \list
131 131 \o 0.0 is the minimum size (pie not drawn).
132 132 \o 1.0 is the maximum size that can fit the chart.
133 133 \endlist
134 134
135 When setting this property the donutInnerSize property is adjusted if necessary, to ensure that the inner size is not greater than the outer size.
136
135 137 Default value is 0.7.
136 138 */
137 139
138 140 /*!
139 141 \qmlproperty real PieSeries::size
140 142
141 143 Defines the pie size.
142 144
143 145 The value is a relative value to the chart rectangle where:
144 146
145 147 \list
146 148 \o 0.0 is the minimum size (pie not drawn).
147 149 \o 1.0 is the maximum size that can fit the chart.
148 150 \endlist
149 151
150 152 Default value is 0.7.
151 153 */
152 154
153 155 /*!
154 156 \property QPieSeries::donutInnerSize
155 157 \brief Defines the donut inner size.
156 158
157 159 The value is a relative value to the chart rectangle where:
158 160
159 161 \list
160 162 \o 0.0 is the minimum size (pie not drawn).
161 163 \o 1.0 is the maximum size that can fit the chart. (donut has no width)
162 164 \endlist
163 165
164 166 The value is never greater then size property.
165 167 Default value is 0.5.
166 168 */
167 169
168 170 /*!
169 171 \qmlproperty real PieSeries::donutInnerSize
170 172
171 173 Defines the donut inner size.
172 174
173 175 The value is a relative value to the chart rectangle where:
174 176
175 177 \list
176 178 \o 0.0 is the minimum size (donut is a pie).
177 179 \o 1.0 is the maximum size that can fit the chart. (donut has no width)
178 180 \endlist
179 181
180 The value is never greater then size property.
182 When setting this property the size property is adjusted if necessary, to ensure that the inner size is not greater than the outer size.
183
181 184 Default value is 0.5.
182 185 */
183 186
184 187 /*!
185 188 \property QPieSeries::startAngle
186 189 \brief Defines the starting angle of the pie.
187 190
188 191 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
189 192
190 193 Default is value is 0.
191 194 */
192 195
193 196 /*!
194 197 \qmlproperty real PieSeries::startAngle
195 198
196 199 Defines the starting angle of the pie.
197 200
198 201 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
199 202
200 203 Default is value is 0.
201 204 */
202 205
203 206 /*!
204 207 \property QPieSeries::endAngle
205 208 \brief Defines the ending angle of the pie.
206 209
207 210 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
208 211
209 212 Default is value is 360.
210 213 */
211 214
212 215 /*!
213 216 \qmlproperty real PieSeries::endAngle
214 217
215 218 Defines the ending angle of the pie.
216 219
217 220 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
218 221
219 222 Default is value is 360.
220 223 */
221 224
222 225 /*!
223 226 \property QPieSeries::count
224 227
225 228 Number of slices in the series.
226 229 */
227 230
228 231 /*!
229 232 \qmlproperty int PieSeries::count
230 233
231 234 Number of slices in the series.
232 235 */
233 236
234 237 /*!
235 238 \property QPieSeries::donut
236 239
237 240 Defines whether the series should be drawn as a donut
238 241 */
239 242
240 243 /*!
241 244 \qmlproperty int PieSeries::donut
242 245
243 246 Defines whether the series should be drawn as a donut
244 247 */
245 248
246 249 /*!
247 250 \fn void QPieSeries::countChanged()
248 251 Emitted when the slice count has changed.
249 252 \sa count
250 253 */
251 254 /*!
252 255 \qmlsignal PieSeries::onCountChanged()
253 256 Emitted when the slice count has changed.
254 257 */
255 258
256 259 /*!
257 260 \property QPieSeries::sum
258 261
259 262 Sum of all slices.
260 263
261 264 The series keeps track of the sum of all slices it holds.
262 265 */
263 266
264 267 /*!
265 268 \qmlproperty real PieSeries::sum
266 269
267 270 Sum of all slices.
268 271
269 272 The series keeps track of the sum of all slices it holds.
270 273 */
271 274
272 275 /*!
273 276 \fn void QPieSeries::sumChanged()
274 277 Emitted when the sum of all slices has changed.
275 278 \sa sum
276 279 */
277 280 /*!
278 281 \qmlsignal PieSeries::onSumChanged()
279 282 Emitted when the sum of all slices has changed. This may happen for example if you add or remove slices, or if you
280 283 change value of a slice.
281 284 */
282 285
283 286 /*!
284 287 \fn void QPieSeries::added(QList<QPieSlice*> slices)
285 288
286 289 This signal is emitted when \a slices have been added to the series.
287 290
288 291 \sa append(), insert()
289 292 */
290 293 /*!
291 294 \qmlsignal PieSeries::onAdded(PieSlice slice)
292 295 Emitted when \a slice has been added to the series.
293 296 */
294 297
295 298 /*!
296 299 \fn void QPieSeries::removed(QList<QPieSlice*> slices)
297 300 This signal is emitted when \a slices have been removed from the series.
298 301 \sa remove()
299 302 */
300 303 /*!
301 304 \qmlsignal PieSeries::onRemoved(PieSlice slice)
302 305 Emitted when \a slice has been removed from the series.
303 306 */
304 307
305 308 /*!
306 309 \fn void QPieSeries::clicked(QPieSlice* slice)
307 310 This signal is emitted when a \a slice has been clicked.
308 311 \sa QPieSlice::clicked()
309 312 */
310 313 /*!
311 314 \qmlsignal PieSeries::onClicked(PieSlice slice)
312 315 This signal is emitted when a \a slice has been clicked.
313 316 */
314 317
315 318 /*!
316 319 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
317 320 This signal is emitted when user has hovered over or away from the \a slice.
318 321 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
319 322 \sa QPieSlice::hovered()
320 323 */
321 324 /*!
322 325 \qmlsignal PieSeries::onHovered(PieSlice slice, bool state)
323 326 This signal is emitted when user has hovered over or away from the \a slice. \a state is true when user has hovered
324 327 over the slice and false when hover has moved away from the slice.
325 328 */
326 329
327 330 /*!
328 331 \qmlmethod PieSlice PieSeries::at(int index)
329 332 Returns slice at \a index. Returns null if the index is not valid.
330 333 */
331 334
332 335 /*!
333 336 \qmlmethod PieSlice PieSeries::find(string label)
334 337 Returns the first slice with \a label. Returns null if the index is not valid.
335 338 */
336 339
337 340 /*!
338 341 \qmlmethod PieSlice PieSeries::append(string label, real value)
339 342 Adds a new slice with \a label and \a value to the pie.
340 343 */
341 344
342 345 /*!
343 346 \qmlmethod bool PieSeries::remove(PieSlice slice)
344 347 Removes the \a slice from the pie. Returns true if the removal was successfull, false otherwise.
345 348 */
346 349
347 350 /*!
348 351 \qmlmethod PieSeries::clear()
349 352 Removes all slices from the pie.
350 353 */
351 354
352 355 /*!
353 356 Constructs a series object which is a child of \a parent.
354 357 */
355 358 QPieSeries::QPieSeries(QObject *parent) :
356 359 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
357 360 {
358 361
359 362 }
360 363
361 364 /*!
362 365 Destroys the series and its slices.
363 366 */
364 367 QPieSeries::~QPieSeries()
365 368 {
366 369 // NOTE: d_prt destroyed by QObject
367 370 }
368 371
369 372 /*!
370 373 Returns QChartSeries::SeriesTypePie.
371 374 */
372 375 QAbstractSeries::SeriesType QPieSeries::type() const
373 376 {
374 377 return QAbstractSeries::SeriesTypePie;
375 378 }
376 379
377 380 /*!
378 381 Appends a single \a slice to the series.
379 382 Slice ownership is passed to the series.
380 383
381 384 Returns true if append was succesfull.
382 385 */
383 386 bool QPieSeries::append(QPieSlice* slice)
384 387 {
385 388 return append(QList<QPieSlice*>() << slice);
386 389 }
387 390
388 391 /*!
389 392 Appends an array of \a slices to the series.
390 393 Slice ownership is passed to the series.
391 394
392 395 Returns true if append was successfull.
393 396 */
394 397 bool QPieSeries::append(QList<QPieSlice*> slices)
395 398 {
396 399 Q_D(QPieSeries);
397 400
398 401 if (slices.count() == 0)
399 402 return false;
400 403
401 404 foreach (QPieSlice* s, slices) {
402 405 if (!s || d->m_slices.contains(s))
403 406 return false;
404 407 if (s->series()) // already added to some series
405 408 return false;
406 409 }
407 410
408 411 foreach (QPieSlice* s, slices) {
409 412 s->setParent(this);
410 413 QPieSlicePrivate::fromSlice(s)->m_series = this;
411 414 d->m_slices << s;
412 415 }
413 416
414 417 d->updateDerivativeData();
415 418
416 419 foreach (QPieSlice* s, slices) {
417 420 connect(s, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
418 421 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
419 422 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
420 423 }
421 424
422 425 emit added(slices);
423 426 emit countChanged();
424 427
425 428 return true;
426 429 }
427 430
428 431 /*!
429 432 Appends a single \a slice to the series and returns a reference to the series.
430 433 Slice ownership is passed to the series.
431 434 */
432 435 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
433 436 {
434 437 append(slice);
435 438 return *this;
436 439 }
437 440
438 441
439 442 /*!
440 443 Appends a single slice to the series with give \a value and \a label.
441 444 Slice ownership is passed to the series.
442 445 */
443 446 QPieSlice* QPieSeries::append(QString label, qreal value)
444 447 {
445 448 QPieSlice* slice = new QPieSlice(label, value);
446 449 append(slice);
447 450 return slice;
448 451 }
449 452
450 453 /*!
451 454 Inserts a single \a slice to the series before the slice at \a index position.
452 455 Slice ownership is passed to the series.
453 456
454 457 Returns true if insert was successfull.
455 458 */
456 459 bool QPieSeries::insert(int index, QPieSlice* slice)
457 460 {
458 461 Q_D(QPieSeries);
459 462
460 463 if (index < 0 || index > d->m_slices.count())
461 464 return false;
462 465
463 466 if (!slice || d->m_slices.contains(slice))
464 467 return false;
465 468
466 469 if (slice->series()) // already added to some series
467 470 return false;
468 471
469 472 slice->setParent(this);
470 473 QPieSlicePrivate::fromSlice(slice)->m_series = this;
471 474 d->m_slices.insert(index, slice);
472 475
473 476 d->updateDerivativeData();
474 477
475 478 connect(slice, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged()));
476 479 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
477 480 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
478 481
479 482 emit added(QList<QPieSlice*>() << slice);
480 483 emit countChanged();
481 484
482 485 return true;
483 486 }
484 487
485 488 /*!
486 489 Removes a single \a slice from the series and deletes the slice.
487 490
488 491 Do not reference the pointer after this call.
489 492
490 493 Returns true if remove was successfull.
491 494 */
492 495 bool QPieSeries::remove(QPieSlice* slice)
493 496 {
494 497 Q_D(QPieSeries);
495 498
496 499 if (!d->m_slices.removeOne(slice))
497 500 return false;
498 501
499 502 d->updateDerivativeData();
500 503
501 504 emit removed(QList<QPieSlice*>() << slice);
502 505 emit countChanged();
503 506
504 507 delete slice;
505 508 slice = 0;
506 509
507 510 return true;
508 511 }
509 512
510 513 /*!
511 514 Clears all slices from the series.
512 515 */
513 516 void QPieSeries::clear()
514 517 {
515 518 Q_D(QPieSeries);
516 519 if (d->m_slices.count() == 0)
517 520 return;
518 521
519 522 QList<QPieSlice*> slices = d->m_slices;
520 523 foreach (QPieSlice* s, d->m_slices) {
521 524 d->m_slices.removeOne(s);
522 525 delete s;
523 526 }
524 527
525 528 d->updateDerivativeData();
526 529
527 530 emit removed(slices);
528 531 emit countChanged();
529 532 }
530 533
531 534 /*!
532 535 Returns a list of slices that belong to this series.
533 536 */
534 537 QList<QPieSlice*> QPieSeries::slices() const
535 538 {
536 539 Q_D(const QPieSeries);
537 540 return d->m_slices;
538 541 }
539 542
540 543 /*!
541 544 returns the number of the slices in this series.
542 545 */
543 546 int QPieSeries::count() const
544 547 {
545 548 Q_D(const QPieSeries);
546 549 return d->m_slices.count();
547 550 }
548 551
549 552 /*!
550 553 Returns true is the series is empty.
551 554 */
552 555 bool QPieSeries::isEmpty() const
553 556 {
554 557 Q_D(const QPieSeries);
555 558 return d->m_slices.isEmpty();
556 559 }
557 560
558 561 /*!
559 562 Returns the sum of all slice values in this series.
560 563
561 564 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
562 565 */
563 566 qreal QPieSeries::sum() const
564 567 {
565 568 Q_D(const QPieSeries);
566 569 return d->m_sum;
567 570 }
568 571
569 572 void QPieSeries::setDonut(bool donut)
570 573 {
571 574 Q_D(QPieSeries);
572 575 d->m_donutChart = donut;
573 576 d->updateDerivativeData();
574 577 }
575 578
576 579 bool QPieSeries::donut() const
577 580 {
578 581 Q_D(const QPieSeries);
579 582 return d->m_donutChart;
580 583 }
581 584
582 585 void QPieSeries::setDonutInnerSize(qreal innerSize)
583 586 {
584 587 Q_D(QPieSeries);
585
586 if (innerSize < 0.0)
587 innerSize = 0.0;
588 if (innerSize > d->m_pieRelativeSize)
589 innerSize = d->m_pieRelativeSize;
590
591 d->m_donutRelativeInnerSize = innerSize;
592 d->updateDerivativeData();
593 emit d->pieSizeChanged();
588 innerSize = qBound(0.0, innerSize, 1.0);
589 d->setSizes(innerSize, qMax(d->m_pieRelativeSize, innerSize));
594 590 }
595 591
596 592 qreal QPieSeries::donutInnerSize() const
597 593 {
598 594 Q_D(const QPieSeries);
599 595 return d->m_donutRelativeInnerSize;
600 596 }
601 597
602 598 void QPieSeries::setHorizontalPosition(qreal relativePosition)
603 599 {
604 600 Q_D(QPieSeries);
605 601
606 602 if (relativePosition < 0.0)
607 603 relativePosition = 0.0;
608 604 if (relativePosition > 1.0)
609 605 relativePosition = 1.0;
610 606
611 607 if (!qFuzzyIsNull(d->m_pieRelativeHorPos - relativePosition)) {
612 608 d->m_pieRelativeHorPos = relativePosition;
613 609 emit d->horizontalPositionChanged();
614 610 }
615 611 }
616 612
617 613 qreal QPieSeries::horizontalPosition() const
618 614 {
619 615 Q_D(const QPieSeries);
620 616 return d->m_pieRelativeHorPos;
621 617 }
622 618
623 619 void QPieSeries::setVerticalPosition(qreal relativePosition)
624 620 {
625 621 Q_D(QPieSeries);
626 622
627 623 if (relativePosition < 0.0)
628 624 relativePosition = 0.0;
629 625 if (relativePosition > 1.0)
630 626 relativePosition = 1.0;
631 627
632 628 if (!qFuzzyIsNull(d->m_pieRelativeVerPos - relativePosition)) {
633 629 d->m_pieRelativeVerPos = relativePosition;
634 630 emit d->verticalPositionChanged();
635 631 }
636 632 }
637 633
638 634 qreal QPieSeries::verticalPosition() const
639 635 {
640 636 Q_D(const QPieSeries);
641 637 return d->m_pieRelativeVerPos;
642 638 }
643 639
644 640 void QPieSeries::setPieSize(qreal relativeSize)
645 641 {
646 642 Q_D(QPieSeries);
643 relativeSize = qBound(0.0, relativeSize, 1.0);
644 d->setSizes(qMin(d->m_donutRelativeInnerSize, relativeSize), relativeSize);
647 645
648 if (relativeSize < 0.0)
649 relativeSize = 0.0;
650 if (relativeSize > 1.0)
651 relativeSize = 1.0;
652
653 if (!qFuzzyIsNull(d->m_pieRelativeSize - relativeSize)) {
654 d->m_pieRelativeSize = relativeSize;
655 emit d->pieSizeChanged();
656 }
657 646 }
658 647
659 648 qreal QPieSeries::pieSize() const
660 649 {
661 650 Q_D(const QPieSeries);
662 651 return d->m_pieRelativeSize;
663 652 }
664 653
665 654
666 655 void QPieSeries::setPieStartAngle(qreal angle)
667 656 {
668 657 Q_D(QPieSeries);
669 658 if (qFuzzyIsNull(d->m_pieStartAngle - angle))
670 659 return;
671 660 d->m_pieStartAngle = angle;
672 661 d->updateDerivativeData();
673 662 emit d->pieStartAngleChanged();
674 663 }
675 664
676 665 qreal QPieSeries::pieStartAngle() const
677 666 {
678 667 Q_D(const QPieSeries);
679 668 return d->m_pieStartAngle;
680 669 }
681 670
682 671 /*!
683 672 Sets the end angle of the pie.
684 673
685 674 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
686 675
687 676 \a angle must be greater than start angle.
688 677
689 678 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
690 679 */
691 680 void QPieSeries::setPieEndAngle(qreal angle)
692 681 {
693 682 Q_D(QPieSeries);
694 683 if (qFuzzyIsNull(d->m_pieEndAngle - angle))
695 684 return;
696 685 d->m_pieEndAngle = angle;
697 686 d->updateDerivativeData();
698 687 emit d->pieEndAngleChanged();
699 688 }
700 689
701 690 /*!
702 691 Returns the end angle of the pie.
703 692
704 693 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
705 694
706 695 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
707 696 */
708 697 qreal QPieSeries::pieEndAngle() const
709 698 {
710 699 Q_D(const QPieSeries);
711 700 return d->m_pieEndAngle;
712 701 }
713 702
714 703 /*!
715 704 Sets the all the slice labels \a visible or invisible.
716 705
717 706 Note that this affects only the current slices in the series.
718 707 If user adds a new slice the default label visibility is false.
719 708
720 709 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
721 710 */
722 711 void QPieSeries::setLabelsVisible(bool visible)
723 712 {
724 713 Q_D(QPieSeries);
725 714 foreach (QPieSlice* s, d->m_slices)
726 715 s->setLabelVisible(visible);
727 716 }
728 717
729 718 void QPieSeries::setLabelsPosition(QPieSlice::LabelPosition position)
730 719 {
731 720 Q_D(QPieSeries);
732 721 foreach (QPieSlice* s, d->m_slices)
733 722 s->setLabelPosition(position);
734 723 }
735 724
736 725 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
737 726
738 727
739 728 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
740 729 QAbstractSeriesPrivate(parent),
741 730 m_pieRelativeHorPos(0.5),
742 731 m_pieRelativeVerPos(0.5),
743 732 m_pieRelativeSize(0.7),
744 733 m_pieStartAngle(0),
745 734 m_pieEndAngle(360),
746 735 m_sum(0),
747 736 m_donutChart(false),
748 737 m_donutRelativeInnerSize(0.5)
749 738 {
750 739 }
751 740
752 741 QPieSeriesPrivate::~QPieSeriesPrivate()
753 742 {
754 743 }
755 744
756 745 void QPieSeriesPrivate::updateDerivativeData()
757 746 {
758 747 // calculate sum of all slices
759 748 qreal sum = 0;
760 749 foreach (QPieSlice* s, m_slices)
761 750 sum += s->value();
762 751
763 752 if (!qFuzzyIsNull(m_sum - sum)) {
764 753 m_sum = sum;
765 754 emit q_func()->sumChanged();
766 755 }
767 756
768 757 // nothing to show..
769 758 if (qFuzzyIsNull(m_sum))
770 759 return;
771 760
772 761 // update slice attributes
773 762 qreal sliceAngle = m_pieStartAngle;
774 763 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
775 764 QVector<QPieSlice*> changed;
776 765 foreach (QPieSlice* s, m_slices) {
777 766 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
778 767 d->setPercentage(s->value() / m_sum);
779 768 d->setStartAngle(sliceAngle);
780 769 d->setAngleSpan(pieSpan * s->percentage());
781 770 sliceAngle += s->angleSpan();
782 771 }
783 772
784 773
785 774 emit calculatedDataChanged();
786 775 }
787 776
777 void QPieSeriesPrivate::setSizes(qreal innerSize, qreal outerSize)
778 {
779 bool changed = false;
780
781 if (!qFuzzyIsNull(m_donutRelativeInnerSize - innerSize)) {
782 m_donutRelativeInnerSize = innerSize;
783 changed = true;
784 }
785
786 if (!qFuzzyIsNull(m_pieRelativeSize - outerSize)) {
787 m_pieRelativeSize = outerSize;
788 changed = true;
789 }
790
791 if (changed)
792 emit pieSizeChanged();
793 }
794
788 795 QPieSeriesPrivate* QPieSeriesPrivate::fromSeries(QPieSeries *series)
789 796 {
790 797 return series->d_func();
791 798 }
792 799
793 800 void QPieSeriesPrivate::sliceValueChanged()
794 801 {
795 802 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
796 803 updateDerivativeData();
797 804 }
798 805
799 806 void QPieSeriesPrivate::sliceClicked()
800 807 {
801 808 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
802 809 Q_ASSERT(m_slices.contains(slice));
803 810 Q_Q(QPieSeries);
804 811 emit q->clicked(slice);
805 812 }
806 813
807 814 void QPieSeriesPrivate::sliceHovered(bool state)
808 815 {
809 816 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
810 817 Q_ASSERT(m_slices.contains(slice));
811 818 Q_Q(QPieSeries);
812 819 emit q->hovered(slice, state);
813 820 }
814 821
815 822 void QPieSeriesPrivate::scaleDomain(Domain& domain)
816 823 {
817 824 Q_UNUSED(domain);
818 825 // does not apply to pie
819 826 }
820 827
821 828 ChartElement* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
822 829 {
823 830 Q_Q(QPieSeries);
824 831 PieChartItem* pie = new PieChartItem(q,presenter);
825 832 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
826 833 pie->setAnimation(new PieAnimation(pie));
827 834 }
828 835 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
829 836 return pie;
830 837 }
831 838
832 839 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
833 840 {
834 841 Q_Q(QPieSeries);
835 842 QList<LegendMarker*> markers;
836 843 foreach(QPieSlice* slice, q->slices()) {
837 844 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
838 845 markers << marker;
839 846 }
840 847 return markers;
841 848 }
842 849
843 850 void QPieSeriesPrivate::initializeAxis(QAbstractAxis* axis)
844 851 {
845 852 Q_UNUSED(axis);
846 853 }
847 854
848 855 QAbstractAxis::AxisType QPieSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
849 856 {
850 857 Q_UNUSED(orientation);
851 858 return QAbstractAxis::AxisTypeNoAxis;
852 859 }
853 860
854 861 #include "moc_qpieseries.cpp"
855 862 #include "moc_qpieseries_p.cpp"
856 863
857 864 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,88 +1,89
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 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef QPIESERIES_P_H
31 31 #define QPIESERIES_P_H
32 32
33 33 #include "qpieseries.h"
34 34 #include "qabstractseries_p.h"
35 35
36 36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 37 class QLegendPrivate;
38 38
39 39 class QPieSeriesPrivate : public QAbstractSeriesPrivate
40 40 {
41 41 Q_OBJECT
42 42
43 43 public:
44 44 QPieSeriesPrivate(QPieSeries *parent);
45 45 ~QPieSeriesPrivate();
46 46
47 47 void scaleDomain(Domain& domain);
48 48 ChartElement* createGraphics(ChartPresenter *presenter);
49 49 QList<LegendMarker*> createLegendMarker(QLegend *legend);
50 50 void initializeAxis(QAbstractAxis* axis);
51 51 QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const;
52 52
53 53 void updateDerivativeData();
54 void setSizes(qreal innerSize, qreal outerSize);
54 55
55 56 static QPieSeriesPrivate* fromSeries(QPieSeries *series);
56 57
57 58 signals:
58 59 void calculatedDataChanged();
59 60 void pieSizeChanged();
60 61 void pieStartAngleChanged();
61 62 void pieEndAngleChanged();
62 63 void horizontalPositionChanged();
63 64 void verticalPositionChanged();
64 65
65 66 public Q_SLOTS:
66 67 void sliceValueChanged();
67 68 void sliceClicked();
68 69 void sliceHovered(bool state);
69 70
70 71 private:
71 72 QList<QPieSlice*> m_slices;
72 73 qreal m_pieRelativeHorPos;
73 74 qreal m_pieRelativeVerPos;
74 75 qreal m_pieRelativeSize;
75 76 qreal m_pieStartAngle;
76 77 qreal m_pieEndAngle;
77 78 qreal m_sum;
78 79 bool m_donutChart;
79 80 qreal m_donutRelativeInnerSize;
80 81
81 82 private:
82 83 friend class QLegendPrivate;
83 84 Q_DECLARE_PUBLIC(QPieSeries)
84 85 };
85 86
86 87 QTCOMMERCIALCHART_END_NAMESPACE
87 88
88 89 #endif // QPIESERIES_P_H
@@ -1,147 +1,147
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 QPIESLICE_H
22 22 #define QPIESLICE_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <QObject>
26 26 #include <QPen>
27 27 #include <QBrush>
28 28 #include <QFont>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31 class QPieSlicePrivate;
32 32 class QPieSeries;
33 33
34 34 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
35 35 {
36 36 Q_OBJECT
37 37 Q_ENUMS(LabelPosition)
38 38 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
39 39 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
40 40 Q_PROPERTY(bool labelVisible READ isLabelVisible WRITE setLabelVisible NOTIFY labelVisibleChanged)
41 41 Q_PROPERTY(LabelPosition labelPosition READ labelPosition WRITE setLabelPosition)
42 42 Q_PROPERTY(bool exploded READ isExploded WRITE setExploded)
43 43 Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged)
44 44 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
45 45 Q_PROPERTY(int borderWidth READ borderWidth WRITE setBorderWidth NOTIFY borderWidthChanged)
46 46 Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged)
47 47 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
48 48 Q_PROPERTY(QBrush labelBrush READ labelBrush WRITE setLabelBrush NOTIFY labelBrushChanged)
49 49 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
50 50 Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont NOTIFY labelFontChanged)
51 51 Q_PROPERTY(qreal labelArmLengthFactor READ labelArmLengthFactor WRITE setLabelArmLengthFactor)
52 52 Q_PROPERTY(qreal explodeDistanceFactor READ explodeDistanceFactor WRITE setExplodeDistanceFactor)
53 53 Q_PROPERTY(qreal percentage READ percentage NOTIFY percentageChanged)
54 54 Q_PROPERTY(qreal startAngle READ startAngle NOTIFY startAngleChanged)
55 55 Q_PROPERTY(qreal angleSpan READ angleSpan NOTIFY angleSpanChanged)
56 56
57 57 public:
58 58 enum LabelPosition {
59 59 LabelOutside,
60 60 LabelInside,
61 LabelInsideTangential,
62 LabelInsideNormal
61 LabelInsideTangential, // better name needed
62 LabelInsideNormal // better name needed
63 63 };
64 64
65 65 public:
66 66 explicit QPieSlice(QObject *parent = 0);
67 67 QPieSlice(QString label, qreal value, QObject *parent = 0);
68 68 virtual ~QPieSlice();
69 69
70 70 void setLabel(QString label);
71 71 QString label() const;
72 72
73 73 void setValue(qreal value);
74 74 qreal value() const;
75 75
76 76 void setLabelVisible(bool visible = true);
77 77 bool isLabelVisible() const;
78 78
79 79 LabelPosition labelPosition();
80 80 void setLabelPosition(LabelPosition position);
81 81
82 82 void setExploded(bool exploded = true);
83 83 bool isExploded() const;
84 84
85 85 void setPen(const QPen &pen);
86 86 QPen pen() const;
87 87
88 88 QColor borderColor();
89 89 void setBorderColor(QColor color);
90 90
91 91 int borderWidth();
92 92 void setBorderWidth(int width);
93 93
94 94 void setBrush(const QBrush &brush);
95 95 QBrush brush() const;
96 96
97 97 QColor color();
98 98 void setColor(QColor color);
99 99
100 100 void setLabelBrush(const QBrush &brush);
101 101 QBrush labelBrush() const;
102 102
103 103 QColor labelColor();
104 104 void setLabelColor(QColor color);
105 105
106 106 void setLabelFont(const QFont &font);
107 107 QFont labelFont() const;
108 108
109 109 void setLabelArmLengthFactor(qreal factor);
110 110 qreal labelArmLengthFactor() const;
111 111
112 112 void setExplodeDistanceFactor(qreal factor);
113 113 qreal explodeDistanceFactor() const;
114 114
115 115 qreal percentage() const;
116 116 qreal startAngle() const;
117 117 qreal angleSpan() const;
118 118
119 119 QPieSeries *series() const;
120 120
121 121 Q_SIGNALS:
122 122 void clicked();
123 123 void hovered(bool state);
124 124 void labelChanged();
125 125 void valueChanged();
126 126 void labelVisibleChanged();
127 127 void penChanged();
128 128 void brushChanged();
129 129 void labelBrushChanged();
130 130 void labelFontChanged();
131 131 void percentageChanged();
132 132 void startAngleChanged();
133 133 void angleSpanChanged();
134 134 void colorChanged();
135 135 void borderColorChanged();
136 136 void borderWidthChanged();
137 137 void labelColorChanged();
138 138
139 139 private:
140 140 QPieSlicePrivate * const d_ptr;
141 141 Q_DECLARE_PRIVATE(QPieSlice)
142 142 Q_DISABLE_COPY(QPieSlice)
143 143 };
144 144
145 145 QTCOMMERCIALCHART_END_NAMESPACE
146 146
147 147 #endif // QPIESLICE_H
General Comments 0
You need to be logged in to leave comments. Login now