Qwt直方图例子注释

来源:互联网 发布:淘宝借贷逾期 编辑:程序博客网 时间:2024/04/26 07:38
qwt帮助文档还是很多的,学习挺耗时,现将单个直方图显示的例子,注释一遍。
方便理解,后期可以直接使用了。效果如下:

Qwt直方图例子注释 - 柳北风儿 - 柳北风儿~~~~~~~欲宇仙炅




main.cpp代码
  1. #include <qapplication.h> 
  2. #include <qmainwindow.h> 
  3. #include <qtoolbar.h> 
  4. #include <qtoolbutton.h> 
  5. #include <qcombobox.h> 
  6. #include "barchart.h" 
  7.  
  8. class MainWindow:public QMainWindow 
  9. public
  10.     MainWindow( QWidget * = NULL ); 
  11.  
  12. private
  13.     BarChart *d_chart; 
  14. }; 
  15.  
  16. MainWindow::MainWindow( QWidget *parent ): 
  17.     QMainWindow( parent ) 
  18.     d_chart = new BarChart( this ); 
  19.     setCentralWidget( d_chart ); 
  20.  
  21.     QToolBar *toolBar = new QToolBar( this ); 
  22.  
  23.     QComboBox *orientationBox = new QComboBox( toolBar ); 
  24.     orientationBox->addItem( "Vertical" ); 
  25.     orientationBox->addItem( "Horizontal" ); 
  26.     orientationBox->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ); 
  27.  
  28.     QToolButton *btnExport = new QToolButton( toolBar ); 
  29.     btnExport->setText( "Export" ); 
  30.     btnExport->setToolButtonStyle( Qt::ToolButtonTextUnderIcon ); 
  31.     connect( btnExport, SIGNAL( clicked() ), d_chart, SLOT( exportChart() ) ); 
  32.  
  33.     toolBar->addWidget( orientationBox ); 
  34.     toolBar->addWidget( btnExport ); 
  35.     addToolBar( toolBar ); 
  36.  
  37.     d_chart->setOrientation( orientationBox->currentIndex() ); 
  38.     connect( orientationBox, SIGNAL( currentIndexChanged(int ) ), 
  39.              d_chart, SLOT( setOrientation( int ) ) ); 
  40.  
  41. int main(int argc, char **argv ) 
  42.     QApplication a( argc, argv ); 
  43.  
  44.     MainWindow mainWindow; 
  45.  
  46.     mainWindow.resize( 600, 400 ); 
  47.     mainWindow.show(); 
  48.  
  49.     return a.exec(); 


barchart.h代码
  1. #ifndef _BAR_CHART_H_ 
  2.  
  3. #include <qwt_plot.h> 
  4. #include <qstringlist.h> 
  5.  
  6. class DistroChartItem; 
  7.  
  8. class BarChart:public QwtPlot 
  9.     Q_OBJECT 
  10.  
  11. public
  12.     BarChart( QWidget * = NULL ); 
  13.  
  14. public Q_SLOTS: 
  15.     void setOrientation(int ); 
  16.     void exportChart(); 
  17.  
  18. private
  19.     void populate(); 
  20.  
  21.     DistroChartItem *d_barChartItem;//自定义直方图类 
  22.     QStringList d_distros;//改变方向重置标题用 
  23. }; 
  24.  
  25. #endif 

barchart.cpp代码
  1. #include "barchart.h" 
  2. #include <qwt_plot_renderer.h> 
  3. #include <qwt_plot_canvas.h> 
  4. #include <qwt_plot_barchart.h> 
  5. #include <qwt_column_symbol.h> 
  6. #include <qwt_plot_layout.h> 
  7. #include <qwt_legend.h> 
  8. #include <qwt_scale_draw.h> 
  9.  
  10. class DistroScaleDraw:public QwtScaleDraw 
  11. public
  12.     DistroScaleDraw( Qt::Orientation orientation, const QStringList &labels ): 
  13.         d_labels( labels ) 
  14.     { 
  15.         setTickLength( QwtScaleDiv::MinorTick, 0 ); 
  16.         setTickLength( QwtScaleDiv::MediumTick, 0 ); 
  17.         setTickLength( QwtScaleDiv::MajorTick, 2 ); 
  18.  
  19.         enableComponent( QwtScaleDraw::Backbone, false ); 
  20.  
  21.         if ( orientation == Qt::Vertical ) 
  22.         { 
  23.             setLabelRotation( -60.0 ); 
  24.         } 
  25.         else 
  26.         { 
  27.             setLabelRotation( -20.0 ); 
  28.         } 
  29.  
  30.         setLabelAlignment( Qt::AlignLeft | Qt::AlignVCenter ); 
  31.     } 
  32.  
  33.     //刻度标签值 
  34.     virtual QwtText label(double value ) const 
  35.     { 
  36.         QwtText lbl; 
  37.  
  38.         constint index = qRound( value );//四舍五入 
  39.         if ( index >= 0 && index <= d_labels.size() ) 
  40.         { 
  41.             lbl = d_labels[ index ]; 
  42.         } 
  43.         return lbl; 
  44.     } 
  45.  
  46. private
  47.     const QStringList d_labels; 
  48. }; 
  49.  
  50. //直方图 
  51. //QwtPlotBarChart将一系列数据显示为bar 
  52. class DistroChartItem:public QwtPlotBarChart 
  53. public
  54.     DistroChartItem(): 
  55.         QwtPlotBarChart( "Page Hits"
  56.     { 
  57.         setLegendMode( QwtPlotBarChart::LegendBarTitles );//显示所有bar各自的标题,而QwtPlotBarChart::LegendChartTitle 显示这个表的标题 
  58.         setLegendIconSize( QSize( 10, 14 ) ); 
  59.     } 
  60.  
  61.     //分配颜色和标题给bar,并更新 
  62.     void addDistro(const QString &distro, const QColor &color ) 
  63.     { 
  64.         d_colors += color; 
  65.         d_distros += distro; 
  66.         itemChanged();//Update the legend of the parent plot. 
  67.     } 
  68.  
  69.     //自定义每个Bar 
  70.     virtual QwtColumnSymbol *specialSymbol( 
  71.         int index,const QPointF& ) const 
  72.     { 
  73.  
  74.  
  75.         QwtColumnSymbol *symbol = new QwtColumnSymbol( QwtColumnSymbol::Box ); 
  76.         symbol->setLineWidth( 1 ); 
  77.         symbol->setFrameStyle( QwtColumnSymbol::Plain );//Raised 
  78.  
  79.         QColor c( Qt::white ); 
  80.         if ( index >= 0 && index < d_colors.size() ) 
  81.             c = d_colors[ index ]; 
  82.  
  83.         symbol->setPalette( c ); 
  84.      
  85.         return symbol; 
  86.     } 
  87.     //自定义每个bar的标题 
  88.     virtual QwtText barTitle(int sampleIndex ) const 
  89.     { 
  90.         QwtText title; 
  91.         if ( sampleIndex >= 0 && sampleIndex < d_distros.size() ) 
  92.             title = d_distros[ sampleIndex ]; 
  93.  
  94.         return title; 
  95.     } 
  96.  
  97. private
  98.     QList<QColor> d_colors; 
  99.     QList<QString> d_distros; 
  100. }; 
  101.  
  102. BarChart::BarChart( QWidget *parent ): 
  103.     QwtPlot( parent ) 
  104.     conststruct  
  105.     { 
  106.         constchar *distro; 
  107.         constint hits; 
  108.         QColor color; 
  109.  
  110.     } pageHits[] = 
  111.     { 
  112.         { "Arch", 1114, QColor("DodgerBlue" ) }, 
  113.         { "Debian", 1373, QColor("#d70751" ) }, 
  114.         { "Fedora", 1638, QColor("SteelBlue" ) }, 
  115.         { "Mageia", 1395, QColor("Indigo" ) }, 
  116.         { "Mint", 3874, QColor( 183, 255, 183 ) }, 
  117.         { "openSuSE", 1532, QColor( 115, 186, 37 ) }, 
  118.         { "Puppy", 1059, QColor("LightSkyBlue" ) }, 
  119.         { "Ubuntu", 2391, QColor("FireBrick" ) } 
  120.     }; 
  121.  
  122.     setAutoFillBackground( true ); 
  123.     setPalette( QColor( "Linen" ) ); 
  124.  
  125.     //设置画布 
  126.     QwtPlotCanvas *canvas = new QwtPlotCanvas(); 
  127.     canvas->setLineWidth( 2 ); 
  128.     canvas->setFrameStyle( QFrame::Box | QFrame::Sunken ); 
  129.     canvas->setBorderRadius( 10 ); 
  130.  
  131.     QPalette canvasPalette( QColor( "Plum" ) ); 
  132.     canvasPalette.setColor( QPalette::Foreground, QColor("Indigo" ) ); 
  133.     canvas->setPalette( canvasPalette ); 
  134.  
  135.     setCanvas( canvas ); 
  136.  
  137.     setTitle( "DistroWatch Page Hit Ranking, April 2012" ); 
  138.  
  139.     d_barChartItem = new DistroChartItem(); 
  140.  
  141.     //数据源 
  142.     QVector< double > samples; 
  143.  
  144.     for ( uint i = 0; i <sizeof( pageHits ) / sizeof( pageHits[ 0 ] ); i++ ) 
  145.     { 
  146.         d_distros += pageHits[ i ].distro; //给标题赋值 
  147.         samples += pageHits[ i ].hits;//给数据赋值 
  148.  
  149.         d_barChartItem->addDistro(  
  150.             pageHits[ i ].distro, pageHits[ i ].color );//更新颜色和标题 
  151.     } 
  152.  
  153.     d_barChartItem->setSamples( samples );//更新数据 
  154.  
  155.     d_barChartItem->attach( this ); 
  156.  
  157.     insertLegend( new QwtLegend() ); 
  158.  
  159.     setOrientation( 0 ); 
  160.     setAutoReplot( false ); 
  161.  
  162. void BarChart::setOrientation(int o ) 
  163.     const Qt::Orientation orientation = 
  164.         ( o == 0 ) ? Qt::Vertical : Qt::Horizontal; 
  165.  
  166.     int axis1 = QwtPlot::xBottom; 
  167.     int axis2 = QwtPlot::yLeft; 
  168.  
  169.     if ( orientation == Qt::Horizontal ) 
  170.         qSwap( axis1, axis2 ); 
  171.  
  172.     d_barChartItem->setOrientation( orientation );//直方图方向 
  173.     d_barChartItem->setSpacing( 20 );//spacing 是两个样本之间的距离(两个直方图之间的距离,或者每组直方图之间的距离 
  174.     d_barChartItem->setMargin( 30);// margin 最外层的直方图与画布的边框矩形之间的距离 
  175.  
  176.     setAxisTitle( axis1, "Distros" ); 
  177.     setAxisMaxMinor( axis1, 3);//设置坐标轴小刻度的最大值 
  178.     setAxisMaxMajor( axis1, 10);//设置坐标轴大刻度的最大值 
  179.     setAxisScaleDraw( axis1, new DistroScaleDraw( orientation, d_distros ) );//需要重新更新标题 
  180.  
  181.  
  182.     setAxisTitle( axis2, "Hits per day ( HPD )" ); 
  183.     setAxisMaxMinor( axis2, 3 ); 
  184.  
  185.     QwtScaleDraw *scaleDraw = new QwtScaleDraw(); 
  186.     scaleDraw->setTickLength( QwtScaleDiv::MediumTick, 4 ); 
  187.     setAxisScaleDraw( axis2, scaleDraw ); 
  188.  
  189.     plotLayout()->setCanvasMargin( 0 ); 
  190.     replot(); 
  191.  
  192. void BarChart::exportChart() 
  193.     QwtPlotRenderer renderer; 
  194.     renderer.exportTo( this, "distrowatch.pdf" ); 
0 0