【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()