当前位置: 首页 > backend >正文

利用qcustomplot绘制曲线图

本文详细介绍了qcustomplot绘制曲线图的流程,一段代码一段代码运行看效果。通过阅读本文,读者可以了解到每一项怎么用代码进行配置,进而实现自己想要的图表效果。(本文只针对曲线图)

1 最简单的图形(入门)

  • 头文件
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include "qcustomplot.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();public:void paintLine(QCustomPlot *customPlot);private:Ui::Widget *ui;
};
#endif // WIDGET_H
  • 源文件
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);paintLine(ui->lineWidget);}Widget::~Widget()
{delete ui;
}void Widget::paintLine(QCustomPlot *customPlot)
{QVector<double> x(101), y(101);for (int i = 0; i < x.size(); i++){x[i] = i / 50.0 - 1;    // x从-1 到 1y[i] = x[i] * x[i];   // 二次函数}customPlot->addGraph();    // 添加一幅折线图customPlot->graph(0)->setData(x, y);    // 为曲线图添加数据 注意这个方法只能是double类型 库内就是这样声明与定义的customPlot->graph(0)->setName("y=x^{2}");// 设置x与y坐标轴的范围customPlot->xAxis->setRange(-1, 1);customPlot->yAxis->setRange(0, 1);// 设置x与y坐标轴的标签customPlot->xAxis->setLabel("x");customPlot->yAxis->setLabel("y");// 显示图例customPlot->legend->setVisible(true);
}
  • 运行效果

在这里插入图片描述

2 美化一下

美化之前,先有几个名词声明一下

  • 轴线
  • 轴刻度线(长的刻度线)
  • 轴子刻度线(短的刻度线)
  • 轴刻度值
  • 轴标签

2.1 设置背景颜色、轴、刻度线、刻度值、刻度标签

    QLinearGradient plotGradient;plotGradient.setStart(0, 0);plotGradient.setFinalStop(0, 350);plotGradient.setColorAt(0, QColor(80, 80, 80));plotGradient.setColorAt(1, QColor(50, 50, 50));customPlot->setBackground(plotGradient);      // 设置背景颜色

在这里插入图片描述

customPlot->axisRect()->setBackground(Qt::red);

在这里插入图片描述

// 设置轴风格customPlot->xAxis->setBasePen(QPen(Qt::white, 1));

在这里插入图片描述

customPlot->xAxis->setTickPen(QPen(Qt::white, 1));    // 轴刻度线和画笔

在这里插入图片描述

注意这两张图像的区别,非常微小的区别

上边这张是设置轴,下边这张是设置轴刻度

customPlot->xAxis->setSubTickPen(QPen(Qt::white, 1));  // 轴子刻度线的画笔

在这里插入图片描述
发现区别了吗?发现具体配置的效果显示在哪了吗?如果没有发现,请仔细对比。

customPlot->xAxis->setTickLabelColor(Qt::white);  // 设置轴刻度颜色

在这里插入图片描述

    customPlot->xAxis->setLabel("标签");                  // 只有设置了标签,轴标签颜色才会显示customPlot->xAxis->setLabelColor(Qt::white);          // 设置轴标签颜色

在这里插入图片描述

    customPlot->xAxis->setTickLengthIn(133);   // 为了效果明显 将值设置的大一点,原来是这个效果 轴线内刻度的长度

在这里插入图片描述

customPlot->xAxis->setTickLengthOut(125);    // 为了效果明显 将值设置的大一点,原来是这个效果 轴线外刻度的长度

在这里插入图片描述

customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow); // 设置轴结束的箭头

在这里插入图片描述

	// 设置网格的风格customPlot->xAxis->grid()->setPen(QPen(Qt::red, 1, Qt::DotLine));

在这里插入图片描述

    // 设置水平线customPlot->yAxis->grid()->setPen(QPen(Qt::yellow, 1, Qt::DotLine));

在这里插入图片描述

    // 设置子刻度线customPlot->xAxis->grid()->setSubGridVisible(true);customPlot->xAxis->grid()->setSubGridPen(QPen(Qt::blue, 1, Qt::DotLine));

在这里插入图片描述

 	customPlot->yAxis->grid()->setSubGridVisible(true);customPlot->yAxis->grid()->setSubGridPen(QPen(Qt::green, 1, Qt::DashLine));

在这里插入图片描述

    // 设置刻度为0时的网格线的画笔customPlot->xAxis->grid()->setZeroLinePen(QPen(Qt::red, 3));customPlot->yAxis->grid()->setZeroLinePen(QPen(Qt::red));

在这里插入图片描述

至此,有关图像轴的设置就介绍完了

2.2 线型

利用官网的例子进行介绍

    // 图表的风格QPen pen;// 存放曲线的风格名称QStringList lineNames;lineNames << "lsNone" << "lsLine" << "lsStepLeft" << "lsStepRight" << "lsStrepCenter" << "lsImpulse";for(int i = 0; i < lineNames.size(); i++){customPlot->addGraph();pen.setColor(QColor(qSin(i * 1 + 1.2) * 80 + 80, qSin(i * 0.3) * 80 + 80, qSin(i * 0.3 + 1.5) * 80 + 80));customPlot->graph()->setPen(pen);customPlot->graph()->setName(lineNames.at(i));customPlot->graph()->setLineStyle((QCPGraph::LineStyle)i);customPlot->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 6));QVector<double> x(15), y(15);for (int j = 0; j < x.size(); j++){x[j] = j / 15.0 * 5 * 3.14 + 0.01;y[j] = 7 * qSin(x[j]) / x[j]  - (i - QCPGraph::lsNone) * 5 + (QCPGraph::lsImpulse) * 5 + 2;}customPlot->graph()->setData(x, y);customPlot->graph()->rescaleAxes(true);}

在这里插入图片描述

本文参考:

https://blog.csdn.net/qq10097355/article/details/104845706https://www.qcustomplot.com/  官网
http://www.xdnf.cn/news/12756.html

相关文章:

  • audio-ovrlipsync-viseme-reference 口型同步 唇形同步 插件
  • Linux系统 - 线程 -6- 线程安全函数和可重入函数
  • Qt的学习(一)
  • Hash类型
  • 题海拾贝:P1091 [NOIP 2004 提高组] 合唱队形
  • WSF07N10 MOSFET 在铲皮机中的应用
  • WebFuture 系统升级提示外键约束的问题处理
  • 【判断既约分数】2022-4-3
  • 图卷积网络:从理论到实践
  • NumPy数组创建
  • C++11新增重要标准(下)
  • mysql 主从复制和分库分表
  • 2000-2020年各省第一产业增加值占GDP比重数据
  • python打卡day47@浙大疏锦行
  • 20250607在荣品的PRO-RK3566开发板的Android13的uboot中使用gpio命令来配置GPIO的状态
  • JavaScript篇:字母侦探:如何快速统计字符串里谁才是‘主角‘?
  • 开疆智能Ethernet/IP转Modbus网关连接施耐德ATV320变频器配置案例
  • 添加禁用状态
  • 【LeetCode】3170. 删除星号以后字典序最小的字符串(贪心 | 优先队列)
  • 开疆智能Ethernet/IP转Modbus网关连接质量流量计配置案例
  • 力扣刷题(第五十天)
  • 海伯森超高速工业相机:超高帧率,工业视觉新 “视” 力
  • Linux(生产消费者模型/线程池)
  • 一类简单而特殊数列的通项公式求法
  • 16-Oracle 23 ai-JSON-Relational Duality-知识准备
  • 靶场(二十)---靶场体会小白心得 ---jacko
  • Docker安装MQEX
  • MobaXterm配置跳转登录堡垒机
  • 详解二叉树遍历的非递归实现
  • Flask与Celery 项目应用(shared_task使用)