记录下,记录下
效果图
直接上代码
// 创建柱状系列
QBarSeries *series = new QBarSeries();
// 创建数据集
QBarSet *setTar = new QBarSet(("tar"));
QBarSet *setReality = new QBarSet(("reality"));
//添加柱状数据
*setTar << 1<<3;
*setReality << 2 << 4;
//设置柱状图颜色
setTar->setColor(QColor("green"));
setReality->setColor(QColor("yellow"));
//设置柱状图标签颜色
setTar->setLabelColor(QColor("black"));
setReality->setLabelColor(QColor("black"));
series->append(setTar);
series->append(setReality);
//设置显示柱状图标签
series->setLabelsVisible(true);
//设置标签显示在内部顶端
series->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd);
// 创建图表对象
QChart *chart = new QChart();
chart->addSeries(series);
chart->setAnimationOptions(QChart::SeriesAnimations);
//X轴
QBarCategoryAxis* axisX = new QBarCategoryAxis();
QStringList categories;
categories <<"11.23"<<"11.24";
axisX->append(categories);
//X轴图例显示在下方
chart->addAxis(axisX, Qt::AlignBottom);
series->attachAxis(axisX);
//Y轴
QValueAxis *axisY = new QValueAxis();
axisY->setRange(0, 10);
//标签颜色
axisY->setLabelsColor(QColor(Qt::GlobalColor::black));
//刻度线颜色
axisY->setGridLineColor(QColor(Qt::GlobalColor::black));
chart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisY);
// 创建图表视图
QChartView * chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
//设置图例为显示状态
chart->legend()->setVisible(true);
//设置图例的显示位置在底部
chart->legend()->setAlignment(Qt::AlignBottom);
//去背景
chart->setBackgroundVisible(false);
setCentralWidget(chartView);