轉自:https://blog.csdn.net/yxy244/article/details/100311112
先看軸部分的名稱約定,根據名稱就可以修改對應的屬性了
1.顯示坐標軸
默認只顯示左y軸和下邊的x軸,調用setVisible(bool)設置軸是否顯示
1 customplot->yAxis2->setVisible(true);//顯示y軸2 2 customplot->xAxis2->setVisible(true);//顯示x軸2
調用setupFullAxesBox,如果某一邊沒有軸會生成一個,並且四邊的軸顯示都設置true
1 customplot->axisRect()->setupFullAxesBox();//四邊安裝軸並顯示
2.軸線顏色
代碼例子:
1 customplot->xAxis->setBasePen(QPen(Qt::red,4)); 2 customplot->yAxis->setBasePen(QPen(Qt::blue,4)); 3 customplot->xAxis2->setBasePen(QPen(Qt::yellow,4)); 4 customplot->yAxis2->setBasePen(QPen(Qt::green,4));
3.網格線顏色
代碼例子:
1 customplot->axisRect()->setBackground(QBrush(Qt::black));//背景黑色 2 customplot->xAxis->grid()->setPen(QPen(QColor(180, 180, 180), 1, Qt::PenStyle::DashLine));//網格白色虛線 3 customplot->yAxis->grid()->setPen(QPen(QColor(180, 180, 180), 1, Qt::PenStyle::DashLine));//網格白色虛線 4 customplot->xAxis->grid()->setSubGridPen(QPen(QColor(50, 50, 50), 1, Qt::DotLine));//網格淺色點線 5 customplot->yAxis->grid()->setSubGridPen(QPen(QColor(50, 50, 50), 1, Qt::DotLine));//網格淺色點線 6 customplot->xAxis->grid()->setSubGridVisible(true);//顯示x軸子網格線 7 customplot->yAxis->grid()->setSubGridVisible(true);//顯示要軸子網格線 8 customplot->xAxis->grid()->setZeroLinePen(QPen(Qt::white));//x軸0線顏色白色 9 customplot->yAxis->grid()->setZeroLinePen(QPen(Qt::white));//y軸0線顏色白色
4.軸矩形背景使用圖片
除了QBrush顏色填充背景,還可以設置圖片作為背景
1 customplot->axisRect()->setBackgroundScaled(true);//啟用背景縮放 2 customplot->axisRect()->setBackgroundScaledMode(Qt::AspectRatioMode::IgnoreAspectRatio);//自由縮放 3 customplot->axisRect()->setBackground(QPixmap(":/image/background.jpg"));//背景圖片
這里有個縮放模式,默認自由縮放,還可以設置KeepAspectRatio或KeepAspectRatioByExpanding,效果如下。
5.刻度線長度和顏色
設置x軸刻度線長度和顏色,這里為了看清楚誇張一點,代碼例子:
1 QPen pen; 2 pen.setColor(Qt::red);//主刻度紅色 3 pen.setWidth(2);//線寬2 4 customplot->xAxis->setTickPen(pen); 5 customplot->xAxis->setTickLengthIn(30);//主刻度向內延伸30 6 customplot->xAxis->setTickLengthOut(10);//主刻度向外延伸10 7 pen.setColor(Qt::blue);//子刻度藍色 8 customplot->xAxis->setSubTickPen(pen); 9 customplot->xAxis->setSubTickLengthIn(15);//子刻度向內延伸15 10 customplot->xAxis->setSubTickLengthOut(5);//子刻度向外延伸5
6.刻度值格式
設置前后對比:
1 customPlot->xAxis->setNumberFormat("gbc");//g靈活的格式,b漂亮的指數形式,c乘號改成× 2 customPlot->xAxis->setNumberPrecision(1);//精度1
setNumberFormat()的部分格式可以參考QString::number()
setNumberPrecision相當於設置 QString::number(double n, char format = 'g', int precision = 6)里的precision
除此之外,還有兩個特有的格式'b'和'c'
b:指數漂亮形式,默認科學計數
會變成
c:乘號變成×,
會變成
舉例:
setNumberFormat("g") 數值小的時候用固定格式,數值大使用科學計數
setNumberFormat("gb") 數值小的時候用固定格式,數值大使用漂亮的10進制冪的指數形式
setNumberFormat("gbc") 在上面的基礎上乘號顯示×
setNumberFormat("fc") 非法格式,格式減少到'f'
setNumberFormat("hello") 非法格式,因為第一個字符不是'e', 'e', 'f', 'g'或'g'。當前格式代碼將不會更改
6.改變刻度起始原點
有些需求要修改刻度顯示的原點,例如原來是-10,-5,0,5,10,15,設置原點為1后變成-14,-9,-4,1,6,11,代碼例子:
1 customplot->xAxis->setRange(-15,15); 2 customplot->xAxis->ticker()->setTickOrigin(1);//改變刻度原點為1
7.刻度數量
一般刻度數量是自動調整的,但也可以手動設置,例如-100到100默認5個主刻度
可以設置成11個主刻度,注意有個刻度步進策略,如果默認是tssReadability,那么customplot有時仍會自動調整,使刻度便於閱讀,代碼例子:
customplot->xAxis->ticker()->setTickCount(11);//11個主刻度
customplot->xAxis->ticker()->setTickStepStrategy(QCPAxisTicker::tssReadability);//可讀性優於設置
8.刻度值顯示和軸標簽
刻度值默認在外部,可以改成在內部,代碼例子:
1 customplot->xAxis->setTickLabels(true);//顯示刻度值 2 customplot->xAxis->setTickLabelSide(QCPAxis::LabelSide::lsInside);//顯示在內部 3 customplot->xAxis->setLabel("this is x Axis Label");//軸標簽
9.線結尾裝飾
坐標軸線結尾可以添加裝飾,例如常用的箭頭esSpikeArrow,下圖QCPLineEnding枚舉的圖案
代碼例子:
customplot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);//x軸終點箭頭圖案 customplot->xAxis->setLowerEnding(QCPLineEnding::esDisc);//x軸起點圓點圖案 customplot->yAxis->setUpperEnding(QCPLineEnding::esSquare);//y軸終點小方塊圖案
10.軸位置偏移量
設置離外部和內部各50,代碼例子:
1 customplot->xAxis->setPadding(50);//填充50的空間 2 customplot->xAxis->setOffset(50);//偏移50