ページ

2014年1月4日土曜日

スクロールバーでX軸の移動 :QCustomPlot


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
customPlot = ui->widget;
ui->horizontalScrollBar->setRange(10, 100);//スクロールバーの範囲
//グラフのタイトル
customPlot->plotLayout()->insertRow(0);
customPlot->plotLayout()->addElement(0, 0, new QCPPlotTitle(customPlot, "グラフのタイトル"));
//グラフのデータを作成
QVector<double> sinX,sinY;//空のベクタ宣言
QVector<double> cosX,cosY;//空のベクタ宣言
for( double d = 0.0 ; d < 10.01 ; d += 0.01 ){
sinX.append( d );
sinY.append( sin(2.0*M_PI*d) );
cosX.append( d );
cosY.append( cos(2.0*M_PI*d) );
}
// グラフのデータを挿入
customPlot->addGraph();
customPlot->graph(0)->setData(sinX, sinY);
customPlot->graph(0)->setName("sin");//凡例の名前
customPlot->addGraph();
customPlot->graph(1)->setData(cosX, cosY);
customPlot->graph(1)->setName("cos");
customPlot->graph(1)->setPen(QPen(Qt::red));//線の色
customPlot->xAxis->setLabel("x");//X軸の目盛のタイトル
customPlot->yAxis->setLabel("y");
//目盛関係
customPlot->xAxis->setRange(0, 1.0);//目盛の始点終点
customPlot->yAxis->setRange(-1.0, 1.0);
customPlot->xAxis->setAutoTickStep(false);
customPlot->yAxis->setAutoTickStep(false);
customPlot->xAxis->setTickStep(0.2);//
customPlot->yAxis->setTickStep(0.2);
// 凡例の表示
customPlot->legend->setVisible(true);
// 凡例の表示位置:右下
customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignBottom|Qt::AlignRight);
customPlot->replot();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_horizontalScrollBar_valueChanged(int value)
{
double xValue = (double)value/10;
//qDebug() << xValue;
customPlot->xAxis->setRange(xValue-1.0, xValue);
customPlot->replot();
}
view raw mainwindow.cpp hosted with ❤ by GitHub
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "qcustomplot.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_horizontalScrollBar_valueChanged(int value);
private:
Ui::MainWindow *ui;
QCustomPlot *customPlot;
};
#endif // MAINWINDOW_H
view raw mainwindow.h hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿