QT使用QtCharts创建图表
工程pro文件需要添加
QT += charts
注意需要引用
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
动手实践代码
QBarSet *set0 = new QBarSet("座席状态");
set0->append(6);
set0->append(3);
set0->append(2);
set0->append(1);
set0->append(1);
set0->append(1);
set0->append(1);
//set0->setColor(Qt::magenta); //设置柱子颜色 同一个QBarSet这里测试不能为每根柱子单独设置颜色或随机生成颜色
QBarSeries *series = new QBarSeries();
series->append(set0);
series->setLabelsVisible(true);//柱子标签数字显示否
series->setLabelsPosition(QAbstractBarSeries::LabelsInsideBase);//柱子标签数字显示位置
//LabelsCenter = 0, LabelsInsideEnd, LabelsInsideBase, LabelsOutsideEnd
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("呼叫中心");
chart->setAnimationOptions(QChart::SeriesAnimations);
QStringList categories;
categories.append("就绪");
categories.append("示忙");
categories.append("排队");
categories.append("振铃");
categories.append("通话");
categories.append("外拨");
QBarCategoryAxis *axis = new QBarCategoryAxis();
QFont Font1("Microsoft YaHei", 30, 254);
axis->setLabelsFont(Font1);
//axis->setLabelsColor(Qt::red);
axis->append(categories);
chart->createDefaultAxes();
chart->setAxisX(axis, series);
//chart->legend()->setVisible(false);
chart->legend()->setAlignment(Qt::AlignBottom);
// chart->legend()->setAlignment(Qt::AlignRight);
chart->legend()->setColor(QColor(222,233,251));//设置颜色
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
auto lb=new QLabel(tr("图表测试"));
lb->setAlignment(Qt::AlignCenter);
QFont Font("Microsoft YaHei", 30, 254);//字休大小颜色
lb->setFont(Font);
auto h2=new QHBoxLayout;
h2->addWidget(lb);
auto layoutMain=new QVBoxLayout;
layoutMain->addLayout(h2);
layoutMain->addWidget(chartView);
this->setLayout(layoutMain);
this->setWindowTitle("图表测试QChartView");
this->resize(800,600);
窗体整体缩小柱子下方的文本不显示了