QCustomPlot自定義Y軸數字格式


QCustomPlot自定義Y軸數字格式

需求背景

某個項目需要顯示一長串曲線圖,其Y軸坐標范圍變化幅度很大,用了默認的配置,由於坐標軸刻度標簽長度不一,曲線總體表現得很難看。

自定義途徑

QCustomPlot 源碼版本是:2.0.1

經過仔細研讀QCustomPlot的源碼,發現設置標簽格式的函數是:

QString QCPAxisTicker::getTickLabel(double tick, const QLocale &locale, QChar formatChar, int precision)
{
  return locale.toString(tick, formatChar.toLatin1(), precision);
}

我需要的標簽最長不超過9個字符,所以
源代碼之外的配置如下:

    customPlot->yAxis->setNumberFormat("f");
    customPlot->yAxis->setNumberPrecision(3);

同時對上述庫函數做了如下修改:

QString QCPAxisTicker::getTickLabel(double tick, const QLocale &locale, QChar formatChar, int precision)
{// 2019-06-28 02:10:22 Mony:修改,最大顯示長度為9
  (void)locale;
  return QString("%1").arg(tick,9,formatChar.toLatin1(), precision);
}

最終效果

在這里插入圖片描述

題外話

  • 修改Y軸自適應范圍的代碼
customPlot->yAxis->rescale(true);
  • 修改X軸動態曲線顯示的代碼(顯示范圍10分鍾)
customPlot->graph(0)->addData(key,value);
customPlot->graph(0)->data().data()->removeBefore(key-600);
customPlot->xAxis->setRange(key-600,key);
customPlot->replot();
  • 改變曲線顯示區域的代碼
QRect rect = customPlot->viewport();  
rect = rect.adjusted(0,-12,0,12);
customPlot->setViewport(rect);        

改變顯示區域的代碼,在custom控件大小發生變化的時候,可能會有一些意想不到的問題,上述代碼僅適用於控件大小確定的場景。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM