|
@@
-1,76
+1,76
|
|
1
|
#include "donutbreakdownchart.h"
|
|
1
|
#include "donutbreakdownchart.h"
|
|
2
|
#include <QPieSlice>
|
|
2
|
#include <QPieSlice>
|
|
3
|
|
|
3
|
|
|
4
|
QTCOMMERCIALCHART_USE_NAMESPACE
|
|
4
|
QTCOMMERCIALCHART_USE_NAMESPACE
|
|
5
|
|
|
5
|
|
|
6
|
//![1]
|
|
6
|
//![1]
|
|
7
|
DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
|
|
7
|
DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
|
|
8
|
:QChart(parent, wFlags)
|
|
8
|
:QChart(parent, wFlags)
|
|
9
|
{
|
|
9
|
{
|
|
10
|
// create the series for main center pie
|
|
10
|
// create the series for main center pie
|
|
11
|
mainSeries = new QPieSeries();
|
|
11
|
mainSeries = new QPieSeries();
|
|
12
|
mainSeries->setPieSize(0.7);
|
|
12
|
mainSeries->setPieSize(0.7);
|
|
13
|
QChart::addSeries(mainSeries);
|
|
13
|
QChart::addSeries(mainSeries);
|
|
14
|
}
|
|
14
|
}
|
|
15
|
//![1]
|
|
15
|
//![1]
|
|
16
|
|
|
16
|
|
|
17
|
//![2]
|
|
17
|
//![2]
|
|
18
|
void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color)
|
|
18
|
void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color)
|
|
19
|
{
|
|
19
|
{
|
|
20
|
// add breakdown series as a slice to center pie
|
|
20
|
// add breakdown series as a slice to center pie
|
|
21
|
QPieSlice *slice = mainSeries->append(breakdownSeries->name(), breakdownSeries->sum());
|
|
21
|
QPieSlice *slice = mainSeries->append(breakdownSeries->name(), breakdownSeries->sum());
|
|
22
|
|
|
22
|
|
|
23
|
// customize the slice
|
|
23
|
// customize the slice
|
|
24
|
slice->setBrush(color);
|
|
24
|
slice->setBrush(color);
|
|
25
|
slice->setLabelVisible();
|
|
25
|
slice->setLabelVisible();
|
|
26
|
slice->setLabelColor(Qt::white);
|
|
26
|
slice->setLabelColor(Qt::white);
|
|
27
|
slice->setLabelPosition(QPieSlice::LabelInsideHorizontal);
|
|
27
|
slice->setLabelPosition(QPieSlice::LabelInsideHorizontal);
|
|
28
|
|
|
28
|
|
|
29
|
// position and customize the breakdown series
|
|
29
|
// position and customize the breakdown series
|
|
30
|
breakdownSeries->setPieSize(0.8);
|
|
30
|
breakdownSeries->setPieSize(0.8);
|
|
31
|
breakdownSeries->setHoleSize(0.7);
|
|
31
|
breakdownSeries->setHoleSize(0.7);
|
|
32
|
breakdownSeries->setLabelsVisible();
|
|
32
|
breakdownSeries->setLabelsVisible();
|
|
33
|
foreach (QPieSlice *slice, breakdownSeries->slices()) {
|
|
33
|
foreach (QPieSlice *slice, breakdownSeries->slices()) {
|
|
34
|
color = color.lighter(110);
|
|
34
|
color = color.lighter(110);
|
|
35
|
slice->setBrush(color);
|
|
35
|
slice->setBrush(color);
|
|
36
|
slice->setLabelFont(QFont("Arial", 7));
|
|
36
|
slice->setLabelFont(QFont("Arial", 8));
|
|
37
|
slice->setLabelArmLengthFactor(0.05);
|
|
37
|
slice->setLabelArmLengthFactor(0.05);
|
|
38
|
}
|
|
38
|
}
|
|
39
|
|
|
39
|
|
|
40
|
// add the series to the chart
|
|
40
|
// add the series to the chart
|
|
41
|
QChart::addSeries(breakdownSeries);
|
|
41
|
QChart::addSeries(breakdownSeries);
|
|
42
|
|
|
42
|
|
|
43
|
// recalculate breakdown donut segments
|
|
43
|
// recalculate breakdown donut segments
|
|
44
|
recalculateAngles();
|
|
44
|
recalculateAngles();
|
|
45
|
}
|
|
45
|
}
|
|
46
|
//![2]
|
|
46
|
//![2]
|
|
47
|
|
|
47
|
|
|
48
|
//![3]
|
|
48
|
//![3]
|
|
49
|
void DonutBreakdownChart::recalculateAngles()
|
|
49
|
void DonutBreakdownChart::recalculateAngles()
|
|
50
|
{
|
|
50
|
{
|
|
51
|
qreal angle = 0;
|
|
51
|
qreal angle = 0;
|
|
52
|
foreach (QPieSlice *slice, mainSeries->slices()) {
|
|
52
|
foreach (QPieSlice *slice, mainSeries->slices()) {
|
|
53
|
QPieSeries *s = find(slice->label());
|
|
53
|
QPieSeries *s = find(slice->label());
|
|
54
|
if (s) {
|
|
54
|
if (s) {
|
|
55
|
s->setPieStartAngle(angle);
|
|
55
|
s->setPieStartAngle(angle);
|
|
56
|
angle += slice->percentage() * 360.0; // full pie is 360.0
|
|
56
|
angle += slice->percentage() * 360.0; // full pie is 360.0
|
|
57
|
s->setPieEndAngle(angle);
|
|
57
|
s->setPieEndAngle(angle);
|
|
58
|
}
|
|
58
|
}
|
|
59
|
}
|
|
59
|
}
|
|
60
|
}
|
|
60
|
}
|
|
61
|
//![3]
|
|
61
|
//![3]
|
|
62
|
|
|
62
|
|
|
63
|
//![4]
|
|
63
|
//![4]
|
|
64
|
QPieSeries *DonutBreakdownChart::find(QString seriesName) const
|
|
64
|
QPieSeries *DonutBreakdownChart::find(QString seriesName) const
|
|
65
|
{
|
|
65
|
{
|
|
66
|
// find pieseries by name
|
|
66
|
// find pieseries by name
|
|
67
|
foreach (QAbstractSeries *series, QChart::series()) {
|
|
67
|
foreach (QAbstractSeries *series, QChart::series()) {
|
|
68
|
QPieSeries *s = qobject_cast<QPieSeries*>(series);
|
|
68
|
QPieSeries *s = qobject_cast<QPieSeries*>(series);
|
|
69
|
if (!s)
|
|
69
|
if (!s)
|
|
70
|
continue;
|
|
70
|
continue;
|
|
71
|
if (s->name() == seriesName)
|
|
71
|
if (s->name() == seriesName)
|
|
72
|
return s;
|
|
72
|
return s;
|
|
73
|
}
|
|
73
|
}
|
|
74
|
return 0;
|
|
74
|
return 0;
|
|
75
|
}
|
|
75
|
}
|
|
76
|
//![4]
|
|
76
|
//![4]
|