Donut Chart Breakdown Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"donutbreakdownchart.h"
#include
"mainslice.h"
#include <QtCharts/QPieSlice>
#include <QtCharts/QPieLegendMarker>
QT_CHARTS_USE_NAMESPACE
DonutBreakdownChart::
DonutBreakdownChart(QGraphicsItem *
parent, Qt::
WindowFlags wFlags)
:
QChart(QChart::
ChartTypeCartesian, parent, wFlags)
{
// create the series for main center pie
m_mainSeries =
new
QPieSeries();
m_mainSeries-&
gt;setPieSize(0.7
);
QChart::
addSeries(m_mainSeries);
}
void
DonutBreakdownChart::
addBreakdownSeries(QPieSeries *
breakdownSeries, QColor color)
{
QFont font("Arial"
, 8
);
// add breakdown series as a slice to center pie
MainSlice *
mainSlice =
new
MainSlice(breakdownSeries);
mainSlice-&
gt;setName(breakdownSeries-&
gt;name());
mainSlice-&
gt;setValue(breakdownSeries-&
gt;sum());
m_mainSeries-&
gt;append(mainSlice);
// customize the slice
mainSlice-&
gt;setBrush(color);
mainSlice-&
gt;setLabelVisible();
mainSlice-&
gt;setLabelColor(Qt::
white);
mainSlice-&
gt;setLabelPosition(QPieSlice::
LabelInsideHorizontal);
mainSlice-&
gt;setLabelFont(font);
// position and customize the breakdown series
breakdownSeries-&
gt;setPieSize(0.8
);
breakdownSeries-&
gt;setHoleSize(0.7
);
breakdownSeries-&
gt;setLabelsVisible();
const
auto
slices =
breakdownSeries-&
gt;slices();
for
(QPieSlice *
slice : slices) {
color =
color.lighter(115
);
slice-&
gt;setBrush(color);
slice-&
gt;setLabelFont(font);
}
// add the series to the chart
QChart::
addSeries(breakdownSeries);
// recalculate breakdown donut segments
recalculateAngles();
// update customize legend markers
updateLegendMarkers();
}
void
DonutBreakdownChart::
recalculateAngles()
{
qreal angle =
0
;
const
auto
slices =
m_mainSeries-&
gt;slices();
for
(QPieSlice *
slice : slices) {
QPieSeries *
breakdownSeries =
qobject_cast&
lt;MainSlice *&
gt;(slice)-&
gt;breakdownSeries();
breakdownSeries-&
gt;setPieStartAngle(angle);
angle +=
slice-&
gt;percentage() *
360.0
; // full pie is 360.0
breakdownSeries-&
gt;setPieEndAngle(angle);
}
}
void
DonutBreakdownChart::
updateLegendMarkers()
{
// go through all markers
const
auto
allseries =
series();
for
(QAbstractSeries *
series : allseries) {
const
auto
markers =
legend()-&
gt;markers(series);
for
(QLegendMarker *
marker : markers) {
QPieLegendMarker *
pieMarker =
qobject_cast&
lt;QPieLegendMarker *&
gt;(marker);
if
(series ==
m_mainSeries) {
// hide markers from main series
pieMarker-&
gt;setVisible(false
);
}
else
{
// modify markers from breakdown series
pieMarker-&
gt;setLabel(QString("%1 %2%"
)
.arg(pieMarker-&
gt;slice()-&
gt;label())
.arg(pieMarker-&
gt;slice()-&
gt;percentage() *
100
, 0
, 'f'
, 2
));
pieMarker-&
gt;setFont(QFont("Arial"
, 8
));
}
}
}
}