Qt-绘制图表
作者:互联网
1 简介
使用Qt的charts模块来绘制图表,案例来自Qt自带的demo。
charts模块简介:Qt Chars模块提供了一系列容易使用的图表组件。需要使用charts组件时,需要导入Qt Charts模块,通过如下方式:QT += charts
在安装Qt Creator的时候,需要勾选上这个模块,不然是不能使用的。
2 Qt Charts总览
Qt Chars支持绘制:坐标图、柱状图、折线图、饼图、曲线图、散点图等。
3 测试及说明
首先创建一个工程,继承至MainWindow组件。
(1)绘制折线图
折线图通过线段展示一系列相关联的点。
使用的类:QLineSeries
测试代码:
1 MainWindow::MainWindow(QWidget *parent) : 2 QMainWindow(parent), 3 ui(new Ui::MainWindow) 4 { 5 ui->setupUi(this); 6 7 /* 8 * LineChart Example 9 */ 10 //添加一个QLineSeries实例 11 QLineSeries *lineseries = new QLineSeries(); 12 //添加数据(点),有两种方式 13 lineseries->append(0, 6); //参数分别为横纵坐标 14 lineseries->append(2, 4); 15 lineseries->append(3, 8); 16 lineseries->append(7, 4); 17 *lineseries << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2); 18 //添加一个QChart实例,为了展示数据 19 QChart *chart = new QChart(); 20 chart->legend()->hide(); //隐藏图例 21 chart->addSeries(lineseries); 22 chart->createDefaultAxes(); 23 chart->setTitle("Simple line chart example"); 24 //添加一个QChartView图例 25 QChartView *chartView = new QChartView(chart); 26 chartView->setRenderHint(QPainter::Antialiasing); //呈现方式 27 //显示 28 this->setCentralWidget(chartView); 29 this->setCentralWidget(chartView); 30 this->resize(400, 300); 31 this->show(); 32 }
运行测试:
(2)绘制曲线图
使用的类:QSplineSeries
测试代码:
1 QSplineSeries *series = new QSplineSeries(); 2 series->setName("spline"); 3 series->append(0, 6); 4 series->append(2, 4); 5 series->append(3, 8); 6 series->append(7, 4); 7 series->append(10, 5); 8 9 QChart *chart = new QChart(); 10 chart->legend()->hide(); 11 chart->addSeries(series); 12 chart->setTitle("Simple spline chart example"); 13 chart->createDefaultAxes(); 14 chart->axisY()->setRange(0, 10); 15 16 QChartView *chartView = new QChartView(chart); 17 chartView->setRenderHint(QPainter::Antialiasing); 18 19 this->setCentralWidget(chartView); 20 this->setCentralWidget(chartView); 21 this->resize(400, 300); 22 this->show();
运行测试:
(3)绘制面积图
使用的类:LineChart
测试代码:
1 QLineSeries *series0 = new QLineSeries(); 2 QLineSeries *series1 = new QLineSeries(); 3 *series0 << QPointF(1, 5) << QPointF(3, 7) << QPointF(7, 6) << QPointF(9, 7) << QPointF(12, 6) 4 << QPointF(16, 7) << QPointF(18, 5); 5 *series1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF(8, 2) << QPointF(12, 3) 6 << QPointF(16, 4) << QPointF(18, 3); 7 8 QAreaSeries *series = new QAreaSeries(series0, series1); 9 series->setName("Batman"); 10 QPen pen(0x059605); 11 pen.setWidth(3); 12 series->setPen(pen); 13 14 QChart *chart = new QChart(); 15 chart->addSeries(series); 16 chart->setTitle("Simple areachart example"); 17 chart->createDefaultAxes(); 18 chart->axisX()->setRange(0, 20); 19 chart->axisY()->setRange(0, 10); 20 21 QChartView *chartView = new QChartView(chart); 22 chartView->setRenderHint(QPainter::Antialiasing); 23 24 this->setCentralWidget(chartView); 25 this->setCentralWidget(chartView); 26 this->resize(400, 300); 27 this->show();
运行测试:
(4)绘制散点图
使用的类: QScatterSeries
测试代码:
1 QScatterSeries *series0 = new QScatterSeries(); 2 series0->setName("scatter1"); 3 series0->setMarkerShape(QScatterSeries::MarkerShapeCircle); 4 series0->setMarkerSize(5.0); 5 6 QScatterSeries *series1 = new QScatterSeries(); 7 series1->setName("scatter2"); 8 series1->setMarkerShape(QScatterSeries::MarkerShapeRectangle); 9 series1->setMarkerSize(10.0); 10 11 QScatterSeries *series2 = new QScatterSeries(); 12 series2->setName("scatter3"); 13 series2->setMarkerShape(QScatterSeries::MarkerShapeRectangle); 14 series2->setMarkerSize(15.0); 15 16 series0->append(0, 6); 17 series0->append(2, 4); 18 series0->append(3, 8); 19 series0->append(7, 4); 20 series0->append(10, 5); 21 *series1 << QPointF(1, 1) << QPointF(3, 3) << QPointF(7, 6) << QPointF(8, 3) << QPointF(10, 2); 22 *series2 << QPointF(1, 5) << QPointF(4, 6) << QPointF(6, 3) << QPointF(9, 5); 23 24 QChart *chart = new QChart(); 25 chart->addSeries(series0); 26 chart->addSeries(series1); 27 chart->addSeries(series2); 28 chart->setTitle("Simple scatterchart example"); 29 chart->createDefaultAxes(); 30 chart->setDropShadowEnabled(false); 31 32 QChartView *chartView = new QChartView(chart); 33 chartView->setRenderHint(QPainter::Antialiasing); 34 35 this->setCentralWidget(chartView); 36 this->resize(400, 300); 37 this->show();
运行测试:
(5)绘制柱状图
使用的类:
测试代码:
1 QBarSet *set0 = new QBarSet("Jane"); 2 QBarSet *set1 = new QBarSet("John"); 3 QBarSet *set2 = new QBarSet("Axel"); 4 QBarSet *set3 = new QBarSet("Mary"); 5 QBarSet *set4 = new QBarSet("Samantha"); 6 7 *set0 << 1 << 2 << 3 << 4 << 5 << 6; 8 *set1 << 5 << 0 << 0 << 4 << 0 << 7; 9 *set2 << 3 << 5 << 8 << 13 << 8 << 5; 10 *set3 << 5 << 6 << 7 << 3 << 4 << 5; 11 *set4 << 9 << 7 << 5 << 3 << 1 << 2; 12 13 QBarSeries *series = new QBarSeries(); 14 series->append(set0); 15 series->append(set1); 16 series->append(set2); 17 series->append(set3); 18 series->append(set4); 19 20 QChart *chart = new QChart(); 21 chart->addSeries(series); 22 chart->setTitle("Simple barchart example"); 23 chart->setAnimationOptions(QChart::SeriesAnimations); 24 25 QStringList categories; 26 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; 27 QBarCategoryAxis *axis = new QBarCategoryAxis(); 28 axis->append(categories); 29 chart->createDefaultAxes(); 30 chart->setAxisX(axis, series); 31 32 chart->legend()->setVisible(true); 33 chart->legend()->setAlignment(Qt::AlignBottom); 34 35 QChartView *chartView = new QChartView(chart); 36 chartView->setRenderHint(QPainter::Antialiasing); 37 38 this->setCentralWidget(chartView); 39 this->resize(400, 300); 40 this->show();
运行测试:
(6)绘制饼图
使用的类:QPieSeries
测试代码:
1 QPieSeries *series = new QPieSeries(); 2 series->append("Jane", 1); 3 series->append("Joe", 2); 4 series->append("Andy", 3); 5 series->append("Barbara", 4); 6 series->append("Axel", 5); 7 8 QPieSlice *slice = series->slices().at(1); 9 slice->setExploded(); 10 slice->setLabelVisible(); 11 slice->setPen(QPen(Qt::darkGreen, 2)); 12 slice->setBrush(Qt::green); 13 14 QChart *chart = new QChart(); 15 chart->addSeries(series); 16 chart->setTitle("Simple piechart example"); 17 chart->legend()->hide(); 18 19 QChartView *chartView = new QChartView(chart); 20 chartView->setRenderHint(QPainter::Antialiasing); 21 22 this->setCentralWidget(chartView); 23 this->resize(400, 300); 24 this->show();
运行测试:
标签:Qt,series,chart,图表,new,chartView,绘制,QChartView,append 来源: https://www.cnblogs.com/mrlayfolk/p/13375358.html