【QT】绘制散点图QScatterSeries

该博客介绍了如何使用QT库来创建一个散点图,包括设置图表标题、创建QScatterSeries对象、添加数据点、调整轴的范围以及设置图表主题等步骤。通过示例代码展示了A店铺和B店铺的接单数量对比,帮助读者理解QT中散点图的绘制过程。
摘要由CSDN通过智能技术生成

相关链接:
QT绘制直方图BarChart
QT绘制饼状图PieChart
QT绘制曲线图SplineChart
QT绘制折线图LineChart
QT绘制面积图AreaChart

0.创建ui窗口:

主要就是拖拉一个Horizontal Layout
在这里插入图片描述
在这里插入图片描述

1.创建Qchart对象:

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

2.创建QScatterSeries对象:

QScatterSeries *scatterSeries1 = new QScatterSeries(chart);
QScatterSeries *scatterSeries2 = new QScatterSeries(chart);

scatterSeries1->setName("A店铺接单数");
scatterSeries1->setPointLabelsFormat("@yPoint");
scatterSeries1->setPointLabelsVisible();
scatterSeries1->setMarkerSize(16); // 设置节点大小

scatterSeries2->setName("B店铺接单数");
scatterSeries2->setPointLabelsFormat("@yPoint");
scatterSeries2->setPointLabelsVisible();
scatterSeries2->setMarkerSize(16);

// 添加节点
scatterSeries1->append(0,6);
scatterSeries1->append(1,10);
scatterSeries1->append(4,12);
scatterSeries1->append(6,5);
scatterSeries2->append(0,18);
scatterSeries2->append(3,13);
scatterSeries2->append(5,7);
scatterSeries2->append(6,2);

3.将QScatterSeries添加到QChart

chart->addSeries(scatterSeries1);
chart->addSeries(scatterSeries2);

4.图表综合效果设置:

//设置动画效果
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.显示图表:

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

代码整体呈现:

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 * createScatterChart() 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(createScatterChart());
	// 显示图标
	ui->verticalLayout->insertWidget(chartView);

}

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

// 绘制散点图
QChart *MainWindow::createScatterChart() const
{
    // scatter chart
    QChart *chart = new QChart();
    chart->setTitle("散点图演示");

    QScatterSeries *scatterSeries1 = new QScatterSeries(chart);
    QScatterSeries *scatterSeries2 = new QScatterSeries(chart);

    scatterSeries1->setName("A店铺接单数");
    scatterSeries1->setPointLabelsFormat("@yPoint");
    scatterSeries1->setPointLabelsVisible();
    scatterSeries1->setMarkerSize(16);

    scatterSeries2->setName("B店铺接单数");
    scatterSeries2->setPointLabelsFormat("@yPoint");
    scatterSeries2->setPointLabelsVisible();
    scatterSeries2->setMarkerSize(16);

    scatterSeries1->append(0,6);
    scatterSeries1->append(1,10);
    scatterSeries1->append(4,12);
    scatterSeries1->append(6,5);
    scatterSeries2->append(0,18);
    scatterSeries2->append(3,13);
    scatterSeries2->append(5,7);
    scatterSeries2->append(6,2);
    chart->addSeries(scatterSeries1);
    chart->addSeries(scatterSeries2);


    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  ");

	chart->setTheme(QChart::ChartThemeLight);
    return chart;

}

在这里插入图片描述
演示代码
链接:https://pan.baidu.com/s/1LkdFXi6LSvYtbU0W3uz0TQ
提取码:marl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值