Pie Chart Customization 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
"mainwidget.h"
#include
"customslice.h"
#include
"pentool.h"
#include
"brushtool.h"
#include <QtWidgets/QPushButton>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QDoubleSpinBox>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QFontDialog>
#include <QtCharts/QChartView>
#include <QtCharts/QPieSeries>
QT_CHARTS_USE_NAMESPACE
MainWidget::
MainWidget(QWidget *
parent)
:
QWidget(parent),
m_slice(0
)
{
// create chart
QChart *
chart =
new
QChart;
chart-&
gt;setTitle("Piechart customization"
);
chart-&
gt;setAnimationOptions(QChart::
AllAnimations);
// create series
m_series =
new
QPieSeries();
*
m_series &
lt;&
lt; new
CustomSlice("Slice 1"
, 10.0
);
*
m_series &
lt;&
lt; new
CustomSlice("Slice 2"
, 20.0
);
*
m_series &
lt;&
lt; new
CustomSlice("Slice 3"
, 30.0
);
*
m_series &
lt;&
lt; new
CustomSlice("Slice 4"
, 40.0
);
*
m_series &
lt;&
lt; new
CustomSlice("Slice 5"
, 50.0
);
m_series-&
gt;setLabelsVisible();
chart-&
gt;addSeries(m_series);
connect(m_series, &
amp;QPieSeries::
clicked, this
, &
amp;MainWidget::
handleSliceClicked);
// chart settings
m_themeComboBox =
new
QComboBox();
m_themeComboBox-&
gt;addItem("Light"
, QChart::
ChartThemeLight);
m_themeComboBox-&
gt;addItem("BlueCerulean"
, QChart::
ChartThemeBlueCerulean);
m_themeComboBox-&
gt;addItem("Dark"
, QChart::
ChartThemeDark);
m_themeComboBox-&
gt;addItem("BrownSand"
, QChart::
ChartThemeBrownSand);
m_themeComboBox-&
gt;addItem("BlueNcs"
, QChart::
ChartThemeBlueNcs);
m_themeComboBox-&
gt;addItem("High Contrast"
, QChart::
ChartThemeHighContrast);
m_themeComboBox-&
gt;addItem("Blue Icy"
, QChart::
ChartThemeBlueIcy);
m_themeComboBox-&
gt;addItem("Qt"
, QChart::
ChartThemeQt);
m_aaCheckBox =
new
QCheckBox();
m_animationsCheckBox =
new
QCheckBox();
m_animationsCheckBox-&
gt;setCheckState(Qt::
Checked);
m_legendCheckBox =
new
QCheckBox();
QFormLayout *
chartSettingsLayout =
new
QFormLayout();
chartSettingsLayout-&
gt;addRow("Theme"
, m_themeComboBox);
chartSettingsLayout-&
gt;addRow("Antialiasing"
, m_aaCheckBox);
chartSettingsLayout-&
gt;addRow("Animations"
, m_animationsCheckBox);
chartSettingsLayout-&
gt;addRow("Legend"
, m_legendCheckBox);
QGroupBox *
chartSettings =
new
QGroupBox("Chart"
);
chartSettings-&
gt;setLayout(chartSettingsLayout);
connect(m_themeComboBox, static_cast
&
lt;void
(QComboBox::
*
)(int
)&
gt;(&
amp;QComboBox::
currentIndexChanged),
this
, &
amp;MainWidget::
updateChartSettings);
connect(m_aaCheckBox, &
amp;QCheckBox::
toggled, this
, &
amp;MainWidget::
updateChartSettings);
connect(m_animationsCheckBox, &
amp;QCheckBox::
toggled, this
, &
amp;MainWidget::
updateChartSettings);
connect(m_legendCheckBox, &
amp;QCheckBox::
toggled, this
, &
amp;MainWidget::
updateChartSettings);
// series settings
m_hPosition =
new
QDoubleSpinBox();
m_hPosition-&
gt;setMinimum(0.0
);
m_hPosition-&
gt;setMaximum(1.0
);
m_hPosition-&
gt;setSingleStep(0.1
);
m_hPosition-&
gt;setValue(m_series-&
gt;horizontalPosition());
m_vPosition =
new
QDoubleSpinBox();
m_vPosition-&
gt;setMinimum(0.0
);
m_vPosition-&
gt;setMaximum(1.0
);
m_vPosition-&
gt;setSingleStep(0.1
);
m_vPosition-&
gt;setValue(m_series-&
gt;verticalPosition());
m_sizeFactor =
new
QDoubleSpinBox();
m_sizeFactor-&
gt;setMinimum(0.0
);
m_sizeFactor-&
gt;setMaximum(1.0
);
m_sizeFactor-&
gt;setSingleStep(0.1
);
m_sizeFactor-&
gt;setValue(m_series-&
gt;pieSize());
m_startAngle =
new
QDoubleSpinBox();
m_startAngle-&
gt;setMinimum(-
720
);
m_startAngle-&
gt;setMaximum(720
);
m_startAngle-&
gt;setValue(m_series-&
gt;pieStartAngle());
m_startAngle-&
gt;setSingleStep(1
);
m_endAngle =
new
QDoubleSpinBox();
m_endAngle-&
gt;setMinimum(-
720
);
m_endAngle-&
gt;setMaximum(720
);
m_endAngle-&
gt;setValue(m_series-&
gt;pieEndAngle());
m_endAngle-&
gt;setSingleStep(1
);
m_holeSize =
new
QDoubleSpinBox();
m_holeSize-&
gt;setMinimum(0.0
);
m_holeSize-&
gt;setMaximum(1.0
);
m_holeSize-&
gt;setSingleStep(0.1
);
m_holeSize-&
gt;setValue(m_series-&
gt;holeSize());
QPushButton *
appendSlice =
new
QPushButton("Append slice"
);
QPushButton *
insertSlice =
new
QPushButton("Insert slice"
);
QPushButton *
removeSlice =
new
QPushButton("Remove selected slice"
);
QFormLayout *
seriesSettingsLayout =
new
QFormLayout();
seriesSettingsLayout-&
gt;addRow("Horizontal position"
, m_hPosition);
seriesSettingsLayout-&
gt;addRow("Vertical position"
, m_vPosition);
seriesSettingsLayout-&
gt;addRow("Size factor"
, m_sizeFactor);
seriesSettingsLayout-&
gt;addRow("Start angle"
, m_startAngle);
seriesSettingsLayout-&
gt;addRow("End angle"
, m_endAngle);
seriesSettingsLayout-&
gt;addRow("Hole size"
, m_holeSize);
seriesSettingsLayout-&
gt;addRow(appendSlice);
seriesSettingsLayout-&
gt;addRow(insertSlice);
seriesSettingsLayout-&
gt;addRow(removeSlice);
QGroupBox *
seriesSettings =
new
QGroupBox("Series"
);
seriesSettings-&
gt;setLayout(seriesSettingsLayout);
connect(m_vPosition,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateSerieSettings);
connect(m_hPosition,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateSerieSettings);
connect(m_sizeFactor,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateSerieSettings);
connect(m_startAngle,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateSerieSettings);
connect(m_endAngle,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateSerieSettings);
connect(m_holeSize,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateSerieSettings);
connect(appendSlice, &
amp;QPushButton::
clicked, this
, &
amp;MainWidget::
appendSlice);
connect(insertSlice, &
amp;QPushButton::
clicked, this
, &
amp;MainWidget::
insertSlice);
connect(removeSlice, &
amp;QPushButton::
clicked, this
, &
amp;MainWidget::
removeSlice);
// slice settings
m_sliceName =
new
QLineEdit("<click a slice>"
);
m_sliceName-&
gt;setSizePolicy(QSizePolicy::
Maximum, QSizePolicy::
Maximum);
m_sliceValue =
new
QDoubleSpinBox();
m_sliceValue-&
gt;setMaximum(1000
);
m_sliceLabelVisible =
new
QCheckBox();
m_sliceLabelArmFactor =
new
QDoubleSpinBox();
m_sliceLabelArmFactor-&
gt;setSingleStep(0.01
);
m_sliceExploded =
new
QCheckBox();
m_sliceExplodedFactor =
new
QDoubleSpinBox();
m_sliceExplodedFactor-&
gt;setSingleStep(0.01
);
m_pen =
new
QPushButton();
m_penTool =
new
PenTool("Slice pen"
, this
);
m_brush =
new
QPushButton();
m_brushTool =
new
BrushTool("Slice brush"
, this
);
m_font =
new
QPushButton();
m_labelBrush =
new
QPushButton();
m_labelBrushTool =
new
BrushTool("Label brush"
, this
);
m_labelPosition =
new
QComboBox(this
);
m_labelPosition-&
gt;addItem("Outside"
, QPieSlice::
LabelOutside);
m_labelPosition-&
gt;addItem("Inside horizontal"
, QPieSlice::
LabelInsideHorizontal);
m_labelPosition-&
gt;addItem("Inside tangential"
, QPieSlice::
LabelInsideTangential);
m_labelPosition-&
gt;addItem("Inside normal"
, QPieSlice::
LabelInsideNormal);
QFormLayout *
sliceSettingsLayout =
new
QFormLayout();
sliceSettingsLayout-&
gt;addRow("Label"
, m_sliceName);
sliceSettingsLayout-&
gt;addRow("Value"
, m_sliceValue);
sliceSettingsLayout-&
gt;addRow("Pen"
, m_pen);
sliceSettingsLayout-&
gt;addRow("Brush"
, m_brush);
sliceSettingsLayout-&
gt;addRow("Label visible"
, m_sliceLabelVisible);
sliceSettingsLayout-&
gt;addRow("Label font"
, m_font);
sliceSettingsLayout-&
gt;addRow("Label brush"
, m_labelBrush);
sliceSettingsLayout-&
gt;addRow("Label position"
, m_labelPosition);
sliceSettingsLayout-&
gt;addRow("Label arm length"
, m_sliceLabelArmFactor);
sliceSettingsLayout-&
gt;addRow("Exploded"
, m_sliceExploded);
sliceSettingsLayout-&
gt;addRow("Explode distance"
, m_sliceExplodedFactor);
QGroupBox *
sliceSettings =
new
QGroupBox("Selected slice"
);
sliceSettings-&
gt;setLayout(sliceSettingsLayout);
connect(m_sliceName, &
amp;QLineEdit::
textChanged, this
, &
amp;MainWidget::
updateSliceSettings);
connect(m_sliceValue,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateSliceSettings);
connect(m_pen, &
amp;QPushButton::
clicked, m_penTool, &
amp;PenTool::
show);
connect(m_penTool, &
amp;PenTool::
changed, this
, &
amp;MainWidget::
updateSliceSettings);
connect(m_brush, &
amp;QPushButton::
clicked, m_brushTool, &
amp;BrushTool::
show);
connect(m_brushTool, &
amp;BrushTool::
changed, this
, &
amp;MainWidget::
updateSliceSettings);
connect(m_font, &
amp;QPushButton::
clicked, this
, &
amp;MainWidget::
showFontDialog);
connect(m_labelBrush, &
amp;QPushButton::
clicked, m_labelBrushTool, &
amp;BrushTool::
show);
connect(m_labelBrushTool, &
amp;BrushTool::
changed, this
, &
amp;MainWidget::
updateSliceSettings);
connect(m_sliceLabelVisible, &
amp;QCheckBox::
toggled, this
, &
amp;MainWidget::
updateSliceSettings);
connect(m_sliceLabelVisible, &
amp;QCheckBox::
toggled, this
, &
amp;MainWidget::
updateSliceSettings);
connect(m_sliceLabelArmFactor,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateSliceSettings);
connect(m_sliceExploded, &
amp;QCheckBox::
toggled, this
, &
amp;MainWidget::
updateSliceSettings);
connect(m_sliceExplodedFactor,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateSliceSettings);
connect(m_labelPosition,
static_cast
&
lt;void
(QComboBox::
*
)(int
)&
gt;(&
amp;QComboBox::
currentIndexChanged),
this
, &
amp;MainWidget::
updateSliceSettings);
// create chart view
m_chartView =
new
QChartView(chart);
// create main layout
QVBoxLayout *
settingsLayout =
new
QVBoxLayout();
settingsLayout-&
gt;addWidget(chartSettings);
settingsLayout-&
gt;addWidget(seriesSettings);
settingsLayout-&
gt;addWidget(sliceSettings);
settingsLayout-&
gt;addStretch();
QGridLayout *
baseLayout =
new
QGridLayout();
baseLayout-&
gt;addLayout(settingsLayout, 0
, 0
);
baseLayout-&
gt;addWidget(m_chartView, 0
, 1
);
setLayout(baseLayout);
updateSerieSettings();
updateChartSettings();
}
void
MainWidget::
updateChartSettings()
{
QChart::
ChartTheme theme =
static_cast
&
lt;QChart::
ChartTheme&
gt;(m_themeComboBox-&
gt;itemData(
m_themeComboBox-&
gt;currentIndex()).toInt());
m_chartView-&
gt;chart()-&
gt;setTheme(theme);
m_chartView-&
gt;setRenderHint(QPainter::
Antialiasing, m_aaCheckBox-&
gt;isChecked());
if
(m_animationsCheckBox-&
gt;checkState() ==
Qt::
Checked)
m_chartView-&
gt;chart()-&
gt;setAnimationOptions(QChart::
AllAnimations);
else
m_chartView-&
gt;chart()-&
gt;setAnimationOptions(QChart::
NoAnimation);
if
(m_legendCheckBox-&
gt;checkState() ==
Qt::
Checked)
m_chartView-&
gt;chart()-&
gt;legend()-&
gt;show();
else
m_chartView-&
gt;chart()-&
gt;legend()-&
gt;hide();
}
void
MainWidget::
updateSerieSettings()
{
m_series-&
gt;setHorizontalPosition(m_hPosition-&
gt;value());
m_series-&
gt;setVerticalPosition(m_vPosition-&
gt;value());
m_series-&
gt;setPieSize(m_sizeFactor-&
gt;value());
m_holeSize-&
gt;setMaximum(m_sizeFactor-&
gt;value());
m_series-&
gt;setPieStartAngle(m_startAngle-&
gt;value());
m_series-&
gt;setPieEndAngle(m_endAngle-&
gt;value());
m_series-&
gt;setHoleSize(m_holeSize-&
gt;value());
}
void
MainWidget::
updateSliceSettings()
{
if
(!
m_slice)
return
;
m_slice-&
gt;setLabel(m_sliceName-&
gt;text());
m_slice-&
gt;setValue(m_sliceValue-&
gt;value());
m_slice-&
gt;setPen(m_penTool-&
gt;pen());
m_slice-&
gt;setBrush(m_brushTool-&
gt;brush());
m_slice-&
gt;setLabelBrush(m_labelBrushTool-&
gt;brush());
m_slice-&
gt;setLabelVisible(m_sliceLabelVisible-&
gt;isChecked());
m_slice-&
gt;setLabelArmLengthFactor(m_sliceLabelArmFactor-&
gt;value());
// We assume that label position index is in sync with the enum
m_slice-&
gt;setLabelPosition((QPieSlice::
LabelPosition)m_labelPosition-&
gt;currentIndex());
m_slice-&
gt;setExploded(m_sliceExploded-&
gt;isChecked());
m_slice-&
gt;setExplodeDistanceFactor(m_sliceExplodedFactor-&
gt;value());
}
void
MainWidget::
handleSliceClicked(QPieSlice *
slice)
{
m_slice =
static_cast
&
lt;CustomSlice *&
gt;(slice);
// name
m_sliceName-&
gt;blockSignals(true
);
m_sliceName-&
gt;setText(slice-&
gt;label());
m_sliceName-&
gt;blockSignals(false
);
// value
m_sliceValue-&
gt;blockSignals(true
);
m_sliceValue-&
gt;setValue(slice-&
gt;value());
m_sliceValue-&
gt;blockSignals(false
);
// pen
m_pen-&
gt;setText(PenTool::
name(m_slice-&
gt;pen()));
m_penTool-&
gt;setPen(m_slice-&
gt;pen());
// brush
m_brush-&
gt;setText(m_slice-&
gt;originalBrush().color().name());
m_brushTool-&
gt;setBrush(m_slice-&
gt;originalBrush());
// label
m_labelBrush-&
gt;setText(BrushTool::
name(m_slice-&
gt;labelBrush()));
m_labelBrushTool-&
gt;setBrush(m_slice-&
gt;labelBrush());
m_font-&
gt;setText(slice-&
gt;labelFont().toString());
m_sliceLabelVisible-&
gt;blockSignals(true
);
m_sliceLabelVisible-&
gt;setChecked(slice-&
gt;isLabelVisible());
m_sliceLabelVisible-&
gt;blockSignals(false
);
m_sliceLabelArmFactor-&
gt;blockSignals(true
);
m_sliceLabelArmFactor-&
gt;setValue(slice-&
gt;labelArmLengthFactor());
m_sliceLabelArmFactor-&
gt;blockSignals(false
);
m_labelPosition-&
gt;blockSignals(true
);
// We assume that label position index is in sync with the enum
m_labelPosition-&
gt;setCurrentIndex(slice-&
gt;labelPosition());
m_labelPosition-&
gt;blockSignals(false
);
// exploded
m_sliceExploded-&
gt;blockSignals(true
);
m_sliceExploded-&
gt;setChecked(slice-&
gt;isExploded());
m_sliceExploded-&
gt;blockSignals(false
);
m_sliceExplodedFactor-&
gt;blockSignals(true
);
m_sliceExplodedFactor-&
gt;setValue(slice-&
gt;explodeDistanceFactor());
m_sliceExplodedFactor-&
gt;blockSignals(false
);
}
void
MainWidget::
showFontDialog()
{
if
(!
m_slice)
return
;
QFontDialog dialog(m_slice-&
gt;labelFont());
dialog.show();
dialog.exec();
m_slice-&
gt;setLabelFont(dialog.currentFont());
m_font-&
gt;setText(dialog.currentFont().toString());
}
void
MainWidget::
appendSlice()
{
*
m_series &
lt;&
lt; new
CustomSlice("Slice "
+
QString::
number(m_series-&
gt;count() +
1
), 10.0
);
}
void
MainWidget::
insertSlice()
{
if
(!
m_slice)
return
;
int
i =
m_series-&
gt;slices().indexOf(m_slice);
m_series-&
gt;insert(i, new
CustomSlice("Slice "
+
QString::
number(m_series-&
gt;count() +
1
), 10.0
));
}
void
MainWidget::
removeSlice()
{
if
(!
m_slice)
return
;
m_sliceName-&
gt;setText("<click a slice>"
);
m_series-&
gt;remove(m_slice);
m_slice =
0
;
}
#include
"moc_mainwidget.cpp"