@@ -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 | */ |
@@ -1,119 +1,136 | |||||
1 | #include "widget.h" |
|
1 | #include "widget.h" | |
2 | #include <QGridLayout> |
|
2 | #include <QGridLayout> | |
3 |
|
3 | |||
4 | #include <QPieSlice> |
|
4 | #include <QPieSlice> | |
5 | #include <QTime> |
|
5 | #include <QTime> | |
6 | #include <QChartView> |
|
6 | #include <QChartView> | |
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 | mainData(0) |
|
13 | mainData(0) | |
14 | { |
|
14 | { | |
15 | setMinimumSize(800, 600); |
|
15 | setMinimumSize(800, 600); | |
16 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
16 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |
17 |
|
17 | |||
|
18 | //! [1] | |||
18 | QChartView *chartView = new QChartView; |
|
19 | QChartView *chartView = new QChartView; | |
19 | chartView->setRenderHint(QPainter::Antialiasing); |
|
20 | chartView->setRenderHint(QPainter::Antialiasing); | |
20 | chartView->chart()->setAnimationOptions(QChart::AllAnimations); |
|
21 | chartView->chart()->setAnimationOptions(QChart::AllAnimations); | |
|
22 | //! [1] | |||
21 |
|
23 | |||
|
24 | //! [2] | |||
22 | mainData = new QPieSeries; |
|
25 | mainData = new QPieSeries; | |
23 | mainData->setPieSize(0.6); |
|
26 | mainData->setPieSize(0.6); | |
|
27 | //! [2] | |||
|
28 | ||||
|
29 | //! [3] | |||
24 | for (int j = 0; j < 4; j++) { |
|
30 | for (int j = 0; j < 4; j++) { | |
25 | // create new slice for the mainData |
|
31 | // create new slice for the mainData | |
26 | QPieSlice *slice = new QPieSlice; |
|
32 | QPieSlice *slice = new QPieSlice; | |
27 | slice->setLabelColor(Qt::white); |
|
33 | slice->setLabelColor(Qt::white); | |
28 | mainData->append(slice); |
|
34 | mainData->append(slice); | |
29 |
|
35 | |||
30 | // create a new detailed data for the slice |
|
36 | // create a new detailed data for the slice | |
31 | QPieSeries *donut = new QPieSeries; |
|
37 | QPieSeries *donut = new QPieSeries; | |
32 | donut->setDonut(); |
|
38 | donut->setDonut(); | |
33 | donut->setDonutInnerSize(mainData->pieSize()); |
|
39 | donut->setDonutInnerSize(mainData->pieSize()); | |
34 | donut->setPieSize(mainData->pieSize() + 0.15); |
|
40 | donut->setPieSize(mainData->pieSize() + 0.15); | |
35 |
|
41 | |||
36 | // when mainData slice is redrawn make sure the detailed data slices are aligned with it |
|
42 | // when mainData slice is redrawn make sure the detailed data slices are aligned with it | |
37 | connect(slice, SIGNAL(startAngleChanged()), this, SLOT(updatedStartAngle())); |
|
43 | connect(slice, SIGNAL(startAngleChanged()), this, SLOT(updatedStartAngle())); | |
38 | connect(slice, SIGNAL(angleSpanChanged()), this, SLOT(updatedAngleSpan())); |
|
44 | connect(slice, SIGNAL(angleSpanChanged()), this, SLOT(updatedAngleSpan())); | |
39 |
|
45 | |||
40 | // create the detailed data |
|
46 | // create the detailed data | |
41 | for (int j = 0; j < 3; j++) { |
|
47 | for (int j = 0; j < 3; j++) { | |
42 | qreal value = 10 + qrand() % 100; |
|
48 | qreal value = 10 + qrand() % 100; | |
43 | QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value); |
|
49 | QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value); | |
44 | donut->append(slice); |
|
50 | donut->append(slice); | |
45 | } |
|
51 | } | |
46 | donut->setLabelsPosition(QPieSlice::LabelOutside); |
|
52 | donut->setLabelsPosition(QPieSlice::LabelOutside); | |
47 | donut->setLabelsVisible(); |
|
53 | donut->setLabelsVisible(); | |
48 | detailedData.append(donut); |
|
54 | detailedData.append(donut); | |
49 |
|
55 | |||
50 | // update the value and label of mainData |
|
56 | // update the value and label of mainData | |
51 | slice->setValue(donut->sum()); |
|
57 | slice->setValue(donut->sum()); | |
52 | slice->setLabel(QString("%1").arg(donut->sum())); |
|
58 | slice->setLabel(QString("%1").arg(donut->sum())); | |
53 | } |
|
59 | } | |
|
60 | //! [3] | |||
54 |
|
61 | |||
|
62 | //! [4] | |||
55 | mainData->setLabelsVisible(); |
|
63 | mainData->setLabelsVisible(); | |
56 | mainData->setLabelsPosition(QPieSlice::LabelInside); |
|
64 | mainData->setLabelsPosition(QPieSlice::LabelInside); | |
57 | chartView->chart()->addSeries(mainData); |
|
65 | chartView->chart()->addSeries(mainData); | |
58 | for (int i = 0; i < detailedData.count(); i++) |
|
66 | for (int i = 0; i < detailedData.count(); i++) | |
59 | chartView->chart()->addSeries(detailedData.at(i)); |
|
67 | chartView->chart()->addSeries(detailedData.at(i)); | |
|
68 | //! [4] | |||
60 |
|
69 | |||
|
70 | //! [5] | |||
61 | // create main layout |
|
71 | // create main layout | |
62 | QGridLayout* mainLayout = new QGridLayout; |
|
72 | QGridLayout* mainLayout = new QGridLayout; | |
63 | mainLayout->addWidget(chartView, 1, 1); |
|
73 | mainLayout->addWidget(chartView, 1, 1); | |
64 | setLayout(mainLayout); |
|
74 | setLayout(mainLayout); | |
|
75 | //! [5] | |||
65 |
|
76 | |||
|
77 | //! [6] | |||
66 | // modify the value of one detailed slice every 2.5 sec |
|
78 | // modify the value of one detailed slice every 2.5 sec | |
67 | QTimer *updateTimer = new QTimer(this); |
|
79 | QTimer *updateTimer = new QTimer(this); | |
68 | connect(updateTimer, SIGNAL(timeout()), this, SLOT(highlight())); |
|
80 | connect(updateTimer, SIGNAL(timeout()), this, SLOT(highlight())); | |
69 | updateTimer->start(2500); |
|
81 | updateTimer->start(2500); | |
|
82 | //! [6] | |||
70 | } |
|
83 | } | |
71 |
|
84 | |||
72 | Widget::~Widget() |
|
85 | Widget::~Widget() | |
73 | { |
|
86 | { | |
74 |
|
87 | |||
75 | } |
|
88 | } | |
76 |
|
89 | |||
|
90 | //! [7] | |||
77 | void Widget::updatedStartAngle() |
|
91 | void Widget::updatedStartAngle() | |
78 | { |
|
92 | { | |
79 | // when the mainData slice has been updated the detailed data QPieSeries object as well |
|
93 | // when the mainData slice has been updated the detailed data QPieSeries object as well | |
80 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
94 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
81 | QPieSeries *detailsDonut = detailedData.at(slice->series()->slices().indexOf(slice)); |
|
95 | QPieSeries *detailsDonut = detailedData.at(slice->series()->slices().indexOf(slice)); | |
82 | detailsDonut->setPieStartAngle(slice->startAngle()); |
|
96 | detailsDonut->setPieStartAngle(slice->startAngle()); | |
83 | } |
|
97 | } | |
84 |
|
98 | |||
85 | void Widget::updatedAngleSpan() |
|
99 | void Widget::updatedAngleSpan() | |
86 | { |
|
100 | { | |
87 | // when the mainData slice has been updated the detailed data QPieSeries object as well |
|
101 | // when the mainData slice has been updated the detailed data QPieSeries object as well | |
88 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
102 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
89 | QPieSeries *detailsDonut = detailedData.at(slice->series()->slices().indexOf(slice)); |
|
103 | QPieSeries *detailsDonut = detailedData.at(slice->series()->slices().indexOf(slice)); | |
90 | detailsDonut->setPieEndAngle(slice->startAngle() + slice->angleSpan()); |
|
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 | void Widget::updateRotation() |
|
122 | void Widget::updateRotation() | |
94 | { |
|
123 | { | |
95 | // update the selected slice |
|
124 | // update the selected slice | |
96 | qreal newValue = 10 + qrand() % 100; |
|
125 | qreal newValue = 10 + qrand() % 100; | |
97 | detailedData.at(detailIndex)->slices().at(sliceIndex)->setValue(newValue); |
|
126 | detailedData.at(detailIndex)->slices().at(sliceIndex)->setValue(newValue); | |
98 | detailedData.at(detailIndex)->slices().at(sliceIndex)->setLabel(QString("%1").arg(newValue)); |
|
127 | detailedData.at(detailIndex)->slices().at(sliceIndex)->setLabel(QString("%1").arg(newValue)); | |
99 |
|
128 | |||
100 | // update the mainData slice with a new sum of the detailed data values |
|
129 | // update the mainData slice with a new sum of the detailed data values | |
101 | mainData->slices().at(detailIndex)->setValue(detailedData.at(detailIndex)->sum()); |
|
130 | mainData->slices().at(detailIndex)->setValue(detailedData.at(detailIndex)->sum()); | |
102 | mainData->slices().at(detailIndex)->setLabel(QString("%1").arg(detailedData.at(detailIndex)->sum())); |
|
131 | mainData->slices().at(detailIndex)->setLabel(QString("%1").arg(detailedData.at(detailIndex)->sum())); | |
103 |
|
132 | |||
104 | // change the explode state of the selected slice back to normal |
|
133 | // change the explode state of the selected slice back to normal | |
105 | detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded(false); |
|
134 | detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded(false); | |
106 | } |
|
135 | } | |
107 |
|
136 | //! [8] | ||
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 | } |
|
General Comments 0
You need to be logged in to leave comments.
Login now