qcustomplot绘制动态曲线

1.配置

(1)将qcustomplot.h与qcustomplot.cpp文件放进项目文件下,添加进工程文件内

(2)在Pro文件内添加printsupport

(3)将widget类提升为qcustomplot类

2.实时数据曲线

1.定时器

//定时器,周期调用realtimeDataSlot()槽,实现动态数据添加到曲线
    QTimer dataTimer;
​

2.实时数据设置

//主轴电流实时数据设置
    void setupRealtimeData_SpindleI(QCustomPlot *widget_spindleI);

3.动态数据

//动态电流数据
    double realDataI();

4.动态响应信号

private slots:
    //主轴电流响应信号
    void realtimeDataSpindleI();

3.cpp实现

1.main函数实现

//设置实时电流
    setupRealtimeData_SpindleI(ui->widget_spindleI);
    ui->widget_spindleI->replot();
    ui->checkBox->setChecked(true);

2.实时数据设置实现

void MainWindow::setupRealtimeData_SpindleI(QCustomPlot *widget_spindleI)
{
    widget_spindleI->addGraph(ui->widget_spindleI->xAxis,ui->widget_spindleI->yAxis);
    widget_spindleI->graph(0)->setPen(QPen(Qt::blue));
    widget_spindleI->graph(0)->setName(QString::fromLocal8Bit("主轴电流"));
    QSharedPointer<QCPAxisTickerDateTime> dataTick(new QCPAxisTickerDateTime);
    //设置时间轴 一共几个格
    ui->widget_spindleI->xAxis->ticker()->setTickCount(4);
    dataTick->setDateTimeFormat("MM.dd-hh:mm:ss");
    ui->widget_spindleI->xAxis->setTicker(dataTick);
​
    ui->widget_spindleI->xAxis->setLabel(QString::fromLocal8Bit("时间/s"));
    ui->widget_spindleI->xAxis->setLabelFont(QFont("宋体",14));
    ui->widget_spindleI->yAxis->setLabel(QString::fromLocal8Bit("mA"));
    ui->widget_spindleI->yAxis->setLabelFont(QFont("宋体",14));
​
    // 允许用户用鼠标拖动轴范围,用鼠标滚轮缩放,点击选择图形:
    ui->widget_spindleI->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | QCP::iSelectLegend |QCP::iSelectPlottables);
    connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSpindleI()));
​
    dataTimer.start(Drawcycle); // Interval 0 means to refresh as fast as possible
    widget_spindleI->legend->setVisible(true);
    //图例边框隐藏
    widget_spindleI->legend->setBorderPen(Qt::NoPen);
    //图例背景透明
    widget_spindleI->legend->setBrush(QColor(255,255,255,150));//设置图例背景
    //设置图例与曲线同步选中
    //设置legend只能选择图例
    widget_spindleI->legend->setSelectableParts(QCPLegend::spItems);
​
​
​
}

3.动态数据实现

double MainWindow::realData()
{
    double num=QRandomGenerator::global()->bounded(10.123);
    return num;
}

4.动态响应信号实现

void MainWindow::realtimeDataSpindleI()
{
    //key的单位是秒
    double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
​
    //添加数据
    //使用随机数产生一条曲线
    double value0 = realDataI();
​
    if (ui->checkBox->isChecked())
        ui->widget_spindleI->graph(0)->addData(key, value0);//添加数据到曲线
​
    //删除8秒之前的数据。这里的8要和下面设置横坐标宽度的8配合起来
    //才能起到想要的效果,可以调整这两个值,观察显示的效果。
     ui->widget_spindleI->graph(0)->data()->remove(key-8);
​
    //自动设定graph(1)曲线y轴的范围,如果不设定,有可能看不到图像
    //也可以用ui->customPlot->yAxis->setRange(up,low)手动设定y轴范围
    ui->widget_spindleI->graph(0)->rescaleValueAxis(true);
    //这里的8,是指横坐标时间宽度为8秒,如果想要横坐标显示更多的时间
    //就把8调整为比较大到值,比如要显示60秒,那就改成60。
    //这时removeDataBefore(key-8)中的8也要改成60,否则曲线显示不完整。
    ui->widget_spindleI->xAxis->setRange(key+0.25, 8, Qt::AlignRight);//设定x轴的范围
    ui->widget_spindleI->replot();
​
}
  • 9
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
qcustomplot是一个基于Qt的绘图库,用于快速绘制各种类型的图表,包括动态曲线图。下面是一个简单的示例程序,演示如何使用qcustomplot绘制动态曲线图。 ```cpp #include <QApplication> #include <QTimer> #include "qcustomplot.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); // 创建QCustomPlot对象 QCustomPlot plot; plot.setWindowTitle("Dynamic Curve"); // 创建曲线对象 QCPGraph *curve = plot.addGraph(); curve->setPen(QPen(Qt::red)); curve->setLineStyle(QCPGraph::lsLine); // 设置坐标轴范围和标签 plot.xAxis->setLabel("Time"); plot.yAxis->setLabel("Value"); plot.xAxis->setRange(0, 10); plot.yAxis->setRange(-1, 1); // 创建定时器,每100毫秒更新一次曲线 QTimer timer; timer.start(100); QObject::connect(&timer, &QTimer::timeout, [&]() { static double t = 0; static double y = 0; t += 0.1; y = sin(t); curve->addData(t, y); plot.xAxis->setRange(t, 10, Qt::AlignRight); plot.replot(); }); // 显示窗口 plot.show(); return a.exec(); } ``` 在这个示例程序中,我们创建了一个QCustomPlot对象,并添加了一个曲线对象。然后设置了坐标轴的范围和标签。接着创建了一个定时器,用于每100毫秒更新一次曲线。在定时器的回调函数中,我们使用sin函数生成一个随时间变化的y值,并将其添加到曲线对象中。然后调整x轴的范围,使曲线能够动态显示。最后调用plot.replot()函数重新绘制曲线。 运行程序后,您应该可以看到一个动态曲线图,它会不断地更新,显示sin函数在时间轴上的变化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值