qcustomplot添加图例_qchart显示点数据

qcustomplot添加图例_qchart显示点数据【实例简介】QCustomPlot动态绘图,解决CPU占用内存高问题【实例截图】Qt动态实时绘图【核心代码】LXTracer::LXTracer(QCustomPlot*_plot,TracerType_type,QObject*parent):QObject(parent),m_plot(_plot),m_type(_type){m_visible=true;m_tracer=…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

【实例简介】QCustomPlot动态绘图,解决CPU占用内存高问题

【实例截图】Qt动态实时绘图

91b8a2c89eca3d03cb2020ec029df597.png

【核心代码】

LXTracer::LXTracer(QCustomPlot *_plot, TracerType _type, QObject *parent)

: QObject(parent),

m_plot(_plot),

m_type(_type)

{

m_visible = true;

m_tracer = Q_NULLPTR;// 跟踪的点

m_label = Q_NULLPTR;// 显示的数值

m_arrow = Q_NULLPTR;// 箭头

if (m_plot)

{

QColor clrDefault(Qt::red);

QBrush brushDefault(Qt::NoBrush);

QPen penDefault(clrDefault);

// penDefault.setBrush(brushDefault);

penDefault.setWidthF(0.5);

m_tracer = new QCPItemTracer(m_plot);

m_tracer->setStyle(QCPItemTracer::tsCircle);

m_tracer->setPen(penDefault);

m_tracer->setBrush(brushDefault);

m_label = new QCPItemText(m_plot);

m_label->setLayer(“overlay”);

m_label->setClipToAxisRect(false);

m_label->setPadding(QMargins(5, 5, 5, 5));

m_label->setBrush(brushDefault);

m_label->setPen(penDefault);

m_label->position->setParentAnchor(m_tracer->position);

// m_label->setFont(QFont(“宋体”, 8));

m_label->setFont(QFont(“Arial”, 8));

m_label->setColor(clrDefault);

m_label->setText(“”);

m_arrow = new QCPItemLine(m_plot);

QPen arrowPen(clrDefault, 1);

m_arrow->setPen(penDefault);

m_arrow->setLayer(“overlay”);

m_arrow->setClipToAxisRect(false);

m_arrow->setHead(QCPLineEnding::esSpikeArrow);//设置头部为箭头形状

switch (m_type)

{

case XAxisTracer:

{

m_tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);

m_tracer->position->setTypeY(QCPItemPosition::ptAxisRectRatio);

m_tracer->setSize(7);

m_label->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter);

m_arrow->end->setParentAnchor(m_tracer->position);

m_arrow->start->setParentAnchor(m_arrow->end);

m_arrow->start->setCoords(0, 20);//偏移量

break;

}

case YAxisTracer:

{

m_tracer->position->setTypeX(QCPItemPosition::ptAxisRectRatio);

m_tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);

m_tracer->setSize(7);

m_label->setPositionAlignment(Qt::AlignRight | Qt::AlignHCenter);

m_arrow->end->setParentAnchor(m_tracer->position);

m_arrow->start->setParentAnchor(m_label->position);

m_arrow->start->setCoords(-20, 0);//偏移量

break;

}

case DataTracer:

{

m_tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);

m_tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);

m_tracer->setSize(5);

m_label->setPositionAlignment(Qt::AlignLeft | Qt::AlignVCenter);

m_arrow->end->setParentAnchor(m_tracer->position);

m_arrow->start->setParentAnchor(m_arrow->end);

m_arrow->start->setCoords(20, 0);

break;

}

default:

break;

}

setVisible(false);

}

}

LXTracer::~LXTracer()

{

if(m_plot)

{

if (m_tracer)

{

m_plot->removeItem(m_tracer);

}

if (m_label)

{

m_plot->removeItem(m_label);

}

if (m_arrow)

{

m_plot->removeItem(m_arrow);

}

}

}

void LXTracer::setPen(const QPen &pen)

{

if(m_tracer)

{

m_tracer->setPen(pen);

}

if(m_arrow)

{

m_arrow->setPen(pen);

}

}

void LXTracer::setBrush(const QBrush &brush)

{

if(m_tracer)

{

m_tracer->setBrush(brush);

}

}

void LXTracer::setLabelPen(const QPen &pen)

{

if(m_label)

{

m_label->setPen(pen);

m_label->setBrush(Qt::NoBrush);

m_label->setColor(pen.color());

}

}

void LXTracer::setText(const QString &text)

{

if(m_label)

{

m_label->setText(text);

}

}

void LXTracer::setVisible(bool vis)

{

m_visible = vis;

if(m_tracer)

{

m_tracer->setVisible(m_visible);

}

if(m_label)

{

m_label->setVisible(m_visible);

}

if(m_arrow)

{

m_arrow->setVisible(m_visible);

}

}

void LXTracer::updatePosition(double xValue, double yValue)

{

if (!m_visible)

{

setVisible(true);

m_visible = true;

}

if (yValue > m_plot->yAxis->range().upper)

{

yValue = m_plot->yAxis->range().upper;

}

switch (m_type)

{

case XAxisTracer:

{

m_tracer->position->setCoords(xValue, 1);

m_label->position->setCoords(0, 15);

m_arrow->start->setCoords(0, 15);

m_arrow->end->setCoords(0, 0);

setText(QString::number(xValue));

break;

}

case YAxisTracer:

{

m_tracer->position->setCoords(0, yValue);

m_label->position->setCoords(-20, 0);

// m_arrow->start->setCoords(20, 0);

// m_arrow->end->setCoords(0, 0);

setText(QString::number(yValue));

break;

}

case DataTracer:

{

m_tracer->position->setCoords(xValue, yValue);

m_label->position->setCoords(20, 0);

setText(QString(“x:%1,y:%2”).arg(xValue).arg(yValue));

break;

}

default:

break;

}

}

LXTraceLine::LXTraceLine(QCustomPlot *_plot, LineType _type, QObject *parent)

: QObject(parent),

m_type(_type),

m_plot(_plot)

{

m_lineV = Q_NULLPTR;

m_lineH = Q_NULLPTR;

initLine();

}

LXTraceLine::~LXTraceLine()

{

if(m_plot)

{

if (m_lineV)

{

m_plot->removeItem(m_lineV);

}

if (m_lineH)

{

m_plot->removeItem(m_lineH);

}

}

}

void LXTraceLine::initLine()

{

if(m_plot)

{

QPen linesPen(Qt::red, 1, Qt::SolidLine);

if(VerticalLine == m_type || Both == m_type)

{

m_lineV = new QCPItemStraightLine(m_plot);//垂直线

m_lineV->setLayer(“overlay”);

m_lineV->setPen(linesPen);

m_lineV->setClipToAxisRect(true);

m_lineV->point1->setCoords(0, 0);

m_lineV->point2->setCoords(0, 0);

}

if(HorizonLine == m_type || Both == m_type)

{

m_lineH = new QCPItemStraightLine(m_plot);//水平线

m_lineH->setLayer(“overlay”);

m_lineH->setPen(linesPen);

m_lineH->setClipToAxisRect(true);

m_lineH->point1->setCoords(0, 0);

m_lineH->point2->setCoords(0, 0);

}

}

}

void LXTraceLine::updatePosition(double xValue, double yValue)

{

if(VerticalLine == m_type || Both == m_type)

{

if(m_lineV)

{

m_lineV->point1->setCoords(xValue, m_plot->yAxis->range().lower);

m_lineV->point2->setCoords(xValue, m_plot->yAxis->range().upper);

}

}

if(HorizonLine == m_type || Both == m_type)

{

if(m_lineH)

{

m_lineH->point1->setCoords(m_plot->xAxis->range().lower, yValue);

m_lineH->point2->setCoords(m_plot->xAxis->range().upper, yValue);

}

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/184185.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【有效】2016/2013/2010 软件的安装解决方法[通俗易懂]

    【有效】2016/2013/2010 软件的安装解决方法[通俗易懂]office相关个性化软件KMS支持2016/2013/2010版本链接:https://pan.baidu.com/s/1atqgWz1wiJnVO8WTLJyShQ提取码:6lpq链接有效麻烦点个赞~

    2022年5月3日
    34
  • linux复制多个文件到另一个文件命令_怎么替换安装目录下的文件

    linux复制多个文件到另一个文件命令_怎么替换安装目录下的文件使用参数-R

    2022年8月23日
    9
  • JDK安装包和Mysql安装包整理

    肯定有许多许多小伙伴在找jdk和MySQL这些东西的安装包,去官网下载有特别慢,所以想在网上找一下,来吧,看完总有一款是你喜欢的。。。。JDK安装包jdk-7u80-windows-x64.exe(jdk1.7win64位)jdk-7u80-windows-x86.exe(jdk1.7win32位)jdk-7u80-linux-i586.tar.gz(jdk1.7Li…

    2022年4月8日
    394
  • protostuff基本使用[通俗易懂]

    protostuff基本使用[通俗易懂][toc]protostuff基本使用protostuff基于GoogleProtobuf,好处就是不用自己写.proto文件即可实现对象的序列化与反序列化,下面给出示例代码。程序代码User.javapackagecn.xpleaf.pojo;publicclassUser{privateStringname;privateintage;…

    2022年5月20日
    67
  • IDEA+Maven 打jar包[通俗易懂]

    IDEA+Maven 打jar包[通俗易懂]IDEA+Maven打jar包(包涵依赖jar)写在前面:​这两天一直在整(gu)理(dao)IDEA用Maven打jar包,网上的教程是各式各样,但是都不能满足我的需求(或者还没有找个正确的),因此综合网上的内容自己整理了一下(以下内容是在mac系统下win可能有一些地方不一样)。软件环境:​IDEA:2017.1.5​Maven:3.3.9…

    2022年5月30日
    48
  • java输入语句怎么写_java输入语句应该怎样写?示例演示

    java输入语句怎么写_java输入语句应该怎样写?示例演示作为初步进入java开发学习的小白来说,就像是小时候刚刚学说话一样,这种经历既是必然的也是有趣的,学习java语言一开始的时候也是得一步步的学习,比如说,java输入语句应该这么去实现呢?一起跟小编来看看吧。第一步:导包。先将java.io.*;以及java.util.*;导入Java代码中。charc=(char)System.in.read();是输入单个字符;inta=cin.nextI…

    2022年7月9日
    34

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号