qcustomplot圆_QCustomPlot使用心得五:坐标轴常用属性设置

先看轴部分的名称约定,根据名称就可以修改对应的属性了

a3a8bc2a651d76c93bb4f2d98bf87ec7.png

e174c75d79187558cbbf1431671070c7.png

1.显示坐标轴

默认只显示左y轴和下边的x轴,调用setVisible(bool)设置轴是否显示

1 customplot->yAxis2->setVisible(true);//显示y轴2

2 customplot->xAxis2->setVisible(true);//显示x轴2

调用setupFullAxesBox,如果某一边没有轴会生成一个,并且四边的轴显示都设置true

4e83b355f88a8048c4a8e7dd498f1a6a.png

1 customplot->axisRect()->setupFullAxesBox();//四边安装轴并显示

2.轴线颜色

10914b279278bf3dff7c1343557ae5b8.png

代码例子:

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.网格线颜色

93e33fbeb4d877d1811bfe38f7a5bf49.png

代码例子:

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颜色填充背景,还可以设置图片作为背景

1d0b86876e98d78988ff5768ca62c496.png

1 customplot->axisRect()->setBackgroundScaled(true);//启用背景缩放

2 customplot->axisRect()->setBackgroundScaledMode(Qt::AspectRatioMode::IgnoreAspectRatio);//自由缩放

3 customplot->axisRect()->setBackground(QPixmap(":/image/background.jpg"));//背景图片

这里有个缩放模式,默认自由缩放,还可以设置KeepAspectRatio或KeepAspectRatioByExpanding,效果如下。

97637a3d7469516f8a678ecedd562f21.png

5.刻度线长度和颜色

4178e372481cc30129d643e4ea42060b.png

设置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.刻度值格式

设置前后对比:

f780d353db21b7b07a84ab803c5c177d.png

f4e77a44dd6e46781f96be5b478bc70b.png

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

6daee1b2c426187e79bae5726c5d4d24.png

除此之外,还有两个特有的格式'b'和'c'

b:指数漂亮形式,默认科学计数

0f88507f387dfa9e5d30ec5f7cd33a01.png

会变成

9fc104ba4d59fd83193f4f034196787a.png

c:乘号变成×,

f890cc87a555c6ae657b8d621c239fde.png

会变成

a04d5a67cce623fde722ca6544d91afc.png

举例:

setNumberFormat("g") 数值小的时候用固定格式,数值大使用科学计数

setNumberFormat("gb") 数值小的时候用固定格式,数值大使用漂亮的10进制幂的指数形式

setNumberFormat("gbc") 在上面的基础上乘号显示×

setNumberFormat("fc") 非法格式,格式减少到'f'

setNumberFormat("hello")  非法格式,因为第一个字符不是'e', 'e', 'f', 'g'或'g'。当前格式代码将不会更改

6.改变刻度起始原点

4e8df33094c689a6ff69e9452a6c97dc.png

有些需求要修改刻度显示的原点,例如原来是-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个主刻度

0c70005191f09a18ef22bca73cf0c6b7.png

14dc43bd853c2b854e5d8b371773fa64.png

可以设置成11个主刻度,注意有个刻度步进策略,如果默认是tssReadability,那么customplot有时仍会自动调整,使刻度便于阅读,代码例子:

customplot->xAxis->ticker()->setTickCount(11);//11个主刻度

customplot->xAxis->ticker()->setTickStepStrategy(QCPAxisTicker::tssReadability);//可读性优于设置

8.刻度值显示和轴标签

c4e925b6bd535a4f431e67b625577178.png

刻度值默认在外部,可以改成在内部,代码例子:

1 customplot->xAxis->setTickLabels(true);//显示刻度值

2 customplot->xAxis->setTickLabelSide(QCPAxis::LabelSide::lsInside);//显示在内部

3 customplot->xAxis->setLabel("this is x Axis Label");//轴标签

9.线结尾装饰

坐标轴线结尾可以添加装饰,例如常用的箭头esSpikeArrow,下图QCPLineEnding枚举的图案

defb15a9c6a269ff3ef1eb6f497b19a8.png

360a0f5ae595b9e60034f1c5d61df5a5.png

代码例子:

customplot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);//x轴终点箭头图案

customplot->xAxis->setLowerEnding(QCPLineEnding::esDisc);//x轴起点圆点图案

customplot->yAxis->setUpperEnding(QCPLineEnding::esSquare);//y轴终点小方块图案

10.轴位置偏移量

6c1f1ba26055244ef441579c472e52ce.png

设置离外部和内部各50,代码例子:

1 customplot->xAxis->setPadding(50);//填充50的空间

2 customplot->xAxis->setOffset(50);//偏移50

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值