QcustomPlot 多条单条曲线光标自动更随的实现

QcustomPlot 多条单条曲线光标自动更随的实现QcustomPlot光标跟随最近有一个需求是能绘制多条曲线且能光标跟随,上网搜了很多相关的资料,如下边这个博客中查到了鼠标更随的相关代码,他的图如下所示——[原文链接地址](https://blog.csdn.net/sunnyloves/article/details/82344815)还有一篇如下所示——[原文链接地址](https://www.cnblogs.com…

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

Jetbrains全系列IDE稳定放心使用

QcustomPlot光标跟随

最近有一个需求是能绘制多条曲线且能光标跟随,上网搜了很多相关的资料,如下边这个博客中查到了鼠标更随的相关代码,他的图如下所示——原文链接


QcustomPlot 多条单条曲线光标自动更随的实现

还有一篇如下所示——原文链接


QcustomPlot 多条单条曲线光标自动更随的实现

第一篇博主的实现方法其实已经比较完善了但是我按照他的方法去做后构造函数有点问题,所以对其做了一些修改之后得到如下结果——


QcustomPlot 多条单条曲线光标自动更随的实现
三条每条100万数据点曲线实时追踪无延迟,边界跳0无数据,本文所用为QcustomPlot2.0.1。 首先说的是对上边所提第一篇博客做的修改,代码如下

#ifndef MYTRACER_H
#define MYTRACER_H

#include <QObject>
#include "qcustomplot.h"
enum TracerType
{ 
   
    XAxisTracer,
    YAxisTracer,
    DataTracer
};

class myTracer : public QObject
{ 
   
    Q_OBJECT
public:



public:
    explicit myTracer(QCustomPlot *_plot,QCPGraph *_graph, TracerType _type);//这里与原贴不同,按照原贴构造总是过不去
    ~myTracer();

    void setPen(const QPen &pen);
    void setBrush(const QBrush &brush);
    void setText(const QString &text);
    void setLabelPen(const QPen &pen);
    void updatePosition(double xValue, double yValue);

protected:
    void setVisible(bool visible);
protected:
    QCustomPlot *plot ;	     //传入实例化的QcustomPlot
    QCPGraph *graph;	   	 //这里是存传入的绘图图层
    QCPItemTracer *tracer;   // 跟踪的点
    QCPItemText *label;   	 // 显示的数值
    QCPItemLine *arrow;  	 // 箭头

    TracerType type;
    bool visible;

signals:

public slots:

};

#endif // MYTRACER_H
#include "mytracer.h"

myTracer::myTracer(QCustomPlot *_plot, QCPGraph *_graph, TracerType _type) : plot(_plot),
    graph(_graph),
    type(_type),
    visible(false)

{ 
   
    if (plot)
    { 
   
        tracer = new QCPItemTracer(plot);
        tracer->setStyle(QCPItemTracer::tsPlus);//可以选择设置追踪光标的样式,这个是小十字,还有大十字,圆点等样式
// tracer->setPen(QPen(Qt::red));
        tracer->setPen(graph->pen());//设置tracer的颜色
        tracer->setBrush(graph->pen().color());
        tracer->setSize(10);

        label = new QCPItemText(plot);
        label->setLayer("overlay");
        label->setClipToAxisRect(false);
        label->setPadding(QMargins(5, 5, 5, 5));

        label->position->setParentAnchor(tracer->position);
        label->setFont(QFont("宋体", 10));

        arrow = new QCPItemLine(plot);
        arrow->setLayer("overlay");
        arrow->setPen(graph->pen());//设置箭头的颜色
        arrow->setClipToAxisRect(false);
        arrow->setHead(QCPLineEnding::esSpikeArrow);

        switch (type) { 
   
        case XAxisTracer:
        { 
   
            tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);
            tracer->position->setTypeY(QCPItemPosition::ptAxisRectRatio);
            label->setBrush(QBrush(QColor(244, 244, 244, 100)));
            label->setPen(QPen(Qt::black));

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

            arrow->end->setParentAnchor(tracer->position);
            arrow->start->setParentAnchor(arrow->end);
            arrow->start->setCoords(20, 0);//偏移量
            break;
        }
        case YAxisTracer:
        { 
   
            tracer->position->setTypeX(QCPItemPosition::ptAxisRectRatio);
            tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);

            label->setBrush(QBrush(QColor(244, 244, 244, 100)));
            label->setPen(QPen(Qt::black));
            label->setPositionAlignment(Qt::AlignRight|Qt::AlignHCenter);

            arrow->end->setParentAnchor(tracer->position);
            arrow->start->setParentAnchor(label->position);
            arrow->start->setCoords(-20, 0);//偏移量
            break;
        }
        case DataTracer:
        { 
   
            tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);
            tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);

            label->setBrush(QBrush(QColor(244, 244, 244, 150)));
            label->setPen(graph->pen());
            label->setPositionAlignment(Qt::AlignLeft|Qt::AlignVCenter);

            arrow->end->setParentAnchor(tracer->position);
            arrow->start->setParentAnchor(arrow->end);
            arrow->start->setCoords(25, 0);
            break;
        }

        default:
            break;
        }

        setVisible(false);
    }
}

myTracer::~myTracer()
{ 
   
    if (tracer)
        plot->removeItem(tracer);
    if (label)
        plot->removeItem(label);
}

void myTracer::setPen(const QPen &pen)
{ 
   
    tracer->setPen(pen);
    arrow->setPen(pen);
}

void myTracer::setBrush(const QBrush &brush)
{ 
   
    tracer->setBrush(brush);
}

void myTracer::setLabelPen(const QPen &pen)
{ 
   
    label->setPen(pen);
}

void myTracer::setText(const QString &text)
{ 
   
    label->setText(text);
}

void myTracer::setVisible(bool visible)
{ 
   
    tracer->setVisible(visible);
    label->setVisible(visible);
    arrow->setVisible(visible);
}

void myTracer::updatePosition(double xValue, double yValue)
{ 
   
    if (!visible)
    { 
   
        setVisible(true);
        visible = true;
    }
    if (yValue > plot->yAxis->range().upper)
        yValue = plot->yAxis->range().upper;
    switch (type) { 
   
    case XAxisTracer:
    { 
   
        tracer->position->setCoords(xValue, 1);
        label->position->setCoords(0, 15);
        arrow->start->setCoords(0, 15);
        arrow->end->setCoords(0, 0);

        break;
    }
    case YAxisTracer:
    { 
   
        tracer->position->setCoords(1, yValue);
        label->position->setCoords(-20, 0);
        break;
    }
    case DataTracer:
    { 
   
        tracer->position->setCoords(xValue, yValue);
        label->position->setCoords(25, 0);
        break;
    }
    default:
        break;
    }
}

附上我的实现主函数

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <string>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{ 
   
    ui->setupUi(this);


    m_Plot = QSharedPointer<QCustomPlot> (new QCustomPlot());
    ui->verticalLayout->addWidget(m_Plot.data());
    //设置基本坐标轴(左侧Y轴和下方X轴)可拖动、可缩放、曲线可选、legend可选、设置伸缩比例,使所有图例可见
    m_Plot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom| QCP::iSelectAxes
                                |QCP::iSelectLegend | QCP::iSelectPlottables);
    m_Plot->legend->setVisible(true);
    m_Plot->legend->setFont(QFont("Helvetica",9));
    m_Plot->legend->setBrush(QColor(255,255,255,100));


    // generate some data:
    QVector<double> x(1000001), y(1000001), y1(1000001), y2(1000001);
    for (int i=0; i<1000001; ++i)
    { 
   
      x[i] = i;
      y[i] = x[i]/50000 * x[i]/50000;
      y1[i] = y[i] * 1.2;
      y2[i] = y[i] * 1.4;
    }
    QVector<QVector<double> > vec;
    vec.push_back(x);
    vec.push_back(y);
    m_value.push_back(vec);
    QVector<QVector<double> >().swap(vec);
    vec.push_back(x);
    vec.push_back(y1);
    m_value.push_back(vec);
    QVector<QVector<double> >().swap(vec);
    vec.push_back(x);
    vec.push_back(y2);
    m_value.push_back(vec);
    QVector<QVector<double> >().swap(vec);

    StaticDataPlot(m_value);
    m_Plot->xAxis->rescale(true);
    m_Plot->axisRect()->axis(QCPAxis::atBottom, 0)->setPadding(10); // add some padding to have space for tags

    m_TraserX = QSharedPointer<myTracer> (new myTracer(m_Plot.data(), m_Plot->graph(0), XAxisTracer));
    connect(m_Plot.data(), SIGNAL(mouseMove(QMouseEvent*)), this,SLOT(showTracer(QMouseEvent*)));
    m_Plot->replot();

}

MainWindow::~MainWindow()
{ 
   
    delete ui;
}

void MainWindow::showTracer(QMouseEvent *event)
{ 
   
    double x = m_Plot->xAxis->pixelToCoord(event->pos().x());
    m_TraserX->updatePosition(x, 0);
    m_TraserX->setText(QString::number(x, 'f', 2));
    for(int i=0;i<m_Graph.size();i++)
    { 
   
        double y = 0;
        QSharedPointer<QCPGraphDataContainer> tmpContainer;
        tmpContainer = m_Graph[i]->data();
        //使用二分法快速查找所在点数据!!!敲黑板,下边这段是重点
        int low = 0, high = tmpContainer->size();
        while(high > low)
        { 
   
            int middle = (low + high) / 2;
            if(x < tmpContainer->constBegin()->mainKey() ||
                    x > (tmpContainer->constEnd()-1)->mainKey())
                break;

            if(x == (tmpContainer->constBegin() + middle)->mainKey())
            { 
   
                y = (tmpContainer->constBegin() + middle)->mainValue();
                break;
            }
            if(x > (tmpContainer->constBegin() + middle)->mainKey())
            { 
   
                low = middle;
            }
            else if(x < (tmpContainer->constBegin() + middle)->mainKey())
            { 
   
                high = middle;
            }
            if(high - low <= 1)
            { 
      //差值计算所在位置数据
                y = (tmpContainer->constBegin()+low)->mainValue() + ( (x - (tmpContainer->constBegin() + low)->mainKey()) *
                    ((tmpContainer->constBegin()+high)->mainValue() - (tmpContainer->constBegin()+low)->mainValue()) ) /
                    ((tmpContainer->constBegin()+high)->mainKey() - (tmpContainer->constBegin()+low)->mainKey());
                break;
            }

        }

        m_Tracer[i]->updatePosition(x, y);
        m_Tracer[i]->setText(QString::number(y, 'f', 2));
    }
    m_Plot->replot();
}

介于很多小伙伴找我咨询代码问题,找到之前面目全非的代码重新修改编译了一下,可能有一点小不同但是效果相同,小手点下关注再去下载哦~
CustomPlotTest.zip

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

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

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


相关推荐

  • xml文件格式化[通俗易懂]

    xml文件格式化[通俗易懂]xml文件格式化看到这样的xml文档是否你的脑袋已经萌化:(ps:此时的内心是崩溃的~~~)那么让我们用UE编辑器进行对xml进行格式化吧!编辑软件:(ps:xml格式化前)**第一步:打开UE文件编辑软件第二步:打开咋们需要格式的xml文件第三步:点击格式第四步:选择XMLlint工具第五步:在弹出的窗口,勾选标签“重格式化并重缩进输出,缩进位置”(ps:英文:Reformat

    2022年7月16日
    23
  • python十进制转换_Python 进制转换

    python十进制转换_Python 进制转换提到进制转换这里需要了解int()这个内置函数,大多数人都会这样用:int(x)其目的一般是将x字符串转化为整数,int()除了这个作用外,还可以将其他进制数转化为十进制数,Python内置函数官方文档从官方文档中我们可以得知内置函数:int(x,base=10)山东掌趣网络科技第二个参数默认base=10,页就是我们常用的字符串转换为十进制整数,由此,我们改变第二个参数可以将其他进制的数转…

    2022年5月12日
    50
  • 大数据——Flume+Kafka+Flume整合模式

    大数据——Flume+Kafka+Flume整合模式创建kafka主题#启动kafka服务kafka-server-start.sh/opt/software/kafka280cala212/conf/kraft/server.properites#创建主题#topic主题名test01#partitions分区数1#replication-factor备份数量1kafka-topics.sh–create–topictest01–partitions1–replication-factor1…

    2022年6月23日
    39
  • mac dg破解激活码-激活码分享2022.02.22

    (mac dg破解激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlHCIQ56F36O-eyJsaWNlbnNlSW…

    2022年4月1日
    57
  • 医学图形图像处理(医学影像和医学图像处理)

    文章目录1图像和数字图像1图像和数字图像  数字图像:被定义为一个二维函数,f(x,y),其中x,y代表空间坐标,f代表点(x,y)处的强度或灰度级。和普通的笛卡尔坐标系有区别,在计算机中坐标系左上角为原点:  图像数字化:图像进入计算机后,对图像进行数字化(映射)。数字图像三要素:  (1)像素:大小决定了图像存储、显示的清晰度;  (2)灰度值:通常为0-255,因为在计算机中通常用一个字节来表示一个像素,即28。  (3)坐标  图像存储在计算机中会丢失信息,因为是从一个连续的

    2022年4月15日
    52
  • 什么叫做解析几何_笛卡尔心形函数

    什么叫做解析几何_笛卡尔心形函数解析几何是进行科学研究的重要的数学工具。比如说,要确定船只在大海中航行的位置,就要确立经纬度,这就需要精确的掌握夭体运行的规律;要改善枪炮的性能,就要精确地掌握抛射物体的运行规律。解决这些问题必须采用解析几何。因为它可以利用字母表示流动坐标,用方程刻划一般平面的曲线。解析几何的发明人就是伟大的数学家笛卡尔。笛卡尔15%年出生在法国,父亲是一位相当富有的律师。8岁时,父亲把他送进荃督教会学校读书。他…

    2022年10月10日
    1

发表回复

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

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