@@ -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 |
@@ -1,9 +1,10 | |||||
1 | !include( ../examples.pri ) { |
|
1 | !include( ../examples.pri ) { | |
2 | error( "Couldn't find the examples.pri file!" ) |
|
2 | error( "Couldn't find the examples.pri file!" ) | |
3 | } |
|
3 | } | |
4 |
|
4 | |||
5 | TARGET = donutbreakdown |
|
5 | TARGET = donutbreakdown | |
6 | SOURCES += main.cpp\ |
|
6 | SOURCES += main.cpp\ | |
7 | widget.cpp |
|
7 | donutbreakdownchart.cpp | |
8 |
|
8 | |||
9 |
HEADERS += |
|
9 | HEADERS += \ | |
|
10 | donutbreakdownchart.h |
@@ -1,11 +1,55 | |||||
1 | #include <QApplication> |
|
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 | int main(int argc, char *argv[]) |
|
8 | int main(int argc, char *argv[]) | |
5 | { |
|
9 | { | |
6 | QApplication a(argc, argv); |
|
10 | QApplication a(argc, argv); | |
7 | Widget w; |
|
11 | ||
8 | w.show(); |
|
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 | return a.exec(); |
|
54 | return a.exec(); | |
11 | } |
|
55 | } |
@@ -1,62 +1,62 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | #include <QApplication> |
|
21 | #include <QApplication> | |
22 | #include <QMainWindow> |
|
22 | #include <QMainWindow> | |
23 | #include <QChartView> |
|
23 | #include <QChartView> | |
24 | #include <QPieSeries> |
|
24 | #include <QPieSeries> | |
25 | #include <QPieSlice> |
|
25 | #include <QPieSlice> | |
26 | #include <QDebug> |
|
26 | #include <QDebug> | |
27 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
27 | QTCOMMERCIALCHART_USE_NAMESPACE | |
28 |
|
28 | |||
29 | int main(int argc, char *argv[]) |
|
29 | int main(int argc, char *argv[]) | |
30 | { |
|
30 | { | |
31 | QApplication a(argc, argv); |
|
31 | QApplication a(argc, argv); | |
32 |
|
32 | |||
33 | //![1] |
|
33 | //![1] | |
34 | QPieSeries *series = new QPieSeries(); |
|
34 | QPieSeries *series = new QPieSeries(); | |
35 | series->setHoleSize(0.35); |
|
35 | series->setHoleSize(0.35); | |
36 | series->append("Protein 4.2%", 4.2); |
|
36 | series->append("Protein 4.2%", 4.2); | |
37 | QPieSlice *slice = series->append("Fat 15.6%", 15.6); |
|
37 | QPieSlice *slice = series->append("Fat 15.6%", 15.6); | |
38 | slice->setExploded(); |
|
38 | slice->setExploded(); | |
39 | slice->setLabelVisible(); |
|
39 | slice->setLabelVisible(); | |
40 | series->append("Other 23.8%", 23.8); |
|
40 | series->append("Other 23.8%", 23.8); | |
41 | series->append("Carbs 56.4%", 56.4); |
|
41 | series->append("Carbs 56.4%", 56.4); | |
42 | //![1] |
|
42 | //![1] | |
43 |
|
43 | |||
44 | //![2] |
|
44 | //![2] | |
45 | QChartView* chartView = new QChartView(); |
|
45 | QChartView* chartView = new QChartView(); | |
46 | chartView->setRenderHint(QPainter::Antialiasing); |
|
46 | chartView->setRenderHint(QPainter::Antialiasing); | |
47 | chartView->chart()->setTitle("Donut with a lemon glaze (100g)"); |
|
47 | chartView->chart()->setTitle("Donut with a lemon glaze (100g)"); | |
48 | chartView->chart()->addSeries(series); |
|
48 | chartView->chart()->addSeries(series); | |
49 |
chartView->chart()->legend()->setAlignment(Qt::Align |
|
49 | chartView->chart()->legend()->setAlignment(Qt::AlignLeft); | |
50 | chartView->chart()->setTheme(QChart::ChartThemeBlueCerulean); |
|
50 | chartView->chart()->setTheme(QChart::ChartThemeBlueCerulean); | |
51 | chartView->chart()->legend()->setFont(QFont("Arial", 7)); |
|
51 | //chartView->chart()->legend()->setFont(QFont("Arial", 7)); | |
52 | //![2] |
|
52 | //![2] | |
53 |
|
53 | |||
54 | //![3] |
|
54 | //![3] | |
55 | QMainWindow window; |
|
55 | QMainWindow window; | |
56 | window.setCentralWidget(chartView); |
|
56 | window.setCentralWidget(chartView); | |
57 | window.resize(400, 300); |
|
57 | window.resize(400, 300); | |
58 | window.show(); |
|
58 | window.show(); | |
59 | //![3] |
|
59 | //![3] | |
60 |
|
60 | |||
61 | return a.exec(); |
|
61 | return a.exec(); | |
62 | } |
|
62 | } |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now