@@ -1,52 +1,35 | |||||
1 | /*! |
|
1 | /*! | |
2 | \example examples/donutbreakdown |
|
2 | \example examples/donutbreakdown | |
3 | \title Donut chart breakdown example |
|
3 | \title Donut chart breakdown example | |
4 | \subtitle |
|
4 | \subtitle | |
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/ |
|
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. |
|
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/ |
|
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/ |
|
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. |
|
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/ |
|
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 | */ |
@@ -1,46 +1,45 | |||||
1 | /*! |
|
1 | /*! | |
2 | \page examples.html |
|
2 | \page examples.html | |
3 | \title Examples |
|
3 | \title Examples | |
4 | \keyword Examples |
|
4 | \keyword Examples | |
5 |
|
5 | |||
6 | \raw HTML |
|
6 | \raw HTML | |
7 | <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable"> |
|
7 | <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable"> | |
8 | <tr> |
|
8 | <tr> | |
9 | <th class="titleheader" width="33%"> |
|
9 | <th class="titleheader" width="33%"> | |
10 | List of examples |
|
10 | List of examples | |
11 | </th> |
|
11 | </th> | |
12 | </tr> |
|
12 | </tr> | |
13 | <tr> |
|
13 | <tr> | |
14 | <td valign="top"> |
|
14 | <td valign="top"> | |
15 | <ul> |
|
15 | <ul> | |
16 | <li><a href="examples-areachart.html">Area chart</a></li> |
|
16 | <li><a href="examples-areachart.html">Area chart</a></li> | |
17 | <li><a href="examples-barchart.html">Bar chart</a></li> |
|
17 | <li><a href="examples-barchart.html">Bar chart</a></li> | |
18 | <li><a href="examples-customchart.html">Custom chart</a></li> |
|
18 | <li><a href="examples-customchart.html">Custom chart</a></li> | |
19 | <li><a href="examples-horizontalbarchart.html">Horizontal bar chart</a></li> |
|
19 | <li><a href="examples-horizontalbarchart.html">Horizontal bar chart</a></li> | |
20 | <li><a href="examples-horizontalstackedbarchart.html">Horizontal stacked bar chart</a></li> |
|
20 | <li><a href="examples-horizontalstackedbarchart.html">Horizontal stacked bar chart</a></li> | |
21 | <li><a href="examples-horizontalpercentbarchart.html">Horizontal percent bar chart</a></li> |
|
21 | <li><a href="examples-horizontalpercentbarchart.html">Horizontal percent bar chart</a></li> | |
22 | <li><a href="examples-legend.html">Legend</a></li> |
|
22 | <li><a href="examples-legend.html">Legend</a></li> | |
23 | <li><a href="examples-linechart.html">Line chart</a></li> |
|
23 | <li><a href="examples-linechart.html">Line chart</a></li> | |
24 | <li><a href="examples-lineandbar.html">Line and barchart</a></li> |
|
24 | <li><a href="examples-lineandbar.html">Line and barchart</a></li> | |
25 | <li><a href="examples-modeldata.html">Model data</a></li> |
|
25 | <li><a href="examples-modeldata.html">Model data</a></li> | |
26 | <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li> |
|
26 | <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li> | |
27 | <li><a href="examples-percentbarchart.html">Percent bar chart</a></li> |
|
27 | <li><a href="examples-percentbarchart.html">Percent bar chart</a></li> | |
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"> |
|
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> | |
36 | <li><a href="examples-splinechart.html">Spline chart</a></li> |
|
35 | <li><a href="examples-splinechart.html">Spline chart</a></li> | |
37 | <li><a href="examples-stackedbarchart.html">Stacked bar chart</a></li> |
|
36 | <li><a href="examples-stackedbarchart.html">Stacked bar chart</a></li> | |
38 | <li><a href="examples-stackedbarchartdrilldown.html">Stacked bar chart drilldown</a></li> |
|
37 | <li><a href="examples-stackedbarchartdrilldown.html">Stacked bar chart drilldown</a></li> | |
39 | <li><a href="examples-temperaturerecords.html">Temperature records</a></li> |
|
38 | <li><a href="examples-temperaturerecords.html">Temperature records</a></li> | |
40 | <li><a href="examples-zoomlinechart.html">Zoom line</a></li> |
|
39 | <li><a href="examples-zoomlinechart.html">Zoom line</a></li> | |
41 | </ul> |
|
40 | </ul> | |
42 | </td> |
|
41 | </td> | |
43 | </tr> |
|
42 | </tr> | |
44 | </table> |
|
43 | </table> | |
45 | \endraw |
|
44 | \endraw | |
46 | */ |
|
45 | */ |
@@ -1,73 +1,76 | |||||
1 | #include "donutbreakdownchart.h" |
|
1 | #include "donutbreakdownchart.h" | |
2 | #include <QPieSlice> |
|
2 | #include <QPieSlice> | |
3 |
|
3 | |||
4 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
4 | QTCOMMERCIALCHART_USE_NAMESPACE | |
5 |
|
5 | |||
6 | //![1] |
|
6 | //![1] | |
7 | DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) |
|
7 | DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) | |
8 | :QChart(parent, wFlags) |
|
8 | :QChart(parent, wFlags) | |
9 | { |
|
9 | { | |
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 | |||
17 | //![2] |
|
17 | //![2] | |
18 | void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color) |
|
18 | void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color) | |
19 | { |
|
19 | { | |
20 | // add breakdown series as a slice to center pie |
|
20 | // add breakdown series as a slice to center pie | |
21 | QPieSlice *slice = mainSeries->append(breakdownSeries->name(), breakdownSeries->sum()); |
|
21 | QPieSlice *slice = mainSeries->append(breakdownSeries->name(), breakdownSeries->sum()); | |
22 |
|
22 | |||
23 | // customize the slice |
|
23 | // customize the slice | |
24 | slice->setBrush(color); |
|
24 | slice->setBrush(color); | |
25 | slice->setLabelVisible(); |
|
25 | slice->setLabelVisible(); | |
26 | slice->setLabelColor(Qt::white); |
|
26 | slice->setLabelColor(Qt::white); | |
27 | slice->setLabelPosition(QPieSlice::LabelInsideHorizontal); |
|
27 | slice->setLabelPosition(QPieSlice::LabelInsideHorizontal); | |
28 |
|
28 | |||
29 | // position and customize the breakdown series |
|
29 | // position and customize the breakdown series | |
30 | breakdownSeries->setPieSize(0.8); |
|
30 | breakdownSeries->setPieSize(0.8); | |
31 | breakdownSeries->setHoleSize(0.7); |
|
31 | breakdownSeries->setHoleSize(0.7); | |
32 | breakdownSeries->setLabelsVisible(); |
|
32 | breakdownSeries->setLabelsVisible(); | |
33 | foreach (QPieSlice *slice, breakdownSeries->slices()) { |
|
33 | foreach (QPieSlice *slice, breakdownSeries->slices()) { | |
34 | color = color.lighter(110); |
|
34 | color = color.lighter(110); | |
35 | slice->setBrush(color); |
|
35 | slice->setBrush(color); | |
36 | slice->setLabelFont(QFont("Arial", 7)); |
|
36 | slice->setLabelFont(QFont("Arial", 7)); | |
37 | slice->setLabelArmLengthFactor(0.05); |
|
37 | slice->setLabelArmLengthFactor(0.05); | |
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; | |
51 | foreach (QPieSlice *slice, mainSeries->slices()) { |
|
52 | foreach (QPieSlice *slice, mainSeries->slices()) { | |
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, |
|
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; | |
68 | if (s->name() == seriesName) |
|
71 | if (s->name() == seriesName) | |
69 | return s; |
|
72 | return s; | |
70 | } |
|
73 | } | |
71 | return 0; |
|
74 | return 0; | |
72 | } |
|
75 | } | |
73 |
|
76 | //![4] |
@@ -1,55 +1,55 | |||||
1 | #include <QApplication> |
|
1 | #include <QApplication> | |
2 | #include <QMainWindow> |
|
2 | #include <QMainWindow> | |
3 | #include <QChartView> |
|
3 | #include <QChartView> | |
4 | #include "donutbreakdownchart.h" |
|
4 | #include "donutbreakdownchart.h" | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
6 | QTCOMMERCIALCHART_USE_NAMESPACE | |
7 |
|
7 | |||
8 | int main(int argc, char *argv[]) |
|
8 | 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); | |
18 | series1->append("Wood", 319663); |
|
18 | series1->append("Wood", 319663); | |
19 | series1->append("Coal", 188500); |
|
19 | series1->append("Coal", 188500); | |
20 | series1->append("Natural gas", 148680); |
|
20 | series1->append("Natural gas", 148680); | |
21 | series1->append("Peat", 94545); |
|
21 | series1->append("Peat", 94545); | |
22 |
|
22 | |||
23 | QPieSeries *series2 = new QPieSeries(); |
|
23 | QPieSeries *series2 = new QPieSeries(); | |
24 | series2->setName("Renewable"); |
|
24 | series2->setName("Renewable"); | |
25 | series2->append("Hydro power", 45875); |
|
25 | series2->append("Hydro power", 45875); | |
26 | series2->append("Wind power", 1060); |
|
26 | series2->append("Wind power", 1060); | |
27 |
|
27 | |||
28 | QPieSeries *series3 = new QPieSeries(); |
|
28 | QPieSeries *series3 = new QPieSeries(); | |
29 | series3->setName("Other"); |
|
29 | series3->setName("Other"); | |
30 | series3->append("Nuclear energy", 238789); |
|
30 | series3->append("Nuclear energy", 238789); | |
31 | series3->append("Import energy", 37802); |
|
31 | series3->append("Import energy", 37802); | |
32 | series3->append("Others", 32441); |
|
32 | series3->append("Others", 32441); | |
33 | //![1] |
|
33 | //![1] | |
34 |
|
34 | |||
35 | //![2] |
|
35 | //![2] | |
36 | DonutBreakdownChart *donutBreakdown = new DonutBreakdownChart(); |
|
36 | DonutBreakdownChart *donutBreakdown = new DonutBreakdownChart(); | |
37 | donutBreakdown->setAnimationOptions(QChart::AllAnimations); |
|
37 | donutBreakdown->setAnimationOptions(QChart::AllAnimations); | |
38 | donutBreakdown->setTitle("Total consumption of energy in Finland 2010"); |
|
38 | donutBreakdown->setTitle("Total consumption of energy in Finland 2010"); | |
39 | donutBreakdown->legend()->setVisible(false); |
|
39 | donutBreakdown->legend()->setVisible(false); | |
40 | donutBreakdown->addBreakdownSeries(series1, Qt::red); |
|
40 | donutBreakdown->addBreakdownSeries(series1, Qt::red); | |
41 | donutBreakdown->addBreakdownSeries(series2, Qt::darkGreen); |
|
41 | donutBreakdown->addBreakdownSeries(series2, Qt::darkGreen); | |
42 | donutBreakdown->addBreakdownSeries(series3, Qt::darkBlue); |
|
42 | donutBreakdown->addBreakdownSeries(series3, Qt::darkBlue); | |
43 | //![2] |
|
43 | //![2] | |
44 |
|
44 | |||
45 | //![3] |
|
45 | //![3] | |
46 | QMainWindow window; |
|
46 | QMainWindow window; | |
47 | QChartView* chartView = new QChartView(donutBreakdown); |
|
47 | QChartView* chartView = new QChartView(donutBreakdown); | |
48 | chartView->setRenderHint(QPainter::Antialiasing); |
|
48 | chartView->setRenderHint(QPainter::Antialiasing); | |
49 | window.setCentralWidget(chartView); |
|
49 | window.setCentralWidget(chartView); | |
50 | window.resize(800, 600); |
|
50 | window.resize(800, 600); | |
51 | window.show(); |
|
51 | window.show(); | |
52 | //![3] |
|
52 | //![3] | |
53 |
|
53 | |||
54 | return a.exec(); |
|
54 | return a.exec(); | |
55 | } |
|
55 | } |
General Comments 0
You need to be logged in to leave comments.
Login now