@@ -1,76 +1,75 | |||
|
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 | 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 |
color = color.lighter(11 |
|
|
34 | color = color.lighter(115); | |
|
35 | 35 | slice->setBrush(color); |
|
36 | 36 | slice->setLabelFont(QFont("Arial", 8)); |
|
37 | slice->setLabelArmLengthFactor(0.05); | |
|
38 | 37 | } |
|
39 | 38 | |
|
40 | 39 | // add the series to the chart |
|
41 | 40 | QChart::addSeries(breakdownSeries); |
|
42 | 41 | |
|
43 | 42 | // recalculate breakdown donut segments |
|
44 | 43 | recalculateAngles(); |
|
45 | 44 | } |
|
46 | 45 | //![2] |
|
47 | 46 | |
|
48 | 47 | //![3] |
|
49 | 48 | void DonutBreakdownChart::recalculateAngles() |
|
50 | 49 | { |
|
51 | 50 | qreal angle = 0; |
|
52 | 51 | foreach (QPieSlice *slice, mainSeries->slices()) { |
|
53 | 52 | QPieSeries *s = find(slice->label()); |
|
54 | 53 | if (s) { |
|
55 | 54 | s->setPieStartAngle(angle); |
|
56 | 55 | angle += slice->percentage() * 360.0; // full pie is 360.0 |
|
57 | 56 | s->setPieEndAngle(angle); |
|
58 | 57 | } |
|
59 | 58 | } |
|
60 | 59 | } |
|
61 | 60 | //![3] |
|
62 | 61 | |
|
63 | 62 | //![4] |
|
64 | 63 | QPieSeries *DonutBreakdownChart::find(QString seriesName) const |
|
65 | 64 | { |
|
66 | 65 | // find pieseries by name |
|
67 | 66 | foreach (QAbstractSeries *series, QChart::series()) { |
|
68 | 67 | QPieSeries *s = qobject_cast<QPieSeries*>(series); |
|
69 | 68 | if (!s) |
|
70 | 69 | continue; |
|
71 | 70 | if (s->name() == seriesName) |
|
72 | 71 | return s; |
|
73 | 72 | } |
|
74 | 73 | return 0; |
|
75 | 74 | } |
|
76 | 75 | //![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 | 12 | //![1] |
|
13 | 13 | // Data from http://www.stat.fi/til/ekul/2010/ekul_2010_2011-12-13_tie_001_en.html |
|
14 | 14 | |
|
15 | 15 | QPieSeries *series1 = new QPieSeries(); |
|
16 | series1->setName("Fossil"); | |
|
16 | series1->setName("Fossil fuels"); | |
|
17 | 17 | series1->append("Oil", 353295); |
|
18 | series1->append("Wood", 319663); | |
|
19 | 18 | series1->append("Coal", 188500); |
|
20 | 19 | series1->append("Natural gas", 148680); |
|
21 | 20 | series1->append("Peat", 94545); |
|
22 | 21 | |
|
23 | 22 | QPieSeries *series2 = new QPieSeries(); |
|
24 | series2->setName("Renewable"); | |
|
23 | series2->setName("Renewables"); | |
|
24 | series2->append("Wood fuels", 319663); | |
|
25 | 25 | series2->append("Hydro power", 45875); |
|
26 | 26 | series2->append("Wind power", 1060); |
|
27 | 27 | |
|
28 | 28 | QPieSeries *series3 = new QPieSeries(); |
|
29 | series3->setName("Other"); | |
|
29 | series3->setName("Others"); | |
|
30 | 30 | series3->append("Nuclear energy", 238789); |
|
31 | 31 | series3->append("Import energy", 37802); |
|
32 |
series3->append("Other |
|
|
32 | series3->append("Other", 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