用QCustomPlot画x轴单位是时间且实时变化的动态图
来源:互联网 发布:js window对象用不了 编辑:程序博客网 时间:2024/05/20 04:09
转载自http://blog.chinaunix.net/uid-11829250-id-5750296.html
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QTimer>#include "qcustomplot.h"namespace Ui { class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); //设置qcustomplot画图属性,实时 void setupRealtimeDataDemo(QCustomPlot *customPlot);private slots: //添加实时数据槽 void realtimeDataSlot();private: Ui::MainWindow *ui; //定时器,周期调用realtimeDataSlot()槽,实现动态数据添加到曲线 QTimer dataTimer;};#endif // MAINWINDOW_H
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QVector>#include <QTimer>#include <QTime>/* *了解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 0
- 用QCustomPlot画x轴单位是时间且实时变化的动态图
- 不用ajax且能实时变化的时间
- QCustomPlot实现实时动态曲线
- QCustomPlot实现实时动态曲线
- QCustomPlot实现动态折线图
- 【分享】分享一个非常漂亮且好用的绘图控件QCustomPlot - qwt的完美替代品
- 政策是随着时间在变化的
- 用JS实现显示当前时间(实时变化)
- QCustomPlot绘制时间日期折线图
- 用Reachability实时监听网络的变化
- QCustomPlot类的继承图
- [图]keil uvision2 中调试时候出现的时间“sec”单位是秒
- 时间戳单位动态转换
- 折线图中,时间单位是天时,同一天的两条数据,在时间轴上展现为两个点,而不是一个点
- echarts动态时间轴,以秒为单位更新
- 时间单位的换算
- 时间的单位换算
- 时间的单位换算
- 卷积与反卷积
- 【PAT】【Advanced Level】1093. Count PAT's (25)
- @RequestMapping用法详解之地址映射
- Scala 入门——Eclipse开发环境搭建
- 在Java web项目中防止用户注销后使用浏览器中的“后退”按钮返回注销前页面
- 用QCustomPlot画x轴单位是时间且实时变化的动态图
- 腾讯 百度 高德 Google 地图导航 实现
- Altium Designer如何设置元件自动编号解决编号冲突
- vi 小技巧
- Android面试——Service相关
- Nuttx启动过程
- HDU 1087 Super Jumping! Jumping! Jumping!
- 挑战程序竞赛系列(50):4.2 推理与动态规划算法(3)
- 惠威T200B刷机 Q群