@@ -0,0 +1,57 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2012 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | #include "mainslice.h" | |
|
22 | ||
|
23 | QTCOMMERCIALCHART_USE_NAMESPACE | |
|
24 | ||
|
25 | //![1] | |
|
26 | MainSlice::MainSlice(QPieSeries *breakdownSeries, QObject *parent) | |
|
27 | : QPieSlice(parent), | |
|
28 | m_breakdownSeries(breakdownSeries) | |
|
29 | { | |
|
30 | connect(this, SIGNAL(percentageChanged()), this, SLOT(updateLabel())); | |
|
31 | } | |
|
32 | //![1] | |
|
33 | ||
|
34 | QPieSeries *MainSlice::breakdownSeries() const | |
|
35 | { | |
|
36 | return m_breakdownSeries; | |
|
37 | } | |
|
38 | ||
|
39 | void MainSlice::setName(QString name) | |
|
40 | { | |
|
41 | m_name = name; | |
|
42 | } | |
|
43 | ||
|
44 | QString MainSlice::name() const | |
|
45 | { | |
|
46 | return m_name; | |
|
47 | } | |
|
48 | ||
|
49 | //![2] | |
|
50 | void MainSlice::updateLabel() | |
|
51 | { | |
|
52 | this->setLabel(QString("%1 %2%").arg(m_name).arg(percentage() * 100, 0, 'f', 2)); | |
|
53 | } | |
|
54 | //![2] | |
|
55 | ||
|
56 | #include "moc_mainslice.cpp" | |
|
57 |
@@ -0,0 +1,48 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2012 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | #ifndef MAINSLICE_H | |
|
22 | #define MAINSLICE_H | |
|
23 | ||
|
24 | #include <QPieSlice> | |
|
25 | #include <QPieSeries> | |
|
26 | ||
|
27 | QTCOMMERCIALCHART_USE_NAMESPACE | |
|
28 | ||
|
29 | class MainSlice : public QPieSlice | |
|
30 | { | |
|
31 | Q_OBJECT | |
|
32 | public: | |
|
33 | MainSlice(QPieSeries *breakdownSeries, QObject *parent = 0); | |
|
34 | ||
|
35 | QPieSeries *breakdownSeries() const; | |
|
36 | ||
|
37 | void setName(QString name); | |
|
38 | QString name() const; | |
|
39 | ||
|
40 | public Q_SLOTS: | |
|
41 | void updateLabel(); | |
|
42 | ||
|
43 | private: | |
|
44 | QPieSeries *m_breakdownSeries; | |
|
45 | QString m_name; | |
|
46 | }; | |
|
47 | ||
|
48 | #endif // MAINSLICE_H |
@@ -1,36 +1,45 | |||
|
1 | 1 | /*! |
|
2 | 2 | \example examples/donutbreakdown |
|
3 | 3 | \title Donut chart breakdown example |
|
4 | 4 | \subtitle |
|
5 | 5 | |
|
6 | 6 | This example shows how to use create a donut breakdown chart using QPieSeries API. |
|
7 | 7 | \image examples_donutbreakdown.png |
|
8 | 8 | |
|
9 | 9 | Let's start by defining some data for the chart. |
|
10 | 10 | |
|
11 | 11 | \snippet ../examples/donutbreakdown/main.cpp 1 |
|
12 | 12 | |
|
13 | 13 | Then we create a chart where we add the data. Note that this is our own chart derived from QChart. |
|
14 | 14 | |
|
15 | 15 | \snippet ../examples/donutbreakdown/main.cpp 2 |
|
16 | 16 | |
|
17 | 17 | Our own chart works so that in the constructor we create a main series |
|
18 | 18 | which aggregates the data provided by the breakdown series. This is the piechart in the center. |
|
19 | 19 | |
|
20 | 20 | \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 1 |
|
21 | 21 | |
|
22 | 22 | When a breakdown series is added the data is used to create a slice in the main series and the |
|
23 | 23 | breakdown series itself is used to create a segment of a donut positioned so that it is aligned |
|
24 | 24 | with the corresponding slice in the main series. |
|
25 | 25 | |
|
26 | 26 | \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 2 |
|
27 | 27 | |
|
28 | 28 | Here's how the start and end angles for the donut segments are calculated. |
|
29 | 29 | |
|
30 | 30 | \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 3 |
|
31 | 31 | |
|
32 | Legend markers are customized to show the breakdown percentage and markers for the main level slices are hidden. | |
|
33 | ||
|
34 | \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 4 | |
|
35 | ||
|
36 | Instead the main level slices show the percentage on the label. | |
|
37 | ||
|
38 | \snippet ../examples/donutbreakdown/mainslice.cpp 1 | |
|
39 | \snippet ../examples/donutbreakdown/mainslice.cpp 2 | |
|
40 | ||
|
32 | 41 | And now that we have our chart defined we can finally create a QChartView and show the chart. |
|
33 | 42 | |
|
34 | 43 | \snippet ../examples/donutbreakdown/main.cpp 3 |
|
35 | 44 | |
|
36 | 45 | */ |
@@ -1,10 +1,12 | |||
|
1 | 1 | !include( ../examples.pri ) { |
|
2 | 2 | error( "Couldn't find the examples.pri file!" ) |
|
3 | 3 | } |
|
4 | 4 | |
|
5 | 5 | TARGET = donutbreakdown |
|
6 | 6 | SOURCES += main.cpp\ |
|
7 | donutbreakdownchart.cpp | |
|
7 | donutbreakdownchart.cpp \ | |
|
8 | mainslice.cpp | |
|
8 | 9 | |
|
9 | 10 | HEADERS += \ |
|
10 | donutbreakdownchart.h | |
|
11 | donutbreakdownchart.h \ | |
|
12 | mainslice.h |
@@ -1,94 +1,110 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | #include "donutbreakdownchart.h" |
|
21 | #include "mainslice.h" | |
|
21 | 22 | #include <QPieSlice> |
|
23 | #include <QPieLegendMarker> | |
|
22 | 24 | |
|
23 | 25 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
24 | 26 | |
|
25 | 27 | //![1] |
|
26 | 28 | DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) |
|
27 | 29 | : QChart(parent, wFlags) |
|
28 | 30 | { |
|
29 | 31 | // create the series for main center pie |
|
30 | mainSeries = new QPieSeries(); | |
|
31 | mainSeries->setPieSize(0.7); | |
|
32 | QChart::addSeries(mainSeries); | |
|
32 | m_mainSeries = new QPieSeries(); | |
|
33 | m_mainSeries->setPieSize(0.7); | |
|
34 | QChart::addSeries(m_mainSeries); | |
|
33 | 35 | } |
|
34 | 36 | //![1] |
|
35 | 37 | |
|
36 | 38 | //![2] |
|
37 | 39 | void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color) |
|
38 | 40 | { |
|
41 | QFont font("Arial", 8); | |
|
42 | ||
|
39 | 43 | // add breakdown series as a slice to center pie |
|
40 | QPieSlice *slice = mainSeries->append(breakdownSeries->name(), breakdownSeries->sum()); | |
|
44 | MainSlice *mainSlice = new MainSlice(breakdownSeries); | |
|
45 | mainSlice->setName(breakdownSeries->name()); | |
|
46 | mainSlice->setValue(breakdownSeries->sum()); | |
|
47 | m_mainSeries->append(mainSlice); | |
|
41 | 48 | |
|
42 | 49 | // customize the slice |
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
|
50 | mainSlice->setBrush(color); | |
|
51 | mainSlice->setLabelVisible(); | |
|
52 | mainSlice->setLabelColor(Qt::white); | |
|
53 | mainSlice->setLabelPosition(QPieSlice::LabelInsideHorizontal); | |
|
54 | mainSlice->setLabelFont(font); | |
|
47 | 55 | |
|
48 | 56 | // position and customize the breakdown series |
|
49 | 57 | breakdownSeries->setPieSize(0.8); |
|
50 | 58 | breakdownSeries->setHoleSize(0.7); |
|
51 | 59 | breakdownSeries->setLabelsVisible(); |
|
52 | 60 | foreach (QPieSlice *slice, breakdownSeries->slices()) { |
|
53 | 61 | color = color.lighter(115); |
|
54 | 62 | slice->setBrush(color); |
|
55 |
slice->setLabelFont( |
|
|
63 | slice->setLabelFont(font); | |
|
56 | 64 | } |
|
57 | 65 | |
|
58 | 66 | // add the series to the chart |
|
59 | 67 | QChart::addSeries(breakdownSeries); |
|
60 | 68 | |
|
61 | 69 | // recalculate breakdown donut segments |
|
62 | 70 | recalculateAngles(); |
|
71 | ||
|
72 | // update customize legend markers | |
|
73 | updateLegendMarkers(); | |
|
63 | 74 | } |
|
64 | 75 | //![2] |
|
65 | 76 | |
|
66 | 77 | //![3] |
|
67 | 78 | void DonutBreakdownChart::recalculateAngles() |
|
68 | 79 | { |
|
69 | 80 | qreal angle = 0; |
|
70 | foreach (QPieSlice *slice, mainSeries->slices()) { | |
|
71 | QPieSeries *s = find(slice->label()); | |
|
72 | if (s) { | |
|
73 | s->setPieStartAngle(angle); | |
|
74 | angle += slice->percentage() * 360.0; // full pie is 360.0 | |
|
75 | s->setPieEndAngle(angle); | |
|
76 | } | |
|
81 | foreach (QPieSlice *slice, m_mainSeries->slices()) { | |
|
82 | QPieSeries *breakdownSeries = qobject_cast<MainSlice *>(slice)->breakdownSeries(); | |
|
83 | breakdownSeries->setPieStartAngle(angle); | |
|
84 | angle += slice->percentage() * 360.0; // full pie is 360.0 | |
|
85 | breakdownSeries->setPieEndAngle(angle); | |
|
77 | 86 | } |
|
78 | 87 | } |
|
79 | 88 | //![3] |
|
80 | 89 | |
|
81 | 90 | //![4] |
|
82 | QPieSeries *DonutBreakdownChart::find(QString seriesName) const | |
|
91 | void DonutBreakdownChart::updateLegendMarkers() | |
|
83 | 92 | { |
|
84 | // find pieseries by name | |
|
93 | // go through all markers | |
|
85 | 94 | foreach (QAbstractSeries *series, QChart::series()) { |
|
86 | QPieSeries *s = qobject_cast<QPieSeries*>(series); | |
|
87 | if (!s) | |
|
88 | continue; | |
|
89 | if (s->name() == seriesName) | |
|
90 | return s; | |
|
95 | foreach (QLegendMarker *marker, legend()->markers(series)) { | |
|
96 | QPieLegendMarker *pieMarker = qobject_cast<QPieLegendMarker *>(marker); | |
|
97 | if (series == m_mainSeries) { | |
|
98 | // hide markers from main series | |
|
99 | pieMarker->setVisible(false); | |
|
100 | } else { | |
|
101 | // modify markers from breakdown series | |
|
102 | pieMarker->setLabel(QString("%1 %2%") | |
|
103 | .arg(pieMarker->slice()->label()) | |
|
104 | .arg(pieMarker->slice()->percentage() * 100, 0, 'f', 2)); | |
|
105 | pieMarker->setFont(QFont("Arial", 8)); | |
|
106 | } | |
|
107 | } | |
|
91 | 108 | } |
|
92 | return 0; | |
|
93 | 109 | } |
|
94 | 110 | //![4] |
@@ -1,42 +1,42 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | #ifndef DONUTBREAKDOWNCHART_H |
|
21 | 21 | #define DONUTBREAKDOWNCHART_H |
|
22 | 22 | |
|
23 | 23 | #include <QChart> |
|
24 | 24 | #include <QPieSeries> |
|
25 | 25 | |
|
26 | 26 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
27 | 27 | |
|
28 | 28 | class DonutBreakdownChart : public QChart |
|
29 | 29 | { |
|
30 | 30 | public: |
|
31 | 31 | DonutBreakdownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); |
|
32 | 32 | void addBreakdownSeries(QPieSeries *series, QColor color); |
|
33 | 33 | |
|
34 | 34 | private: |
|
35 | 35 | void recalculateAngles(); |
|
36 | QPieSeries *find(QString seriesName) const; | |
|
36 | void updateLegendMarkers(); | |
|
37 | 37 | |
|
38 | 38 | private: |
|
39 | QPieSeries *mainSeries; | |
|
39 | QPieSeries *m_mainSeries; | |
|
40 | 40 | }; |
|
41 | 41 | |
|
42 | 42 | #endif // DONUTBREAKDOWNCHART_H |
@@ -1,77 +1,77 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | #include <QApplication> |
|
21 | 21 | #include <QMainWindow> |
|
22 | 22 | #include <QStatusBar> |
|
23 | 23 | #include <QChartView> |
|
24 | 24 | #include "donutbreakdownchart.h" |
|
25 | 25 | |
|
26 | 26 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
27 | 27 | |
|
28 | 28 | int main(int argc, char *argv[]) |
|
29 | 29 | { |
|
30 | 30 | QApplication a(argc, argv); |
|
31 | 31 | |
|
32 | 32 | //![1] |
|
33 | 33 | // Graph is based on data of 'Total consumption of energy increased by 10 per cent in 2010' |
|
34 | 34 | // Statistics Finland, 13 December 2011 |
|
35 | 35 | // http://www.stat.fi/til/ekul/2010/ekul_2010_2011-12-13_tie_001_en.html |
|
36 | 36 | |
|
37 | 37 | QPieSeries *series1 = new QPieSeries(); |
|
38 | 38 | series1->setName("Fossil fuels"); |
|
39 | 39 | series1->append("Oil", 353295); |
|
40 | 40 | series1->append("Coal", 188500); |
|
41 | 41 | series1->append("Natural gas", 148680); |
|
42 | 42 | series1->append("Peat", 94545); |
|
43 | 43 | |
|
44 | 44 | QPieSeries *series2 = new QPieSeries(); |
|
45 | 45 | series2->setName("Renewables"); |
|
46 | 46 | series2->append("Wood fuels", 319663); |
|
47 | 47 | series2->append("Hydro power", 45875); |
|
48 | 48 | series2->append("Wind power", 1060); |
|
49 | 49 | |
|
50 | 50 | QPieSeries *series3 = new QPieSeries(); |
|
51 | 51 | series3->setName("Others"); |
|
52 | 52 | series3->append("Nuclear energy", 238789); |
|
53 | 53 | series3->append("Import energy", 37802); |
|
54 | 54 | series3->append("Other", 32441); |
|
55 | 55 | //![1] |
|
56 | 56 | |
|
57 | 57 | //![2] |
|
58 | 58 | DonutBreakdownChart *donutBreakdown = new DonutBreakdownChart(); |
|
59 | 59 | donutBreakdown->setAnimationOptions(QChart::AllAnimations); |
|
60 | 60 | donutBreakdown->setTitle("Total consumption of energy in Finland 2010"); |
|
61 |
donutBreakdown->legend()->set |
|
|
61 | donutBreakdown->legend()->setAlignment(Qt::AlignRight); | |
|
62 | 62 | donutBreakdown->addBreakdownSeries(series1, Qt::red); |
|
63 | 63 | donutBreakdown->addBreakdownSeries(series2, Qt::darkGreen); |
|
64 | 64 | donutBreakdown->addBreakdownSeries(series3, Qt::darkBlue); |
|
65 | 65 | //![2] |
|
66 | 66 | |
|
67 | 67 | //![3] |
|
68 | 68 | QMainWindow window; |
|
69 | 69 | QChartView *chartView = new QChartView(donutBreakdown); |
|
70 | 70 | chartView->setRenderHint(QPainter::Antialiasing); |
|
71 | 71 | window.setCentralWidget(chartView); |
|
72 |
window.resize(800, |
|
|
72 | window.resize(800, 500); | |
|
73 | 73 | window.show(); |
|
74 | 74 | //![3] |
|
75 | 75 | |
|
76 | 76 | return a.exec(); |
|
77 | 77 | } |
General Comments 0
You need to be logged in to leave comments.
Login now