##// END OF EJS Templates
fine tuning donutbreakdownchart
Jani Honkonen -
r1905:2f36b74b4262
parent child
Show More
@@ -1,76 +1,75
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 QChart::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(115);
35 slice->setBrush(color);
35 slice->setBrush(color);
36 slice->setLabelFont(QFont("Arial", 8));
36 slice->setLabelFont(QFont("Arial", 8));
37 slice->setLabelArmLengthFactor(0.05);
38 }
37 }
39
38
40 // add the series to the chart
39 // add the series to the chart
41 QChart::addSeries(breakdownSeries);
40 QChart::addSeries(breakdownSeries);
42
41
43 // recalculate breakdown donut segments
42 // recalculate breakdown donut segments
44 recalculateAngles();
43 recalculateAngles();
45 }
44 }
46 //![2]
45 //![2]
47
46
48 //![3]
47 //![3]
49 void DonutBreakdownChart::recalculateAngles()
48 void DonutBreakdownChart::recalculateAngles()
50 {
49 {
51 qreal angle = 0;
50 qreal angle = 0;
52 foreach (QPieSlice *slice, mainSeries->slices()) {
51 foreach (QPieSlice *slice, mainSeries->slices()) {
53 QPieSeries *s = find(slice->label());
52 QPieSeries *s = find(slice->label());
54 if (s) {
53 if (s) {
55 s->setPieStartAngle(angle);
54 s->setPieStartAngle(angle);
56 angle += slice->percentage() * 360.0; // full pie is 360.0
55 angle += slice->percentage() * 360.0; // full pie is 360.0
57 s->setPieEndAngle(angle);
56 s->setPieEndAngle(angle);
58 }
57 }
59 }
58 }
60 }
59 }
61 //![3]
60 //![3]
62
61
63 //![4]
62 //![4]
64 QPieSeries *DonutBreakdownChart::find(QString seriesName) const
63 QPieSeries *DonutBreakdownChart::find(QString seriesName) const
65 {
64 {
66 // find pieseries by name
65 // find pieseries by name
67 foreach (QAbstractSeries *series, QChart::series()) {
66 foreach (QAbstractSeries *series, QChart::series()) {
68 QPieSeries *s = qobject_cast<QPieSeries*>(series);
67 QPieSeries *s = qobject_cast<QPieSeries*>(series);
69 if (!s)
68 if (!s)
70 continue;
69 continue;
71 if (s->name() == seriesName)
70 if (s->name() == seriesName)
72 return s;
71 return s;
73 }
72 }
74 return 0;
73 return 0;
75 }
74 }
76 //![4]
75 //![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 //![1]
13 // 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
14
14
15 QPieSeries *series1 = new QPieSeries();
15 QPieSeries *series1 = new QPieSeries();
16 series1->setName("Fossil");
16 series1->setName("Fossil fuels");
17 series1->append("Oil", 353295);
17 series1->append("Oil", 353295);
18 series1->append("Wood", 319663);
19 series1->append("Coal", 188500);
18 series1->append("Coal", 188500);
20 series1->append("Natural gas", 148680);
19 series1->append("Natural gas", 148680);
21 series1->append("Peat", 94545);
20 series1->append("Peat", 94545);
22
21
23 QPieSeries *series2 = new QPieSeries();
22 QPieSeries *series2 = new QPieSeries();
24 series2->setName("Renewable");
23 series2->setName("Renewables");
24 series2->append("Wood fuels", 319663);
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("Others");
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("Other", 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