天天看點

Qwt源碼解讀之QwtPlotItem類(二)

      Qwt架構提供了幾種标準的圖元實作。如網格(QwtPlotGrid類)、标簽(QwtPlotMarker類)、标尺(QwtPlotScaleItem類)、顯示SVG格式圖形檔案的圖元(QwtPlotSvgItem類)、頻譜圖(QwtPlotSpectrogram類)、曲線(QwtPlotCurve類)、柱狀圖(QwtPlotHistogram類)、間隔區域(QwtPlotIntervalCurve類)、三維散點圖(QwtPlotSpectroCurve類)等。使用者可以繼承QwtPlotItem實作符合自己需要的圖元。

1、QwtPlotCurve類:曲線類圖元。這個類是使用最多的一個類,它表征一系列點資料。QwtPlotCurve繼承自QwtPlotSeriesItem<T>類模闆,其中T為QPointF。

1)屬性:

class QwtPlotCurve::PrivateData
{
public:
    PrivateData():
        style( QwtPlotCurve::Lines ),
        baseline( 0.0 ),
        symbol( NULL ),
        attributes( 0 ),
        paintAttributes( QwtPlotCurve::ClipPolygons ),
        legendAttributes( 0 )
    {
        pen = QPen( Qt::black );
        curveFitter = new QwtSplineCurveFitter;
    }

    ~PrivateData()
    {
        delete symbol;
        delete curveFitter;
    }

    QwtPlotCurve::CurveStyle style; // 曲線風格:
    double baseline; // 基線,填充時的基準

    const QwtSymbol *symbol; // 節點符号
    QwtCurveFitter *curveFitter; // 曲線拟合

    QPen pen;
    QBrush brush; // 填充刷子

    QwtPlotCurve::CurveAttributes attributes;
    QwtPlotCurve::PaintAttributes paintAttributes;

    QwtPlotCurve::LegendAttributes legendAttributes;
};
           

通過設定曲線的屬性值,可以得到各種各樣的曲線樣子和表現方式。

Qwt源碼解讀之QwtPlotItem類(二)

2)資料設定接口:

void setSamples( const QVector<QPointF> & );
           

幾乎每個QwtPlotItem子類(Representator)都有這樣一個接口函數,用來設定資料。但由于不同的QwtPlotItem子類需要不同的資料,是以沒有把這個函數定義為虛函數。

3)尋找最近點:

int closestPoint( const QPoint &pos, double *dist = NULL ) const;
           

這是一個定義良好的接口示例。傳回值int表示最近點在曲線資料中的索引;dist 為最近點到給定點pos的距離。

4)設定節點符号表現形式(指針傳遞):

void setSymbol( const QwtSymbol *s );
    const QwtSymbol *symbol() const;
           

其實作如下:

/*!
  Assign a symbol

  \param symbol Symbol
  \sa symbol()
*/
void QwtPlotCurve::setSymbol( const QwtSymbol *symbol )
{
    if ( symbol != d_data->symbol ) //  先判斷是否是同一個對象指派
    {
        delete d_data->symbol; // 删除舊的節點符号對象
        d_data->symbol = symbol; // 指派
        itemChanged(); // 更新legend并重新整理plot
    }
}

/*!
  \return Current symbol or NULL, when no symbol has been assigned
  \sa setSymbol()
*/
const QwtSymbol *QwtPlotCurve::symbol() const
{
    return d_data->symbol;
}
           

2、QwtPlotIntervalCurve類:繪制兩條曲線之間的間隔區域,常用來顯示錯誤杆或區域。繼承自QwtPlotSeriesItem<T>類模闆,其中T為QwtIntervalSample。[y1,y2] = f(x)

Qwt源碼解讀之QwtPlotItem類(二)

3、QwtPlotHistogram類:柱狀統計圖。繼承自QwtPlotSeriesItem<T>類模闆,其中T為QwtIntervalSample。要注意QwtPlotHistogram與QwtPlotIntervalCurve的差別。

了解QwtIntervalSample的定義: A sample of the types (x1-x2, y) or (x, y1-y2) 。

4、QwtPlotSpectroCurve類: 三維散點圖,用顔色表示Z軸。繼承自QwtPlotSeriesItem<T>類模闆,其中T為QwtPoint3D。

5、QwtPlotGrid類:繪制坐标網格。

該類提供了豐富的屬性設定接口,通過設定不同的屬性值,可以得到非常漂亮的網格底圖。

6、QwtPlotMarker類:繪制标簽。有橫線,豎線,十字架三種線風格。

void setXValue( double );
    void setYValue( double );
    void setValue( double, double );
    void setValue( const QPointF & );
           

    double xValue;

    double yValue;

表示什麼意思(屬性)?

标簽與線的對齊方式:

void setLabelAlignment( Qt::Alignment );
    Qt::Alignment labelAlignment() const;
           

标簽的繪制(布局)方向:

void setLabelOrientation( Qt::Orientation );
    Qt::Orientation labelOrientation() const;
           

7、QwtPlotScaleItem類:用于在畫布内繪制标尺的類。

A class which draws a scale inside the plot canvas.

QwtPlotScaleItem can be used to draw an axis inside the plot canvas. It might by synchronized to one of the axis of the plot, but can also display its own ticks and labels.

It is allowed to synchronize the scale item with a disabled axis. In plots with vertical and horizontal scale items, it might be necessary to remove ticks at the intersections, by overloading updateScaleDiv().

The scale might be at a specific position (f.e 0.0) or it might be aligned to a canvas border.

通過 

    void setXAxis( int axis ); //horizontal  水準方向

    void setYAxis( int axis ); // vertical  豎直方向

的設定可以指定QwtPlotScaleItem綁定哪一個坐标軸。

通過

void setScaleDiv( const QwtScaleDiv& );

的設定可以讓QwtPlotScaleItem顯示自己獨立的刻度尺。

8、QwtPlotSvgItem類:顯示SVG格式圖檔資料的圖元。

在例子svgmap裡面示範了 QwtPlotSvgItem類的使用。用于導入一個SVG格式的圖檔。

9、QwtPlotRasterItem類:顯示栅格資料。

A class, which displays raster data.

Raster data is a grid of pixel values, that can be represented as a QImage. It is used for many types of information like spectrograms, cartograms, geographical maps ...

Often a plot has several types of raster data organized in layers. ( f.e a geographical map, with weather statistics ). Using setAlpha() raster items can be stacked easily.

QwtPlotRasterItem is only implemented for images of the following formats: QImage::Format_Indexed8, QImage::Format_ARGB32.

QwtPlotRasterItem是一個抽象類,定義了一個将資料渲染成QImage的純虛函數。

/*!
      \brief Render an image 

      An implementation of render() might iterate over all
      pixels of imageRect. Each pixel has to be translated into 
      the corresponding position in scale coordinates using the maps.
      This position can be used to look up a value in a implementation
      specific way and to map it into a color.

      \param xMap X-Scale Map
      \param yMap Y-Scale Map
      \param area Requested area for the image in scale coordinates
      \param imageSize Requested size of the image
     */
    virtual QImage renderImage( const QwtScaleMap &xMap,
        const QwtScaleMap &yMap, const QRectF &area,
        const QSize &imageSize ) const = 0;
           

10、QwtPlotSpectrogram類: 頻譜圖圖元,繼承自QwtPlotRasterItem類。

Qwt源碼解讀之QwtPlotItem類(二)

繼續閱讀