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