Advertisement
Guest User

Untitled

a guest
May 13th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // header //
  2. #ifndef _BAR_CHART_H_
  3.  
  4. #include <qwt_plot.h>
  5.  
  6. class QwtPlotMultiBarChart;
  7.  
  8. class BarChart: public QwtPlot
  9. {
  10.     Q_OBJECT
  11.  
  12. public:
  13.     BarChart( QWidget * = NULL );
  14.  
  15. public Q_SLOTS:
  16.     void setMode( int );
  17.     void setOrientation( int );
  18.     void exportChart();
  19.  
  20. private:
  21.     void populate();
  22.  
  23.     QwtPlotMultiBarChart *d_barChartItem;
  24. };
  25.  
  26. #endif
  27.  
  28. //chart//
  29.  
  30.  
  31.  
  32. #include "barchart.h"
  33. #include <qwt_plot_renderer.h>
  34. #include <qwt_plot_canvas.h>
  35. #include <qwt_plot_multi_barchart.h>
  36. #include <qwt_column_symbol.h>
  37. #include <qwt_plot_layout.h>
  38. #include <qwt_legend.h>
  39. #include <qwt_scale_draw.h>
  40.  
  41. BarChart::BarChart( QWidget *parent ):
  42.     QwtPlot( parent )
  43. {
  44.     setAutoFillBackground( true );
  45.  
  46.     setPalette( Qt::white );
  47.     canvas()->setPalette( QColor( "LemonChiffon" ) );
  48.  
  49.     setTitle( "Bar Chart" );
  50.  
  51.     setAxisTitle( QwtPlot::yLeft, "Whatever" );
  52.     setAxisTitle( QwtPlot::xBottom, "Whatever" );
  53.  
  54.     d_barChartItem = new QwtPlotMultiBarChart( "Bar Chart " );
  55.     d_barChartItem->setLayoutPolicy( QwtPlotMultiBarChart::AutoAdjustSamples );
  56.     d_barChartItem->setSpacing( 20 );
  57.     d_barChartItem->setMargin( 3 );
  58.  
  59.     d_barChartItem->attach( this );
  60.  
  61.     insertLegend( new QwtLegend() );
  62.  
  63.     populate();
  64.     setOrientation( 0 );
  65.  
  66.     setAutoReplot( true );
  67. }
  68.  
  69. void BarChart::populate()
  70. {
  71.     static const char *colors[] = { "DarkOrchid", "SteelBlue", "Gold" };
  72.  
  73.     const int numSamples = 5;
  74.     const int numBars = sizeof( colors ) / sizeof( colors[0] );
  75.  
  76.     QList<QwtText> titles;
  77.     for ( int i = 0; i < numBars; i++ )
  78.     {
  79.         QString title("Bar %1");
  80.         titles += title.arg( i );
  81.     }
  82.     d_barChartItem->setBarTitles( titles );
  83.     d_barChartItem->setLegendIconSize( QSize( 10, 14 ) );
  84.  
  85.     for ( int i = 0; i < numBars; i++ )
  86.     {
  87.         QwtColumnSymbol *symbol = new QwtColumnSymbol( QwtColumnSymbol::Box );
  88.         symbol->setLineWidth( 2 );
  89.         symbol->setFrameStyle( QwtColumnSymbol::Raised );
  90.         symbol->setPalette( QPalette( colors[i] ) );
  91.  
  92.         d_barChartItem->setSymbol( i, symbol );
  93.     }
  94.  
  95.     QVector< QVector<double> > series;
  96.     for ( int i = 0; i < numSamples; i++ )
  97.     {
  98.         QVector<double> values;
  99.         for ( int j = 0; j < numBars; j++ )
  100.             values += ( 2 + qrand() % 8 );
  101.  
  102.         series += values;
  103.     }
  104.  
  105.     d_barChartItem->setSamples( series );
  106. }
  107.  
  108. void BarChart::setMode( int mode )
  109. {
  110.     if ( mode == 0 )
  111.     {
  112.         d_barChartItem->setStyle( QwtPlotMultiBarChart::Grouped );
  113.     }
  114.     else
  115.     {
  116.         d_barChartItem->setStyle( QwtPlotMultiBarChart::Stacked );
  117.     }
  118. }
  119.  
  120. void BarChart::setOrientation( int orientation )
  121. {
  122.     QwtPlot::Axis axis1, axis2;
  123.  
  124.     if ( orientation == 0 )
  125.     {
  126.         axis1 = QwtPlot::xBottom;
  127.         axis2 = QwtPlot::yLeft;
  128.  
  129.         d_barChartItem->setOrientation( Qt::Vertical );
  130.     }
  131.     else
  132.     {
  133.         axis1 = QwtPlot::yLeft;
  134.         axis2 = QwtPlot::xBottom;
  135.  
  136.         d_barChartItem->setOrientation( Qt::Horizontal );
  137.     }
  138.  
  139.     setAxisScale( axis1, 0, d_barChartItem->dataSize() - 1, 1.0 );
  140.     setAxisAutoScale( axis2 );
  141.  
  142.     QwtScaleDraw *scaleDraw1 = axisScaleDraw( axis1 );
  143.     scaleDraw1->enableComponent( QwtScaleDraw::Backbone, false );
  144.     scaleDraw1->enableComponent( QwtScaleDraw::Ticks, false );
  145.  
  146.     QwtScaleDraw *scaleDraw2 = axisScaleDraw( axis2 );
  147.     scaleDraw2->enableComponent( QwtScaleDraw::Backbone, true );
  148.     scaleDraw2->enableComponent( QwtScaleDraw::Ticks, true );
  149.  
  150.     plotLayout()->setAlignCanvasToScale( axis1, true );
  151.     plotLayout()->setAlignCanvasToScale( axis2, false );
  152.  
  153.     plotLayout()->setCanvasMargin( 0 );
  154.     updateCanvasMargins();
  155.  
  156.     replot();
  157. }
  158.  
  159. void BarChart::exportChart()
  160. {
  161.     QwtPlotRenderer renderer;
  162.     renderer.exportTo( this, "barchart.pdf" );
  163. }
  164.  
  165. // main //
  166.  
  167. #include <qapplication.h>
  168. #include <qmainwindow.h>
  169. #include <qtoolbar.h>
  170. #include <qtoolbutton.h>
  171. #include <qcombobox.h>
  172. #include "barchart.h"
  173.  
  174. class MainWindow: public QMainWindow
  175. {
  176. public:
  177.     MainWindow( QWidget * = NULL );
  178.  
  179. private:
  180.     BarChart *d_chart;
  181. };
  182.  
  183. MainWindow::MainWindow( QWidget *parent ):
  184.     QMainWindow( parent )
  185. {
  186.     d_chart = new BarChart( this );
  187.     setCentralWidget( d_chart );
  188.  
  189.     QToolBar *toolBar = new QToolBar( this );
  190.  
  191.     QComboBox *typeBox = new QComboBox( toolBar );
  192.     typeBox->addItem( "Grouped" );
  193.     typeBox->addItem( "Stacked" );
  194.     typeBox->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
  195.  
  196.     QComboBox *orientationBox = new QComboBox( toolBar );
  197.     orientationBox->addItem( "Vertical" );
  198.     orientationBox->addItem( "Horizontal" );
  199.     orientationBox->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
  200.  
  201.     QToolButton *btnExport = new QToolButton( toolBar );
  202.     btnExport->setText( "Export" );
  203.     btnExport->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );
  204.     connect( btnExport, SIGNAL( clicked() ), d_chart, SLOT( exportChart() ) );
  205.  
  206.     toolBar->addWidget( typeBox );
  207.     toolBar->addWidget( orientationBox );
  208.     toolBar->addWidget( btnExport );
  209.     addToolBar( toolBar );
  210.  
  211.     d_chart->setMode( typeBox->currentIndex() );
  212.     connect( typeBox, SIGNAL( currentIndexChanged( int ) ),
  213.              d_chart, SLOT( setMode( int ) ) );
  214.  
  215.     d_chart->setOrientation( orientationBox->currentIndex() );
  216.     connect( orientationBox, SIGNAL( currentIndexChanged( int ) ),
  217.              d_chart, SLOT( setOrientation( int ) ) );
  218. }
  219.  
  220. int main( int argc, char **argv )
  221. {
  222.     QApplication a( argc, argv );
  223.  
  224.     MainWindow mainWindow;
  225.  
  226.     mainWindow.resize( 600, 400 );
  227.     mainWindow.show();
  228.  
  229.     return a.exec();
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement