qt组直方图例子-qwt

来源:互联网 发布:哪款刷机软件最好用 编辑:程序博客网 时间:2024/05/16 01:53
qwt中 多组直方图的例子如下:
qt组直方图例子-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 *typeBox = new QComboBox( toolBar ); 
  24.     typeBox->addItem( "Grouped" ); 
  25.     typeBox->addItem( "Stacked" ); 
  26.     typeBox->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ); 
  27.  
  28.     QComboBox *orientationBox = new QComboBox( toolBar ); 
  29.     orientationBox->addItem( "Vertical" ); 
  30.     orientationBox->addItem( "Horizontal" ); 
  31.     orientationBox->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ); 
  32.  
  33.     QToolButton *btnExport = new QToolButton( toolBar ); 
  34.     btnExport->setText( "Export" ); 
  35.     btnExport->setToolButtonStyle( Qt::ToolButtonTextUnderIcon ); 
  36.     connect( btnExport, SIGNAL( clicked() ), d_chart, SLOT( exportChart() ) ); 
  37.  
  38.     toolBar->addWidget( typeBox ); 
  39.     toolBar->addWidget( orientationBox ); 
  40.     toolBar->addWidget( btnExport ); 
  41.     addToolBar( toolBar ); 
  42.  
  43.     d_chart->setMode( typeBox->currentIndex() ); 
  44.     connect( typeBox, SIGNAL( currentIndexChanged( int ) ), 
  45.              d_chart, SLOT( setMode( int ) ) ); 
  46.  
  47.     d_chart->setOrientation( orientationBox->currentIndex() ); 
  48.     connect( orientationBox, SIGNAL( currentIndexChanged(int ) ), 
  49.              d_chart, SLOT( setOrientation( int ) ) ); 
  50.  
  51. int main(int argc, char **argv ) 
  52.     QApplication a( argc, argv ); 
  53.  
  54.     MainWindow mainWindow; 
  55.  
  56.     mainWindow.resize( 600, 400 ); 
  57.     mainWindow.show(); 
  58.  
  59.     return a.exec(); 

barchart.h代码
  1. #ifndef _BAR_CHART_H_ 
  2.  
  3. #include <qwt_plot.h> 
  4.  
  5. /**
  6. *QwtPlotMultiBarChart显示一系列样本数据,每个样本由一组集合数据组成。
  7. * 每一个数据都可以显示成一个直方图,当然每一组形成的直方图,既可以并排显示,也可以累加显示。
  8. * 每一个集合的直方图,都可以用QwtColumnSymbol修饰
  9. */ 
  10. class QwtPlotMultiBarChart; 
  11.  
  12. class BarChart:public QwtPlot 
  13.     Q_OBJECT 
  14.  
  15. public
  16.     BarChart( QWidget * = NULL ); 
  17.  
  18. public Q_SLOTS: 
  19.     void setMode(int ); 
  20.     void setOrientation(int ); 
  21.     void exportChart(); 
  22.  
  23. private
  24.     void populate(); 
  25.  
  26.     QwtPlotMultiBarChart *d_barChartItem; 
  27. }; 
  28.  
  29. #endif 

barchat.cpp代码

  1. #include "barchart.h" 
  2. #include <qwt_plot_renderer.h> 
  3. #include <qwt_plot_canvas.h> 
  4. #include <qwt_plot_multi_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. BarChart::BarChart( QWidget *parent ): 
  11.     QwtPlot( parent ) 
  12.     setAutoFillBackground( true ); 
  13.  
  14.     setPalette( Qt::white ); 
  15.     canvas()->setPalette( QColor( "LemonChiffon" ) ); 
  16.  
  17.     setTitle( "Bar Chart" ); 
  18.  
  19.     setAxisTitle( QwtPlot::yLeft, "Whatever" ); 
  20.     setAxisTitle( QwtPlot::xBottom, "Whatever" ); 
  21.  
  22.     d_barChartItem = new QwtPlotMultiBarChart( "Bar Chart " ); 
  23.     d_barChartItem->setLayoutPolicy( QwtPlotMultiBarChart::AutoAdjustSamples   ); 
  24.     d_barChartItem->setSpacing( 20 );//spacing 是两个样本之间的距离(两个直方图之间的距离,或者每组直方图之间的距离 
  25.     d_barChartItem->setMargin( 3);// margin 最外层的直方图与画布的边框矩形之间的距离 
  26.  
  27.     d_barChartItem->attach( this ); 
  28.  
  29.     insertLegend( new QwtLegend() );//插入描述符 
  30.  
  31.     populate(); 
  32.     setOrientation( 0 ); 
  33.  
  34.     setAutoReplot( true ); 
  35.  
  36. void BarChart::populate() 
  37.     staticconst char *colors[] = { "DarkOrchid", "SteelBlue", "Gold" }; 
  38.  
  39.     constint numSamples = 5; 
  40.     constint numBars = sizeof( colors ) / sizeof( colors[0] ); 
  41.  
  42.     QList<QwtText> titles; 
  43.     for (int i = 0; i < numBars; i++ ) 
  44.     { 
  45.         QString title("Bar %1"); 
  46.         titles += title.arg( i ); 
  47.     } 
  48.     d_barChartItem->setBarTitles( titles );//统一设置直方图的标题,这些标题是给legend用的 
  49.     d_barChartItem->setLegendIconSize( QSize( 10, 14 ) ); 
  50.  
  51.     for (int i = 0; i < numBars; i++ ) 
  52.     { 
  53.         QwtColumnSymbol *symbol = new QwtColumnSymbol( QwtColumnSymbol::Box ); 
  54.         symbol->setLineWidth( 1 );//设置这个Box的边框宽度 
  55.         symbol->setFrameStyle( QwtColumnSymbol::Plain  );//或者Raised 和NoFrame 
  56.         symbol->setPalette( QPalette( colors[i] ) ); 
  57.  
  58.         d_barChartItem->setSymbol( i, symbol );//每一组内部的直方图设置 
  59.     } 
  60.      
  61.     QVector< QVector<double> > series;//二维数据 
  62.     for (int i = 0; i < numSamples; i++ )//所有样本(直方图组) 
  63.     { 
  64.         QVector<double> values; 
  65.         for (int j = 0; j < numBars; j++ )//每组内的直方图赋值 
  66.             values += ( 2 + qrand() % 8 ); 
  67.  
  68.         series += values; 
  69.     } 
  70.  
  71.     d_barChartItem->setSamples( series ); 
  72. //设置直方图显示方式 
  73. void BarChart::setMode(int mode ) 
  74.     if ( mode == 0 ) 
  75.     { 
  76.         d_barChartItem->setStyle( QwtPlotMultiBarChart::Grouped ); 
  77.     } 
  78.     else 
  79.     { 
  80.         d_barChartItem->setStyle( QwtPlotMultiBarChart::Stacked ); 
  81.     } 
  82. //设置直方图显示方向和坐标轴变化 
  83. void BarChart::setOrientation(int orientation ) 
  84.     QwtPlot::Axis axis1, axis2; 
  85.  
  86.     if ( orientation == 0 ) 
  87.     { 
  88.         axis1 = QwtPlot::xBottom; 
  89.         axis2 = QwtPlot::yLeft; 
  90.  
  91.         d_barChartItem->setOrientation( Qt::Vertical ); 
  92.     } 
  93.     else 
  94.     { 
  95.         axis1 = QwtPlot::yLeft; 
  96.         axis2 = QwtPlot::xBottom; 
  97.  
  98.         d_barChartItem->setOrientation( Qt::Horizontal ); 
  99.     } 
  100.     //设置x轴刻度 
  101.     setAxisScale( axis1, 0, d_barChartItem->dataSize() - 1, 1.0 ); 
  102.     //设置y周刻度自动调整 
  103.     setAxisAutoScale( axis2 ); 
  104.  
  105.     //Backbone = the line where the ticks are located.    Ticks    Labels 
  106.     //重新绘制坐标轴刻度 
  107.     QwtScaleDraw *scaleDraw1 = axisScaleDraw( axis1 ); 
  108.     scaleDraw1->enableComponent( QwtScaleDraw::Backbone,false ); 
  109.     scaleDraw1->enableComponent( QwtScaleDraw::Ticks, false ); 
  110.     QwtScaleDraw *scaleDraw2 = axisScaleDraw( axis2 ); 
  111.  
  112.     scaleDraw2->enableComponent( QwtScaleDraw::Backbone,true ); 
  113.     scaleDraw2->enableComponent( QwtScaleDraw::Ticks, true ); 
  114.     scaleDraw2->enableComponent( QwtScaleDraw::Labels ,true ); 
  115.  
  116.  
  117.  
  118.  
  119.     plotLayout()->setAlignCanvasToScale( axis1, true ); 
  120.     plotLayout()->setAlignCanvasToScale( axis2, false ); 
  121.  
  122.     plotLayout()->setCanvasMargin( 0 ); 
  123.     updateCanvasMargins(); 
  124.  
  125.     replot(); 
  126.  
  127. void BarChart::exportChart() 
  128.     QwtPlotRenderer renderer; 
  129.     renderer.exportTo( this, "barchart.pdf" ); 
0 0