##// 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 */
@@ -27,6 +27,8
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>
@@ -14,45 +14,51 Widget::Widget(QWidget *parent)
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()
@@ -60,9 +66,9 Widget::~Widget()
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;
@@ -70,7 +76,9 void Widget::updateRotation()
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());
@@ -94,3 +102,4 void Widget::explodeSlice(bool exploded)
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