相关链接:
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