ページ

2014年1月8日水曜日

目盛の調整 :Qwt

標準の場合

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_legend.h>
MainWindow::MainWindow(QWidget *parent) :
QwtPlot(parent)
{
setTitle( "Sin Curve" );//グラフのタイトル
//グラフの線
QwtPlotCurve *curve = new QwtPlotCurve();
curve->attach(this);
//setAxisScale( QwtPlot::xBottom, 0, 360 );//X軸 目盛
// グリッドの設定
QwtPlotGrid *grid = new QwtPlotGrid();
//grid->enableXMin(true);//中間縦線の表示
grid->attach( this );
setAxisScale(2,0,360,20);//X軸,始点,終点,ステップ
const int kArraySize = 37;
double plotX[kArraySize] = {}; // x
double plotY[kArraySize] = {}; // y
int i= 0;
for(int k = 0; k <= 360; k+=10 ){
double x = k;
double y = sin(k*M_PI/180);
qDebug() << i << "::" << k << "::" << x <<"::"<<y;
plotX[i] = x;
plotY[i] = y;
++i;
}
curve->setSamples(plotX, plotY, kArraySize);
resize( 600, 400 );
}
MainWindow::~MainWindow()
{
delete ui;
}
view raw mainwindow.cpp hosted with ❤ by GitHub


QwtScaleDiv を使って目盛を調整
majorTicks,mediumTicks,minorTicks の設定

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
MainWindow::MainWindow(QWidget *parent) :
QwtPlot(parent)
{
setTitle( "Sin Curve" );//グラフのタイトル
//グラフの線
QwtPlotCurve *curve = new QwtPlotCurve();
curve->attach(this);
// グリッドの設定
QwtPlotGrid *grid = new QwtPlotGrid();
//grid->enableXMin(true);//中間縦線の表示
grid->attach( this );
QList<double> majorTicks;
majorTicks << 0 << 90 << 180 << 270 << 360;
QList<double> mediumTicks;
mediumTicks << 30 << 60 << 120 << 150 << 210 << 240 << 300 << 330;
QList<double> minorTicks;
minorTicks << 10 << 20 << 40 << 50 << 70 << 80
<< 100 << 110 << 130 << 140 << 160 << 170 << 190
<< 200 << 220 << 230 << 250 << 260 << 280 << 290
<< 310 << 320 << 340 << 350;
QwtScaleDiv div( 0, 360 );
div.setTicks(QwtScaleDiv::MajorTick, majorTicks);
div.setTicks(QwtScaleDiv::MediumTick, mediumTicks);
div.setTicks(QwtScaleDiv::MinorTick, minorTicks);
setAxisScaleDiv(QwtPlot::xBottom, div);
const int kArraySize = 37;
double plotX[kArraySize] = {}; // x
double plotY[kArraySize] = {}; // y
int i= 0;
for(int k = 0; k <= 360; k+=10 ){
double x = k;
double y = sin(k*M_PI/180);
qDebug() << i << "::" << k << "::" << x <<"::"<<y;
plotX[i] = x;
plotY[i] = y;
++i;
}
curve->setSamples(plotX, plotY, kArraySize);
resize( 600, 400 );
}
MainWindow::~MainWindow()
{
delete ui;
}
view raw mainwindow2.cpp hosted with ❤ by GitHub


QwtScaleDraw を使って目盛を調整
目盛の文字の色を変更

#ifndef MYQWTSCALEDRAW_H
#define MYQWTSCALEDRAW_H
#include <qwt_scale_draw.h>
#include <QColor>
class MyQwtScaleDraw : public QwtScaleDraw
{
public:
MyQwtScaleDraw();
virtual QwtText label( double value ) const
{
qDebug() << value ;
QwtText scaleLabel = QwtText (QString::number (value));
scaleLabel.setColor(QColor( "blue" ));
return scaleLabel;
}
private:
};
#endif // MYQWTSCALEDRAW_H
QwtScaleDraw を継承したクラスを作成して呼び出す。
setAxisScaleDraw( QwtPlot::xBottom, new MyQwtScaleDraw() );

MediumTick の表示

#ifndef MYQWTSCALEDRAW_H
#define MYQWTSCALEDRAW_H
#include <qwt_scale_draw.h>
class MyQwtScaleDraw : public QwtScaleDraw
{
public:
MyQwtScaleDraw();
void draw(QPainter *painter, const QPalette& palette) const
{
QwtScaleDraw::draw(painter, palette);
if ( hasComponent(QwtAbstractScaleDraw::Labels) )
{
//MediumTick の表示
const QList<double> &mediumTicks = scaleDiv().ticks(QwtScaleDiv::MediumTick);
for (int i = 0; i < (int)mediumTicks.count(); i++)
{
const double v = mediumTicks[i];
//qDebug() << v;
if ( scaleDiv().contains(v) )
drawLabel(painter, mediumTicks[i]);
}
}
}
};
#endif // MYQWTSCALEDRAW_H


目盛りの非表示
plot->enableAxis(QwtPlot::xBottom, false);

目盛りの最大値の取得
plot->setAutoReplot(true);
plot->axisScaleDiv(QwtPlot::xBottom).upperBound();
plot->axisScaleDiv(QwtPlot::xBottom).interval().maxValue();


0 件のコメント:

コメントを投稿