DoughnutChart3D Example
#include "mainwindow.h"
#include <QVariant>
#include <QGroupBox>
#include <QCheckBox>
#include <QComboBox>
#include <QSpinBox>
#include <QFormLayout>
MainWindow::MainWindow()
: DemoChartWindow(tr("Pie Chart 3D"))
{
setChart(Q_NULL);
createSeriesParametrs();
createDoughnutSeries();
updateValueParameters();
}
void MainWindow::createSeriesParametrs()
{
QGroupBox* seriesTypeGroup = createGroupParameters(tr("Series"));
QFormLayout* localLayout = (QFormLayout*)seriesTypeGroup->layout();
m_explodedCheckBox = new QCheckBox(tr("Exploded"), seriesTypeGroup);
connect(m_explodedCheckBox, SIGNAL(stateChanged(int)), this, SLOT(explodedSeriesChanged(int)));
m_rotateSlider = new QSlider(Qt::Horizontal, seriesTypeGroup);
m_rotateSlider->setRange(-180, 180);
m_rotateSlider->setSliderPosition(0);
m_rotateSlider->setSingleStep(1);
connect(m_rotateSlider, SIGNAL(valueChanged(int)), this, SLOT(rotateSeriesChanged(int)));
m_aspectAngleSlider = new QSlider(Qt::Horizontal, seriesTypeGroup);
m_aspectAngleSlider->setRange(1, 90);
m_aspectAngleSlider->setSingleStep(1);
connect(m_aspectAngleSlider, SIGNAL(valueChanged(int)), this, SLOT(aspectAngleChanged(int)));
m_editorHole = new QSpinBox(seriesTypeGroup);
m_editorHole->setMinimumWidth(60);
m_editorHole->setRange(0, 100);
m_editorHole->setValue(60);
connect(m_editorHole, SIGNAL(valueChanged(int)), this, SLOT(holeSeriesChanged(int)));
localLayout->addRow(m_explodedCheckBox);
localLayout->addRow(new QLabel(tr("Pie Rotation Angle:"), seriesTypeGroup));
localLayout->addRow(m_rotateSlider);
m_captionAspectAngle = new QLabel(tr("Pie Aspect Angle:"), seriesTypeGroup);
localLayout->addRow(m_captionAspectAngle);
localLayout->addRow(m_aspectAngleSlider);
m_captionEditorHole = new QLabel(tr("Hole Radius:"), seriesTypeGroup);
localLayout->addRow(m_captionEditorHole);
localLayout->addRow(m_editorHole);
m_dataLabelsGroup = createGroupParameters(tr("Show Data Labels"), true);
localLayout = (QFormLayout*)m_dataLabelsGroup->layout();
connect(m_dataLabelsGroup, SIGNAL(toggled(bool)), this, SLOT(showDataLabels(bool)));
m_posDataLabelsSwitcher = new QComboBox(m_dataLabelsGroup);
m_posDataLabelsSwitcher->addItem(tr("Label Outside"), QVariant(ChartPieSeriesLabel::PieLabelOutside));
m_posDataLabelsSwitcher->addItem(tr("Label Inside"), QVariant(ChartPieSeriesLabel::PieLabelInside));
m_posDataLabelsSwitcher->setCurrentIndex(0);
connect(m_posDataLabelsSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(labelsPositionChanged(int)));
localLayout->addRow(m_posDataLabelsSwitcher);
}
static void createPiePoint(ChartPieSeries* series, const QString& legendText, qreal value, bool special = false)
{
QString str = legendText;
QString strValue;
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
strValue = strValue.asprintf(", %.01f", value);
#else
strValue = strValue.sprintf(", %.01f", value);
#endif
str += strValue;
ChartDataPoint* point = series->add(value, special);
point->setLabel(str);
point->setLegendText(legendText);
}
void MainWindow::createDoughnutSeries()
{
createTitle(tr("Travel"));
m_chart->legend()->setVisible(true);
m_chart->legend()->setVerticalAlignment(ChartLegend::LegendFar);
ChartDoughnutSeries3D* series = new ChartDoughnutSeries3D();
m_chart->appendSeries(series);
::createPiePoint(series, QObject::tr("Other"), 1131, true);
::createPiePoint(series, QObject::tr("USA"), 1139, true);
::createPiePoint(series, QObject::tr("Taiwan"), 823, true);
::createPiePoint(series, QObject::tr("France"), 423);
::createPiePoint(series, QObject::tr("China"), 223);
::createPiePoint(series, QObject::tr("Australia"), 523);
::createPiePoint(series, QObject::tr("Germany"), 724);
::createPiePoint(series, QObject::tr("Italy"), 1025);
setValueHole(60);
m_aspectAngleSlider->setSliderPosition(((ChartDoughnutSeries3D*)m_chart->series().at(0))->aspectAngle());
}
void MainWindow::setValueHole(int val)
{
ChartDoughnutSeries3D* series2D = (ChartDoughnutSeries3D*)m_chart->series().at(0);
Q_ASSERT(series2D != Q_NULL);
series2D->setHolePercent(val);
}
void MainWindow::updateValueParameters()
{
DemoChartWindow::updateValueParameters();
labelsPositionChanged(m_posDataLabelsSwitcher->currentIndex());
explodedSeriesChanged(m_explodedCheckBox->checkState());
rotateSeriesChanged(m_rotateSlider->value());
if (m_editorHole->isEnabled())
holeSeriesChanged(m_editorHole->value());
}
void MainWindow::labelsPositionChanged(int index)
{
QVariant var = m_posDataLabelsSwitcher->itemData(index);
const SeriesList& listSeries = m_chart->series();
for (int i = 0, count = listSeries.count(); i < count; ++i)
{
ChartDoughnutSeries3D* series = (ChartDoughnutSeries3D*)listSeries.at(i);
ChartPieSeriesLabel* label = (ChartPieSeriesLabel*)series->label();
label->setPosition((ChartPieSeriesLabel::PieLabelPosition)(var.toUInt()));
}
}
void MainWindow::explodedSeriesChanged(int state)
{
const SeriesList& listSeries = m_chart->series();
for (int i = 0, count = listSeries.count(); i < count; ++i)
{
ChartDoughnutSeries3D* series = (ChartDoughnutSeries3D*)listSeries.at(i);
series->setExplodeRelativeSizePercent(state == Qt::Checked ? 15 : 0);
}
}
void MainWindow::rotateSeriesChanged(int value)
{
const SeriesList& listSeries = m_chart->series();
for (int i = 0, count = listSeries.count(); i < count; ++i)
{
ChartDoughnutSeries3D* series = (ChartDoughnutSeries3D*)listSeries.at(i);
series->setRotation(value);
}
}
void MainWindow::aspectAngleChanged(int value)
{
const SeriesList& listSeries = m_chart->series();
for (int i = 0, count = listSeries.count(); i < count; ++i)
{
ChartDoughnutSeries3D* series = qobject_cast<ChartDoughnutSeries3D*>(listSeries.at(i));
Q_ASSERT(series != Q_NULL);
series->setAspectAngle(value);
}
}
void MainWindow::holeSeriesChanged(int val)
{
const SeriesList& listSeries = m_chart->series();
if (listSeries.count() == 0 )
return;
ChartDoughnutSeries3D* series2D = (ChartDoughnutSeries3D*)m_chart->series().at(0);
Q_ASSERT(series2D != Q_NULL);
series2D->setHolePercent(val);
}