#include
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include<QtCharts/QChartView>
#include<QtCharts/QPieSeries>
#include<QtCharts/QPieSlice>
#include<QtCharts/QLineSeries>
#include<QtCharts/QSplineSeries>
#include
#include
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
QPieSeries * pie_series = new QPieSeries(this);
connect(pie_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(onPieSeriesClicked(QPieSlice*)));
//定义各扇形切片的颜色
static const QStringList list_pie_color = {
"#6480D6","#A1DC85","#FFAD25","#FF7777","#84D1EF","#4CB383",
};
//设置数据
QList<qreal> list_data = {3.1, 3.2, 3.3, 3.4, 3.5, 3.6};
//扇形
for (int i = 0; i < list_pie_color.size(); i++) {
QPieSlice* pie_slice = new QPieSlice(this);
pie_slice->setLabelVisible(true);
pie_slice->setValue(list_data[i]);
pie_slice->setLabel(QString::number(list_data[i]));
pie_slice->setColor(list_pie_color[i]);
pie_slice->setLabelColor(list_pie_color[i]);
pie_slice->setBorderColor(list_pie_color[i]);
pie_series->append(pie_slice);
}
//图表视图
chart2 = new QChart();
chart2->setTitle("XXX统计饼图");
//设置暗黑主题
chart2->setTheme(QChart::ChartThemeDark);
//标题字体
QFont font = qApp->font();
font.setBold(true);
font.setPointSize(16);
chart2->setTitleFont(font);
//加入饼图
chart2->addSeries(pie_series);
chart2->setAnimationOptions(QChart::SeriesAnimations);
//图例
chart2->legend()->setAlignment(Qt::AlignBottom);
chart2->legend()->setBackgroundVisible(false);
for (int i = 0; i < list_pie_color.size(); i++) {
chart2->legend()->markers(pie_series)[i]->setLabel("统计项" + QString::number(i+1));
}
//加入绘画视图
QChartView* chartView = new QChartView(this);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->setChart(chart2);
//加入布局
QHBoxLayout *lay=new QHBoxLayout(this);
lay->setContentsMargins(0, 0, 0, 0);
lay->addWidget(chartView);
ui->widget_2->setLayout(lay);
//------------------------------------------------------------------------
}
void Form::onPieSeriesClicked(QPieSlice* slice)
{
slice->setExploded(!slice->isExploded());
}