##// END OF EJS Templates
donutbreakdown example now has customized legendmarkers
Jani Honkonen -
r2233:222c26d04db8
parent child
Show More
@@ -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 \example examples/donutbreakdown
2 \example examples/donutbreakdown
3 \title Donut chart breakdown example
3 \title Donut chart breakdown example
4 \subtitle
4 \subtitle
5
5
6 This example shows how to use create a donut breakdown chart using QPieSeries API.
6 This example shows how to use create a donut breakdown chart using QPieSeries API.
7 \image examples_donutbreakdown.png
7 \image examples_donutbreakdown.png
8
8
9 Let's start by defining some data for the chart.
9 Let's start by defining some data for the chart.
10
10
11 \snippet ../examples/donutbreakdown/main.cpp 1
11 \snippet ../examples/donutbreakdown/main.cpp 1
12
12
13 Then we create a chart where we add the data. Note that this is our own chart derived from QChart.
13 Then we create a chart where we add the data. Note that this is our own chart derived from QChart.
14
14
15 \snippet ../examples/donutbreakdown/main.cpp 2
15 \snippet ../examples/donutbreakdown/main.cpp 2
16
16
17 Our own chart works so that in the constructor we create a main series
17 Our own chart works so that in the constructor we create a main series
18 which aggregates the data provided by the breakdown series. This is the piechart in the center.
18 which aggregates the data provided by the breakdown series. This is the piechart in the center.
19
19
20 \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 1
20 \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 1
21
21
22 When a breakdown series is added the data is used to create a slice in the main series and the
22 When a breakdown series is added the data is used to create a slice in the main series and the
23 breakdown series itself is used to create a segment of a donut positioned so that it is aligned
23 breakdown series itself is used to create a segment of a donut positioned so that it is aligned
24 with the corresponding slice in the main series.
24 with the corresponding slice in the main series.
25
25
26 \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 2
26 \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 2
27
27
28 Here's how the start and end angles for the donut segments are calculated.
28 Here's how the start and end angles for the donut segments are calculated.
29
29
30 \snippet ../examples/donutbreakdown/donutbreakdownchart.cpp 3
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 And now that we have our chart defined we can finally create a QChartView and show the chart.
41 And now that we have our chart defined we can finally create a QChartView and show the chart.
33
42
34 \snippet ../examples/donutbreakdown/main.cpp 3
43 \snippet ../examples/donutbreakdown/main.cpp 3
35
44
36 */
45 */
@@ -1,10 +1,12
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 donutbreakdownchart.cpp
7 donutbreakdownchart.cpp \
8 mainslice.cpp
8
9
9 HEADERS += \
10 HEADERS += \
10 donutbreakdownchart.h
11 donutbreakdownchart.h \
12 mainslice.h
@@ -1,94 +1,110
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 #include "donutbreakdownchart.h"
20 #include "donutbreakdownchart.h"
21 #include "mainslice.h"
21 #include <QPieSlice>
22 #include <QPieSlice>
23 #include <QPieLegendMarker>
22
24
23 QTCOMMERCIALCHART_USE_NAMESPACE
25 QTCOMMERCIALCHART_USE_NAMESPACE
24
26
25 //![1]
27 //![1]
26 DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
28 DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
27 : QChart(parent, wFlags)
29 : QChart(parent, wFlags)
28 {
30 {
29 // create the series for main center pie
31 // create the series for main center pie
30 mainSeries = new QPieSeries();
32 m_mainSeries = new QPieSeries();
31 mainSeries->setPieSize(0.7);
33 m_mainSeries->setPieSize(0.7);
32 QChart::addSeries(mainSeries);
34 QChart::addSeries(m_mainSeries);
33 }
35 }
34 //![1]
36 //![1]
35
37
36 //![2]
38 //![2]
37 void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color)
39 void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color)
38 {
40 {
41 QFont font("Arial", 8);
42
39 // add breakdown series as a slice to center pie
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 // customize the slice
49 // customize the slice
43 slice->setBrush(color);
50 mainSlice->setBrush(color);
44 slice->setLabelVisible();
51 mainSlice->setLabelVisible();
45 slice->setLabelColor(Qt::white);
52 mainSlice->setLabelColor(Qt::white);
46 slice->setLabelPosition(QPieSlice::LabelInsideHorizontal);
53 mainSlice->setLabelPosition(QPieSlice::LabelInsideHorizontal);
54 mainSlice->setLabelFont(font);
47
55
48 // position and customize the breakdown series
56 // position and customize the breakdown series
49 breakdownSeries->setPieSize(0.8);
57 breakdownSeries->setPieSize(0.8);
50 breakdownSeries->setHoleSize(0.7);
58 breakdownSeries->setHoleSize(0.7);
51 breakdownSeries->setLabelsVisible();
59 breakdownSeries->setLabelsVisible();
52 foreach (QPieSlice *slice, breakdownSeries->slices()) {
60 foreach (QPieSlice *slice, breakdownSeries->slices()) {
53 color = color.lighter(115);
61 color = color.lighter(115);
54 slice->setBrush(color);
62 slice->setBrush(color);
55 slice->setLabelFont(QFont("Arial", 8));
63 slice->setLabelFont(font);
56 }
64 }
57
65
58 // add the series to the chart
66 // add the series to the chart
59 QChart::addSeries(breakdownSeries);
67 QChart::addSeries(breakdownSeries);
60
68
61 // recalculate breakdown donut segments
69 // recalculate breakdown donut segments
62 recalculateAngles();
70 recalculateAngles();
71
72 // update customize legend markers
73 updateLegendMarkers();
63 }
74 }
64 //![2]
75 //![2]
65
76
66 //![3]
77 //![3]
67 void DonutBreakdownChart::recalculateAngles()
78 void DonutBreakdownChart::recalculateAngles()
68 {
79 {
69 qreal angle = 0;
80 qreal angle = 0;
70 foreach (QPieSlice *slice, mainSeries->slices()) {
81 foreach (QPieSlice *slice, m_mainSeries->slices()) {
71 QPieSeries *s = find(slice->label());
82 QPieSeries *breakdownSeries = qobject_cast<MainSlice *>(slice)->breakdownSeries();
72 if (s) {
83 breakdownSeries->setPieStartAngle(angle);
73 s->setPieStartAngle(angle);
84 angle += slice->percentage() * 360.0; // full pie is 360.0
74 angle += slice->percentage() * 360.0; // full pie is 360.0
85 breakdownSeries->setPieEndAngle(angle);
75 s->setPieEndAngle(angle);
76 }
77 }
86 }
78 }
87 }
79 //![3]
88 //![3]
80
89
81 //![4]
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 foreach (QAbstractSeries *series, QChart::series()) {
94 foreach (QAbstractSeries *series, QChart::series()) {
86 QPieSeries *s = qobject_cast<QPieSeries*>(series);
95 foreach (QLegendMarker *marker, legend()->markers(series)) {
87 if (!s)
96 QPieLegendMarker *pieMarker = qobject_cast<QPieLegendMarker *>(marker);
88 continue;
97 if (series == m_mainSeries) {
89 if (s->name() == seriesName)
98 // hide markers from main series
90 return s;
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 //![4]
110 //![4]
@@ -1,42 +1,42
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 #ifndef DONUTBREAKDOWNCHART_H
20 #ifndef DONUTBREAKDOWNCHART_H
21 #define DONUTBREAKDOWNCHART_H
21 #define DONUTBREAKDOWNCHART_H
22
22
23 #include <QChart>
23 #include <QChart>
24 #include <QPieSeries>
24 #include <QPieSeries>
25
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
27
28 class DonutBreakdownChart : public QChart
28 class DonutBreakdownChart : public QChart
29 {
29 {
30 public:
30 public:
31 DonutBreakdownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
31 DonutBreakdownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
32 void addBreakdownSeries(QPieSeries *series, QColor color);
32 void addBreakdownSeries(QPieSeries *series, QColor color);
33
33
34 private:
34 private:
35 void recalculateAngles();
35 void recalculateAngles();
36 QPieSeries *find(QString seriesName) const;
36 void updateLegendMarkers();
37
37
38 private:
38 private:
39 QPieSeries *mainSeries;
39 QPieSeries *m_mainSeries;
40 };
40 };
41
41
42 #endif // DONUTBREAKDOWNCHART_H
42 #endif // DONUTBREAKDOWNCHART_H
@@ -1,77 +1,77
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 #include <QApplication>
20 #include <QApplication>
21 #include <QMainWindow>
21 #include <QMainWindow>
22 #include <QStatusBar>
22 #include <QStatusBar>
23 #include <QChartView>
23 #include <QChartView>
24 #include "donutbreakdownchart.h"
24 #include "donutbreakdownchart.h"
25
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
27
28 int main(int argc, char *argv[])
28 int main(int argc, char *argv[])
29 {
29 {
30 QApplication a(argc, argv);
30 QApplication a(argc, argv);
31
31
32 //![1]
32 //![1]
33 // Graph is based on data of 'Total consumption of energy increased by 10 per cent in 2010'
33 // Graph is based on data of 'Total consumption of energy increased by 10 per cent in 2010'
34 // Statistics Finland, 13 December 2011
34 // Statistics Finland, 13 December 2011
35 // http://www.stat.fi/til/ekul/2010/ekul_2010_2011-12-13_tie_001_en.html
35 // http://www.stat.fi/til/ekul/2010/ekul_2010_2011-12-13_tie_001_en.html
36
36
37 QPieSeries *series1 = new QPieSeries();
37 QPieSeries *series1 = new QPieSeries();
38 series1->setName("Fossil fuels");
38 series1->setName("Fossil fuels");
39 series1->append("Oil", 353295);
39 series1->append("Oil", 353295);
40 series1->append("Coal", 188500);
40 series1->append("Coal", 188500);
41 series1->append("Natural gas", 148680);
41 series1->append("Natural gas", 148680);
42 series1->append("Peat", 94545);
42 series1->append("Peat", 94545);
43
43
44 QPieSeries *series2 = new QPieSeries();
44 QPieSeries *series2 = new QPieSeries();
45 series2->setName("Renewables");
45 series2->setName("Renewables");
46 series2->append("Wood fuels", 319663);
46 series2->append("Wood fuels", 319663);
47 series2->append("Hydro power", 45875);
47 series2->append("Hydro power", 45875);
48 series2->append("Wind power", 1060);
48 series2->append("Wind power", 1060);
49
49
50 QPieSeries *series3 = new QPieSeries();
50 QPieSeries *series3 = new QPieSeries();
51 series3->setName("Others");
51 series3->setName("Others");
52 series3->append("Nuclear energy", 238789);
52 series3->append("Nuclear energy", 238789);
53 series3->append("Import energy", 37802);
53 series3->append("Import energy", 37802);
54 series3->append("Other", 32441);
54 series3->append("Other", 32441);
55 //![1]
55 //![1]
56
56
57 //![2]
57 //![2]
58 DonutBreakdownChart *donutBreakdown = new DonutBreakdownChart();
58 DonutBreakdownChart *donutBreakdown = new DonutBreakdownChart();
59 donutBreakdown->setAnimationOptions(QChart::AllAnimations);
59 donutBreakdown->setAnimationOptions(QChart::AllAnimations);
60 donutBreakdown->setTitle("Total consumption of energy in Finland 2010");
60 donutBreakdown->setTitle("Total consumption of energy in Finland 2010");
61 donutBreakdown->legend()->setVisible(false);
61 donutBreakdown->legend()->setAlignment(Qt::AlignRight);
62 donutBreakdown->addBreakdownSeries(series1, Qt::red);
62 donutBreakdown->addBreakdownSeries(series1, Qt::red);
63 donutBreakdown->addBreakdownSeries(series2, Qt::darkGreen);
63 donutBreakdown->addBreakdownSeries(series2, Qt::darkGreen);
64 donutBreakdown->addBreakdownSeries(series3, Qt::darkBlue);
64 donutBreakdown->addBreakdownSeries(series3, Qt::darkBlue);
65 //![2]
65 //![2]
66
66
67 //![3]
67 //![3]
68 QMainWindow window;
68 QMainWindow window;
69 QChartView *chartView = new QChartView(donutBreakdown);
69 QChartView *chartView = new QChartView(donutBreakdown);
70 chartView->setRenderHint(QPainter::Antialiasing);
70 chartView->setRenderHint(QPainter::Antialiasing);
71 window.setCentralWidget(chartView);
71 window.setCentralWidget(chartView);
72 window.resize(800, 600);
72 window.resize(800, 500);
73 window.show();
73 window.show();
74 //![3]
74 //![3]
75
75
76 return a.exec();
76 return a.exec();
77 }
77 }
General Comments 0
You need to be logged in to leave comments. Login now