@@ -1,52 +1,35 | |||
|
1 | 1 | /*! |
|
2 | 2 | \example examples/donutbreakdown |
|
3 | 3 | \title Donut chart breakdown example |
|
4 | 4 | \subtitle |
|
5 | 5 | |
|
6 | 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/ |
|
|
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/ |
|
|
14 | \snippet ../examples/donutbreakdown/main.cpp 2 | |
|
15 | 15 | |
|
16 | Following block of code creates the slices for the mainData QPieSeries. | |
|
17 | Then for every created slice a new series is created that stores the detailed data. | |
|
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. | |
|
16 | Our own chart works so that in the constructor we create a main series | |
|
17 | which aggregates the data provided by the breakdown series. This is the piechart in the center. | |
|
20 | 18 | |
|
21 |
\snippet ../examples/donutbreakdown/ |
|
|
19 | \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 1 | |
|
22 | 20 | |
|
23 | Set the labels of the mainData to enabled and their postion to Outside. | |
|
24 | Then add the mainData and detailedData series to the chart. | |
|
21 | When a breakdown series is added the data is used to create a slice in the main series and the | |
|
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/ |
|
|
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/ |
|
|
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. | |
|
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. | |
|
31 | And now that we have our chart defined we can finally create a QChartView and show the chart. | |
|
35 | 32 | |
|
36 |
\snippet ../examples/donutbreakdown/ |
|
|
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 | |
|
33 | \snippet ../examples/donutbreakdown/main.cpp 3 | |
|
51 | 34 | |
|
52 | 35 | */ |
@@ -1,46 +1,45 | |||
|
1 | 1 | /*! |
|
2 | 2 | \page examples.html |
|
3 | 3 | \title Examples |
|
4 | 4 | \keyword Examples |
|
5 | 5 | |
|
6 | 6 | \raw HTML |
|
7 | 7 | <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable"> |
|
8 | 8 | <tr> |
|
9 | 9 | <th class="titleheader" width="33%"> |
|
10 | 10 | List of examples |
|
11 | 11 | </th> |
|
12 | 12 | </tr> |
|
13 | 13 | <tr> |
|
14 | 14 | <td valign="top"> |
|
15 | 15 | <ul> |
|
16 | 16 | <li><a href="examples-areachart.html">Area chart</a></li> |
|
17 | 17 | <li><a href="examples-barchart.html">Bar chart</a></li> |
|
18 | 18 | <li><a href="examples-customchart.html">Custom chart</a></li> |
|
19 | 19 | <li><a href="examples-horizontalbarchart.html">Horizontal bar chart</a></li> |
|
20 | 20 | <li><a href="examples-horizontalstackedbarchart.html">Horizontal stacked bar chart</a></li> |
|
21 | 21 | <li><a href="examples-horizontalpercentbarchart.html">Horizontal percent bar chart</a></li> |
|
22 | 22 | <li><a href="examples-legend.html">Legend</a></li> |
|
23 | 23 | <li><a href="examples-linechart.html">Line chart</a></li> |
|
24 | 24 | <li><a href="examples-lineandbar.html">Line and barchart</a></li> |
|
25 | 25 | <li><a href="examples-modeldata.html">Model data</a></li> |
|
26 | 26 | <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li> |
|
27 | 27 | <li><a href="examples-percentbarchart.html">Percent bar chart</a></li> |
|
28 | 28 | <li><a href="examples-piechart.html">Pie chart</a></li> |
|
29 | 29 | <li><a href="examples-donutchart.html">Donut chart</a></li> |
|
30 | 30 | <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li> |
|
31 |
<li><a href="examples-donut.html"> |
|
|
32 | <li><a href="examples-donutbreakdown.html">Donut chart drilldown</a></li> | |
|
31 | <li><a href="examples-donutbreakdown.html">Donut breakdown chart</a></li> | |
|
33 | 32 | <li><a href="examples-presenterchart.html">Presenter chart</a></li> |
|
34 | 33 | <li><a href="examples-scatterchart.html">Scatter chart</a></li> |
|
35 | 34 | <li><a href="examples-scatterinteractions.html">Scatter interactions</a></li> |
|
36 | 35 | <li><a href="examples-splinechart.html">Spline chart</a></li> |
|
37 | 36 | <li><a href="examples-stackedbarchart.html">Stacked bar chart</a></li> |
|
38 | 37 | <li><a href="examples-stackedbarchartdrilldown.html">Stacked bar chart drilldown</a></li> |
|
39 | 38 | <li><a href="examples-temperaturerecords.html">Temperature records</a></li> |
|
40 | 39 | <li><a href="examples-zoomlinechart.html">Zoom line</a></li> |
|
41 | 40 | </ul> |
|
42 | 41 | </td> |
|
43 | 42 | </tr> |
|
44 | 43 | </table> |
|
45 | 44 | \endraw |
|
46 | 45 | */ |
@@ -1,73 +1,76 | |||
|
1 | 1 | #include "donutbreakdownchart.h" |
|
2 | 2 | #include <QPieSlice> |
|
3 | 3 | |
|
4 | 4 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
5 | 5 | |
|
6 | 6 | //![1] |
|
7 | 7 | DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) |
|
8 | 8 | :QChart(parent, wFlags) |
|
9 | 9 | { |
|
10 | 10 | // create the series for main center pie |
|
11 | 11 | mainSeries = new QPieSeries(); |
|
12 | 12 | mainSeries->setPieSize(0.7); |
|
13 | addSeries(mainSeries); | |
|
13 | QChart::addSeries(mainSeries); | |
|
14 | 14 | } |
|
15 | 15 | //![1] |
|
16 | 16 | |
|
17 | 17 | //![2] |
|
18 | 18 | void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color) |
|
19 | 19 | { |
|
20 | 20 | // add breakdown series as a slice to center pie |
|
21 | 21 | QPieSlice *slice = mainSeries->append(breakdownSeries->name(), breakdownSeries->sum()); |
|
22 | 22 | |
|
23 | 23 | // customize the slice |
|
24 | 24 | slice->setBrush(color); |
|
25 | 25 | slice->setLabelVisible(); |
|
26 | 26 | slice->setLabelColor(Qt::white); |
|
27 | 27 | slice->setLabelPosition(QPieSlice::LabelInsideHorizontal); |
|
28 | 28 | |
|
29 | 29 | // position and customize the breakdown series |
|
30 | 30 | breakdownSeries->setPieSize(0.8); |
|
31 | 31 | breakdownSeries->setHoleSize(0.7); |
|
32 | 32 | breakdownSeries->setLabelsVisible(); |
|
33 | 33 | foreach (QPieSlice *slice, breakdownSeries->slices()) { |
|
34 | 34 | color = color.lighter(110); |
|
35 | 35 | slice->setBrush(color); |
|
36 | 36 | slice->setLabelFont(QFont("Arial", 7)); |
|
37 | 37 | slice->setLabelArmLengthFactor(0.05); |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | // add the series to the chart |
|
41 | addSeries(breakdownSeries); | |
|
41 | QChart::addSeries(breakdownSeries); | |
|
42 | 42 | |
|
43 | 43 | // recalculate breakdown donut segments |
|
44 | 44 | recalculateAngles(); |
|
45 | 45 | } |
|
46 | 46 | //![2] |
|
47 | 47 | |
|
48 | //![3] | |
|
48 | 49 | void DonutBreakdownChart::recalculateAngles() |
|
49 | 50 | { |
|
50 | 51 | qreal angle = 0; |
|
51 | 52 | foreach (QPieSlice *slice, mainSeries->slices()) { |
|
52 | 53 | QPieSeries *s = find(slice->label()); |
|
53 | 54 | if (s) { |
|
54 | 55 | s->setPieStartAngle(angle); |
|
55 | angle += slice->percentage() * 360.0; | |
|
56 | angle += slice->percentage() * 360.0; // full pie is 360.0 | |
|
56 | 57 | s->setPieEndAngle(angle); |
|
57 | 58 | } |
|
58 | 59 | } |
|
59 | 60 | } |
|
61 | //![3] | |
|
60 | 62 | |
|
63 | //![4] | |
|
61 | 64 | QPieSeries *DonutBreakdownChart::find(QString seriesName) const |
|
62 | 65 | { |
|
63 | 66 | // find pieseries by name |
|
64 |
foreach (QAbstractSeries *series, |
|
|
67 | foreach (QAbstractSeries *series, QChart::series()) { | |
|
65 | 68 | QPieSeries *s = qobject_cast<QPieSeries*>(series); |
|
66 | 69 | if (!s) |
|
67 | 70 | continue; |
|
68 | 71 | if (s->name() == seriesName) |
|
69 | 72 | return s; |
|
70 | 73 | } |
|
71 | 74 | return 0; |
|
72 | 75 | } |
|
73 | ||
|
76 | //![4] |
@@ -1,55 +1,55 | |||
|
1 | 1 | #include <QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <QChartView> |
|
4 | 4 | #include "donutbreakdownchart.h" |
|
5 | 5 | |
|
6 | 6 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
7 | 7 | |
|
8 | 8 | int main(int argc, char *argv[]) |
|
9 | 9 | { |
|
10 | 10 | QApplication a(argc, argv); |
|
11 | 11 | |
|
12 | //![1] | |
|
12 | 13 | // Data from http://www.stat.fi/til/ekul/2010/ekul_2010_2011-12-13_tie_001_en.html |
|
13 | 14 | |
|
14 | //![1] | |
|
15 | 15 | QPieSeries *series1 = new QPieSeries(); |
|
16 | 16 | series1->setName("Fossil"); |
|
17 | 17 | series1->append("Oil", 353295); |
|
18 | 18 | series1->append("Wood", 319663); |
|
19 | 19 | series1->append("Coal", 188500); |
|
20 | 20 | series1->append("Natural gas", 148680); |
|
21 | 21 | series1->append("Peat", 94545); |
|
22 | 22 | |
|
23 | 23 | QPieSeries *series2 = new QPieSeries(); |
|
24 | 24 | series2->setName("Renewable"); |
|
25 | 25 | series2->append("Hydro power", 45875); |
|
26 | 26 | series2->append("Wind power", 1060); |
|
27 | 27 | |
|
28 | 28 | QPieSeries *series3 = new QPieSeries(); |
|
29 | 29 | series3->setName("Other"); |
|
30 | 30 | series3->append("Nuclear energy", 238789); |
|
31 | 31 | series3->append("Import energy", 37802); |
|
32 | 32 | series3->append("Others", 32441); |
|
33 | 33 | //![1] |
|
34 | 34 | |
|
35 | 35 | //![2] |
|
36 | 36 | DonutBreakdownChart *donutBreakdown = new DonutBreakdownChart(); |
|
37 | 37 | donutBreakdown->setAnimationOptions(QChart::AllAnimations); |
|
38 | 38 | donutBreakdown->setTitle("Total consumption of energy in Finland 2010"); |
|
39 | 39 | donutBreakdown->legend()->setVisible(false); |
|
40 | 40 | donutBreakdown->addBreakdownSeries(series1, Qt::red); |
|
41 | 41 | donutBreakdown->addBreakdownSeries(series2, Qt::darkGreen); |
|
42 | 42 | donutBreakdown->addBreakdownSeries(series3, Qt::darkBlue); |
|
43 | 43 | //![2] |
|
44 | 44 | |
|
45 | 45 | //![3] |
|
46 | 46 | QMainWindow window; |
|
47 | 47 | QChartView* chartView = new QChartView(donutBreakdown); |
|
48 | 48 | chartView->setRenderHint(QPainter::Antialiasing); |
|
49 | 49 | window.setCentralWidget(chartView); |
|
50 | 50 | window.resize(800, 600); |
|
51 | 51 | window.show(); |
|
52 | 52 | //![3] |
|
53 | 53 | |
|
54 | 54 | return a.exec(); |
|
55 | 55 | } |
General Comments 0
You need to be logged in to leave comments.
Login now