QT dessine un graphe QSplineSeries

Liens connexes:
QT dessine un histogramme BarChart
QT dessine un camembert PieChart
QT dessine un graphique linéaire LineChart
QT dessine un nuage de points ScatterChart
QT dessine un graphique en aires AreaChart

0. Créer une fenêtre d'interface utilisateur:

Faites glisser principalement unHorizontal Layout
Insérez la description de l'image ici
Insérez la description de l'image ici

1. Créez un objet Qchart:

QChart *chart = new QChart();
chart->setTitle("折线图图演示"); //设置表格标题

2. Créez un objet QSplineSeries:

QSplineSeries *series1 = new QSplineSeries(chart);
QSplineSeries *series2 = new QSplineSeries(chart);
// 
series1->setName("A店铺接单数");
series2->setName("B店铺接单数");
// 设置节点数值
*series1 << QPointF(0,4) << QPointF(2,7) << QPointF(3,9) << QPointF(8,13) << QPointF(9,16);
*series2 << QPointF(0,1) << QPointF(3,4) << QPointF(4,7) << QPointF(6,11) << QPointF(8,15)<< QPointF(9,18);

3. Ajouter QSplineSeries à QChart

chart->addSeries(series1);
chart->addSeries(series2);

4. Tableau des paramètres d'effets complets:

//设置动画效果
chart->setAnimationOptions(QChart::AllAnimations);

chart->createDefaultAxes();//设置网格线
chart->axes(Qt::Horizontal).first()->setRange(0, 7);//设置x轴范围
chart->axes(Qt::Vertical).first()->setRange(0, 20);//设置y轴范围

// Add space to label to add space between labels and axis 在标签和轴之间加空格
QValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());
Q_ASSERT(axisY);
axisY->setLabelFormat("%.1f  ");

//设置图表主题(会修改到扇区的颜色)
// 可选: QChart::ChartThemeLight
// QChart::ChartThemeBlueCerulean
// QChart::ChartThemeDark
// QChart::ChartThemeBrownSand
// QChart::ChartThemeBlueNcs
// QChart::ChartThemeHighContrast
// QChart::ChartThemeBlueIcy
// QChart::ChartThemeQt
chart->setTheme(QChart::ChartThemeDark);

5. Afficher le graphique:

QChartView *chartView;
chartView = new QChartView(chart);
ui->verticalLayout->insertWidget(chartView);

La présentation générale du code:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtCharts>

QT_BEGIN_NAMESPACE
namespace Ui {
    
     class MainWindow; }
QT_END_NAMESPACE

QT_CHARTS_BEGIN_NAMESPACE
class QChartView;
class QChart;
QT_CHARTS_END_NAMESPACE

QT_CHARTS_USE_NAMESPACE

class MainWindow : public QMainWindow
{
    
    
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    QChart * createSplineChart() const; // 绘制自定义的曲线图
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);

	// 创建QChartView 对象
	QChartView *chartView;
	// QChartView 对象载入曲线图
	chartView = new QChartView(createSplineChart());
	// 显示图标
	ui->verticalLayout->insertWidget(chartView);

}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}

//绘制曲线图
QChart *MainWindow::createSplineChart() const
{
    
    
    QChart *chart = new QChart();
    chart->setTitle("曲线图演示");


    QSplineSeries *series1 = new QSplineSeries(chart);
    QSplineSeries *series2 = new QSplineSeries(chart);
    series1->setName("A店铺接单数");
    series2->setName("B店铺接单数");
    *series1 << QPointF(0,4) << QPointF(2,7) << QPointF(3,9) << QPointF(8,13) << QPointF(9,16);
    *series2 << QPointF(0,1) << QPointF(3,4) << QPointF(4,7) << QPointF(6,11) << QPointF(8,15)<< QPointF(9,18);

    chart->addSeries(series1);
    chart->addSeries(series2);


    chart->createDefaultAxes();
    chart->axes(Qt::Horizontal).first()->setRange(0, 9);
    chart->axes(Qt::Vertical).first()->setRange(0, 20);

    // Add space to label to add space between labels and axis 在标签和轴之间加空格
    QValueAxis *axisY = qobject_cast<QValueAxis*>(chart->axes(Qt::Vertical).first());
    Q_ASSERT(axisY);
    axisY->setLabelFormat("%.1f  ");
    chart->setTheme(QChart::ChartThemeLight);
    return chart;
}

Insérez la description de l'image ici

Je suppose que tu aimes

Origine blog.csdn.net/sazass/article/details/112892959
conseillé
Classement