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继承的组件
 
QwtPlotItemplot能显示的类,如果想要实现自己绘画图形,要继承此类实现rtti和draw接口
QwtPlotPanner平移器    (用鼠标左键平移)
QwtPlotMagnifier 放大器    (用鼠标滚轮缩放)
QwtPlotCanvas画布
QwtScaleMap比例图---可以提供一个逻辑区域到实际区域的坐标转换
QwtScaleWidget比例窗口
QwtScaleDiv比例布局
QwtLegent标注
QwtPlotLayout布局管理器
QwtScaleDraw自画坐标轴
 
 

QwtPlotCure简介

 
常见接口
setPen设置画笔
setData设置曲线的数据
setStyle设置曲线形式,点、直线、虚线等等
setCurveAttribute设置曲线属性,一般设置Fitted
attch把曲线附加到QwlPlot上
 
下面看一个小例子,结果如下:
 
 
 
源代码:

 

[cpp]  view plain copy
  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:604487178@qq.com 
  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:604487178@qq.com 
  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. }  



  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值