ページ

2014年1月4日土曜日

凡例とQCPLayoutGrid :QCustomPlot:Qt



#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);//スクロールバーの範囲
//グラフのデータを作成
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);
// 凡例のレイアウト
QCPLayoutGrid *myCPLayoutGrid = new QCPLayoutGrid; // グリッドエレメントの作成
//既存のレイアウトの中にmyCPLayoutGridを追加Row(行), Column(列)
customPlot->plotLayout()->addElement(0, 1, myCPLayoutGrid);
myCPLayoutGrid->addElement(0, 0, customPlot->legend); //作成したグリッドエレメントの行1列1に凡例を追加
myCPLayoutGrid->addElement(1,0,new QCPLayoutElement);//凡例の下に空のエレメントを追加
myCPLayoutGrid->setMargins(QMargins(0,10,10,0));//マージンの設定
myCPLayoutGrid->setRowStretchFactor(0, 0.1);//凡例がはいる行を小さくする
customPlot->plotLayout()->setColumnStretchFactor(1, 0.1); //列1の横幅を小さくする
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


0 件のコメント:

コメントを投稿