【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 的抛物线图#includemainwindow.h#includeui_mainwindow.h#includeQTimer#includemywidget.hvoidTickerFixedDemo(QCustomPlot*customPlot){// 数据y x^2QVectordoublex(101),y(101);for(inti0;i101;i){x[i]i/50.0-1;y[i]x[i]*x[i];}customPlot-addGraph();customPlot-graph(0)-setData(x,y);QSharedPointerQCPAxisTickerFixedticker(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*customPlotnewQCustomPlot(this);setCentralWidget(customPlot);TickerFixedDemo(customPlot);}MainWindow::~MainWindow(){deleteui;}2、QCPAxisTickerLog —— 对数刻度用于对数坐标轴常用于数据跨越多个数量级时。Demo: 对数 X 轴的指数衰减曲线// 2. QCPAxisTickerLog对数刻度 voidTickerLogDemo(QCustomPlot*customPlot){// 创建指数数据QVectordoublex(100),y(100);for(inti0;i100;i){x[i]i1;// 1 到 100y[i]qExp(x[i]/20.0);// 指数增长}customPlot-addGraph();customPlot-graph(0)-setData(x,y);// 设置对数刻度器Y轴QSharedPointerQCPAxisTickerLoglogTicker(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){QVectordoublex,y;for(doublexi-2*M_PI;xi2*M_PI;xi0.01){xxi;yqSin(xi);}customPlot-addGraph();customPlot-graph(0)-setData(x,y);QSharedPointerQCPAxisTickerPipiTicker(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#includeQApplication#includeqcustomplot.hintmain(intargc,char*argv[]){QApplicationa(argc,argv);QCustomPlot plot;// 数据7 天销售额QVectordoublekeys{0,1,2,3,4,5,6};// 数值键QVectorQStringlabels{Mon,Tue,Wed,Thu,Fri,Sat,Sun};QVectordoublevalues{12,19,15,18,22,30,25};plot.addGraph();plot.graph(0)-setData(keys,values);// 设置文本刻度器QSharedPointerQCPAxisTickerTexttextTicker(newQCPAxisTickerText);for(inti0;ikeys.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 nowQDateTime::currentDateTime().addDays(-7);// 从7天前开始QVectordoubletimes,values;for(inthour0;hour24*7;hour){QDateTime dtnow.addSecs(3600*hour);timesdt.toSecsSinceEpoch();values2010*qSin(hour*0.2);}customPlot-addGraph();customPlot-graph(0)-setData(times,values);QSharedPointerQCPAxisTickerDateTimedtTicker(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){QVectordoubleseconds,load;for(ints0;s3*3600;s60){secondss;load5030*qSin(s/600.0);}customPlot-addGraph();customPlot-graph(0)-setData(seconds,load);QSharedPointerQCPAxisTickerTimetimeTicker(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 1970setDateTimeFormat()QCPAxisTickerTime时:分:秒秒时间偏移setTimeFormat()