##// END OF EJS Templates
Update donutbreakdown example docs
Jani Honkonen -
r1888:7618c8da3444
parent child
Show More
@@ -5,48 +5,31
5
5
6 This example shows how to use create a donut breakdown chart using QPieSeries API.
6 This example shows how to use create a donut breakdown chart using QPieSeries API.
7
7
8 Let's start by creating a QChartView instance and enabling the Antialiasing on it. Last line enables the animations of the chart.
8 Let's start by defining some data for the chart.
9
9
10 \snippet ../examples/donutbreakdown/widget.cpp 1
10 \snippet ../examples/donutbreakdown/main.cpp 1
11
11
12 PieSeries is used to present the general data.
12 Then we create a chart where we add the data. Note that this this is our own chart derived from QChart.
13
13
14 \snippet ../examples/donutbreakdown/widget.cpp 2
14 \snippet ../examples/donutbreakdown/main.cpp 2
15
15
16 Following block of code creates the slices for the mainData QPieSeries.
16 Our own chart works so that in the constructor we create a main series
17 Then for every created slice a new series is created that stores the detailed data.
17 which aggregates the data provided by the breakdown series. This is the piechart in the center.
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
18
21 \snippet ../examples/donutbreakdown/widget.cpp 3
19 \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 1
22
20
23 Set the labels of the mainData to enabled and their postion to Outside.
21 When a breakdown series is added the data is used to create a slice in the main series and the
24 Then add the mainData and detailedData series to the chart.
22 breakdown series itself is used to create a segment of a donut positioned so that it is aligned
23 with the corresponding slice in the main series.
25
24
26 \snippet ../examples/donutbreakdown/widget.cpp 4
25 \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 2
27
26
28 Finally the widget is placed in a layout used by the application.
27 Here's how the start and end angles for the donut segments are calculated.
29
28
30 \snippet ../examples/donutbreakdown/widget.cpp 5
29 \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 3
31
30
32 To show that the detailed data stays aligned with the main data every 2.5 sec. one of the slices is modified.
31 And now that we have our chart defined we can finally create a QChartView and show the chart.
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
32
36 \snippet ../examples/donutbreakdown/widget.cpp 6
33 \snippet ../examples/donutbreakdown/main.cpp 3
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/donutbreakdown/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/donutbreakdown/widget.cpp 8
51
34
52 */
35 */
@@ -28,8 +28,7
28 <li><a href="examples-piechart.html">Pie chart</a></li>
28 <li><a href="examples-piechart.html">Pie chart</a></li>
29 <li><a href="examples-donutchart.html">Donut chart</a></li>
29 <li><a href="examples-donutchart.html">Donut chart</a></li>
30 <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li>
30 <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li>
31 <li><a href="examples-donut.html">Nested donuts chart</a></li>
31 <li><a href="examples-donutbreakdown.html">Donut breakdown chart</a></li>
32 <li><a href="examples-donutbreakdown.html">Donut chart drilldown</a></li>
33 <li><a href="examples-presenterchart.html">Presenter chart</a></li>
32 <li><a href="examples-presenterchart.html">Presenter chart</a></li>
34 <li><a href="examples-scatterchart.html">Scatter chart</a></li>
33 <li><a href="examples-scatterchart.html">Scatter chart</a></li>
35 <li><a href="examples-scatterinteractions.html">Scatter interactions</a></li>
34 <li><a href="examples-scatterinteractions.html">Scatter interactions</a></li>
@@ -10,7 +10,7 DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags
10 // create the series for main center pie
10 // create the series for main center pie
11 mainSeries = new QPieSeries();
11 mainSeries = new QPieSeries();
12 mainSeries->setPieSize(0.7);
12 mainSeries->setPieSize(0.7);
13 addSeries(mainSeries);
13 QChart::addSeries(mainSeries);
14 }
14 }
15 //![1]
15 //![1]
16
16
@@ -38,13 +38,14 void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor
38 }
38 }
39
39
40 // add the series to the chart
40 // add the series to the chart
41 addSeries(breakdownSeries);
41 QChart::addSeries(breakdownSeries);
42
42
43 // recalculate breakdown donut segments
43 // recalculate breakdown donut segments
44 recalculateAngles();
44 recalculateAngles();
45 }
45 }
46 //![2]
46 //![2]
47
47
48 //![3]
48 void DonutBreakdownChart::recalculateAngles()
49 void DonutBreakdownChart::recalculateAngles()
49 {
50 {
50 qreal angle = 0;
51 qreal angle = 0;
@@ -52,16 +53,18 void DonutBreakdownChart::recalculateAngles()
52 QPieSeries *s = find(slice->label());
53 QPieSeries *s = find(slice->label());
53 if (s) {
54 if (s) {
54 s->setPieStartAngle(angle);
55 s->setPieStartAngle(angle);
55 angle += slice->percentage() * 360.0;
56 angle += slice->percentage() * 360.0; // full pie is 360.0
56 s->setPieEndAngle(angle);
57 s->setPieEndAngle(angle);
57 }
58 }
58 }
59 }
59 }
60 }
61 //![3]
60
62
63 //![4]
61 QPieSeries *DonutBreakdownChart::find(QString seriesName) const
64 QPieSeries *DonutBreakdownChart::find(QString seriesName) const
62 {
65 {
63 // find pieseries by name
66 // find pieseries by name
64 foreach (QAbstractSeries *series, this->series()) {
67 foreach (QAbstractSeries *series, QChart::series()) {
65 QPieSeries *s = qobject_cast<QPieSeries*>(series);
68 QPieSeries *s = qobject_cast<QPieSeries*>(series);
66 if (!s)
69 if (!s)
67 continue;
70 continue;
@@ -70,4 +73,4 QPieSeries *DonutBreakdownChart::find(QString seriesName) const
70 }
73 }
71 return 0;
74 return 0;
72 }
75 }
73
76 //![4]
@@ -9,9 +9,9 int main(int argc, char *argv[])
9 {
9 {
10 QApplication a(argc, argv);
10 QApplication a(argc, argv);
11
11
12 //![1]
12 // Data from http://www.stat.fi/til/ekul/2010/ekul_2010_2011-12-13_tie_001_en.html
13 // Data from http://www.stat.fi/til/ekul/2010/ekul_2010_2011-12-13_tie_001_en.html
13
14
14 //![1]
15 QPieSeries *series1 = new QPieSeries();
15 QPieSeries *series1 = new QPieSeries();
16 series1->setName("Fossil");
16 series1->setName("Fossil");
17 series1->append("Oil", 353295);
17 series1->append("Oil", 353295);
General Comments 0
You need to be logged in to leave comments. Login now