QCustomPlot绘图工具之数据与图例的选中,曲线的显示与隐藏,放大被框选数据等操作

本文介绍了如何使用QCustomPlot在QT中进行绘图,包括引入库、设置图形属性,以及实现数据选择、曲线的显示与隐藏、放大选中数据等功能。通过示例代码详细讲解了各个操作的实现步骤,帮助读者掌握QCustomPlot的高级用法。
摘要由CSDN通过智能技术生成

画图之前一定要下载QCustomPlot,下载后还要在QT中引入QCustomPlot,然后拖出Widget控件,将其提升为QCustomPlot。

详细操作我在上个文章中写过,请参考QT绘图用QCustomPlot库,简单画一个曲线图,涉及了QCustomPlot的最基础知识(画图必用到的属性)_麦子穗的博客-CSDN博客_qt画曲线

先看运行后的效果图。

1654153283344

项目头文件代码:

1. 引入QCustomPlot的头文件(必须引入,不然会报错)

#include "qcustomplot.h"

2. 在头文件中声明两个函数

selectDataGraph(QCustomPlot *customPlot);此函数用来绘图

selectData(QCustomPlot *customPlot);此函数用来设置选择数据的相关属性或者参数

// 绘图
    void        selectDataGraph(QCustomPlot *customPlot);
    // 选择图形中的数据
    void        selectData(QCustomPlot *customPlot);

 3. 在头文件中声明私有槽函数用来实现数据选择和曲线的显示与隐藏

private slots:
    // 设置显示/隐藏菜单
    void contextMenuRequest(QPoint pos);
    // 选中图例中的项,或者选中曲线时
    void showGraph();
    // 被选中的曲线隐藏
    void hideGraph();
    // 显示框选数据
    void selectedShow();

项目文件.cpp代码:

1. 在设计界面拖动一个Widget控件提升为QCustomPlot,此控件的类名设置为selectDataGraph。

在构造函数中调用绘图函数时,需要将此控件作为参数传进去。代码如下

2. 绘图函数代码实现

//绘图
void SelectData::selectDataGraph(QCustomPlot *customPlot)
{
    customPlot->legend->setVisible(true);
    customPlot->legend->setFont(QFont("Helvetica",9));

    QPen pen;
    //曲线风格
    QStringList lineNames;
    lineNames << "lsNone" << "lsLine" << "lsStepLeft" << "lsStepRight" << "lsStepCenter" << "lsImpulse";

    for(int i = QCPGraph::lsNone;i <= QCPGraph::lsImpulse; ++i)
    {
        customPlot->addGraph();
        pen.setColor(QColor(qSin(i*1+1.2)*80+80, qSin(i*0.3+0)*80+80, qSin(i*0.3+1.5)*80+80));
        customPlot->graph()->setPen(pen);//设置图表的画笔
        customPlot->graph()->setName(lineNames.at(i-QCPGraph::lsNone));
        customPlot->graph()->setLineStyle((QCPGraph::LineStyle)i);  // 设置图表线段的风格
        customPlot->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));  // 设置图表散点图的样式

        //数据
        QVector<double> x(15), y(15);
        for (int j=0; j<15; ++j)
        {
            x[j] = j/15.0 * 5*3.14 + 0.01;
            y[j] = 7*qSin(x[j])/x[j] - (i-QCPGraph::lsNone)*5 + (QCPGraph::lsImpulse)*5 + 2;
        }

        customPlot->graph()->setData(x, 
Qt中,可以使用QCustomPlot库来绘制曲线图,并且也可以实现曲线图例选中功能。 QCustomPlot是一个非常强大的Qt绘图库,它提供了丰富的绘图功能,可以绘制曲线、散点、柱状图等。要实现多曲线图例选中功能,可以按照以下步骤进行操作: 首先,我们需要创建一个QCustomPlot对象,并向其添加曲线。可以使用addGraph()函数添加多个曲线。例如: QCustomPlot *customPlot = new QCustomPlot(this); QCPGraph *graph1 = customPlot->addGraph(); QCPGraph *graph2 = customPlot->addGraph(); ... 然后,我们需要为每个曲线指定一个名称,以便在图例显示。可以使用graph1->setName()函数为曲线1指定名称,graph2->setName()函数为曲线2指定名称,以此类推。 graph1->setName("曲线1"); graph2->setName("曲线2"); ... 接下来,我们需要显示图例。可以使用customPlot->legend->setVisible()函数将图例设置为可见,并根据需要设置图例的位置。 customPlot->legend->setVisible(true); customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignBottom | Qt::AlignRight); 最后,我们需要设置曲线图例选中功能。可以使用graph1->setSelectable()函数将曲线1设置为可选中,并根据需要设置曲线选中状态。 graph1->setSelectable(QCP::stSingleData); graph1->setSelection(QCPDataSelection(graph1->data()->dataRange())); 通过以上步骤,我们可以实现曲线图例选中功能。当图例中的某个曲线选中时,可以通过相应的槽函数来处理选中事件,例如: connect(customPlot, &QCustomPlot::selectionChangedByUser, this, &MyClass::handleSelectionChanged); void MyClass::handleSelectionChanged() { // 处理选中事件 // 例如,可以根据选中曲线来进行相应的操作 QCPDataSelection selection = customPlot->graph(0)->selection(); if (!selection.isEmpty()) { // 曲线1被选中 } else { // 曲线1未被选中 } } 以上就是使用QCustomPlot库实现多曲线图例选中的简单示例。通过设置曲线的名称、显示图例并设置选中功能,我们可以轻松地实现这个功能。
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值