qt之QCustomPlot动态时间轴更新曲线

一、前言

        使用qcustomplot进阶用法, 横坐标采用时间显示,纵坐标为数值当前值,主要实现的是横坐标点和时间对应,但不全部显示时间,只显示设定数量时间点,例如:曲线x长度10000,设置时间点7个,那么在坐标轴上会看见5-7个时间点,而不是密密麻麻的时间。详细见正文。

二、环境

window10

qt5.7

qcustomplot2.0

三、正文

头文件定义:

    QVector<double> time;
    QVector<double> y1_value;
    void fw_data();//复位数据
    void QCustomPlot_Init(QCustomPlot *CustomPlot);
    void QCustomPlot_Updata1(QCustomPlot *CustomPlot);

源文件核心代码:

//复位数据
void MainWindow_YL::fw_data()
{
    //开始采集前清空数据
    y1_value.clear();//清除y数据
    time.clear();
    for(int i=0;i<60;i++){
        y1_value.append(0);
        time.append(QDateTime::currentDateTime().addSecs(-60).addSecs(i).toTime_t());
    }
    //清空曲线显示
    QCustomPlot_Updata1(ui->customplot1);
}
void MainWindow_YL::QCustomPlot_Init(QCustomPlot *CustomPlot)
{
    //添加曲线
    CustomPlot->addGraph(CustomPlot->xAxis,CustomPlot->yAxis);//以左侧y轴为准
    QPen graphPen0;
    graphPen0.setWidthF(5);//曲线粗度
    graphPen0.setColor(QColor(55, 156, 212));//设置曲线颜色
    CustomPlot->graph(0)->setPen(graphPen0);//使用画笔绘制曲线
    CustomPlot->graph(0)->setName(QString(""));
    QLinearGradient plotGradient0;
    plotGradient0.setStart(0, 0);
    plotGradient0.setFinalStop(0, 350);
    plotGradient0.setColorAt(0, QColor(55, 156, 212, 255));//80, 80, 80
    plotGradient0.setColorAt(1, QColor(55, 156, 212, 10));//50, 50, 50
    CustomPlot->graph(0)->setBrush(plotGradient0); //设置曲线与x轴0点覆盖颜色

    //设置横坐标显示倍数
//    QSharedPointer<QCPAxisTickerPi> pITicker(new QCPAxisTickerPi());//QCPAxisTickerPi设置Pi刻度标签
//    CustomPlot->xAxis->setTicker(pITicker);
//    pITicker->setPiSymbol("");//设置数值后面π替换为*
//    pITicker->setFractionStyle(QCPAxisTickerPi::fsFloatingPoint);//设置小数部分显示float型
//    pITicker->setPiValue(1);//设置*表示的值 原代表π=3.14,先换为m_multiple//50ms传输一包,这里1秒就是20包,1/20=0.05

    //设置横坐标时间坐标轴 智能指针
    QSharedPointer<QCPAxisTickerDateTime> timer(new QCPAxisTickerDateTime);
    //timer->setDateTimeFormat("yyyy-MM-dd");    //设置时间格式
    timer->setDateTimeFormat("hh-mm-ss");    //设置时间格式
    timer->setTickCount(7);    //设置时间轴 一共几格
    QFont font;
    font.setFamily("黑体");
    font.setPixelSize(20);
    CustomPlot->xAxis->setTickLabelFont(font);
    CustomPlot->xAxis->setTickLabelRotation(0);//设置label 旋转角度30° 横着显示可能显示不全
    timer->setTickStepStrategy(QCPAxisTicker::tssMeetTickCount);//允许执行可读性较差的滴答步骤,从而有助于更接近所请求的滴答计数
    //设置坐标轴
    CustomPlot->xAxis->setTicker(timer);

    //设置坐标标题
//    CustomPlot->xAxis->setLabel(QString::fromUtf8("时间(s)"));
//    CustomPlot->yAxis->setLabel(QString::fromUtf8("plotname"));
    //设置坐标轴范围
    CustomPlot->xAxis->setRange(0,10);//设置x轴范围
    CustomPlot->yAxis->setRange(0,6);//设置y轴范围
    //设置网格虚线
    CustomPlot->xAxis->grid()->setVisible(false);
    CustomPlot->yAxis->grid()->setVisible(false);
    //设置风格颜色
//    QLinearGradient plotGradient;
//    plotGradient.setStart(0, 0);
//    plotGradient.setFinalStop(0, 350);
//    plotGradient.setColorAt(0, QColor(55, 156, 212, 255));//80, 80, 80
//    plotGradient.setColorAt(1, QColor(55, 156, 212, 100));//50, 50, 50
//    CustomPlot->setBackground(plotGradient);      // 设置背景颜色
    CustomPlot->setBackground(Qt::transparent);
    CustomPlot->setStyleSheet("background: transparent;");
    QLinearGradient axisRectGradient;
    axisRectGradient.setStart(0, 0);
    axisRectGradient.setFinalStop(0, 350);
    axisRectGradient.setColorAt(0, QColor(90, 160, 100, 50));
    axisRectGradient.setColorAt(1, QColor(90, 160, 100, 50));
    CustomPlot->axisRect()->setBackground(axisRectGradient);   // 设置QCPAxisRect背景颜色
    CustomPlot->xAxis->setBasePen(QPen(Qt::gray,2));//设置x轴坐标轴颜色
    CustomPlot->yAxis->setBasePen(QPen(Qt::gray,2));//设置y轴坐标轴颜色//black
    CustomPlot->xAxis->setTickPen(QPen(Qt::gray, 2));  // 轴刻度线的画笔
    CustomPlot->yAxis->setTickPen(QPen(Qt::gray,2));//black
    CustomPlot->xAxis->setSubTickPen(QPen(Qt::gray, 2)); // 轴子刻度线的画笔
    CustomPlot->yAxis->setSubTickPen(QPen(Qt::gray,2));//black
    CustomPlot->xAxis->setTickLabelColor(QColor(55, 156, 212));//设置x轴坐标颜色
    CustomPlot->yAxis->setTickLabelColor(QColor(55, 156, 212));//设置y轴坐标颜色
    CustomPlot->xAxis->setLabelColor(Qt::gray);//设置x轴名称颜色
    CustomPlot->yAxis->setLabelColor(Qt::gray);//设置y轴名称颜色
    //设置曲线名称显示
    CustomPlot->legend->setBrush(QColor(0, 0, 0, 0));//设置图例提示背景色(曲线名称背景色)
    CustomPlot->legend->setVisible(false);//设置曲线名称不可见
    CustomPlot->legend->setTextColor(Qt::white);//black

    CustomPlot->yAxis->setVisible(false);//设置y轴不可见

    CustomPlot->replot();//重绘制
}
//刷新曲线1
void MainWindow_YL::QCustomPlot_Updata1(QCustomPlot *CustomPlot)
{
    //设置坐标轴范围
    CustomPlot->xAxis->setRange(time.at(0),time.at(time.size()-1));//设置y轴范围
    //设置y轴范围,y轴不可见,但需要设置曲线高度显示范围
    if(ui->pushButton4->isChecked()){
        CustomPlot->yAxis->setRange(0,0.7);
    }
    else if(ui->pushButton5->isChecked()){
        CustomPlot->yAxis->setRange(0,2.5);
    }
    else if(ui->pushButton6->isChecked()){
        CustomPlot->yAxis->setRange(0,6);
    }
    CustomPlot->graph(0)->setData(time,y1_value);//设置数据
//    CustomPlot->yAxis->rescale(true);//设置Y轴坐标系 自动缩放以正常显示所有的数据
    CustomPlot->graph(0)->rescaleAxes(true);//根据图像最高点最低点自动缩放坐标轴
    CustomPlot->replot();//重绘制
}

本例子中y轴被我隐藏,但是在刷新曲线函数中可以看见,在我选择不同的控件时,更新的y轴范围不同,更适合显示全部曲线,

        如图可见曲线横坐标显示为时间,在程序中这个曲线范围是60个点,定时1秒采集1次数据,采集数据时间和坐标轴时间对应。

初始化函数:

    QCustomPlot_Init(ui->customplot1);
    fw_data();//初始化执行复位数据

        执行一次初始化函数,在复位一次数据。

        复位数据方式是清空缓存数据,在更新空数据进去,这样曲线看着就不是一点一点挤小,在平移了。

     初始化增加:

    connect(ui->pushButton4,&QPushButton::clicked,[=](){//选中对应传感器
        gaugepan[0]->setRingColor(QColor(54, 192, 254));
        gaugepan[1]->setRingColor(QColor(180, 180, 180));
        gaugepan[2]->setRingColor(QColor(180, 180, 180));
        gaugepan[1]->setValue(0);//未选中数值清零
        gaugepan[2]->setValue(0);//未选中数值清零
        ui->pushButton4->setChecked(true);
        ui->pushButton5->setChecked(false);
        ui->pushButton6->setChecked(false);
        fw_data();//曲线数据复位
    });
    connect(ui->pushButton5,&QPushButton::clicked,[=](){//选中对应传感器
        gaugepan[0]->setRingColor(QColor(180, 180, 180));
        gaugepan[1]->setRingColor(QColor(54, 192, 254));
        gaugepan[2]->setRingColor(QColor(180, 180, 180));
        gaugepan[0]->setValue(0);//未选中数值清零
        gaugepan[2]->setValue(0);//未选中数值清零
        ui->pushButton4->setChecked(false);
        ui->pushButton5->setChecked(true);
        ui->pushButton6->setChecked(false);
        fw_data();//曲线数据复位
    });
    connect(ui->pushButton6,&QPushButton::clicked,[=](){//选中对应传感器
        gaugepan[0]->setRingColor(QColor(180, 180, 180));
        gaugepan[1]->setRingColor(QColor(180, 180, 180));
        gaugepan[2]->setRingColor(QColor(54, 192, 254));
        gaugepan[0]->setValue(0);//未选中数值清零
        gaugepan[1]->setValue(0);//未选中数值清零
        ui->pushButton4->setChecked(false);
        ui->pushButton5->setChecked(false);
        ui->pushButton6->setChecked(true);
        fw_data();//曲线数据复位
    });
    ui->pushButton4->click();//默认选择第一个

        此部分程序就是看一下是如何操作每个按键点击之后清空数据的。

        最终效果如下:

四、结语

  • 9
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: QCustomPlot是一个基于Qt的开源C++图形库,可以用来绘制各种类型的图表,包括动态曲线图。它提供了丰富的功能和灵活的接口,可以满足不同需求的绘图需求。在使用QCustomPlot绘制动态曲线图时,可以通过添加数据点、设置坐标轴范围、设置曲线颜色等方式实现动态更新。同时,QCustomPlot还支持鼠标交互、图例显示、多图层等功能,方便用户进行数据分析和可视化。 ### 回答2: QCustomPlot是一个很好用的Qt绘图库,可以方便地为Qt应用程序绘制曲线图、柱状图、散点图、趋势图等等,支持多种图形效果和动画效果。 要制作一个动态曲线图,首先需要设置曲线图的样式、颜色、轴等等,然后添加数据点并更新曲线图。具体实现步骤如下: Step1:引入QCustomPlot库和相关头文件。(如果使用Qt Creator,通过Qt Maintenance Tool安装QCustomPlot库,然后将头文件包含到项目中。) Step2:在UI窗口中添加QCustomPlot的widget,设定其大小和位置。 Step3:在代码里创建QCustomPlot对象,并设置X、Y轴的范围、精度、网格线等信息。 QCustomPlot *customplot = new QCustomPlot(this); customplot->xAxis->setRange(0, 10); //X轴范围 customplot->yAxis->setRange(-5, 5); //Y轴范围 customplot->xAxis->setAutoTickStep(false); customplot->xAxis->setTickStep(1); //X轴刻度步长 customplot->yAxis->setAutoTickStep(false); customplot->yAxis->setTickStep(1); //Y轴刻度步长 customplot->xAxis->grid()->setVisible(true); customplot->yAxis->grid()->setVisible(true); customplot->xAxis->setLabel("Time (s)"); //X轴标签 customplot->yAxis->setLabel("Amplitude"); //Y轴标签 customplot->addGraph(); customplot->graph(0)->setPen(QPen(Qt::blue)); //曲线颜色 Step4:每隔一段时间(例如100ms)更新一下QCustomPlot对象的数据,并刷新曲线图。 QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(updatePlot())); timer->start(100); // 100ms更新一次 //updatePlot()函数更新数据并刷新曲线图 void updatePlot() { static double t = 0; //时间 static QVector<double> x(101), y(101); //100个数据点 for (int i = 0; i < 101; ++i) { x[i] = t + i/100.0; y[i] = qSin(x[i]); } customplot->graph(0)->setData(x, y); customplot->replot(); //刷新曲线图 t += 0.1; //时间加0.1秒 } 这样,一个简单的动态曲线图就完成了。根据实际需要,可以进一步美化曲线图、调整曲线参数等等。 ### 回答3: Qt QCustomPlot是一个功能强大的C++绘图库,它可以用于创建各种各样的图表,包括动态曲线图。在Qt QCustomPlot中,可以使用QCPGraph类轻松地实现动态曲线图的绘制,该类提供了许多有用的方法和属性,可用于定义和控制图形的外观和行为。 要创建一个动态曲线图,首先要创建一个QCustomPlot对象,用于表示整个图表,并设置其外观和行为。然后,使用QCPGraph类创建一个或多个数据曲线,并将其添加到QCustomPlot对象中。接着,通过添加数据点或设置一个定时器,在动态环境下更新曲线数据,使它们动起来。 在QCustomPlot中实现动态曲线图的步骤如下: 1.创建一个QCustomPlot对象,设置其外观和行为。例如,可以设置背景颜色、坐标轴标签、坐标范围和网格线等属性。 2.创建一个或多个QCPGraph对象,并将其添加至QCustomPlot对象中。对于每个QCPGraph对象,可以设置线条颜色、粗细、线型和数据点圆圈等属性,以及曲线的名称和X、Y坐标数据。 3.在动态环境下更新曲线数据。可以使用addData()方法向曲线中添加数据点,或使用setData()方法设置所有的数据点。可以使用startTimer()方法启动一个定时器,定时更新曲线数据,或在用户操作期间实时更新数据。 4.根据需要,可以添加QCPItemText、QCPItemLine和QCPItemRect等其他元素,以丰富图表的内容。 综上所述,Qt QCustomPlot是一个功能强大的C++绘图库,它可以方便地创建各种各样的图表,包括动态曲线图。使用QCPGraph类和其他元素,可以轻松定义和控制图形的外观和行为,使其在动态的环境下呈现出最佳效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大桶矿泉水

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值