##// END OF EJS Templates
Documented the donut example
Marek Rosa -
r1713:428dd7229243
parent child
Show More
@@ -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 \page examples.html
2 \page examples.html
3 \title Examples
3 \title Examples
4 \keyword Examples
4 \keyword Examples
5
5
6 \raw HTML
6 \raw HTML
7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
8 <tr>
8 <tr>
9 <th class="titleheader" width="33%">
9 <th class="titleheader" width="33%">
10 List of examples
10 List of examples
11 </th>
11 </th>
12 </tr>
12 </tr>
13 <tr>
13 <tr>
14 <td valign="top">
14 <td valign="top">
15 <ul>
15 <ul>
16 <li><a href="examples-areachart.html">Area chart</a></li>
16 <li><a href="examples-areachart.html">Area chart</a></li>
17 <li><a href="examples-barchart.html">Bar chart</a></li>
17 <li><a href="examples-barchart.html">Bar chart</a></li>
18 <li><a href="examples-customchart.html">Custom chart</a></li>
18 <li><a href="examples-customchart.html">Custom chart</a></li>
19 <li><a href="examples-horizontalbarchart.html">Horizontal bar chart</a></li>
19 <li><a href="examples-horizontalbarchart.html">Horizontal bar chart</a></li>
20 <li><a href="examples-horizontalstackedbarchart.html">Horizontal stacked bar chart</a></li>
20 <li><a href="examples-horizontalstackedbarchart.html">Horizontal stacked bar chart</a></li>
21 <li><a href="examples-horizontalpercentbarchart.html">Horizontal percent bar chart</a></li>
21 <li><a href="examples-horizontalpercentbarchart.html">Horizontal percent bar chart</a></li>
22 <li><a href="examples-legend.html">Legend</a></li>
22 <li><a href="examples-legend.html">Legend</a></li>
23 <li><a href="examples-linechart.html">Line chart</a></li>
23 <li><a href="examples-linechart.html">Line chart</a></li>
24 <li><a href="examples-lineandbar.html">Line and barchart</a></li>
24 <li><a href="examples-lineandbar.html">Line and barchart</a></li>
25 <li><a href="examples-modeldata.html">Model data</a></li>
25 <li><a href="examples-modeldata.html">Model data</a></li>
26 <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li>
26 <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li>
27 <li><a href="examples-percentbarchart.html">Percent bar chart</a></li>
27 <li><a href="examples-percentbarchart.html">Percent bar chart</a></li>
28 <li><a href="examples-piechart.html">Pie chart</a></li>
28 <li><a href="examples-piechart.html">Pie chart</a></li>
29 <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li>
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 <li><a href="examples-presenterchart.html">Presenter chart</a></li>
32 <li><a href="examples-presenterchart.html">Presenter chart</a></li>
31 <li><a href="examples-scatterchart.html">Scatter chart</a></li>
33 <li><a href="examples-scatterchart.html">Scatter chart</a></li>
32 <li><a href="examples-scatterinteractions.html">Scatter interactions</a></li>
34 <li><a href="examples-scatterinteractions.html">Scatter interactions</a></li>
33 <li><a href="examples-splinechart.html">Spline chart</a></li>
35 <li><a href="examples-splinechart.html">Spline chart</a></li>
34 <li><a href="examples-stackedbarchart.html">Stacked bar chart</a></li>
36 <li><a href="examples-stackedbarchart.html">Stacked bar chart</a></li>
35 <li><a href="examples-stackedbarchartdrilldown.html">Stacked bar chart drilldown</a></li>
37 <li><a href="examples-stackedbarchartdrilldown.html">Stacked bar chart drilldown</a></li>
36 <li><a href="examples-zoomlinechart.html">Zoom line</a></li>
38 <li><a href="examples-zoomlinechart.html">Zoom line</a></li>
37 </ul>
39 </ul>
38 </td>
40 </td>
39 </tr>
41 </tr>
40 </table>
42 </table>
41 \endraw
43 \endraw
42 */
44 */
@@ -1,96 +1,105
1 #include "widget.h"
1 #include "widget.h"
2 #include <QChartView>
2 #include <QChartView>
3 #include <QPieSeries>
3 #include <QPieSeries>
4 #include <QPieSlice>
4 #include <QPieSlice>
5 #include <QTime>
5 #include <QTime>
6 #include <QGridLayout>
6 #include <QGridLayout>
7 #include <QTimer>
7 #include <QTimer>
8
8
9 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
10
10
11 Widget::Widget(QWidget *parent)
11 Widget::Widget(QWidget *parent)
12 : QWidget(parent)
12 : QWidget(parent)
13 {
13 {
14 setMinimumSize(800, 600);
14 setMinimumSize(800, 600);
15 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
15 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
16
16
17 //! [1]
17 QChartView *chartView = new QChartView;
18 QChartView *chartView = new QChartView;
18 chartView->setRenderHint(QPainter::Antialiasing);
19 chartView->setRenderHint(QPainter::Antialiasing);
20 chartView->chart()->setAnimationOptions(QChart::AllAnimations);
21 //! [1]
19
22
23 //! [2]
20 qreal minSize = 0.1;
24 qreal minSize = 0.1;
21 qreal maxSize = 0.9;
25 qreal maxSize = 0.9;
22 int donutsCount = 5;
26 int donutsCount = 5;
27 //! [2]
28
29 //! [3]
23 for (int i = 0; i < donutsCount; i++) {
30 for (int i = 0; i < donutsCount; i++) {
24 QPieSeries *donut = new QPieSeries;
31 QPieSeries *donut = new QPieSeries;
25 donut->setDonut();
32 donut->setDonut();
26 donut->setLabelsVisible();
27 int sliceCount = 3 + qrand() % 3;
33 int sliceCount = 3 + qrand() % 3;
28 for (int j = 0; j < sliceCount; j++) {
34 for (int j = 0; j < sliceCount; j++) {
29 qreal value = 100 + qrand() % 100;
35 qreal value = 100 + qrand() % 100;
30 QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value);
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 connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool)));
40 connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool)));
32 donut->append(slice);
41 donut->append(slice);
33 donut->slices().last()->setLabelVisible(true);
34 donut->slices().last()->setLabelColor(Qt::white);
35 donut->setDonutInnerSize(minSize + i * (maxSize - minSize) / donutsCount);
42 donut->setDonutInnerSize(minSize + i * (maxSize - minSize) / donutsCount);
36 donut->setPieSize(minSize + (i + 1) * (maxSize - minSize) / donutsCount);
43 donut->setPieSize(minSize + (i + 1) * (maxSize - minSize) / donutsCount);
37 }
44 }
38 m_donuts.append(donut);
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 chartView->chart()->addSeries(donut);
46 chartView->chart()->addSeries(donut);
44 }
47 }
48 //! [3]
45
49
46 // create main layout
50 // create main layout
51 //! [4]
47 QGridLayout* mainLayout = new QGridLayout;
52 QGridLayout* mainLayout = new QGridLayout;
48 mainLayout->addWidget(chartView, 1, 1);
53 mainLayout->addWidget(chartView, 1, 1);
49 setLayout(mainLayout);
54 setLayout(mainLayout);
55 //! [4]
50
56
51 chartView->chart()->setAnimationOptions(QChart::AllAnimations);
57 //! [5]
52
53 updateTimer = new QTimer(this);
58 updateTimer = new QTimer(this);
54 connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation()));
59 connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation()));
55 updateTimer->start(1500);
60 updateTimer->start(1250);
61 //! [5]
56 }
62 }
57
63
58 Widget::~Widget()
64 Widget::~Widget()
59 {
65 {
60
66
61 }
67 }
62
68
69 //! [6]
63 void Widget::updateRotation()
70 void Widget::updateRotation()
64 {
71 {
65 // int tobeupdated = qrand() % m_donutsGroup.count();
66 for (int i = 0; i < m_donuts.count(); i++) {
72 for (int i = 0; i < m_donuts.count(); i++) {
67 QPieSeries *donut = m_donuts.at(i);
73 QPieSeries *donut = m_donuts.at(i);
68 qreal phaseShift = -50 + qrand() % 100;
74 qreal phaseShift = -50 + qrand() % 100;
69 donut->setPieStartAngle(donut->pieStartAngle() + phaseShift);
75 donut->setPieStartAngle(donut->pieStartAngle() + phaseShift);
70 donut->setPieEndAngle(donut->pieEndAngle() + phaseShift);
76 donut->setPieEndAngle(donut->pieEndAngle() + phaseShift);
71 }
77 }
72 }
78 }
79 //! [6]
73
80
81 //! [7]
74 void Widget::explodeSlice(bool exploded)
82 void Widget::explodeSlice(bool exploded)
75 {
83 {
76 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
84 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
77 if (exploded) {
85 if (exploded) {
78 updateTimer->stop();
86 updateTimer->stop();
79 qreal sliceStartAngle = slice->startAngle();
87 qreal sliceStartAngle = slice->startAngle();
80 qreal sliceEndAngle = slice->startAngle() + slice->angleSpan();
88 qreal sliceEndAngle = slice->startAngle() + slice->angleSpan();
81
89
82 QPieSeries *donut = slice->series();
90 QPieSeries *donut = slice->series();
83 qreal seriesIndex = m_donuts.indexOf(donut);
91 qreal seriesIndex = m_donuts.indexOf(donut);
84 for (int i = seriesIndex + 1; i < m_donuts.count(); i++) {
92 for (int i = seriesIndex + 1; i < m_donuts.count(); i++) {
85 m_donuts.at(i)->setPieStartAngle(sliceEndAngle);
93 m_donuts.at(i)->setPieStartAngle(sliceEndAngle);
86 m_donuts.at(i)->setPieEndAngle(360 + sliceStartAngle);
94 m_donuts.at(i)->setPieEndAngle(360 + sliceStartAngle);
87 }
95 }
88 } else {
96 } else {
89 for (int i = 0; i < m_donuts.count(); i++) {
97 for (int i = 0; i < m_donuts.count(); i++) {
90 m_donuts.at(i)->setPieStartAngle(0);
98 m_donuts.at(i)->setPieStartAngle(0);
91 m_donuts.at(i)->setPieEndAngle(360);
99 m_donuts.at(i)->setPieEndAngle(360);
92 }
100 }
93 updateTimer->start();
101 updateTimer->start();
94 }
102 }
95 slice->setExploded(exploded);
103 slice->setExploded(exploded);
96 }
104 }
105 //! [7]
General Comments 0
You need to be logged in to leave comments. Login now