QtCharts第一章:繪製曲線
摘要:Qt開發應用程序時,常用到曲線繪製,以往主要用第三方Qwt庫,Qt5.7以上版本已經自帶了QtCharts用於曲線繪製。本文主要講解利用QtCharts繪製曲線的基本步驟,同時講解一些修飾曲線,使其更加美觀的騷操作。
繪圖三要素
繪製一個曲線,要有三個要素(類)組成,其一是畫布QChartView,其二是圖形QChart,其三是曲線QSplineSeries(或其他)。
類名 | 描述 |
---|---|
QChartView | 用於顯示曲線 |
QChart | 相當於QwtPlot,圖表框架 |
QSplineSeries | 相當於QwtPlotCurve,曲線,有多種類型 |
QChartView
QChart
座標軸操作
設置座標軸範圍
函數名稱:void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
函數描述:Sets the range shown on the axis. Depending on the actual axis type the min and max parameters are converted to appropriate types. If the conversion is impossible then the function call does nothing.
調用方法:
chart->axisX()->setRange(0,2000);
chart->axisY()->setRange(0,1000);
設置座標軸刻度
函數名稱:void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
函數描述:Sets the range shown on the axis. Depending on the actual axis type the min and max parameters are converted to appropriate types. If the conversion is impossible then the function call does nothing.
座標軸綜合代碼和效果
QValueAxis *axisX = new QValueAxis;
axisX->setRange(0, 50); //設置座標軸範圍
axisX->setGridLineVisible(true); //網格線可見
axisX->setTickCount(20); //設置座標軸顯示數字數目
axisX->setMinorTickCount(3); //設置每格小刻度線的數目
axisX->setLabelFormat("%d"); //設置座標軸顯示數字,整數、N位小數等
chartView->chart()->setAxisX(axisX, series);