1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Charts module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29#include "donutbreakdownchart.h"
30#include "mainslice.h"
31#include <QtCharts/QPieSlice>
32#include <QtCharts/QPieLegendMarker>
33
34QT_CHARTS_USE_NAMESPACE
35
36//![1]
37DonutBreakdownChart::DonutBreakdownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
38 : QChart(QChart::ChartTypeCartesian, parent, wFlags)
39{
40 // create the series for main center pie
41 m_mainSeries = new QPieSeries();
42 m_mainSeries->setPieSize(0.7);
43 QChart::addSeries(series: m_mainSeries);
44}
45//![1]
46
47//![2]
48void DonutBreakdownChart::addBreakdownSeries(QPieSeries *breakdownSeries, QColor color)
49{
50 QFont font("Arial", 8);
51
52 // add breakdown series as a slice to center pie
53 MainSlice *mainSlice = new MainSlice(breakdownSeries);
54 mainSlice->setName(breakdownSeries->name());
55 mainSlice->setValue(breakdownSeries->sum());
56 m_mainSeries->append(slice: mainSlice);
57
58 // customize the slice
59 mainSlice->setBrush(color);
60 mainSlice->setLabelVisible();
61 mainSlice->setLabelColor(Qt::white);
62 mainSlice->setLabelPosition(QPieSlice::LabelInsideHorizontal);
63 mainSlice->setLabelFont(font);
64
65 // position and customize the breakdown series
66 breakdownSeries->setPieSize(0.8);
67 breakdownSeries->setHoleSize(0.7);
68 breakdownSeries->setLabelsVisible();
69 const auto slices = breakdownSeries->slices();
70 for (QPieSlice *slice : slices) {
71 color = color.lighter(f: 115);
72 slice->setBrush(color);
73 slice->setLabelFont(font);
74 }
75
76 // add the series to the chart
77 QChart::addSeries(series: breakdownSeries);
78
79 // recalculate breakdown donut segments
80 recalculateAngles();
81
82 // update customize legend markers
83 updateLegendMarkers();
84}
85//![2]
86
87//![3]
88void DonutBreakdownChart::recalculateAngles()
89{
90 qreal angle = 0;
91 const auto slices = m_mainSeries->slices();
92 for (QPieSlice *slice : slices) {
93 QPieSeries *breakdownSeries = qobject_cast<MainSlice *>(object: slice)->breakdownSeries();
94 breakdownSeries->setPieStartAngle(angle);
95 angle += slice->percentage() * 360.0; // full pie is 360.0
96 breakdownSeries->setPieEndAngle(angle);
97 }
98}
99//![3]
100
101//![4]
102void DonutBreakdownChart::updateLegendMarkers()
103{
104 // go through all markers
105 const auto allseries = series();
106 for (QAbstractSeries *series : allseries) {
107 const auto markers = legend()->markers(series);
108 for (QLegendMarker *marker : markers) {
109 QPieLegendMarker *pieMarker = qobject_cast<QPieLegendMarker *>(object: marker);
110 if (series == m_mainSeries) {
111 // hide markers from main series
112 pieMarker->setVisible(false);
113 } else {
114 // modify markers from breakdown series
115 pieMarker->setLabel(QString("%1 %2%")
116 .arg(a: pieMarker->slice()->label())
117 .arg(a: pieMarker->slice()->percentage() * 100, fieldWidth: 0, fmt: 'f', prec: 2));
118 pieMarker->setFont(QFont("Arial", 8));
119 }
120 }
121 }
122}
123//![4]
124

source code of qtcharts/examples/charts/donutbreakdown/donutbreakdownchart.cpp