QT chart 写个柱状图
时间: 2023-09-16 07:14:54 浏览: 116
好的,你可以使用QT中的QChart和QBarSeries来创建柱状图。以下是一个简单的示例代码:
```cpp
#include <QtCharts/QChartView>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QValueAxis>
#include <QtCharts/QCategoryAxis>
#include <QtWidgets/QApplication>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 创建柱状图
QChart *chart = new QChart();
chart->setTitle("柱状图示例");
// 创建柱状图数据
QBarSeries *series = new QBarSeries();
QBarSet *set0 = new QBarSet("A");
QBarSet *set1 = new QBarSet("B");
QBarSet *set2 = new QBarSet("C");
*set0 << 1 << 2 << 3 << 4 << 5;
*set1 << 5 << 4 << 3 << 2 << 1;
*set2 << 3 << 3 << 3 << 3 << 3;
series->append(set0);
series->append(set1);
series->append(set2);
// 设置柱状图轴
QValueAxis *axisY = new QValueAxis();
axisY->setTitleText("数量");
axisY->setLabelFormat("%d");
axisY->setRange(0, 6);
QCategoryAxis *axisX = new QCategoryAxis();
axisX->setTitleText("项目");
axisX->append("第一项", 0);
axisX->append("第二项", 1);
axisX->append("第三项", 2);
axisX->append("第四项", 3);
axisX->append("第五项", 4);
// 添加柱状图到图表
chart->addSeries(series);
chart->addAxis(axisX, Qt::AlignBottom);
chart->addAxis(axisY, Qt::AlignLeft);
// 显示图表
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->resize(400, 300);
chartView->show();
return a.exec();
}
```
这个示例代码会生成一个带有三个数据系列的柱状图。每个数据系列都由五个柱子组成,分别表示五个项目(第一项到第五项)。你可以根据自己的需要修改代码来生成不同的柱状图。
阅读全文