qt组直方图例子-qwt
来源:互联网 发布:哪款刷机软件最好用 编辑:程序博客网 时间:2024/05/16 01:53
qwt中 多组直方图的例子如下:
main.cpp代码
- #include <qapplication.h>
- #include <qmainwindow.h>
- #include <qtoolbar.h>
- #include <qtoolbutton.h>
- #include <qcombobox.h>
- #include "barchart.h"
- class MainWindow:public QMainWindow
- {
- public:
- MainWindow( QWidget * = NULL );
- private:
- BarChart *d_chart;
- };
- MainWindow::MainWindow( QWidget *parent ):
- QMainWindow( parent )
- {
- d_chart = new BarChart( this );
- setCentralWidget( d_chart );
- QToolBar *toolBar = new QToolBar( this );
- QComboBox *typeBox = new QComboBox( toolBar );
- typeBox->addItem( "Grouped" );
- typeBox->addItem( "Stacked" );
- typeBox->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
- QComboBox *orientationBox = new QComboBox( toolBar );
- orientationBox->addItem( "Vertical" );
- orientationBox->addItem( "Horizontal" );
- orientationBox->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
- QToolButton *btnExport = new QToolButton( toolBar );
- btnExport->setText( "Export" );
- btnExport->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );
- connect( btnExport, SIGNAL( clicked() ), d_chart, SLOT( exportChart() ) );
- toolBar->addWidget( typeBox );
- toolBar->addWidget( orientationBox );
- toolBar->addWidget( btnExport );
- addToolBar( toolBar );
- d_chart->setMode( typeBox->currentIndex() );
- connect( typeBox, SIGNAL( currentIndexChanged( int ) ),
- d_chart, SLOT( setMode( int ) ) );
- d_chart->setOrientation( orientationBox->currentIndex() );
- connect( orientationBox, SIGNAL( currentIndexChanged(int ) ),
- d_chart, SLOT( setOrientation( int ) ) );
- }
- int main(int argc, char **argv )
- {
- QApplication a( argc, argv );
- MainWindow mainWindow;
- mainWindow.resize( 600, 400 );
- mainWindow.show();
- return a.exec();
- }
barchart.h代码
- #ifndef _BAR_CHART_H_
- #include <qwt_plot.h>
- /**
- *QwtPlotMultiBarChart显示一系列样本数据,每个样本由一组集合数据组成。
- * 每一个数据都可以显示成一个直方图,当然每一组形成的直方图,既可以并排显示,也可以累加显示。
- * 每一个集合的直方图,都可以用QwtColumnSymbol修饰
- */
- class QwtPlotMultiBarChart;
- class BarChart:public QwtPlot
- {
- Q_OBJECT
- public:
- BarChart( QWidget * = NULL );
- public Q_SLOTS:
- void setMode(int );
- void setOrientation(int );
- void exportChart();
- private:
- void populate();
- QwtPlotMultiBarChart *d_barChartItem;
- };
- #endif
barchat.cpp代码
- #include "barchart.h"
- #include <qwt_plot_renderer.h>
- #include <qwt_plot_canvas.h>
- #include <qwt_plot_multi_barchart.h>
- #include <qwt_column_symbol.h>
- #include <qwt_plot_layout.h>
- #include <qwt_legend.h>
- #include <qwt_scale_draw.h>
- BarChart::BarChart( QWidget *parent ):
- QwtPlot( parent )
- {
- setAutoFillBackground( true );
- setPalette( Qt::white );
- canvas()->setPalette( QColor( "LemonChiffon" ) );
- setTitle( "Bar Chart" );
- setAxisTitle( QwtPlot::yLeft, "Whatever" );
- setAxisTitle( QwtPlot::xBottom, "Whatever" );
- d_barChartItem = new QwtPlotMultiBarChart( "Bar Chart " );
- d_barChartItem->setLayoutPolicy( QwtPlotMultiBarChart::AutoAdjustSamples );
- d_barChartItem->setSpacing( 20 );//spacing 是两个样本之间的距离(两个直方图之间的距离,或者每组直方图之间的距离
- d_barChartItem->setMargin( 3);// margin 最外层的直方图与画布的边框矩形之间的距离
- d_barChartItem->attach( this );
- insertLegend( new QwtLegend() );//插入描述符
- populate();
- setOrientation( 0 );
- setAutoReplot( true );
- }
- void BarChart::populate()
- {
- staticconst char *colors[] = { "DarkOrchid", "SteelBlue", "Gold" };
- constint numSamples = 5;
- constint numBars = sizeof( colors ) / sizeof( colors[0] );
- QList<QwtText> titles;
- for (int i = 0; i < numBars; i++ )
- {
- QString title("Bar %1");
- titles += title.arg( i );
- }
- d_barChartItem->setBarTitles( titles );//统一设置直方图的标题,这些标题是给legend用的
- d_barChartItem->setLegendIconSize( QSize( 10, 14 ) );
- for (int i = 0; i < numBars; i++ )
- {
- QwtColumnSymbol *symbol = new QwtColumnSymbol( QwtColumnSymbol::Box );
- symbol->setLineWidth( 1 );//设置这个Box的边框宽度
- symbol->setFrameStyle( QwtColumnSymbol::Plain );//或者Raised 和NoFrame
- symbol->setPalette( QPalette( colors[i] ) );
- d_barChartItem->setSymbol( i, symbol );//每一组内部的直方图设置
- }
- QVector< QVector<double> > series;//二维数据
- for (int i = 0; i < numSamples; i++ )//所有样本(直方图组)
- {
- QVector<double> values;
- for (int j = 0; j < numBars; j++ )//每组内的直方图赋值
- values += ( 2 + qrand() % 8 );
- series += values;
- }
- d_barChartItem->setSamples( series );
- }
- //设置直方图显示方式
- void BarChart::setMode(int mode )
- {
- if ( mode == 0 )
- {
- d_barChartItem->setStyle( QwtPlotMultiBarChart::Grouped );
- }
- else
- {
- d_barChartItem->setStyle( QwtPlotMultiBarChart::Stacked );
- }
- }
- //设置直方图显示方向和坐标轴变化
- void BarChart::setOrientation(int orientation )
- {
- QwtPlot::Axis axis1, axis2;
- if ( orientation == 0 )
- {
- axis1 = QwtPlot::xBottom;
- axis2 = QwtPlot::yLeft;
- d_barChartItem->setOrientation( Qt::Vertical );
- }
- else
- {
- axis1 = QwtPlot::yLeft;
- axis2 = QwtPlot::xBottom;
- d_barChartItem->setOrientation( Qt::Horizontal );
- }
- //设置x轴刻度
- setAxisScale( axis1, 0, d_barChartItem->dataSize() - 1, 1.0 );
- //设置y周刻度自动调整
- setAxisAutoScale( axis2 );
- //Backbone = the line where the ticks are located. Ticks Labels
- //重新绘制坐标轴刻度
- QwtScaleDraw *scaleDraw1 = axisScaleDraw( axis1 );
- scaleDraw1->enableComponent( QwtScaleDraw::Backbone,false );
- scaleDraw1->enableComponent( QwtScaleDraw::Ticks, false );
- QwtScaleDraw *scaleDraw2 = axisScaleDraw( axis2 );
- scaleDraw2->enableComponent( QwtScaleDraw::Backbone,true );
- scaleDraw2->enableComponent( QwtScaleDraw::Ticks, true );
- scaleDraw2->enableComponent( QwtScaleDraw::Labels ,true );
- plotLayout()->setAlignCanvasToScale( axis1, true );
- plotLayout()->setAlignCanvasToScale( axis2, false );
- plotLayout()->setCanvasMargin( 0 );
- updateCanvasMargins();
- replot();
- }
- void BarChart::exportChart()
- {
- QwtPlotRenderer renderer;
- renderer.exportTo( this, "barchart.pdf" );
- }
0 0
- qt组直方图例子-qwt
- Qwt直方图例子注释
- GDAL+qwt直方图显示
- qt+qwt
- QWT+Qt creator
- Qt qwt的example
- Qt Qwt打点画图
- Qt qwt学习 一
- Qt- QWT 安装(mingw32)
- Qt--安装QWT
- ubuntu qt 配置qwt
- Qt Creator 安装 QWT
- 利用GDAL和QWT绘制图像直方图2--QWT绘制直方图
- 利用GDAL和QWT绘制图像直方图2--QWT绘制直方图
- Qwt例子-CpuPlot注释版
- 安装QWT到QT中
- 基于Qwt的Qt客户端
- QT creator使用qwt插件
- 复习pdo,插入,查询
- USACO 刷水
- 说“无法对数据库'Alarm'执行删除,因为它正用于复制”
- hive表数据导出到csv乱码原因及解决方案
- 1412202059-hpu-1005:C语言考试练习题_排列
- qt组直方图例子-qwt
- 萨法司法所分
- 主机centos7系统与VBOX下的XP系统实现文件共享
- ubuntu+ndk编译ffmpeg
- android第一个APP
- 最简单的方法实现MIDI音乐循环播放 (不用在MCI回调中处理)
- Spring 线程池
- 句柄和指针的区别
- 使用libevent编写Linux服务