@@ -1,108 +1,109 | |||
|
1 | 1 | #include "widget.h" |
|
2 | 2 | #include <QChartView> |
|
3 | 3 | #include <QChart> |
|
4 | 4 | #include <QLegend> |
|
5 | 5 | #include <QPieSeries> |
|
6 | 6 | #include <QPieSlice> |
|
7 | 7 | #include <QTime> |
|
8 | 8 | #include <QGridLayout> |
|
9 | 9 | #include <QTimer> |
|
10 | 10 | |
|
11 | 11 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
12 | 12 | |
|
13 | 13 | Widget::Widget(QWidget *parent) |
|
14 | 14 | : QWidget(parent) |
|
15 | 15 | { |
|
16 | 16 | setMinimumSize(800, 600); |
|
17 | 17 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
18 | 18 | |
|
19 | 19 | //! [1] |
|
20 | 20 | QChartView *chartView = new QChartView; |
|
21 | 21 | chartView->setRenderHint(QPainter::Antialiasing); |
|
22 | 22 | QChart *chart = chartView->chart(); |
|
23 | 23 | chart->setAnimationOptions(QChart::AllAnimations); |
|
24 | 24 | chart->legend()->setVisible(false); |
|
25 | chart->setTitle("Nested donuts example"); | |
|
25 | 26 | //! [1] |
|
26 | 27 | |
|
27 | 28 | //! [2] |
|
28 | 29 | qreal minSize = 0.1; |
|
29 | 30 | qreal maxSize = 0.9; |
|
30 | 31 | int donutCount = 5; |
|
31 | 32 | //! [2] |
|
32 | 33 | |
|
33 | 34 | //! [3] |
|
34 | 35 | for (int i = 0; i < donutCount; i++) { |
|
35 | 36 | QPieSeries *donut = new QPieSeries; |
|
36 | 37 | int sliceCount = 3 + qrand() % 3; |
|
37 | 38 | for (int j = 0; j < sliceCount; j++) { |
|
38 | 39 | qreal value = 100 + qrand() % 100; |
|
39 | 40 | QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value); |
|
40 | 41 | slice->setLabelVisible(true); |
|
41 | 42 | slice->setLabelColor(Qt::white); |
|
42 | 43 | slice->setLabelPosition(QPieSlice::LabelInsideTangential); |
|
43 | 44 | connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool))); |
|
44 | 45 | donut->append(slice); |
|
45 | 46 | donut->setHoleSize(minSize + i * (maxSize - minSize) / donutCount); |
|
46 | 47 | donut->setPieSize(minSize + (i + 1) * (maxSize - minSize) / donutCount); |
|
47 | 48 | } |
|
48 | 49 | m_donuts.append(donut); |
|
49 | 50 | chartView->chart()->addSeries(donut); |
|
50 | 51 | } |
|
51 | 52 | //! [3] |
|
52 | 53 | |
|
53 | 54 | // create main layout |
|
54 | 55 | //! [4] |
|
55 | 56 | QGridLayout* mainLayout = new QGridLayout; |
|
56 | 57 | mainLayout->addWidget(chartView, 1, 1); |
|
57 | 58 | setLayout(mainLayout); |
|
58 | 59 | //! [4] |
|
59 | 60 | |
|
60 | 61 | //! [5] |
|
61 | 62 | updateTimer = new QTimer(this); |
|
62 | 63 | connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation())); |
|
63 | 64 | updateTimer->start(1250); |
|
64 | 65 | //! [5] |
|
65 | 66 | } |
|
66 | 67 | |
|
67 | 68 | Widget::~Widget() |
|
68 | 69 | { |
|
69 | 70 | |
|
70 | 71 | } |
|
71 | 72 | |
|
72 | 73 | //! [6] |
|
73 | 74 | void Widget::updateRotation() |
|
74 | 75 | { |
|
75 | 76 | for (int i = 0; i < m_donuts.count(); i++) { |
|
76 | 77 | QPieSeries *donut = m_donuts.at(i); |
|
77 | 78 | qreal phaseShift = -50 + qrand() % 100; |
|
78 | 79 | donut->setPieStartAngle(donut->pieStartAngle() + phaseShift); |
|
79 | 80 | donut->setPieEndAngle(donut->pieEndAngle() + phaseShift); |
|
80 | 81 | } |
|
81 | 82 | } |
|
82 | 83 | //! [6] |
|
83 | 84 | |
|
84 | 85 | //! [7] |
|
85 | 86 | void Widget::explodeSlice(bool exploded) |
|
86 | 87 | { |
|
87 | 88 | QPieSlice *slice = qobject_cast<QPieSlice *>(sender()); |
|
88 | 89 | if (exploded) { |
|
89 | 90 | updateTimer->stop(); |
|
90 | 91 | qreal sliceStartAngle = slice->startAngle(); |
|
91 | 92 | qreal sliceEndAngle = slice->startAngle() + slice->angleSpan(); |
|
92 | 93 | |
|
93 | 94 | QPieSeries *donut = slice->series(); |
|
94 | 95 | qreal seriesIndex = m_donuts.indexOf(donut); |
|
95 | 96 | for (int i = seriesIndex + 1; i < m_donuts.count(); i++) { |
|
96 | 97 | m_donuts.at(i)->setPieStartAngle(sliceEndAngle); |
|
97 | 98 | m_donuts.at(i)->setPieEndAngle(360 + sliceStartAngle); |
|
98 | 99 | } |
|
99 | 100 | } else { |
|
100 | 101 | for (int i = 0; i < m_donuts.count(); i++) { |
|
101 | 102 | m_donuts.at(i)->setPieStartAngle(0); |
|
102 | 103 | m_donuts.at(i)->setPieEndAngle(360); |
|
103 | 104 | } |
|
104 | 105 | updateTimer->start(); |
|
105 | 106 | } |
|
106 | 107 | slice->setExploded(exploded); |
|
107 | 108 | } |
|
108 | 109 | //! [7] |
@@ -1,138 +1,139 | |||
|
1 | 1 | #include "widget.h" |
|
2 | 2 | #include <QGridLayout> |
|
3 | 3 | #include <QPieSlice> |
|
4 | 4 | #include <QTime> |
|
5 | 5 | #include <QChartView> |
|
6 | 6 | #include <QChart> |
|
7 | 7 | #include <QLegend> |
|
8 | 8 | #include <QTimer> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | Widget::Widget(QWidget *parent) |
|
13 | 13 | : QWidget(parent), |
|
14 | 14 | mainData(0) |
|
15 | 15 | { |
|
16 | 16 | setMinimumSize(800, 600); |
|
17 | 17 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
18 | 18 | |
|
19 | 19 | //! [1] |
|
20 | 20 | QChartView *chartView = new QChartView; |
|
21 | 21 | chartView->setRenderHint(QPainter::Antialiasing); |
|
22 | 22 | QChart *chart = chartView->chart(); |
|
23 | 23 | chart->setAnimationOptions(QChart::AllAnimations); |
|
24 | 24 | chart->legend()->setVisible(false); |
|
25 | chart->setTitle("Donut breakdown example"); | |
|
25 | 26 | //! [1] |
|
26 | 27 | |
|
27 | 28 | //! [2] |
|
28 | 29 | mainData = new QPieSeries; |
|
29 | 30 | mainData->setPieSize(0.6); |
|
30 | 31 | //! [2] |
|
31 | 32 | |
|
32 | 33 | //! [3] |
|
33 | 34 | for (int j = 0; j < 4; j++) { |
|
34 | 35 | // create new slice for the mainData |
|
35 | 36 | QPieSlice *slice = new QPieSlice; |
|
36 | 37 | slice->setLabelColor(Qt::white); |
|
37 | 38 | mainData->append(slice); |
|
38 | 39 | |
|
39 | 40 | // create a new detailed data for the slice |
|
40 | 41 | QPieSeries *donut = new QPieSeries; |
|
41 | 42 | donut->setHoleSize(mainData->pieSize()); |
|
42 | 43 | donut->setPieSize(mainData->pieSize() + 0.15); |
|
43 | 44 | |
|
44 | 45 | // when mainData slice is redrawn make sure the detailed data slices are aligned with it |
|
45 | 46 | connect(slice, SIGNAL(startAngleChanged()), this, SLOT(updatedStartAngle())); |
|
46 | 47 | connect(slice, SIGNAL(angleSpanChanged()), this, SLOT(updatedAngleSpan())); |
|
47 | 48 | |
|
48 | 49 | // create the detailed data |
|
49 | 50 | for (int j = 0; j < 3; j++) { |
|
50 | 51 | qreal value = 10 + qrand() % 100; |
|
51 | 52 | QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value); |
|
52 | 53 | donut->append(slice); |
|
53 | 54 | } |
|
54 | 55 | donut->setLabelsPosition(QPieSlice::LabelOutside); |
|
55 | 56 | donut->setLabelsVisible(); |
|
56 | 57 | detailedData.append(donut); |
|
57 | 58 | |
|
58 | 59 | // update the value and label of mainData |
|
59 | 60 | slice->setValue(donut->sum()); |
|
60 | 61 | slice->setLabel(QString("%1").arg(donut->sum())); |
|
61 | 62 | } |
|
62 | 63 | //! [3] |
|
63 | 64 | |
|
64 | 65 | //! [4] |
|
65 | 66 | mainData->setLabelsVisible(); |
|
66 | 67 | mainData->setLabelsPosition(QPieSlice::LabelInsideHorizontal); |
|
67 | 68 | chartView->chart()->addSeries(mainData); |
|
68 | 69 | for (int i = 0; i < detailedData.count(); i++) |
|
69 | 70 | chartView->chart()->addSeries(detailedData.at(i)); |
|
70 | 71 | //! [4] |
|
71 | 72 | |
|
72 | 73 | //! [5] |
|
73 | 74 | // create main layout |
|
74 | 75 | QGridLayout* mainLayout = new QGridLayout; |
|
75 | 76 | mainLayout->addWidget(chartView, 1, 1); |
|
76 | 77 | setLayout(mainLayout); |
|
77 | 78 | //! [5] |
|
78 | 79 | |
|
79 | 80 | //! [6] |
|
80 | 81 | // modify the value of one detailed slice every 2.5 sec |
|
81 | 82 | QTimer *updateTimer = new QTimer(this); |
|
82 | 83 | connect(updateTimer, SIGNAL(timeout()), this, SLOT(highlight())); |
|
83 | 84 | updateTimer->start(2500); |
|
84 | 85 | //! [6] |
|
85 | 86 | } |
|
86 | 87 | |
|
87 | 88 | Widget::~Widget() |
|
88 | 89 | { |
|
89 | 90 | |
|
90 | 91 | } |
|
91 | 92 | |
|
92 | 93 | //! [7] |
|
93 | 94 | void Widget::updatedStartAngle() |
|
94 | 95 | { |
|
95 | 96 | // when the mainData slice has been updated the detailed data QPieSeries object as well |
|
96 | 97 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
97 | 98 | QPieSeries *detailsDonut = detailedData.at(slice->series()->slices().indexOf(slice)); |
|
98 | 99 | detailsDonut->setPieStartAngle(slice->startAngle()); |
|
99 | 100 | } |
|
100 | 101 | |
|
101 | 102 | void Widget::updatedAngleSpan() |
|
102 | 103 | { |
|
103 | 104 | // when the mainData slice has been updated the detailed data QPieSeries object as well |
|
104 | 105 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
105 | 106 | QPieSeries *detailsDonut = detailedData.at(slice->series()->slices().indexOf(slice)); |
|
106 | 107 | detailsDonut->setPieEndAngle(slice->startAngle() + slice->angleSpan()); |
|
107 | 108 | } |
|
108 | 109 | //! [7] |
|
109 | 110 | |
|
110 | 111 | //! [8] |
|
111 | 112 | void Widget::highlight() |
|
112 | 113 | { |
|
113 | 114 | // choose one random detailed data slice to be updated. |
|
114 | 115 | detailIndex = qrand() % mainData->count(); |
|
115 | 116 | sliceIndex = qrand() % detailedData.at(detailIndex)->count(); |
|
116 | 117 | |
|
117 | 118 | // set the slice to exploded to make the change easier to observe |
|
118 | 119 | detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded(); |
|
119 | 120 | |
|
120 | 121 | // give the user time to focus on the slice that will be changed |
|
121 | 122 | QTimer::singleShot(1000, this, SLOT(updateRotation())); |
|
122 | 123 | } |
|
123 | 124 | |
|
124 | 125 | void Widget::updateRotation() |
|
125 | 126 | { |
|
126 | 127 | // update the selected slice |
|
127 | 128 | qreal newValue = 10 + qrand() % 100; |
|
128 | 129 | detailedData.at(detailIndex)->slices().at(sliceIndex)->setValue(newValue); |
|
129 | 130 | detailedData.at(detailIndex)->slices().at(sliceIndex)->setLabel(QString("%1").arg(newValue)); |
|
130 | 131 | |
|
131 | 132 | // update the mainData slice with a new sum of the detailed data values |
|
132 | 133 | mainData->slices().at(detailIndex)->setValue(detailedData.at(detailIndex)->sum()); |
|
133 | 134 | mainData->slices().at(detailIndex)->setLabel(QString("%1").arg(detailedData.at(detailIndex)->sum())); |
|
134 | 135 | |
|
135 | 136 | // change the explode state of the selected slice back to normal |
|
136 | 137 | detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded(false); |
|
137 | 138 | } |
|
138 | 139 | //! [8] |
General Comments 0
You need to be logged in to leave comments.
Login now