QCustomplot 图元对象

一、是什么

     说起图,大家一下就可能想到折线图、柱状图和饼图等,但是除了这些显眼的东西以外其实还有很多东西辅助的存在着,有了这些辅助的东西图才会看起来有意义,或者说更加的真实、有说服力。这些东西都包括那些呢?首先坐标轴肯定是不能少了的,还有网格线、图例和示意说明等。这一节我们就重点来围绕这个示意说明也就是QCPAbstractItem来做以解释

二、效果图

     这里我将首先贴张效果图,主要是为了展示QCPAbstractItem的用途,有需要的同学可以深入的了解下。图上出了一条折线之外,还有坐标轴、网格线和图例,那么下一小节我将会重点的来说这个示意说明都有哪些。

图1 QCPAbstractItem示例

三、代码解读

1、说这些示意类前,我先给大家介绍一个类QCPItemPosition,这个是描述位置的一个类,示意项都是包含了这个类才具有位置信息,那么这个类有什么妙用呢,呵呵呵。。。那就要问他的成员PositionType枚举了,这个枚举有4个值,接下类我分别介绍下:

  • ptAbsolute:Static positioning in pixels, starting from the top left corner of the viewport/widget.该值为默认值,根据像素设置位置,从视口的左上角开始算起
  • ptViewportRatio:Static positioning given by a fraction of the viewport size. For example, if you call setCoords(0, 0), the position will be at the top
     left corner of the viewport/widget. setCoords(1, 1) will be at the bottom right corner, setCoords(0.5, 0) will be horizontally centered and
     vertically at the top of the viewport/widget, etc.按比例设置,依赖于视口大小
  • ptAxisRectRatio:Static positioning given by a fraction of the axis rect size . For example, if you call setCoords(0, 0), the position will be at the top left corner of the axis rect. setCoords(1, 1) will be at the bottom right corner, setCoords(0.5, 0) will be horizontally centered and vertically at the top of the axis rect, etc. You can also go beyond the axis rect by providing negative coordinates or coordinates larger than 1.按比例设置位置,依赖于坐标轴矩形大小,区别于按视口大小
  • ptPlotCoords:Dynamic positioning at a plot coordinate defined by two axes .依赖于两个坐标轴

    看到上述4个枚举,小伙伴应该猜到他是什么意思了吧,没错他就是表明这个位置信息被解析的一种方式。

2、接下来我们来看一下到底有哪些示意说明

  • 直线(QCPItemStraightLine):直线,顾名思义就是给定两个点他会无限延伸,他使用了两个QCPItemPosition变量来存储位置
  • 线段(QCPItemLine):比对于直线,线段就是一段,而不是两端自动延长
  • 曲线(QCPItemCurve):参数线,如图1种的带箭头曲线
  • 矩形(QCPItemRect):
  • 椭圆(QCPItemEllipse)
  • 文本(QCPItemText):图1中所有的文本描述都来自这个类,可能有的同学会问,为什么右侧的两个文本框有左侧的垂直线,其实他不是文本框的,而是那条带箭头的线尾部的一种装饰。
  • 小圆球(QCPItemTracer):如图1种所示的红色和绿色实心圆
  • 图片(QCPItemPixmap)
  • 括弧(QCPItemBracket):如图1种Wavepacket表示的垂直右大括号

3、QCustomPlot提供了不少的示意类,接下来我说下QCPItemText和QCPItemCurve

    QCPItemText示意代码如下:

QCPItemText *phaseTracerText = new QCPItemText(ui.widget_18);//构造一个文本
    ui.widget_18->addItem(phaseTracerText);//添加到图
    phaseTracerText->position->setType(QCPItemPosition::ptAxisRectRatio);//设置文本坐标解析方式,前文中有提到QCPItemPosition类的PositionType枚举
    phaseTracerText->setPositionAlignment(Qt::AlignRight | Qt::AlignBottom);//设置位置在矩形区域的位置
    phaseTracerText->position->setCoords(1.0, 0.95); // 设置位置,注意第三行代码的枚举类型和这儿的值解析方式有关系
    phaseTracerText->setText("Points of fixed\nphase define\nphase velocity vp");//文本描述
    phaseTracerText->setTextAlignment(Qt::AlignLeft);//设置文本在矩形区域的位置
    phaseTracerText->setFont(QFont(font().family(), 9));//设置文本的字体
    phaseTracerText->setPadding(QMargins(8, 0, 0, 0));//设置文本所在矩形的margins

    QCPItemCurve示意代码如下:

QCPItemCurve *phaseTracerArrow = new QCPItemCurve(ui.widget_18);//构造一个带参数线
    ui.widget_18->addItem(phaseTracerArrow);//添加到图中
    phaseTracerArrow->start->setParentAnchor(phaseTracerText->left);//曲线的开始点为文本的左位置
    phaseTracerArrow->startDir->setParentAnchor(phaseTracerArrow->start);//同步自身的锚点
    phaseTracerArrow->startDir->setCoords(-40, 0); // x轴偏移40个像素
    phaseTracerArrow->end->setParentAnchor(phaseTracer->position);//曲线的结束点为实心圆的位置
    phaseTracerArrow->end->setCoords(10, 10);//偏移
    phaseTracerArrow->endDir->setParentAnchor(phaseTracerArrow->end);//同步自身的锚点
    phaseTracerArrow->endDir->setCoords(30, 30);//偏移
    phaseTracerArrow->setHead(QCPLineEnding::esSpikeArrow);//设置首部形状(箭头)
    phaseTracerArrow->setTail(QCPLineEnding(QCPLineEnding::esBar, (phaseTracerText->bottom->pixelPoint().y() - phaseTracerText->top->pixelPoint().y())*0.85));//设置尾部形状(一条竖线)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值