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