qcustomplot时间坐标轴画直线_用QCustomPlot画x轴单位是时间且实时变化的动态图

本文介绍如何利用QCustomPlot库创建一个显示实时数据的图表,其中x轴为时间坐标轴,能够自动更新并显示最近8秒内的数据。通过设置x轴为日期时间类型,并设置自定义时间格式,实现时间的显示。同时,添加两个数据曲线并根据复选框状态决定是否添加数据点,最后调整坐标轴范围以保持最新数据的可见性。
摘要由CSDN通过智能技术生成

#include /*

*了解x坐标轴的方法,添加数据的方式

*

*/

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui->setupUi(this);

setupRealtimeDataDemo(ui->customPlot);

ui->customPlot->replot();

ui->checkBox_temp->setChecked(true);

ui->checkBox_hui->setChecked(true);

}

MainWindow::~MainWindow()

{

delete ui;

}

//画图初始化

void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)

{

customPlot->addGraph(); // blue line

customPlot->graph(0)->setPen(QPen(Qt::blue));

customPlot->graph(0)->setName("temp");

//customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));

//customPlot->graph(0)->setAntialiasedFill(false);

customPlot->addGraph(); // red line

customPlot->graph(1)->setPen(QPen(Qt::red));

customPlot->graph(1)->setName("hui");

//customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));

customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);

customPlot->xAxis->setDateTimeFormat("hh:mm:ss");

customPlot->xAxis->setAutoTickStep(false);

customPlot->xAxis->setTickStep(2);

customPlot->axisRect()->setupFullAxesBox();

// make left and bottom axes transfer their ranges to right and top axes:

//connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));

//connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));

// setup a timer that repeatedly calls MainWindow::realtimeDataSlot:

connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));

dataTimer.start(0); // Interval 0 means to refresh as fast as possible

customPlot->legend->setVisible(true);

}

void MainWindow::realtimeDataSlot()

{

//key的单位是秒

double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;

qsrand(QTime::currentTime().msec() + QTime::currentTime().second() * 10000);

//使用随机数产生两条曲线

double value0 = qrand() % 100;

double value1 = qrand() % 80;

if (ui->checkBox_temp->isChecked())

ui->customPlot->graph(0)->addData(key, value0);//添加数据1到曲线1

if (ui->checkBox_hui->isChecked())

ui->customPlot->graph(1)->addData(key, value1);//添加数据2到曲线2

//删除8秒之前的数据。这里的8要和下面设置横坐标宽度的8配合起来

//才能起到想要的效果,可以调整这两个值,观察显示的效果。

ui->customPlot->graph(0)->removeDataBefore(key-8);

ui->customPlot->graph(1)->removeDataBefore(key-8);

//自动设定graph(1)曲线y轴的范围,如果不设定,有可能看不到图像

//也可以用ui->customPlot->yAxis->setRange(up,low)手动设定y轴范围

ui->customPlot->graph(0)->rescaleValueAxis();

ui->customPlot->graph(1)->rescaleValueAxis(true);

//这里的8,是指横坐标时间宽度为8秒,如果想要横坐标显示更多的时间

//就把8调整为比较大到值,比如要显示60秒,那就改成60。

//这时removeDataBefore(key-8)中的8也要改成60,否则曲线显示不完整。

ui->customPlot->xAxis->setRange(key+0.25, 8, Qt::AlignRight);//设定x轴的范围

ui->customPlot->replot();

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值