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控件大小發生變化的時候,可能會有一些意想不到的問題,上述代碼僅適用於控件大小確定的場景。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章