ページ

2013年11月28日木曜日

データの追記

タイマーを使って擬似的にリアルタイムなグラフの作成に挑戦

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>

QVector<double> xval;//空のベクタ宣言
QVector<double> yval;//空のベクタ宣言
double xyData = 1;

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

    xval.append(0);
    xval.append(1);

    yval.append(0);
    yval.append(1);

    setTitle( "Plot Demo" );//グラフのタイトル
    setTitle("first_plot");
    setAxisTitle(QwtPlot::xBottom, " time [Second]");
    setAxisScale(QwtPlot::xBottom, 0,100 );
    setAxisTitle(QwtPlot::yLeft, "Value");
    setAxisScale(QwtPlot::yLeft, 0,100 );
    setAutoReplot(true);  // データの追記で必要
    
    curve = new QwtPlotCurve();
    curve->setPen(QPen(Qt::blue));
    curve->setSamples(xval.data(),yval.data(),xval.count());
    curve->attach(this);

    resize( 600, 400 );

    // タイマー
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(1000);
}

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

void MainWindow::update()
{
    qDebug() << "hello" << xyData;

    xyData += 1;

    xval.append(xyData);
    yval.append(xyData);

    curve->setSamples(xval.data(), yval.data(), xyData+1 );
}



サインカーブでやってみる。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>

QVector<double> xval;//空のベクタ宣言
QVector<double> yval;//空のベクタ宣言

int kArraySize = 1000;
//double x[kArraySize] = {}; // x
//double y[kArraySize] = {}; // y
double xData = 0;

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

    setTitle( "Plot Demo" );//グラフのタイトル
    setTitle("first_plot");
    setAxisTitle(QwtPlot::xBottom, " time [Second]");
    setAxisScale(QwtPlot::xBottom, 0,1 );
    setAxisTitle(QwtPlot::yLeft, "Value");
    setAxisScale(QwtPlot::yLeft, -1,1 );
    setAutoReplot(true);  // データの追記で必要

    curve = new QwtPlotCurve();
    curve->setPen(QPen(Qt::blue));
    //curve->setSamples(xval.data(),yval.data(),xval.count());
    curve->attach(this);

    resize( 600, 400 );

    // タイマー
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(5);
}

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

void MainWindow::update()
{
    qDebug() << "hello" << xData;

    if( xData < 1000 ){
        double x = xData/(1000-1.0);
        xval.append( x );
        yval.append( sin(2.0*M_PI*x) );

        curve->setSamples(xval.data(), yval.data(), xData);
        xData += 1;
    }else{
        timer->stop();
    }

}


x軸の移動を考えてみる
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <qwt_plot_magnifier.h>

QVector<double> xval;//空のベクタ宣言
QVector<double> yval;//空のベクタ宣言

double xData = 0;

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

    setTitle( "Plot Demo" );//グラフのタイトル
    setTitle("first_plot");
    setAxisTitle(QwtPlot::xBottom, " time [Second]");
    setAxisScale(QwtPlot::xBottom, 0,1 );   // x軸
    setAxisTitle(QwtPlot::yLeft, "Value");
    setAxisScale(QwtPlot::yLeft, -1,1 );
    setAutoReplot(true);  // データの追記で必要

    curve = new QwtPlotCurve();
    curve->setPen(QPen(Qt::blue));
    curve->attach(this);

    QwtPlotMagnifier* magnifier = new QwtPlotMagnifier( canvas());// 拡大縮小
    magnifier->setMouseButton(Qt::LeftButton);

    resize( 600, 400 );

    // タイマー
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(30);
}

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

void MainWindow::update()
{
        double x = xData/(100-1.0);
        xval.append( x );
        yval.append( sin(2.0*M_PI*x) );

        curve->setSamples(xval.data(), yval.data(), xval.count());

        // x軸を移動
        if( xval.last() > 1){
            setAxisScale(QwtPlot::xBottom,  xval.last()-1 ,xval.last() );
        }

        xData += 1;
}


もっと便利な方法があるかもしれません。


0 件のコメント:

コメントを投稿