qwt横轴为时间轴,纵轴为随机数

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sishu2384/article/details/77336028
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qwt_date_scale_draw.h"
#include "qwt_plot.h"
#include <qdatetime.h>
#include <QDateTime>
#include <qwt_plot_magnifier.h>
#include <QTimer>
#include <qwt_symbol.h>
#include <QPoint>
#include <qwt_plot_grid.h>
#include <qwt_plot_curve.h>

 

 
QDateTime m_baseTime;
QVector<double> m_xTime;
QVector<double> m_yVal;
QwtPlotCurve *plotcurve;
int m_timeAreaWidth;
QVector<QPointF> m_vp;
double m_timeData[3600];

 
class TimeScakeDrawR1: public QwtScaleDraw
{
    public:
    TimeScakeDrawR1(const QDateTime &base):
      baseTime(base){
    }

 
    virtual QwtText label(double v) const
    {
      QDateTime upTime = baseTime.addSecs((int )v);
//    return upTime.toString("yyyy-MM-dd\nhh:mm:ss");
      return upTime.toString("hh:mm:ss");
    }

 
    private:
    QDateTime baseTime;
};

 

 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

 
    m_timeAreaWidth = 60;
    m_baseTime = QDateTime::currentDateTime();

 
    QwtPlotGrid *grid = new QwtPlotGrid;//网格
    grid->enableX(true);
    grid->enableY(true);
    grid->setMajorPen(Qt::red,0,Qt::DotLine);
    grid->attach(ui->qwtPlot);

 
    plotcurve = new QwtPlotCurve();
    plotcurve->setTitle("氨气");//曲线名字
    plotcurve->setPen( Qt::darkCyan, 2 );//曲线的颜色 宽度;

 
    ui->qwtPlot->setAxisScaleDraw(QwtPlot::xBottom, new TimeScakeDrawR1(m_baseTime));
    ui->qwtPlot->setAxisScale(QwtPlot::yLeft, 0,200,20);

 
    (void) new QwtPlotMagnifier(ui->qwtPlot->canvas());//可以放大放

 
    plotcurve->setSymbol(new QwtSymbol(QwtSymbol::Cross, Qt::NoBrush,
        QPen(Qt::blue), QSize(5, 5) ) );

 
    QTimer *timer = new QTimer(this);
    timer->start(1000);
    connect(timer,SIGNAL(timeout()),this,SLOT(replotCurves()));
}

 
MainWindow::~MainWindow()
{
    delete ui;
}

 
void MainWindow::replotCurves()
{
    QPointF pt;
    qint64 timeVal = (QDateTime::currentDateTime().toMSecsSinceEpoch()-m_baseTime.toMSecsSinceEpoch())/1000;

 
    if(m_vp.size() >= m_timeAreaWidth)
    {
        m_vp.pop_front();
    }

 
    pt.setX(timeVal);
    pt.setY(rand()%200);
    m_vp.append(pt);

 
    qint64 k = timeVal;
    for ( int j = 0; j < m_timeAreaWidth; j++ )
    {
        m_timeData[j] = k--;
    }

 
    ui->qwtPlot->setAxisScale(QwtPlot::xBottom, m_timeData[m_timeAreaWidth - 1 ], m_timeData[0]);
    plotcurve->setSamples(m_vp);
    plotcurve->attach(ui->qwtPlot);
    ui->qwtPlot->replot();
}

展开阅读全文

没有更多推荐了,返回首页