QWT的使用

QT下开源图表库比较少,用的比较多的就是QWT, QWT提供的API不够简洁,绘图过程也比较复杂. 这一点不如Java的JFreeChart方便.

一. 编译安装

     QWT最新版本是6.1, QWT5与QWT6一点也不兼容,windows下编译非常简单:

     cd $(QWT_ROOT)

     qmake 

     nmake 

     QWT5.1的版本支持VC6编译,QWT6只支持VS编译,不能用vc6编译.  

 二. 编译运行

     vs编译示例代码,报错: moc_plot.obj : error LNK2001: 无法解析的外部符号 "public: static struct QMetaObject const QwtPlot::staticMetaObject" (?staticMetaObject@QwtPlot@@2UQMetaObject@@B)

      解决方案: 在*.pro 加上QWT_DLL宏定义即可

 

三. 自定义XY轴

     示例代码分析发现,大部分示例xy轴都是默认double数据显示,可以通过子类化QwtScaleDraw来自定义xy轴显示数据,代码如下:

Cpp代码  
  1. #include <QApplication>  
  2. #include <QTime>  
  3. #include <qwt/qwt_plot.h>  
  4. #include <qwt/qwt_plot_curve.h>  
  5. #include <qwt/qwt_legend.h>  
  6. #include <qwt/qwt_plot_grid.h>  
  7. #include <qwt/qwt_symbol.h>  
  8. #include <qwt/qwt_plot_zoneitem.h>  
  9. #include "plot.h"  
  10. #include <qwt/qwt_picker_machine.h>  
  11. #include <qwt/qwt_scale_draw.h>  
  12.   
  13.   
  14. // 自定义y轴的显示时标  
  15. class TimeScaleDraw: public QwtScaleDraw  
  16. {  
  17. public:  
  18.     TimeScaleDraw()  
  19.     {  
  20.     }  
  21.   
  22.     virtual QwtText label( double v ) const  
  23.     {  
  24.         QDateTime dt;  
  25.         dt.setTime_t((int)v);  
  26.         return dt.toString("yyyy-MM-dd\n hh:mm:ss");  
  27.     }  
  28. private:  
  29.     QTime baseTime;  
  30. };  
  31.   
  32.   
  33. int GetTime(const QString &pTime)  
  34. {  
  35.     QDateTime dt=QDateTime::fromString(pTime,"yyyy-MM-dd hh:mm:ss");  
  36.     return dt.toTime_t();  
  37. }  
  38.   
  39. int main(int argc,char **argv)  
  40. {  
  41.     QApplication app(argc,argv);  
  42.   
  43.     QwtPlot plot;  
  44.     plot.setTitle("Plot Demo");  
  45.     plot.setCanvasBackground(Qt::white);  
  46.     plot.setAxisScale(QwtPlot::yLeft,0.0,10.0);  
  47.     plot.insertLegend(new QwtLegend(),QwtPlot::RightLegend);  
  48.   
  49.     // 设置自定义的x坐标值(以时标为x轴)  
  50.     plot.setAxisScaleDraw( QwtPlot::xBottom,new TimeScaleDraw());  
  51.     plot.setAxisScale(QwtPlot::xBottom,GetTime("2013-09-29 00:00:00"), GetTime("2013-09-30 00:00:00"),14400); // 设置起始点  
  52.   
  53.     // 网格  
  54.     QwtPlotGrid *grid=new QwtPlotGrid();  
  55.     //grid->setMajorPen( Qt::white, 0, Qt::SolidLine );  
  56.     //grid->setMinorPen( Qt::gray, 0 , Qt::SolidLine );  
  57.     grid->attach(&plot);  
  58.   
  59.     // 数据标示带  
  60.     QwtPlotZoneItem* zone = new QwtPlotZoneItem();  
  61.     zone->setPen( Qt::darkGray );  
  62.     zone->setBrush( QColor( "#834358" ) );  
  63.     zone->setOrientation( Qt::Horizontal );  
  64.     zone->setInterval( 3.8, 5.7 );  
  65.     zone->attach(&plot);  
  66.   
  67.     // 曲线  
  68.     QwtPlotCurve *curve=new QwtPlotCurve;  
  69.     curve->setTitle("Some Points");  
  70.     curve->setPen(Qt::blue,0.5);  
  71.     curve->setRenderHint(QwtPlotItem::RenderAntialiased,true);  
  72.   
  73.     // 黄色小圆圈标记曲线上的数据  
  74.     QwtSymbol *symbol=new QwtSymbol(QwtSymbol::Ellipse,QBrush(Qt::yellow),QPen(Qt::red,2),QSize(8,8));  
  75.     curve->setSymbol(symbol);  
  76.   
  77.     // 数据源  
  78.     QPolygonF points;  
  79.     /* 
  80.     points << QPointF( 0.0, 4.4 ) <<QPointF(0.6,3.9)<< QPointF( 1.0, 3.0 ) 
  81.         << QPointF( 2.0, 4.5 ) << QPointF( 3.0, 6.8 ) 
  82.         << QPointF( 4.0, 7.9 ) << QPointF( 5.0, 7.1 ); 
  83.     */  
  84.   
  85.     points<<QPointF(GetTime("2013-09-29 00:00:00"),4.5)  
  86.         <<QPointF(GetTime("2013-09-29 04:00:00"),3.9)  
  87.         <<QPointF(GetTime("2013-09-29 08:00:00"),3.0)  
  88.         <<QPointF(GetTime("2013-09-29 12:00:00"),7.9);  
  89.   
  90.     curve->setSamples( points );  
  91.   
  92.         // 添加曲线  
  93.     curve->attach(&plot);  
  94.   
  95.     // 曲线跟踪线(拷贝自$(QWT_ROOT)/playground/curvetracker)  
  96.     CurveTracker* tracker = new CurveTracker( plot.canvas() );  
  97.     tracker->setStateMachine( new QwtPickerTrackerMachine() );  
  98.     tracker->setRubberBandPen( QPen( "MediumOrchid" ) );  
  99.   
  100.     plot.resize(600,400);  
  101.     plot.show();  
  102.   
  103.     return app.exec();  
  104. }  

 

    运行截图:

    

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值