0.创建ui窗口:
主要就是拖拉一个Horizontal Layout
1.创建QChart对象:
1 QChart *chart = new QChart();
2 chart->setTitle("面积图演示"); //设置表格标题
2.创建QLineSeries对象:
1 QLineSeries *series1 = new QLineSeries(chart);
2 series1->setName("A店铺接单数");
3 // 添加节点
4 series1->append(0,8);
5 series1->append(1,10);
6 series1->append(2,12);
7 series1->append(3,17);
8 series1->append(4,19);
9 series1->append(5,18);
10 series1->append(6,20);
11
12 QLineSeries *series2 = new QLineSeries(chart);
13 series2->setName("B店铺接单数");
14 series2->append(0,1);
15 series2->append(1,4);
16 series2->append(2,5);
17 series2->append(3,3);
18 series2->append(4,11);
19 series2->append(5,16);
20 series2->append(6,19);
3.创建QAreaSeries对象:
1 //(区域上限,区域下限)
2 QAreaSeries *area1 = new QAreaSeries(series1, 0);
3 QAreaSeries *area2 = new QAreaSeries(series2, series1);
4 area1->setName("A区域");
5 area2->setName("B区域");
4.将QAreaSeries添加到QChart
1 chart->addSeries(area1);
2 chart->addSeries(area2);
5.图表综合效果设置:
1 //设置动画效果
2 chart->setAnimationOptions(QChart::AllAnimations);
3
4 chart->createDefaultAxes();//设置网格线
5 chart->axes(Qt::Horizontal).first()->setRange(0, 7);//设置x轴范围
6 chart->axes(Qt::Vertical).first()->setRange(0, 20);//设置y轴范围
7
8 // Add space to label to add space between labels and axis 在标签和轴之间加空格
9 QValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());
10 Q_ASSERT(axisY);
11 axisY->setLabelFormat("%.1f ");
12
13 //设置图表主题(会修改到扇区的颜色)
14 // 可选: QChart::ChartThemeLight
15 // QChart::ChartThemeBlueCerulean
16 // QChart::ChartThemeDark
17 // QChart::ChartThemeBrownSand
18 // QChart::ChartThemeBlueNcs
19 // QChart::ChartThemeHighContrast
20 // QChart::ChartThemeBlueIcy
21 // QChart::ChartThemeQt
22 chart->setTheme(QChart::ChartThemeDark);
6.显示图表:
1 QChartView *chartView;
2 chartView = new QChartView(chart);
3 ui->verticalLayout->insertWidget(chartView);
代码整体呈现:
mainwindow.h
1 #ifndef MAINWINDOW_H
2 #define MAINWINDOW_H
3
4 #include <QMainWindow>
5 #include <QtCharts>
6
7 QT_BEGIN_NAMESPACE
8 namespace Ui { class MainWindow; }
9 QT_END_NAMESPACE
10
11 QT_CHARTS_BEGIN_NAMESPACE
12 class QChartView;
13 class QChart;
14 QT_CHARTS_END_NAMESPACE
15
16 QT_CHARTS_USE_NAMESPACE
17
18 class MainWindow : public QMainWindow
19 {
20 Q_OBJECT
21
22 public:
23 MainWindow(QWidget *parent = nullptr);
24 ~MainWindow();
25 QChart * createAreaChart() const; // 绘制自定义的面积图
26 private:
27 Ui::MainWindow *ui;
28 };
29 #endif // MAINWINDOW_H
mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3
4 MainWindow::MainWindow(QWidget *parent)
5 : QMainWindow(parent)
6 , ui(new Ui::MainWindow)
7 {
8 ui->setupUi(this);
9
10 // 创建QChartView 对象
11 QChartView *chartView;
12 // QChartView 对象载入面积图
13 chartView = new QChartView(createAreaChart());
14 // 显示图标
15 ui->verticalLayout->insertWidget(chartView);
16
17 }
18
19 MainWindow::~MainWindow()
20 {
21 delete ui;
22 }
23
24 //绘制面积图
25 QChart *MainWindow::createAreaChart() const
26 {
27
28 QChart *chart = new QChart();
29 chart->setTitle("面积图演示");
30
31 QLineSeries *series1 = new QLineSeries(chart);
32 series1->setName("A店铺接单数");
33
34 series1->append(0,8);
35 series1->append(1,10);
36 series1->append(2,12);
37 series1->append(3,17);
38 series1->append(4,19);
39 series1->append(5,18);
40 series1->append(6,20);
41
42 QLineSeries *series2 = new QLineSeries(chart);
43 series2->setName("B店铺接单数");
44 series2->append(0,1);
45 series2->append(1,4);
46 series2->append(2,5);
47 series2->append(3,3);
48 series2->append(4,11);
49 series2->append(5,16);
50 series2->append(6,19);
51
52 QAreaSeries *area1 = new QAreaSeries(series1, 0);
53 QAreaSeries *area2 = new QAreaSeries(series2, series1);
54 area1->setName("A区域");
55 area2->setName("B区域");
56 chart->addSeries(area1);
57 chart->addSeries(area2);
58 chart->createDefaultAxes();
59 chart->axes(Qt::Horizontal).first()->setRange(0, 6);
60 chart->axes(Qt::Vertical).first()->setRange(0, 20);
61
62 // Add space to label to add space between labels and axis在标签和轴之间加空格
63 QValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());
64 Q_ASSERT(axisY);
65 axisY->setLabelFormat("%.1f ");
66 chart->setTheme(QChart::ChartThemeBrownSand);
67 return chart;
68 }