@@ -0,0 +1,73 | |||
|
1 | #include "donutbreakdownchart.h" | |
|
2 | #include <QPieSlice> | |
|
3 | ||
|
4 | QTCOMMERCIALCHART_USE_NAMESPACE | |
|
5 | ||
|
6 | //![1] | |
|
7 | DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) | |
|
8 | :QChart(parent, wFlags) | |
|
9 | { | |
|
10 | // create the series for main center pie | |
|
11 | mainSeries = new QPieSeries(); | |
|
12 | mainSeries->setPieSize(0.7); | |
|
13 | addSeries(mainSeries); | |
|
14 | } | |
|
15 | //![1] | |
|
16 | ||
|
17 | //![2] | |
|
18 | void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color) | |
|
19 | { | |
|
20 | // add breakdown series as a slice to center pie | |
|
21 | QPieSlice *slice = mainSeries->append(breakdownSeries->name(), breakdownSeries->sum()); | |
|
22 | ||
|
23 | // customize the slice | |
|
24 | slice->setBrush(color); | |
|
25 | slice->setLabelVisible(); | |
|
26 | slice->setLabelColor(Qt::white); | |
|
27 | slice->setLabelPosition(QPieSlice::LabelInsideHorizontal); | |
|
28 | ||
|
29 | // position and customize the breakdown series | |
|
30 | breakdownSeries->setPieSize(0.8); | |
|
31 | breakdownSeries->setHoleSize(0.7); | |
|
32 | breakdownSeries->setLabelsVisible(); | |
|
33 | foreach (QPieSlice *slice, breakdownSeries->slices()) { | |
|
34 | color = color.lighter(110); | |
|
35 | slice->setBrush(color); | |
|
36 | slice->setLabelFont(QFont("Arial", 7)); | |
|
37 | slice->setLabelArmLengthFactor(0.05); | |
|
38 | } | |
|
39 | ||
|
40 | // add the series to the chart | |
|
41 | addSeries(breakdownSeries); | |
|
42 | ||
|
43 | // recalculate breakdown donut segments | |
|
44 | recalculateAngles(); | |
|
45 | } | |
|
46 | //![2] | |
|
47 | ||
|
48 | void DonutBreakdownChart::recalculateAngles() | |
|
49 | { | |
|
50 | qreal angle = 0; | |
|
51 | foreach (QPieSlice *slice, mainSeries->slices()) { | |
|
52 | QPieSeries *s = find(slice->label()); | |
|
53 | if (s) { | |
|
54 | s->setPieStartAngle(angle); | |
|
55 | angle += slice->percentage() * 360.0; | |
|
56 | s->setPieEndAngle(angle); | |
|
57 | } | |
|
58 | } | |
|
59 | } | |
|
60 | ||
|
61 | QPieSeries *DonutBreakdownChart::find(QString seriesName) const | |
|
62 | { | |
|
63 | // find pieseries by name | |
|
64 | foreach (QAbstractSeries *series, this->series()) { | |
|
65 | QPieSeries *s = qobject_cast<QPieSeries*>(series); | |
|
66 | if (!s) | |
|
67 | continue; | |
|
68 | if (s->name() == seriesName) | |
|
69 | return s; | |
|
70 | } | |
|
71 | return 0; | |
|
72 | } | |
|
73 |
@@ -0,0 +1,23 | |||
|
1 | #ifndef DONUTBREAKDOWNCHART_H | |
|
2 | #define DONUTBREAKDOWNCHART_H | |
|
3 | ||
|
4 | #include <QChart> | |
|
5 | #include <QPieSeries> | |
|
6 | ||
|
7 | QTCOMMERCIALCHART_USE_NAMESPACE | |
|
8 | ||
|
9 | class DonutBreakdownChart : public QChart | |
|
10 | { | |
|
11 | public: | |
|
12 | DonutBreakdownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); | |
|
13 | void addBreakdownSeries(QPieSeries *series, QColor color); | |
|
14 | ||
|
15 | private: | |
|
16 | void recalculateAngles(); | |
|
17 | QPieSeries *find(QString seriesName) const; | |
|
18 | ||
|
19 | private: | |
|
20 | QPieSeries *mainSeries; | |
|
21 | }; | |
|
22 | ||
|
23 | #endif // DONUTBREAKDOWNCHART_H |
@@ -4,6 +4,7 | |||
|
4 | 4 | |
|
5 | 5 | TARGET = donutbreakdown |
|
6 | 6 | SOURCES += main.cpp\ |
|
7 | widget.cpp | |
|
7 | donutbreakdownchart.cpp | |
|
8 | 8 | |
|
9 |
HEADERS += |
|
|
9 | HEADERS += \ | |
|
10 | donutbreakdownchart.h |
@@ -1,11 +1,55 | |||
|
1 | 1 | #include <QApplication> |
|
2 | #include "widget.h" | |
|
2 | #include <QMainWindow> | |
|
3 | #include <QChartView> | |
|
4 | #include "donutbreakdownchart.h" | |
|
5 | ||
|
6 | QTCOMMERCIALCHART_USE_NAMESPACE | |
|
3 | 7 | |
|
4 | 8 | int main(int argc, char *argv[]) |
|
5 | 9 | { |
|
6 | 10 | QApplication a(argc, argv); |
|
7 | Widget w; | |
|
8 | w.show(); | |
|
11 | ||
|
12 | // Data from http://www.stat.fi/til/ekul/2010/ekul_2010_2011-12-13_tie_001_en.html | |
|
13 | ||
|
14 | //![1] | |
|
15 | QPieSeries *series1 = new QPieSeries(); | |
|
16 | series1->setName("Fossil"); | |
|
17 | series1->append("Oil", 353295); | |
|
18 | series1->append("Wood", 319663); | |
|
19 | series1->append("Coal", 188500); | |
|
20 | series1->append("Natural gas", 148680); | |
|
21 | series1->append("Peat", 94545); | |
|
22 | ||
|
23 | QPieSeries *series2 = new QPieSeries(); | |
|
24 | series2->setName("Renewable"); | |
|
25 | series2->append("Hydro power", 45875); | |
|
26 | series2->append("Wind power", 1060); | |
|
27 | ||
|
28 | QPieSeries *series3 = new QPieSeries(); | |
|
29 | series3->setName("Other"); | |
|
30 | series3->append("Nuclear energy", 238789); | |
|
31 | series3->append("Import energy", 37802); | |
|
32 | series3->append("Others", 32441); | |
|
33 | //![1] | |
|
34 | ||
|
35 | //![2] | |
|
36 | DonutBreakdownChart *donutBreakdown = new DonutBreakdownChart(); | |
|
37 | donutBreakdown->setAnimationOptions(QChart::AllAnimations); | |
|
38 | donutBreakdown->setTitle("Total consumption of energy in Finland 2010"); | |
|
39 | donutBreakdown->legend()->setVisible(false); | |
|
40 | donutBreakdown->addBreakdownSeries(series1, Qt::red); | |
|
41 | donutBreakdown->addBreakdownSeries(series2, Qt::darkGreen); | |
|
42 | donutBreakdown->addBreakdownSeries(series3, Qt::darkBlue); | |
|
43 | //![2] | |
|
44 | ||
|
45 | //![3] | |
|
46 | QMainWindow window; | |
|
47 | QChartView* chartView = new QChartView(donutBreakdown); | |
|
48 | chartView->setRenderHint(QPainter::Antialiasing); | |
|
49 | window.setCentralWidget(chartView); | |
|
50 | window.resize(800, 600); | |
|
51 | window.show(); | |
|
52 | //![3] | |
|
9 | 53 | |
|
10 | 54 | return a.exec(); |
|
11 | 55 | } |
@@ -46,9 +46,9 int main(int argc, char *argv[]) | |||
|
46 | 46 | chartView->setRenderHint(QPainter::Antialiasing); |
|
47 | 47 | chartView->chart()->setTitle("Donut with a lemon glaze (100g)"); |
|
48 | 48 | chartView->chart()->addSeries(series); |
|
49 |
chartView->chart()->legend()->setAlignment(Qt::Align |
|
|
49 | chartView->chart()->legend()->setAlignment(Qt::AlignLeft); | |
|
50 | 50 | chartView->chart()->setTheme(QChart::ChartThemeBlueCerulean); |
|
51 | chartView->chart()->legend()->setFont(QFont("Arial", 7)); | |
|
51 | //chartView->chart()->legend()->setFont(QFont("Arial", 7)); | |
|
52 | 52 | //![2] |
|
53 | 53 | |
|
54 | 54 | //![3] |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now