##// END OF EJS Templates
Nesteddonuts chart docs updated
Marek Rosa -
r1968:570404538427
parent child
Show More
@@ -1,128 +1,128
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20 #include "widget.h"
20 #include "widget.h"
21 #include <QChartView>
21 #include <QChartView>
22 #include <QChart>
22 #include <QChart>
23 #include <QLegend>
23 #include <QLegend>
24 #include <QPieSeries>
24 #include <QPieSeries>
25 #include <QPieSlice>
25 #include <QPieSlice>
26 #include <QTime>
26 #include <QTime>
27 #include <QGridLayout>
27 #include <QGridLayout>
28 #include <QTimer>
28 #include <QTimer>
29
29
30 QTCOMMERCIALCHART_USE_NAMESPACE
30 QTCOMMERCIALCHART_USE_NAMESPACE
31
31
32 Widget::Widget(QWidget *parent)
32 Widget::Widget(QWidget *parent)
33 : QWidget(parent)
33 : QWidget(parent)
34 {
34 {
35 setMinimumSize(800, 600);
35 setMinimumSize(800, 600);
36 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
36 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
37
37
38 //! [1]
38 //! [1]
39 QChartView *chartView = new QChartView;
39 QChartView *chartView = new QChartView;
40 chartView->setRenderHint(QPainter::Antialiasing);
40 chartView->setRenderHint(QPainter::Antialiasing);
41 QChart *chart = chartView->chart();
41 QChart *chart = chartView->chart();
42 chart->setAnimationOptions(QChart::AllAnimations);
43 chart->legend()->setVisible(false);
42 chart->legend()->setVisible(false);
44 chart->setTitle("Nested donuts demo");
43 chart->setTitle("Nested donuts demo");
44 chart->setAnimationOptions(QChart::AllAnimations);
45 //! [1]
45 //! [1]
46
46
47 //! [2]
47 //! [2]
48 qreal minSize = 0.1;
48 qreal minSize = 0.1;
49 qreal maxSize = 0.9;
49 qreal maxSize = 0.9;
50 int donutCount = 5;
50 int donutCount = 5;
51 //! [2]
51 //! [2]
52
52
53 //! [3]
53 //! [3]
54 for (int i = 0; i < donutCount; i++) {
54 for (int i = 0; i < donutCount; i++) {
55 QPieSeries *donut = new QPieSeries;
55 QPieSeries *donut = new QPieSeries;
56 int sliceCount = 3 + qrand() % 3;
56 int sliceCount = 3 + qrand() % 3;
57 for (int j = 0; j < sliceCount; j++) {
57 for (int j = 0; j < sliceCount; j++) {
58 qreal value = 100 + qrand() % 100;
58 qreal value = 100 + qrand() % 100;
59 QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value);
59 QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value);
60 slice->setLabelVisible(true);
60 slice->setLabelVisible(true);
61 slice->setLabelColor(Qt::white);
61 slice->setLabelColor(Qt::white);
62 slice->setLabelPosition(QPieSlice::LabelInsideTangential);
62 slice->setLabelPosition(QPieSlice::LabelInsideTangential);
63 connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool)));
63 connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool)));
64 donut->append(slice);
64 donut->append(slice);
65 donut->setHoleSize(minSize + i * (maxSize - minSize) / donutCount);
65 donut->setHoleSize(minSize + i * (maxSize - minSize) / donutCount);
66 donut->setPieSize(minSize + (i + 1) * (maxSize - minSize) / donutCount);
66 donut->setPieSize(minSize + (i + 1) * (maxSize - minSize) / donutCount);
67 }
67 }
68 m_donuts.append(donut);
68 m_donuts.append(donut);
69 chartView->chart()->addSeries(donut);
69 chartView->chart()->addSeries(donut);
70 }
70 }
71 //! [3]
71 //! [3]
72
72
73 // create main layout
73 // create main layout
74 //! [4]
74 //! [4]
75 QGridLayout* mainLayout = new QGridLayout;
75 QGridLayout* mainLayout = new QGridLayout;
76 mainLayout->addWidget(chartView, 1, 1);
76 mainLayout->addWidget(chartView, 1, 1);
77 setLayout(mainLayout);
77 setLayout(mainLayout);
78 //! [4]
78 //! [4]
79
79
80 //! [5]
80 //! [5]
81 updateTimer = new QTimer(this);
81 updateTimer = new QTimer(this);
82 connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation()));
82 connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation()));
83 updateTimer->start(1250);
83 updateTimer->start(1250);
84 //! [5]
84 //! [5]
85 }
85 }
86
86
87 Widget::~Widget()
87 Widget::~Widget()
88 {
88 {
89
89
90 }
90 }
91
91
92 //! [6]
92 //! [6]
93 void Widget::updateRotation()
93 void Widget::updateRotation()
94 {
94 {
95 for (int i = 0; i < m_donuts.count(); i++) {
95 for (int i = 0; i < m_donuts.count(); i++) {
96 QPieSeries *donut = m_donuts.at(i);
96 QPieSeries *donut = m_donuts.at(i);
97 qreal phaseShift = -50 + qrand() % 100;
97 qreal phaseShift = -50 + qrand() % 100;
98 donut->setPieStartAngle(donut->pieStartAngle() + phaseShift);
98 donut->setPieStartAngle(donut->pieStartAngle() + phaseShift);
99 donut->setPieEndAngle(donut->pieEndAngle() + phaseShift);
99 donut->setPieEndAngle(donut->pieEndAngle() + phaseShift);
100 }
100 }
101 }
101 }
102 //! [6]
102 //! [6]
103
103
104 //! [7]
104 //! [7]
105 void Widget::explodeSlice(bool exploded)
105 void Widget::explodeSlice(bool exploded)
106 {
106 {
107 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
107 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
108 if (exploded) {
108 if (exploded) {
109 updateTimer->stop();
109 updateTimer->stop();
110 qreal sliceStartAngle = slice->startAngle();
110 qreal sliceStartAngle = slice->startAngle();
111 qreal sliceEndAngle = slice->startAngle() + slice->angleSpan();
111 qreal sliceEndAngle = slice->startAngle() + slice->angleSpan();
112
112
113 QPieSeries *donut = slice->series();
113 QPieSeries *donut = slice->series();
114 qreal seriesIndex = m_donuts.indexOf(donut);
114 qreal seriesIndex = m_donuts.indexOf(donut);
115 for (int i = seriesIndex + 1; i < m_donuts.count(); i++) {
115 for (int i = seriesIndex + 1; i < m_donuts.count(); i++) {
116 m_donuts.at(i)->setPieStartAngle(sliceEndAngle);
116 m_donuts.at(i)->setPieStartAngle(sliceEndAngle);
117 m_donuts.at(i)->setPieEndAngle(360 + sliceStartAngle);
117 m_donuts.at(i)->setPieEndAngle(360 + sliceStartAngle);
118 }
118 }
119 } else {
119 } else {
120 for (int i = 0; i < m_donuts.count(); i++) {
120 for (int i = 0; i < m_donuts.count(); i++) {
121 m_donuts.at(i)->setPieStartAngle(0);
121 m_donuts.at(i)->setPieStartAngle(0);
122 m_donuts.at(i)->setPieEndAngle(360);
122 m_donuts.at(i)->setPieEndAngle(360);
123 }
123 }
124 updateTimer->start();
124 updateTimer->start();
125 }
125 }
126 slice->setExploded(exploded);
126 slice->setExploded(exploded);
127 }
127 }
128 //! [7]
128 //! [7]
@@ -1,49 +1,50
1 /*!
1 /*!
2 \example demos/nesteddonuts
2 \example demos/nesteddonuts
3 \title Nested donuts demo
3 \title Nested donuts demo
4 \subtitle
4 \subtitle
5
5
6 This example shows how to use create a nested donuts chart using QPieSeries API.
6 This example shows how to use create a nested donuts chart using QPieSeries API.
7 \image demos_nesteddonuts.png
7 \image demos_nesteddonuts.png
8
8
9 Let's start by creating a QChartView instance and enabling the Antialiasing on it. Last line enables the animations of the chart.
9 Let's start by creating a QChartView instance and enabling the Antialiasing on it. QChart object is then obtained from the QChartView instance.
10 The legend is disabled and the title of the chart is set. Last line enables the animations of the chart.
10
11
11 \snippet ../demos/nesteddonuts/widget.cpp 1
12 \snippet ../demos/nesteddonuts/widget.cpp 1
12
13
13 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.
14 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.
14 minSize is the relative inner size of the smallest donut. maxSize is the relative outer size of the biggest donut.
15 minSize is the relative inner size of the smallest donut. maxSize is the relative outer size of the biggest donut.
15
16
16 \snippet ../demos/nesteddonuts/widget.cpp 2
17 \snippet ../demos/nesteddonuts/widget.cpp 2
17
18
18 Following block of code defines the individual donuts and their slices. First new QPieSeries object is created.
19 Following block of code defines the individual donuts and their slices. First new QPieSeries object is created.
19 Callign setDonut() changes pie into a donut. The number of slices in each donut is randomized.
20 The number of slices in each donut is randomized.
20 The internal for loop creates the slices with a random value and label same as the value.
21 The internal for loop creates the slices with a random value and label same as the value.
21 Next the label of the slice is set to be visible and its color is set to white.
22 Next the label of the slice is set to be visible and its color is set to white.
22 To make the example more interesting the hovered signal of the slice is connected to widget's slot which inner workings are explained later.
23 To make the example more interesting the hovered signal of the slice is connected to widget's slot which inner workings are explained later.
23 Finally the slice is added to the donut. The donut's size is adjusted to achieve the nesting of the donuts.
24 Finally the slice is added to the donut. The donut's size is adjusted to achieve the nesting of the donuts.
24 Then the donut is added to the widget's list of donuts and to the chart.
25 Then the donut is added to the widget's list of donuts and to the chart.
25
26
26 \snippet ../demos/nesteddonuts/widget.cpp 3
27 \snippet ../demos/nesteddonuts/widget.cpp 3
27
28
28 Finally the widget is placed in a layout used by the application.
29 Finally the widget is placed in a layout used by the application.
29
30
30 \snippet ../demos/nesteddonuts/widget.cpp 4
31 \snippet ../demos/nesteddonuts/widget.cpp 4
31
32
32 To make the example more interesting the donuts are rotated randomly every 1.25 sec.
33 To make the example more interesting the donuts are rotated randomly every 1.25 sec.
33
34
34 \snippet ../demos/nesteddonuts/widget.cpp 5
35 \snippet ../demos/nesteddonuts/widget.cpp 5
35
36
36 The widget's updatedRotation slot is defined below.
37 The widget's updatedRotation slot is defined below.
37 It goes through all of the donuts and modifies thier current rotation by a random value.
38 It goes through all of the donuts and modifies thier current rotation by a random value.
38
39
39 \snippet ../demos/nesteddonuts/widget.cpp 6
40 \snippet ../demos/nesteddonuts/widget.cpp 6
40
41
41 The earlier mentioned explodeSlice slot code is provided below.
42 The earlier mentioned explodeSlice slot code is provided below.
42 If the slice is set to exploded then stop the timer that controls the donuts rotation.
43 If the slice is set to exploded then stop the timer that controls the donuts rotation.
43 Then the slice's start and end agles are obtained from the slice.
44 Then the slice's start and end angles are obtained from the slice.
44 To highlight the selected slice all the other donuts that lie outward from the one that contains the selected slice
45 To highlight the selected slice all the other donuts that lie outward from the one that contains the selected slice
45 have their start and end angles modified so that they wouldn't "block" the way for the hightlighted slice.
46 have their start and end angles modified so that they wouldn't "block" the way for the hightlighted slice.
46 If the slice is no longer selected return to the original state.
47 If the slice is no longer selected return to the original state.
47
48
48 \snippet ../demos/nesteddonuts/widget.cpp 7
49 \snippet ../demos/nesteddonuts/widget.cpp 7
49 */
50 */
General Comments 0
You need to be logged in to leave comments. Login now