@@ -0,0 +1,48 | |||
|
1 | /*! | |
|
2 | \example examples/donut | |
|
3 | \title Donut example | |
|
4 | \subtitle | |
|
5 | ||
|
6 | This example shows how to use create a nested donuts chart using QPieSeries API. | |
|
7 | ||
|
8 | Let's start by creating a QChartView instance and enabling the Antialiasing on it. Last line enables the animations of the chart. | |
|
9 | ||
|
10 | \snippet ../examples/donut/widget.cpp 1 | |
|
11 | ||
|
12 | Three variables are defined that will be used to define the donut chart. Min and max size define the relative size of the whole donut. | |
|
13 | minSize is the relative inner size of the smallest donut. maxSize is the relative outer size of the biggest donut. | |
|
14 | ||
|
15 | \snippet ../examples/donut/widget.cpp 2 | |
|
16 | ||
|
17 | Following block of code defines the individual donuts and their slices. First new QPieSeries object is created. | |
|
18 | Callign setDonut() changes pie into a donut. The number of slices in each donut is randomized. | |
|
19 | The internal for loop creates the slices with a random value and label same as the value. | |
|
20 | Next the label of the slice is set to be visible and its color is set to white. | |
|
21 | To make the example more interesting the hovered signal of the slice is connected to widget's slot which inner workings are explained later. | |
|
22 | Finally the slice is added to the donut. The donut's size is adjusted to achive the nesting of the donuts. | |
|
23 | Then the donut is added to the widget's list of donuts and to the chart. | |
|
24 | ||
|
25 | \snippet ../examples/donut/widget.cpp 3 | |
|
26 | ||
|
27 | Finally the widget is placed in a layout used by the application. | |
|
28 | ||
|
29 | \snippet ../examples/donut/widget.cpp 4 | |
|
30 | ||
|
31 | To make the example more interesting the donuts are rotated randomly every 1.25 sec. | |
|
32 | ||
|
33 | \snippet ../examples/donut/widget.cpp 5 | |
|
34 | ||
|
35 | The widget's updatedRotation slot is defined below. | |
|
36 | It goes through all of the donuts and modifies thier current rotation by a random value. | |
|
37 | ||
|
38 | \snippet ../examples/donut/widget.cpp 6 | |
|
39 | ||
|
40 | The earlier mentioned explodeSlice slot code is provided below. | |
|
41 | If the slice is set to exploded then stop the timer that controls the donuts rotation. | |
|
42 | Then the slice's start and end agles are obtained from the slice. | |
|
43 | To highlight the selected slice all the other donuts that lie outward from the one that contains the selected slice | |
|
44 | have their start and end angles modified so that they wouldn't "block" the way for the hightlighted slice. | |
|
45 | If the slice is no longer selected return to the original state. | |
|
46 | ||
|
47 | \snippet ../examples/donut/widget.cpp 7 | |
|
48 | */ |
@@ -1,42 +1,44 | |||
|
1 | 1 | /*! |
|
2 | 2 | \page examples.html |
|
3 | 3 | \title Examples |
|
4 | 4 | \keyword Examples |
|
5 | 5 | |
|
6 | 6 | \raw HTML |
|
7 | 7 | <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable"> |
|
8 | 8 | <tr> |
|
9 | 9 | <th class="titleheader" width="33%"> |
|
10 | 10 | List of examples |
|
11 | 11 | </th> |
|
12 | 12 | </tr> |
|
13 | 13 | <tr> |
|
14 | 14 | <td valign="top"> |
|
15 | 15 | <ul> |
|
16 | 16 | <li><a href="examples-areachart.html">Area chart</a></li> |
|
17 | 17 | <li><a href="examples-barchart.html">Bar chart</a></li> |
|
18 | 18 | <li><a href="examples-customchart.html">Custom chart</a></li> |
|
19 | 19 | <li><a href="examples-horizontalbarchart.html">Horizontal bar chart</a></li> |
|
20 | 20 | <li><a href="examples-horizontalstackedbarchart.html">Horizontal stacked bar chart</a></li> |
|
21 | 21 | <li><a href="examples-horizontalpercentbarchart.html">Horizontal percent bar chart</a></li> |
|
22 | 22 | <li><a href="examples-legend.html">Legend</a></li> |
|
23 | 23 | <li><a href="examples-linechart.html">Line chart</a></li> |
|
24 | 24 | <li><a href="examples-lineandbar.html">Line and barchart</a></li> |
|
25 | 25 | <li><a href="examples-modeldata.html">Model data</a></li> |
|
26 | 26 | <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li> |
|
27 | 27 | <li><a href="examples-percentbarchart.html">Percent bar chart</a></li> |
|
28 | 28 | <li><a href="examples-piechart.html">Pie chart</a></li> |
|
29 | 29 | <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li> |
|
30 | <li><a href="examples-donut.html">Donut chart</a></li> | |
|
31 | <li><a href="examples-donutdrilldown.html">Donut chart drilldown</a></li> | |
|
30 | 32 | <li><a href="examples-presenterchart.html">Presenter chart</a></li> |
|
31 | 33 | <li><a href="examples-scatterchart.html">Scatter chart</a></li> |
|
32 | 34 | <li><a href="examples-scatterinteractions.html">Scatter interactions</a></li> |
|
33 | 35 | <li><a href="examples-splinechart.html">Spline chart</a></li> |
|
34 | 36 | <li><a href="examples-stackedbarchart.html">Stacked bar chart</a></li> |
|
35 | 37 | <li><a href="examples-stackedbarchartdrilldown.html">Stacked bar chart drilldown</a></li> |
|
36 | 38 | <li><a href="examples-zoomlinechart.html">Zoom line</a></li> |
|
37 | 39 | </ul> |
|
38 | 40 | </td> |
|
39 | 41 | </tr> |
|
40 | 42 | </table> |
|
41 | 43 | \endraw |
|
42 | 44 | */ |
@@ -1,96 +1,105 | |||
|
1 | 1 | #include "widget.h" |
|
2 | 2 | #include <QChartView> |
|
3 | 3 | #include <QPieSeries> |
|
4 | 4 | #include <QPieSlice> |
|
5 | 5 | #include <QTime> |
|
6 | 6 | #include <QGridLayout> |
|
7 | 7 | #include <QTimer> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | Widget::Widget(QWidget *parent) |
|
12 | 12 | : QWidget(parent) |
|
13 | 13 | { |
|
14 | 14 | setMinimumSize(800, 600); |
|
15 | 15 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
16 | 16 | |
|
17 | //! [1] | |
|
17 | 18 | QChartView *chartView = new QChartView; |
|
18 | 19 | chartView->setRenderHint(QPainter::Antialiasing); |
|
20 | chartView->chart()->setAnimationOptions(QChart::AllAnimations); | |
|
21 | //! [1] | |
|
19 | 22 | |
|
23 | //! [2] | |
|
20 | 24 | qreal minSize = 0.1; |
|
21 | 25 | qreal maxSize = 0.9; |
|
22 | 26 | int donutsCount = 5; |
|
27 | //! [2] | |
|
28 | ||
|
29 | //! [3] | |
|
23 | 30 | for (int i = 0; i < donutsCount; i++) { |
|
24 | 31 | QPieSeries *donut = new QPieSeries; |
|
25 | 32 | donut->setDonut(); |
|
26 | donut->setLabelsVisible(); | |
|
27 | 33 | int sliceCount = 3 + qrand() % 3; |
|
28 | 34 | for (int j = 0; j < sliceCount; j++) { |
|
29 | 35 | qreal value = 100 + qrand() % 100; |
|
30 | 36 | QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value); |
|
37 | slice->setLabelVisible(true); | |
|
38 | slice->setLabelColor(Qt::white); | |
|
39 | slice->setLabelPosition(QPieSlice::LabelInsideTangential); | |
|
31 | 40 | connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool))); |
|
32 | 41 | donut->append(slice); |
|
33 | donut->slices().last()->setLabelVisible(true); | |
|
34 | donut->slices().last()->setLabelColor(Qt::white); | |
|
35 | 42 | donut->setDonutInnerSize(minSize + i * (maxSize - minSize) / donutsCount); |
|
36 | 43 | donut->setPieSize(minSize + (i + 1) * (maxSize - minSize) / donutsCount); |
|
37 | 44 | } |
|
38 | 45 | m_donuts.append(donut); |
|
39 | qreal phase = qrand() % 180; | |
|
40 | donut->setLabelsPosition(QPieSlice::LabelInsideTangential); | |
|
41 | donut->setPieStartAngle(phase); | |
|
42 | donut->setPieEndAngle(360 + phase); | |
|
43 | 46 | chartView->chart()->addSeries(donut); |
|
44 | 47 | } |
|
48 | //! [3] | |
|
45 | 49 | |
|
46 | 50 | // create main layout |
|
51 | //! [4] | |
|
47 | 52 | QGridLayout* mainLayout = new QGridLayout; |
|
48 | 53 | mainLayout->addWidget(chartView, 1, 1); |
|
49 | 54 | setLayout(mainLayout); |
|
55 | //! [4] | |
|
50 | 56 | |
|
51 | chartView->chart()->setAnimationOptions(QChart::AllAnimations); | |
|
52 | ||
|
57 | //! [5] | |
|
53 | 58 | updateTimer = new QTimer(this); |
|
54 | 59 | connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation())); |
|
55 |
updateTimer->start(150 |
|
|
60 | updateTimer->start(1250); | |
|
61 | //! [5] | |
|
56 | 62 | } |
|
57 | 63 | |
|
58 | 64 | Widget::~Widget() |
|
59 | 65 | { |
|
60 | 66 | |
|
61 | 67 | } |
|
62 | 68 | |
|
69 | //! [6] | |
|
63 | 70 | void Widget::updateRotation() |
|
64 | 71 | { |
|
65 | // int tobeupdated = qrand() % m_donutsGroup.count(); | |
|
66 | 72 | for (int i = 0; i < m_donuts.count(); i++) { |
|
67 | 73 | QPieSeries *donut = m_donuts.at(i); |
|
68 | 74 | qreal phaseShift = -50 + qrand() % 100; |
|
69 | 75 | donut->setPieStartAngle(donut->pieStartAngle() + phaseShift); |
|
70 | 76 | donut->setPieEndAngle(donut->pieEndAngle() + phaseShift); |
|
71 | 77 | } |
|
72 | 78 | } |
|
79 | //! [6] | |
|
73 | 80 | |
|
81 | //! [7] | |
|
74 | 82 | void Widget::explodeSlice(bool exploded) |
|
75 | 83 | { |
|
76 | 84 | QPieSlice *slice = qobject_cast<QPieSlice *>(sender()); |
|
77 | 85 | if (exploded) { |
|
78 | 86 | updateTimer->stop(); |
|
79 | 87 | qreal sliceStartAngle = slice->startAngle(); |
|
80 | 88 | qreal sliceEndAngle = slice->startAngle() + slice->angleSpan(); |
|
81 | 89 | |
|
82 | 90 | QPieSeries *donut = slice->series(); |
|
83 | 91 | qreal seriesIndex = m_donuts.indexOf(donut); |
|
84 | 92 | for (int i = seriesIndex + 1; i < m_donuts.count(); i++) { |
|
85 | 93 | m_donuts.at(i)->setPieStartAngle(sliceEndAngle); |
|
86 | 94 | m_donuts.at(i)->setPieEndAngle(360 + sliceStartAngle); |
|
87 | 95 | } |
|
88 | 96 | } else { |
|
89 | 97 | for (int i = 0; i < m_donuts.count(); i++) { |
|
90 | 98 | m_donuts.at(i)->setPieStartAngle(0); |
|
91 | 99 | m_donuts.at(i)->setPieEndAngle(360); |
|
92 | 100 | } |
|
93 | 101 | updateTimer->start(); |
|
94 | 102 | } |
|
95 | 103 | slice->setExploded(exploded); |
|
96 | 104 | } |
|
105 | //! [7] |
General Comments 0
You need to be logged in to leave comments.
Login now