使用Qt的charts模块来绘制图表,案例来自Qt自带的demo。
charts模块简介:Qt Chars模块提供了一系列容易使用的图表组件。需要使用charts组件时,需要导入Qt Charts模块,通过如下方式:QT += charts
在安装Qt Creator的时候,需要勾选上这个模块,不然是不能使用的。
Qt Chars支持绘制:坐标图、柱状图、折线图、饼图、曲线图、散点图等。
首先创建一个工程,继承至MainWindow组件。
(1)绘制折线图
折线图通过线段展示一系列相关联的点。
使用的类:QLineSeries
测试代码:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/\*
\* LineChart Example
\*/
//添加一个QLineSeries实例
QLineSeries \*lineseries = new QLineSeries();
//添加数据(点),有两种方式
lineseries->append(, ); //参数分别为横纵坐标
lineseries->append(, );
lineseries->append(, );
lineseries->append(, );
\*lineseries << QPointF(, ) << QPointF(, ) << QPointF(, ) << QPointF(, ) << QPointF(, );
//添加一个QChart实例,为了展示数据
QChart \*chart = new QChart();
chart->legend()->hide(); //隐藏图例
chart->addSeries(lineseries);
chart->createDefaultAxes();
chart->setTitle("Simple line chart example");
//添加一个QChartView图例
QChartView \*chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing); //呈现方式
//显示
this->setCentralWidget(chartView);
this->setCentralWidget(chartView);
this->resize(, );
this->show();
}
运行测试:
(2)绘制曲线图
使用的类:QSplineSeries
测试代码:
QSplineSeries \*series = new QSplineSeries();
series->setName("spline");
series->append(, );
series->append(, );
series->append(, );
series->append(, );
series->append(, );
QChart \*chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->setTitle("Simple spline chart example");
chart->createDefaultAxes();
chart->axisY()->setRange(, );
QChartView \*chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
this->setCentralWidget(chartView);
this->setCentralWidget(chartView);
this->resize(, );
this->show();
运行测试:
(3)绘制面积图
使用的类:LineChart
测试代码:
QLineSeries \*series0 = new QLineSeries();
QLineSeries \*series1 = new QLineSeries();
\*series0 << QPointF(, ) << QPointF(, ) << QPointF(, ) << QPointF(, ) << QPointF(, )
<< QPointF(, ) << QPointF(, );
\*series1 << QPointF(, ) << QPointF(, ) << QPointF(, ) << QPointF(, ) << QPointF(, )
<< QPointF(, ) << QPointF(, );
QAreaSeries \*series = new QAreaSeries(series0, series1);
series->setName("Batman");
QPen pen(0x059605);
pen.setWidth();
series->setPen(pen);
QChart \*chart = new QChart();
chart->addSeries(series);
chart->setTitle("Simple areachart example");
chart->createDefaultAxes();
chart->axisX()->setRange(, );
chart->axisY()->setRange(, );
QChartView \*chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
this->setCentralWidget(chartView);
this->setCentralWidget(chartView);
this->resize(, );
this->show();
运行测试:
(4)绘制散点图
使用的类: QScatterSeries
测试代码:
QScatterSeries \*series0 = new QScatterSeries();
series0->setName("scatter1");
series0->setMarkerShape(QScatterSeries::MarkerShapeCircle);
series0->setMarkerSize(5.0);
QScatterSeries \*series1 = new QScatterSeries();
series1->setName("scatter2");
series1->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
series1->setMarkerSize(10.0);
QScatterSeries \*series2 = new QScatterSeries();
series2->setName("scatter3");
series2->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
series2->setMarkerSize(15.0);
series0->append(, );
series0->append(, );
series0->append(, );
series0->append(, );
series0->append(, );
\*series1 << QPointF(, ) << QPointF(, ) << QPointF(, ) << QPointF(, ) << QPointF(, );
\*series2 << QPointF(, ) << QPointF(, ) << QPointF(, ) << QPointF(, );
QChart \*chart = new QChart();
chart->addSeries(series0);
chart->addSeries(series1);
chart->addSeries(series2);
chart->setTitle("Simple scatterchart example");
chart->createDefaultAxes();
chart->setDropShadowEnabled(false);
QChartView \*chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
this->setCentralWidget(chartView);
this->resize(, );
this->show();
运行测试:
(5)绘制柱状图
使用的类:
测试代码:
QBarSet \*set0 = new QBarSet("Jane");
QBarSet \*set1 = new QBarSet("John");
QBarSet \*set2 = new QBarSet("Axel");
QBarSet \*set3 = new QBarSet("Mary");
QBarSet \*set4 = new QBarSet("Samantha");
\*set0 << << << << << << ;
\*set1 << << << << << << ;
\*set2 << << << << << << ;
\*set3 << << << << << << ;
\*set4 << << << << << << ;
QBarSeries \*series = new QBarSeries();
series->append(set0);
series->append(set1);
series->append(set2);
series->append(set3);
series->append(set4);
QChart \*chart = new QChart();
chart->addSeries(series);
chart->setTitle("Simple barchart example");
chart->setAnimationOptions(QChart::SeriesAnimations);
QStringList categories;
categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
QBarCategoryAxis \*axis = new QBarCategoryAxis();
axis->append(categories);
chart->createDefaultAxes();
chart->setAxisX(axis, series);
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
QChartView \*chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
this->setCentralWidget(chartView);
this->resize(, );
this->show();
运行测试:
(6)绘制饼图
使用的类:QPieSeries
测试代码:
QPieSeries \*series = new QPieSeries();
series->append("Jane", );
series->append("Joe", );
series->append("Andy", );
series->append("Barbara", );
series->append("Axel", );
QPieSlice \*slice = series->slices().at();
slice->setExploded();
slice->setLabelVisible();
slice->setPen(QPen(Qt::darkGreen, ));
slice->setBrush(Qt::green);
QChart \*chart = new QChart();
chart->addSeries(series);
chart->setTitle("Simple piechart example");
chart->legend()->hide();
QChartView \*chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
this->setCentralWidget(chartView);
this->resize(, );
this->show();
运行测试:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章