2013年3月9日星期六

Qwt 筆記 - 圖表上的 Marker 和 Picker


Plot::Plot(QWidget *parent) : QwtPlot(parent)
{
..........
..........

// marker
this->marker = new QwtPlotMarker();
this->marker->setLabelAlignment(Qt::AlignLeft | Qt::AlignBottom);
this->marker->setLabelOrientation(Qt::Vertical);
this->marker->setLineStyle(QwtPlotMarker::VLine);
this->marker->setLinePen(QPen(Qt::yellow, 2, Qt::SolidLine));

this->marker->attach(this);

// picker
this->picker = new QwtPlotPicker(this->canvas());
this->picker->setStateMachine(new QwtPickerPolygonMachine());

this->picker->setRubberBandPen(QColor(Qt::yellow));
this->picker->setRubberBand(QwtPicker::NoRubberBand);

QObject::connect(this->picker, SIGNAL(moved(QPoint)), this, SLOT(picker_moved(QPoint)));

..........
..........
}

void Plot::picker_moved(QPoint pos)
{
..........
..........

        double xTop = this->invTransform(QwtPlot::xBottom,pos.x());
        double yLeft = this->invTransform(QwtPlot::yLeft, pos.y());

        this->marker->setValue(xTop, yLeft);

        // update the screen
        this->replot();
   
        ..........
..........
}

Qwt 筆記 - 更改軸標示 (axis label) 的顏色


QwtText title(titlename); // titlename = 名稱
title.setColor(Qt::red); // 例子: 紅色
pLogPlot->setAxisTitle(QwtPlot::yLeft, title);

Qwt 筆記 - 在同一圖表上顯示不同比例的曲線


如果有兩條曲線的 Y 方向的資料範圍相差很大 (例如: 一條是 0-100, 另一條是 0-10000), 假如它們同時顯示同一個圖表上, 那麼我們會看不見範圍小的曲線的變化.
所以我們會用兩個不同的 Y 軸來處理:

QwtPlot *pLogPlot;
QwtPlotCurve *pFirstCurve, *pSecondCurve;
..........
..........
// First curve
pLogPlot->setAxisScale(QwtPlot::yLeft, pFirstCurve->minYValue(), pFirstCurve->maxYValue());
pFirstCurve->setYAxis(QwtPlot::yLeft);
pLogPlot->enableAxis(QwtPlot::yLeft, true);

// Second curve
pLogPlot->setAxisScale(QwtPlot::yRight, pSecondCurve->minYValue(), pSecondCurve->maxYValue());
pSecondCurve->setYAxis(QwtPlot::yRight);
pLogPlot->enableAxis(QwtPlot::yRight, true);
..........
..........

功能說明參考:
QwtPlot::enableAxis
QwtPlot::setAxisScale
QwtPlotItem::setYAxis

Qwt 筆記 - 更改 Qwt 圖表上軸線標示的顯示方式


QwtPlot 預設是將輸入的數值 (double 類型) 用 QLocale::system().toString() 來轉換為文字再繪畫出來 (參考 QwtAbstractScaleDraw::label 的說明)
若需要更改標示的顯示方式 (例如: 時間軸的顯示), 我們可以先建立一個承繼 QwtScaleDraw 的 class, 再重寫 label() 功能:

class TimeAxis : public QwtScaleDraw {
public:
explicit TimeAxis();

// override label function
QwtText label(double offset) const;
};

QwtText TimeAxis::label(double offset) const {
// starting time
QTime time(0, 0, 0);

    time = time.addSecs(offset);

    return QwtText(time.toString("hh:mm:ss"));
}

然後用 QwtPlot 的 setAxisScaleDraw() 功能來設定新的軸:

LogPlot::LogPlot(QWidget *parent) : QwtPlot(parent)
{
    this->pTime_axis = new TimeAxis();
    this->setAxisScaleDraw(xBottom, this->pTime_axis);
}

Qwt 筆記 - 在 Ubuntu 安裝 Qwt 開發工具 (DEB 版本)


Qwt 是一個用來繪畫圖表的 Qt 程序庳. 雖然在網上有不少文章教大家如何用 Qwt 的源碼來安裝, 不過如果你不需要最新版本, 其實你可以直接安裝 DEB 版本:

sudo apt-get install libqwt-doc
sudo apt-get install libqwt-dev

完成後你便可以使用 Qwt, 同時也可以在 QtDesigner 看到 Qwt 的元件.