qwt-QwtPlot

QwtPlot是用來繪製二維圖像的widget。在它的畫板上可以無限制的顯示繪畫組件。繪畫組件可以是曲線(QwtPlotCurve)、標記(QwtPlotMarker)、網格(QwtPlotGrid)、或者其它從QwtPlotItem繼承的組件。
 

QwtPlot擁有4個axes(軸線)

yLeft  Y axis left of the canvas.
yRight  Y axis right of the canvas.
xBottom  X axis below the canvas.
xTop  X axis above the canvas.
 

常用函數接口

setAxisTitle 設置軸標題
enableAxis 主要是顯示xTop,yRight座標軸
setAxisMaxMajor 設置某個某個座標軸擴大比例尺的最大間隔數目
setAxisMaxMinor 設置某個某個座標軸縮小比例尺的最大間隔數目
setAxisScale 禁用自動縮放比例尺,爲某個座標軸指定一個修改的比例尺
insertLegend 添加圖例(標註)
 

常用組件

QwtPlotCurve 曲線
QwtPlotMarker 標記
QwtPlotGrid 網格
QwtPlotHistogram 直方圖
other 從QwtPlotItem繼承的組件
 
QwtPlotItem plot能顯示的類,如果想要實現自己繪畫圖形,要繼承此類實現rtti和draw接口
QwtPlotPanner 平移器    (用鼠標左鍵平移)
QwtPlotMagnifier  放大器    (用鼠標滾輪縮放)
QwtPlotCanvas 畫布
QwtScaleMap 比例圖---可以提供一個邏輯區域到實際區域的座標轉換
QwtScaleWidget 比例窗口
QwtScaleDiv 比例佈局
QwtLegent 標註
QwtPlotLayout 佈局管理器
QwtScaleDraw 自畫座標軸
 
 

QwtPlotCure簡介

 
常見接口
setPen 設置畫筆
setData 設置曲線的數據
setStyle 設置曲線形式,點、直線、虛線等等
setCurveAttribute 設置曲線屬性,一般設置Fitted
attch 把曲線附加到QwlPlot上
 
下面看一個小例子,結果如下:
 
 
 
源代碼:

 

  1. #include <QtGui/QApplication>  
  2. #include <Qt/qmath.h>  
  3. #include <QVector>  
  4. #include <qwt_plot.h>  
  5. #include <qwt_plot_curve.h>  
  6. #include <qwt_plot_magnifier.h>  
  7. #include <qwt_plot_panner.h>  
  8. #include <qwt_legend.h>  
  9.   
  10. int main(int argc, char *argv[])  
  11. {  
  12.     QApplication a(argc, argv);  
  13.   
  14.     QwtPlot plot(QwtText("CppQwtExample1"));  
  15.     plot.resize(640,400);  
  16.     //設置座標軸的名稱  
  17.     plot.setAxisTitle(QwtPlot::xBottom, "x->");  
  18.     plot.setAxisTitle(QwtPlot::yLeft, "y->");  
  19.     //設置座標軸的範圍  
  20.     plot.setAxisScale(QwtPlot::xBottom, 0.0, 2.0 * M_PI);  
  21.     plot.setAxisScale(QwtPlot::yLeft, -1.0, 1.0);  
  22.     //設置右邊標註  
  23.     plot.insertLegend(new QwtLegend(), QwtPlot::RightLegend);  
  24.   
  25.     //使用滾輪放大/縮小  
  26.     (voidnew QwtPlotMagnifier( plot.canvas() );  
  27.   
  28.     //使用鼠標左鍵平移  
  29.     (voidnew QwtPlotPanner( plot.canvas() );  
  30.   
  31.     //計算曲線數據  
  32.     QVector<double> xs;  
  33.     QVector<double> ys;  
  34.     for (double x = 0; x < 2.0 * M_PI; x+=(M_PI / 10.0))  
  35.     {  
  36.         xs.append(x);  
  37.         ys.append(qSin(x));  
  38.     }  
  39.     //構造麴線數據  
  40.     QwtPointArrayData * const data = new QwtPointArrayData(xs, ys);  
  41.     QwtPlotCurve curve("Sine");  
  42.     curve.setData(data);//設置數據  
  43.     curve.setStyle(QwtPlotCurve::Lines);//直線形式  
  44.     curve.setCurveAttribute(QwtPlotCurve::Fitted, true);//是曲線更光滑  
  45.     curve.setPen(QPen(Qt::blue));//設置畫筆  
  46.   
  47.     curve.attach(&plot);//把曲線附加到plot上  
  48.   
  49.     plot.show();  
  50.   
  51.     return a.exec();  
  52. }  



轉自http://blog.csdn.net/zhx6044/article/details/9114733

學習這個東西方向對了,就對了一半

今天我開始學習基於qt庫的一個開源的繪製2維的統計圖的庫--qwt。

我們畫東西首先要有一個容器,不然都是徒勞,今天我們就介紹這個類--QwtPlot

它繼承自QFrame和QwtPlotDict,QFrame提供一個QWidget的框架,QwtPlotDict爲QwtPlot管理在其中的plot items,就是繪製的項。在QwtPlot上我們可以繪製無限多個的plot items,這些plot items可以是曲線,標記,格子以及繼承自QwtPlotItem的子類。一個QwtPlot可以有四個軸,每個plot item連接到x和y軸上。在軸上的比例變換可以使用QwtScaleDiv,對於plot items比例可以使用QwtScaleEngine來計算,在每個軸上,QwtScaleEngine可以被單獨設置。

在QwtPlot中有兩個枚舉類型。

Axis,軸,5個值,一個QwtPlot除了x和y,還有top和bottom軸,第五個是axisCnt,軸數,枚舉從0開始,第五個爲4,說明一共四個軸。另一個是LegendPosition,圖例的位置。

它有五個值,分別指定插入一個圖例仔什麼位置,四個都是和x和y軸的位置有關,最後一個是特殊的,它允許不在這個Plot中,就是外部的。


這是今天寫的一個小例子

  1. /************************************************ 
  2. * 
  3. *author:周翔 
  4. *e-mail:[email protected] 
  5. *blog:http://blog.csdn.net/zhx6044 
  6. * 
  7. * 
  8. *************************************************/  
  9.   
  10. #ifndef PLOT_H  
  11. #define PLOT_H  
  12.   
  13. #include <qwt_plot.h>  
  14. #include <qwt_plot_curve.h>  
  15. #include <qwt_plot_picker.h>  
  16. #include <qwt_plot_panner.h>  
  17. #include <qwt_plot_magnifier.h>  
  18. #include <qwt_legend.h>  
  19. #include <qwt_plot_grid.h>  
  20. #include <qwt_picker_machine.h>  
  21.   
  22.   
  23.   
  24.   
  25. class Plot : public QwtPlot  
  26. {  
  27.     Q_OBJECT  
  28. public:  
  29.     explicit Plot(QWidget *parent = 0);  
  30.     ~Plot();  
  31. signals:  
  32.       
  33. public slots:  
  34.   
  35.   
  36. private:  
  37.     QwtPlotCurve *sinCurve;  
  38.     QwtPlotCurve *cosCurve;  
  39.     QwtPlotPicker *picker;  
  40.   
  41.   
  42.     void initCanvas();  
  43.     void initAxes();  
  44.     void initCurves();  
  45.       
  46. };  
  47.   
  48. #endif // PLOT_H  
  49.   
  50. /************************************************ 
  51. * 
  52. *author:周翔 
  53. *e-mail:[email protected] 
  54. *blog:http://blog.csdn.net/zhx6044 
  55. * 
  56. * 
  57. *************************************************/  
  58.   
  59. #include "plot.h"  
  60.   
  61.   
  62. double _sin2(double x)  
  63. {  
  64.     return ::sin(2 * x);  
  65. }  
  66.   
  67. /** 
  68.  * @brief The FunctionData class 將一個函數包裝一下,就像一個函數對象一樣,但是又不一樣,沒有基於運行符()重載 
  69.  * 而是基於多態,我們只需要繼承它,實現相關的虛函數即可,它內部有自己的調用規則 
  70.  */  
  71. class FunctionData: public QwtSyntheticPointData  
  72. {  
  73. public:  
  74.     FunctionData(double(*y)(double)):  
  75.         QwtSyntheticPointData(100),  
  76.         d_y(y)  
  77.     {  
  78.     }  
  79.   
  80.     virtual double y(double x) const  
  81.     {  
  82.         return d_y(x);  
  83.     }  
  84.   
  85. private:  
  86.     double(*d_y)(double);  
  87. };  
  88.   
  89.   
  90. Plot::Plot(QWidget *parent) :  
  91.     QwtPlot(parent)  
  92. {  
  93.     initCanvas();  
  94. }  
  95. Plot::~Plot()  
  96. {  
  97.   
  98. }  
  99.   
  100. void Plot::initCanvas()  
  101. {  
  102.     //右鍵拖拽  
  103.     (new QwtPlotPanner(this->canvas()))->setMouseButton(Qt::RightButton);  
  104.   
  105.     //y軸在放大的時候,座標不變化  
  106.     (new QwtPlotMagnifier(this->canvas()))->setAxisEnabled(QwtPlot::yLeft,false);  
  107.   
  108.   
  109.     //一個選擇器,十字線,以xBottom和yLeft座標  
  110.     picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,  
  111.                                QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn,  
  112.                                this->canvas());  
  113.     picker->setStateMachine(new QwtPickerDragPointMachine());//拖拽點起作用  
  114.     picker->setRubberBandPen(QPen(QColor(Qt::white)));  
  115.     picker->setTrackerPen(QColor(Qt::yellow));  
  116.   
  117.   
  118.   
  119.     setAutoFillBackground(true);  
  120.   
  121.     this->canvas()->setPalette(QPalette (QColor(Qt::darkCyan)));  
  122.   
  123.     setTitle("sin(x) and cos(x)");  
  124.   
  125.     //這個會根據畫板中的圖在RightLegend顯示一個圖例  
  126.     insertLegend(new QwtLegend(),QwtPlot::RightLegend);  
  127.   
  128.   
  129.     QwtPlotGrid *grid = new QwtPlotGrid;//網格  
  130.     grid->enableXMin(true);  
  131.     grid->setMajPen(QPen(Qt::white, 0, Qt::DotLine));//大格子  
  132.     grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));//大格子裏的小格子  
  133.     grid->attach(this);  
  134.   
  135.   
  136.   
  137.   
  138.     initAxes();  
  139.     initCurves();  
  140. }  
  141.   
  142.   
  143. void Plot::initAxes()  
  144. {  
  145.   
  146.   
  147.     setAxisTitle(QwtPlot::yLeft, QObject::trUtf8("y 軸"));  
  148.     setAxisScale(QwtPlot::yLeft, -1.0, 1.0);  
  149.   
  150.     setAxisTitle(QwtPlot::xBottom,QObject::trUtf8("x 軸"));  
  151.     setAxisScale(QwtPlot::xBottom,-5.0,5.0);  
  152. }  
  153.   
  154. void Plot::initCurves()  
  155. {  
  156.     sinCurve = new QwtPlotCurve("y = sin(2x)");  
  157.     //切換渲染提示 啓用抗鋸齒  
  158.     sinCurve->setRenderHint(QwtPlotItem::RenderAntialiased);  
  159.     sinCurve->setLegendAttribute(QwtPlotCurve::LegendShowLine);  
  160.     sinCurve->setPen(QPen(Qt::yellow));  
  161.     sinCurve->attach(this);  
  162.   
  163.   
  164.     cosCurve = new QwtPlotCurve("y = cos(x)");  
  165.     cosCurve->setRenderHint(QwtPlotItem::RenderAntialiased);  
  166.     //這個會改變曲線在QwtLegend顯示的樣式,可以是線,矩形,符號,  
  167.     cosCurve->setLegendAttribute(QwtPlotCurve::LegendNoAttribute);  
  168.     cosCurve->setPen(QPen(Qt::red));  
  169.     cosCurve->attach(this);  
  170.   
  171.     sinCurve->setData(new FunctionData(_sin2));  
  172.     cosCurve->setData(new FunctionData(::cos));  
  173.   
  174. }  



發佈了59 篇原創文章 · 獲贊 27 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章