void Widget::initChartBar()
{
// 创建柱状图
QChart* pChart = new QtCharts::QChart();
pChart->setTitle("Multiple Bar Chart with Dates");
// 创建柱状图序列
QBarSeries* pSeries = new QtCharts::QBarSeries();
// 启用柱状图标签显示,可以把数据显示柱状图上
pSeries->setLabelsVisible(true);
pChart->addSeries(pSeries);
// 创建并添加柱状图数据集合
QtCharts::QBarSet* pSet0 = new QtCharts::QBarSet("A产品");
*pSet0 << 100 << 300 << 1000;
pSet0->setLabelColor(QColor(Qt::black)); //设置柱状图字体颜色
// pSet0->setColor(QColor(182,181,31)); //设置柱的颜色
pSeries->append(pSet0);
QtCharts::QBarSet* pSet1 = new QtCharts::QBarSet("B产品");
*pSet1 << 400 << 900<< 1400;
pSet1->setLabelColor(QColor(Qt::black));
// pSet1->setColor(QColor(250,109,29)); //设置柱的颜色
pSeries->append(pSet1);
QList<QString> DateKey;
DateKey << "2024-03-21" << "2024-03-22" << "2024-03-23";
// 创建柱状图视图
QtCharts::QChartView* pChartView = new QtCharts::QChartView(pChart);
pChartView->setRenderHint(QPainter::Antialiasing);
// 设置X轴和Y轴
QtCharts::QBarCategoryAxis *axisX = new QtCharts::QBarCategoryAxis();
axisX->append(DateKey);
pChart->addAxis(axisX, Qt::AlignBottom);
pSeries->attachAxis(axisX);
QtCharts::QValueAxis *axisY = new QtCharts::QValueAxis();
pChart->addAxis(axisY, Qt::AlignLeft);
pSeries->attachAxis(axisY);
axisY->setRange(0, 2000);
axisY->setLabelFormat("%d");
pChart->legend()->setVisible(true);
pChart->legend()->setAlignment(Qt::AlignBottom);
QVBoxLayout* pVBoxLayout = new QVBoxLayout(this);
pVBoxLayout->addWidget(pChartView);
}