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

【QCustomPlot教程09】QCustomPlot 刻度器系统

【QCustomPlot教程09】QCustomPlot 刻度器系统

  • 一、简介
    • 二、6 种常用刻度器
    • 1、QCPAxisTickerFixed —— 固定间隔刻度
    • 2、QCPAxisTickerLog —— 对数刻度
    • 3、QCPAxisTickerPi —— π 刻度
    • 4、QCPAxisTickerText —— 文本刻度
    • 5、QCPAxisTickerDateTime —— 日期时间刻度
    • 6、QCPAxisTickerTime —— 时间刻度(时:分:秒)
  • 三、总结对比表

原创作者:郑同学的笔记
原文链接:https://zhengjunxue.blog.csdn.net/article/details/155241911

一、简介

QCustomPlot 刻度器系统详解
在 QCustomPlot 中,坐标轴上的刻度(tick)和标签(label)由 刻度器(Ticker) 控制。

所有刻度器都继承自基类 QCPAxisTicker,通过 axis->setTicker(ticker) 设置。

基类:QCPAxisTicker
虚基类,定义了生成刻度的核心接口。
实际使用中,我们总是使用其子类。

QCustomPlot 刻度器(Ticker),包含6 种常用刻度器

二、6 种常用刻度器

1、QCPAxisTickerFixed —— 固定间隔刻度

适用于需要固定步长的线性轴。

Demo: 固定步长 0.5 的抛物线图

#include"mainwindow.h"#include"ui_mainwindow.h"#include<QTimer>#include"mywidget.h"voidTickerFixedDemo(QCustomPlot*customPlot){// 数据:y = x^2QVector<double>x(101),y(101);for(inti=0;i<101;++i){x[i]=i/50.0-1;y[i]=x[i]*x[i];}customPlot->addGraph();customPlot->graph(0)->setData(x,y);QSharedPointer<QCPAxisTickerFixed>ticker(newQCPAxisTickerFixed);ticker->setTickStep(0.5);customPlot->xAxis->setTicker(ticker);customPlot->xAxis->setRange(-1,1);customPlot->yAxis->setRange(0,1);customPlot->replot();}MainWindow::MainWindow(QWidget*parent):QMainWindow(parent),ui(newUi::MainWindow){ui->setupUi(this);QCustomPlot*customPlot=newQCustomPlot(this);setCentralWidget(customPlot);TickerFixedDemo(customPlot);}MainWindow::~MainWindow(){deleteui;}

2、QCPAxisTickerLog —— 对数刻度

用于对数坐标轴,常用于数据跨越多个数量级时。

Demo: 对数 X 轴的指数衰减曲线

// ========== 2. QCPAxisTickerLog:对数刻度 ==========voidTickerLogDemo(QCustomPlot*customPlot){// 创建指数数据QVector<double>x(100),y(100);for(inti=0;i<100;++i){x[i]=i+1;// 1 到 100y[i]=qExp(x[i]/20.0);// 指数增长}customPlot->addGraph();customPlot->graph(0)->setData(x,y);// 设置对数刻度器(Y轴)QSharedPointer<QCPAxisTickerLog>logTicker(newQCPAxisTickerLog);logTicker->setLogBase(10);logTicker->setSubTickCount(9);// 每个数量级9个子刻度customPlot->yAxis->setTicker(logTicker);customPlot->yAxis->setScaleType(QCPAxis::stLogarithmic);customPlot->xAxis->setRange(0,100);customPlot->yAxis->setRange(1,1000);customPlot->replot();}

注意:必须同时调用 axis->setScaleType(QCPAxis::stLogarithmic)。

3、QCPAxisTickerPi —— π 刻度

专为三角函数设计,自动将刻度显示为 π 的分数形式(如 π/2, π, 3π/2…)。

Demo: 正弦函数,X 轴用 π 刻度

// ========== 3. QCPAxisTickerPi:π刻度 ==========voidTickerPiDemo(QCustomPlot*customPlot){QVector<double>x,y;for(doublexi=-2*M_PI;xi<=2*M_PI;xi+=0.01){x<<xi;y<<qSin(xi);}customPlot->addGraph();customPlot->graph(0)->setData(x,y);QSharedPointer<QCPAxisTickerPi>piTicker(newQCPAxisTickerPi);piTicker->setPiSymbol(QString::fromUtf8("π"));piTicker->setPiValue(M_PI);piTicker->setTickCount(8);customPlot->xAxis->setTicker(piTicker);customPlot->xAxis->setRange(-2*M_PI,2*M_PI);customPlot->yAxis->setRange(-1.2,1.2);customPlot->replot();}

4、QCPAxisTickerText —— 文本刻度

将数值映射为自定义文本标签,适用于分类数据(如“周一”、“周二”)。

Demo: 每日销售额(X 轴为星期几)

// main.cpp#include<QApplication>#include"qcustomplot.h"intmain(intargc,char*argv[]){QApplicationa(argc,argv);QCustomPlot plot;// 数据:7 天销售额QVector<double>keys={0,1,2,3,4,5,6};// 数值键QVector<QString>labels={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};QVector<double>values={12,19,15,18,22,30,25};plot.addGraph();plot.graph(0)->setData(keys,values);// 设置文本刻度器QSharedPointer<QCPAxisTickerText>textTicker(newQCPAxisTickerText);for(inti=0;i<keys.size();++i){textTicker->addTick(keys[i],labels[i]);// 键 → 标签}plot.xAxis->setTicker(textTicker);plot.xAxis->setTicks(true);plot.xAxis->setAutoTicks(false);// 禁用自动生成plot.xAxis->setRange(-0.5,6.5);plot.yAxis->setRange(0,35);plot.replot();plot.show();returna.exec();}

5、QCPAxisTickerDateTime —— 日期时间刻度

用于显示日期+时间,底层使用 QDateTime(单位:秒 since 1970-01-01 UTC)。

Demo: 过去 7 天的数据(每小时一个点)

// ========== 5. QCPAxisTickerDateTime:日期时间刻度 ==========voidTickerDateTimeDemo(QCustomPlot*customPlot){QDateTime now=QDateTime::currentDateTime().addDays(-7);// 从7天前开始QVector<double>times,values;for(inthour=0;hour<24*7;++hour){QDateTime dt=now.addSecs(3600*hour);times<<dt.toSecsSinceEpoch();values<<20+10*qSin(hour*0.2);}customPlot->addGraph();customPlot->graph(0)->setData(times,values);QSharedPointer<QCPAxisTickerDateTime>dtTicker(newQCPAxisTickerDateTime);dtTicker->setDateTimeFormat("MM-dd\nhh:mm");dtTicker->setTickCount(8);customPlot->xAxis->setTicker(dtTicker);customPlot->xAxis->setRange(times.first(),times.last());customPlot->yAxis->setRange(0,35);customPlot->replot();}

注意:QCustomPlot 的 DateTime 刻度器要求 X 数据为 自 1970-01-01 UTC 起的秒数(double)。

6、QCPAxisTickerTime —— 时间刻度(时:分:秒)

专用于时间间隔(如 00:00:00 到 02:30:00),不包含日期。

Demo: 3 小时内的任务耗时监控

// ========== 6. QCPAxisTickerTime:时间刻度(时:分:秒) ==========voidTickerTimeDemo(QCustomPlot*customPlot){QVector<double>seconds,load;for(ints=0;s<=3*3600;s+=60){seconds<<s;load<<50+30*qSin(s/600.0);}customPlot->addGraph();customPlot->graph(0)->setData(seconds,load);QSharedPointer<QCPAxisTickerTime>timeTicker(newQCPAxisTickerTime);timeTicker->setTimeFormat("%h:%m");timeTicker->setTickCount(7);customPlot->xAxis->setTicker(timeTicker);customPlot->xAxis->setRange(0,3*3600);customPlot->yAxis->setRange(0,100);customPlot->replot();}

QCPAxisTickerTime 的输入是纯秒数(double),表示从 00:00:00 开始的时间偏移。

三、总结对比表

刻度器用途数据类型关键设置
QCPAxisTickerFixed固定步长doublesetTickStep()
QCPAxisTickerLog对数轴>0 doublesetScaleType(stLogarithmic)
QCPAxisTickerPi三角函数double (弧度)setPiSymbol(), setPiValue()
QCPAxisTickerText分类标签double (作为键)addTick(key, label)
QCPAxisTickerDateTime日期+时间秒(since 1970)setDateTimeFormat()
QCPAxisTickerTime时:分:秒秒(时间偏移)setTimeFormat()
http://www.jsqmd.com/news/1186154/

相关文章:

  • 2026年7月最新石家庄万国官方售后客服中心地址电话及服务网点分布 - 万国中国官方服务中心
  • C语言旅程之扫雷游戏(基础版讲解)
  • 如何快速上手SkinTokens-bf16:从零开始实现3D模型自动绑定的完整教程
  • 从零实现C语言Web服务器:深入解析Socket、HTTP与Linux网络编程
  • 2026福清市暴雨多发!楼顶阳光房阳台窗边漏水如何彻底解决? - 宅安选房屋修缮
  • AI驱动的跨境电商智能体集群解决方案
  • 2026 收钻石不踩坑,当场结算无套路|深圳本地6家钻石回收门店实测盘点 - 名奢变现站
  • 银行级多维聚合:生产环境下的pandas实战指南
  • C/C++服装销售管理系统:从零到一的项目实战指南
  • 智能化的用户体验创新方法
  • 从0到1部署SeedVC-MLX:Linux环境下的完整安装与运行指南 [特殊字符]
  • 本地大模型也能跑!SmartShell接入Ollama教程:离线使用AI命令生成
  • 告别原生终端:在Windows 11/10中配置XShell与WinSCP高效连接WSL2 Ubuntu
  • 深入理解Phi-3-mini-4k-instruct_rai_1.7.1_hybrid的架构设计:终极指南
  • 2026年7月最新天梭南京鼓楼吾悦广场维修保养服务电话 - 天梭服务中心
  • 芯片制造探秘:从硅石到晶圆的工艺全景与市场格局
  • 新手也能学会:用微信自带功能给照片加文字水印 - 软件工具教程方法
  • 多维聚合实战:从GROUP BY到立方体思维的数据操作
  • NV-KERMT-70M-v2代码实现详解:从SMILES解析到分子嵌入生成
  • 2026年保密单位私有化即时通讯横向对比 - IM软件测评
  • OpenViking 上下文数据库 | 08 - 会话如何变成长时记忆:Session 与 Compressor
  • 如何提升后端代码的可维护性:设计模式应用
  • DGRunkeeperSwitch动画优化:打造流畅滑动切换效果的终极指南
  • Bitters核心变量全解析:掌握Sass样式定制的黄金法则
  • 2026筑宅安|榆林卫生间漏水专业维修,解决墙面潮湿发霉、渗水到楼下难题 - 筑宅安
  • 2026年杭州江干区拱墅区家电维修业主真实体验分享 - 简单到家
  • Viola-Jones检测器(VJ)--- 从理论到实践的工程化拆解
  • 避免照片被盗用?2026年加水印的正确操作步骤 - 软件工具教程方法
  • 从配置到推理:SmolLM2-135M-Instruct_rai_1.7.1_hybrid模型参数详解与调优技巧
  • 专网环境下企业即时通讯要检查哪些边界 - 内网通讯研究