效果图:

QChartView绘制饼状图-LMLPHP

#include "mainwindow.h"
#include "form.h"
#include <QApplication>
#include <QtCharts>
#include <QPieSeries>
#include <QVector> int main(int argc, char *argv[])
{
QApplication a(argc, argv); QVector<int> vector_monitNumber;
vector_monitNumber.append();
vector_monitNumber.append();
vector_monitNumber.append();
vector_monitNumber.append();
vector_monitNumber.append();
vector_monitNumber.append();
vector_monitNumber.append();
vector_monitNumber.append();
vector_monitNumber.append();
vector_monitNumber.append();
vector_monitNumber.append();
vector_monitNumber.append(); int sum = ;
for(int i=;i<vector_monitNumber.size();i++)
{
sum += vector_monitNumber.at(i);
} int float_month1 = vector_monitNumber.at()*/sum;
QPieSlice *slice1 = new QPieSlice(QString(""), float_month1);
//slice1->setLabelVisible(true);
slice1->setLabel(QString("1月份%1").arg(vector_monitNumber.at()));
//slice1->setLabelColor(Qt::yellow);
slice1->setLabelVisible();
//slice1->setLabelColor(Qt::white);
slice1->setLabelPosition(QPieSlice::LabelOutside); //slice1->setLabelPosition(QPieSlice::LabelInsideTangential);
slice1->setBrush(QColor(,,)); QPieSlice *slice2 = new QPieSlice(QString(""), );
slice2->setLabelVisible(true);
slice2->setLabel(QString("2月份%1").arg(vector_monitNumber.at()));
//slice2->setLabelColor(Qt::red);
//slice2->setLabelPosition(QPieSlice::LabelInsideTangential);
slice2->setBrush(QColor(,,)); QPieSlice *slice3 = new QPieSlice(QString(""), );
slice3->setLabelVisible(true);
// slice3->setLabelColor(Qt::blue);
slice3->setLabel(QString("3月份%1").arg(vector_monitNumber.at()));
slice3->setLabelPosition(QPieSlice::LabelOutside);
slice3->setBrush(QColor(,,)); QPieSlice *slice4 = new QPieSlice(QString(""), );
slice4->setLabelVisible(true);
// slice3->setLabelColor(Qt::blue);
slice4->setLabel(QString("4月份%1").arg(vector_monitNumber.at()));
slice4->setLabelPosition(QPieSlice::LabelOutside);
slice4->setBrush(QColor(,,)); QPieSlice *slice5 = new QPieSlice(QString(""), );
slice5->setLabelVisible(true);
// slice3->setLabelColor(Qt::blue);
slice5->setLabel(QString("5月份%1").arg(vector_monitNumber.at()));
slice5->setLabelPosition(QPieSlice::LabelOutside);
slice5->setBrush(QColor(,,)); QPieSlice *slice6 = new QPieSlice(QString(""), );
slice6->setLabelVisible(true);
// slice3->setLabelColor(Qt::blue);
slice6->setLabel(QString("6月份%1").arg(vector_monitNumber.at()));
slice6->setLabelPosition(QPieSlice::LabelOutside);
slice6->setBrush(QColor(,,)); QPieSlice *slice7 = new QPieSlice(QString(""), );
slice7->setLabelVisible(true);
slice7->setLabel(QString("7月份%1").arg(vector_monitNumber.at()));
slice7->setLabelPosition(QPieSlice::LabelOutside);
slice7->setBrush(QColor(,,)); QPieSlice *slice8 = new QPieSlice(QString(""), );
slice8->setLabelVisible(true);
slice8->setLabel(QString("8月份%1").arg(vector_monitNumber.at()));
slice8->setLabelPosition(QPieSlice::LabelOutside);
slice8->setBrush(QColor(,,)); QPieSlice *slice9 = new QPieSlice(QString(""), );
slice9->setLabelVisible(true);
slice9->setLabel(QString("9月份%1").arg(vector_monitNumber.at()));
slice9->setLabelPosition(QPieSlice::LabelOutside);
slice9->setBrush(QColor(,,)); QPieSlice *slice10 = new QPieSlice(QString(""), );
slice10->setLabelVisible(true);
slice10->setLabel(QString("10月份%1").arg(vector_monitNumber.at()));
slice10->setLabelPosition(QPieSlice::LabelOutside);
slice10->setBrush(QColor(,,)); QPieSlice *slice11 = new QPieSlice(QString(""), );
slice11->setLabelVisible(true);
slice11->setLabel(QString("11月份%1").arg(vector_monitNumber.at()));
slice11->setLabelPosition(QPieSlice::LabelOutside);
slice11->setBrush(QColor(,,)); QPieSlice *slice12 = new QPieSlice(QString(""), );
slice12->setLabelVisible(true);
slice12->setLabel(QString("12月份%1").arg(vector_monitNumber.at()));
slice12->setLabelPosition(QPieSlice::LabelOutside);
slice12->setBrush(QColor(,,)); QPieSeries *series = new QPieSeries(); //饼图中间空心的大小
series->setHoleSize(0.35);
// series->append("Protein 4.2%", 4.2);
// series->append("Carbs 56.4%", 56.4);
// series->append("Other 23.8%", 23.8); series->append(slice1);
series->append(slice2);
series->append(slice3);
series->append(slice4);
series->append(slice5);
series->append(slice6);
series->append(slice7);
series->append(slice8);
series->append(slice9);
series->append(slice10);
series->append(slice11);
series->append(slice12); QChartView *chartView = new QChartView();
chartView->setRenderHint(QPainter::Antialiasing);
//chartView->chart()->setTitle("DonutChart Example");
chartView->chart()->addSeries(series);
chartView->chart()->legend()->setAlignment(Qt::AlignRight);
//chartView->chart()->setTheme(QChart::ChartThemeBlueCerulean);
//chartView->chart()->legend()->setFont(QFont("Arial",7));
//chartView->chart()->legend()->setVisible(false); MainWindow w;
w.resize(, );
w.setCentralWidget(chartView);
w.show(); return a.exec();
}
05-11 20:24