ページ

2013年11月27日水曜日

サインカーブ


.pro ファイルに
CONFIG += qwt
を追記

ヘッダー
plot.h
#ifndef PLOT_H
#define PLOT_H

#include <QMainWindow>
#include <qwt_plot.h>
#include <qwt_legend.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_curve.h>

namespace Ui {
class Plot;
}

class Plot : public QwtPlot
{
    Q_OBJECT
    
public:
    explicit Plot(QWidget *parent = 0);
    ~Plot();
    
private:
    Ui::Plot *ui;
    class QwtPlotCurve *curve;
    void plotCurve();
};

#endif // PLOT_H

main.cpp
#include "plot.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    qDebug("%s", "main.cpp");

    Plot w;
    w.show();
    
    return a.exec();
}


#include "plot.h"
#include "ui_plot.h"


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

    setTitle( "Plot Demo" );//グラフのタイトル
    insertLegend( new QwtLegend() );// 凡例を表示

    // y軸
    setAxisTitle(QwtPlot::yLeft, "y abc");//タイトル
    //setAxisScale( QwtPlot::yLeft, 0.0, 10.0 );//目盛

    // x軸
    setAxisTitle(QwtPlot::xBottom, "x abc");

    // グリッドの設定
    QwtPlotGrid *grid = new QwtPlotGrid();
    grid->enableXMin(true);
    grid->attach( this );


    curve = new QwtPlotCurve();
    curve->setTitle("sine wave");
    curve->setRenderHint(QwtPlotItem::RenderAntialiased);
    curve->setPen(QPen(Qt::red));
    curve->attach(this);

    // 曲線の描画
    plotCurve();

    curve->attach( this );

    resize( 600, 400 );

    //setAxisScale( QwtPlot::xBottom, 0.5, 2.0 );//目盛
}

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

void Plot::plotCurve()
{
    const int kArraySize = 1000;

    double x[kArraySize] = {}; // x
    double y[kArraySize] = {}; // y

    for (int i = 0; i < kArraySize; ++i) {
        x[i] = i/(kArraySize-1.0);
        y[i] = sin(2.0*M_PI*x[i]);
    }

    curve->setSamples(x, y, kArraySize);
}








0 件のコメント:

コメントを投稿