##// END OF EJS Templates
Documented the donutdrilldown chart example
Marek Rosa -
r1714:d63ddbb6caa3
parent child
Show More
@@ -0,0 +1,52
1 /*!
2 \example examples/donutdrilldown
3 \title Donut chart drilldown example
4 \subtitle
5
6 This example shows how to use create a donut drilldown 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/donutdrilldown/widget.cpp 1
11
12 PieSeries is used to present the general data.
13
14 \snippet ../examples/donutdrilldown/widget.cpp 2
15
16 Following block of code creates the slices for the mainData QPieSeries.
17 Then for every created slice a new series is created that stores the detailed data.
18 The details series is set to be a donut. Its size is adjusted so that it wraps around the mainData pie.
19 Next two signals from the mainData pie's slices are connected. This is used to keep the mainData slices agligned with their respective details series.
20
21 \snippet ../examples/donutdrilldown/widget.cpp 3
22
23 Set the labels of the mainData to enabled and their postion to Outside.
24 Then add the mainData and detailedData series to the chart.
25
26 \snippet ../examples/donutdrilldown/widget.cpp 4
27
28 Finally the widget is placed in a layout used by the application.
29
30 \snippet ../examples/donutdrilldown/widget.cpp 5
31
32 To show that the detailed data stays aligned with the main data every 2.5 sec. one of the slices is modified.
33 It should be noted that int this example the mainData slices should not be modified directly as the change
34 cannot be applied to the detailed slices without the extra knowledge on how the split looks like.
35
36 \snippet ../examples/donutdrilldown/widget.cpp 6
37
38 When the mainData slice layout is changed the detailed data layout has to be modified accordingly.
39 This is achived by setting the start and end angle of the detailed series.
40
41 \snippet ../examples/donutdrilldown/widget.cpp 7
42
43 Highlight slot selects a random slice for the modification.
44 The slice is set to exploded to notify the user that its going to be changed.
45 The actual data modification is delayed by 1 sec to give a user a chance to focus on the slice.
46
47 Then the slice is modified. Respective mainData slice is modified as well to contain as the value the sum of the detailed series slices values.
48 Finally the slice exploded state is set to false.
49
50 \snippet ../examples/donutdrilldown/widget.cpp 8
51
52 */
@@ -15,12 +15,18 Widget::Widget(QWidget *parent)
15 15 setMinimumSize(800, 600);
16 16 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
17 17
18 //! [1]
18 19 QChartView *chartView = new QChartView;
19 20 chartView->setRenderHint(QPainter::Antialiasing);
20 21 chartView->chart()->setAnimationOptions(QChart::AllAnimations);
22 //! [1]
21 23
24 //! [2]
22 25 mainData = new QPieSeries;
23 26 mainData->setPieSize(0.6);
27 //! [2]
28
29 //! [3]
24 30 for (int j = 0; j < 4; j++) {
25 31 // create new slice for the mainData
26 32 QPieSlice *slice = new QPieSlice;
@@ -51,22 +57,29 Widget::Widget(QWidget *parent)
51 57 slice->setValue(donut->sum());
52 58 slice->setLabel(QString("%1").arg(donut->sum()));
53 59 }
60 //! [3]
54 61
62 //! [4]
55 63 mainData->setLabelsVisible();
56 64 mainData->setLabelsPosition(QPieSlice::LabelInside);
57 65 chartView->chart()->addSeries(mainData);
58 66 for (int i = 0; i < detailedData.count(); i++)
59 67 chartView->chart()->addSeries(detailedData.at(i));
68 //! [4]
60 69
70 //! [5]
61 71 // create main layout
62 72 QGridLayout* mainLayout = new QGridLayout;
63 73 mainLayout->addWidget(chartView, 1, 1);
64 74 setLayout(mainLayout);
75 //! [5]
65 76
77 //! [6]
66 78 // modify the value of one detailed slice every 2.5 sec
67 79 QTimer *updateTimer = new QTimer(this);
68 80 connect(updateTimer, SIGNAL(timeout()), this, SLOT(highlight()));
69 81 updateTimer->start(2500);
82 //! [6]
70 83 }
71 84
72 85 Widget::~Widget()
@@ -74,6 +87,7 Widget::~Widget()
74 87
75 88 }
76 89
90 //! [7]
77 91 void Widget::updatedStartAngle()
78 92 {
79 93 // when the mainData slice has been updated the detailed data QPieSeries object as well
@@ -89,6 +103,21 void Widget::updatedAngleSpan()
89 103 QPieSeries *detailsDonut = detailedData.at(slice->series()->slices().indexOf(slice));
90 104 detailsDonut->setPieEndAngle(slice->startAngle() + slice->angleSpan());
91 105 }
106 //! [7]
107
108 //! [8]
109 void Widget::highlight()
110 {
111 // choose one random detailed data slice to be updated.
112 detailIndex = qrand() % mainData->count();
113 sliceIndex = qrand() % detailedData.at(detailIndex)->count();
114
115 // set the slice to exploded to make the change easier to observe
116 detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded();
117
118 // give the user time to focus on the slice that will be changed
119 QTimer::singleShot(1000, this, SLOT(updateRotation()));
120 }
92 121
93 122 void Widget::updateRotation()
94 123 {
@@ -104,16 +133,4 void Widget::updateRotation()
104 133 // change the explode state of the selected slice back to normal
105 134 detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded(false);
106 135 }
107
108 void Widget::highlight()
109 {
110 // choose one random detailed data slice to be updated.
111 detailIndex = qrand() % mainData->count();
112 sliceIndex = qrand() % detailedData.at(detailIndex)->count();
113
114 // set the slice to exploded to make the change easier to observe
115 detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded();
116
117 // give the user time to focus on the slice that will be changed
118 QTimer::singleShot(1000, this, SLOT(updateRotation()));
119 }
136 //! [8]
General Comments 0
You need to be logged in to leave comments. Login now