@@ -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 |
@@ -29,6 +29,15 | |||
|
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 |
@@ -4,7 +4,9 | |||
|
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 |
@@ -18,7 +18,9 | |||
|
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 | |
@@ -27,23 +29,29 DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags | |||
|
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); |
@@ -52,7 +60,7 void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor | |||
|
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 |
@@ -60,6 +68,9 void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor | |||
|
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 | |
@@ -67,28 +78,33 void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor | |||
|
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] |
@@ -33,10 +33,10 public: | |||
|
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 |
@@ -58,7 +58,7 int main(int argc, char *argv[]) | |||
|
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); |
@@ -69,7 +69,7 int main(int argc, char *argv[]) | |||
|
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 |
General Comments 0
You need to be logged in to leave comments.
Login now