Hi everyone,

I am currently trying to plot a stacked bar chart. However the width of the bars are not equal. I am plotting 5 bars with each bar stacked with two. I have attached an image of the bar chart. Only the first and last bar are not in equal size as the rest of the bars. I have also attached the portion of code which generates the barchart, below. Please guide me on how to solve this issue.

barchart.jpg

Qt Code:
  1. BarChart::BarChart(QWidget *parent)
  2. : QwtPlot(parent)
  3. {
  4. setAutoFillBackground(true);
  5. setPalette(Qt::white);
  6. canvas()->setPalette(QColor ("Black")); //background color
  7.  
  8. barChart = new QwtPlotMultiBarChart;
  9. barChart->setLayoutPolicy(QwtPlotMultiBarChart::AutoAdjustSamples);
  10. barChart->setSpacing(20);
  11. barChart->setMargin(20);
  12. barChart->setStyle(QwtPlotMultiBarChart::Stacked);
  13. barChart->attach(this);
  14.  
  15.  
  16. for(int i = 0; i < 2; ++i)
  17. {
  18. QwtColumnSymbol *symbol = new QwtColumnSymbol(QwtColumnSymbol::Box);
  19. QPalette barColor = (i == 0) ? Qt::green : Qt::red ;
  20. symbol->setPalette(barColor);
  21. symbol->setFrameStyle(QwtColumnSymbol::Raised);
  22. symbol->setLineWidth(1);
  23. barChart->setSymbol(i, symbol);
  24. }
  25.  
  26. QVector<QVector<double> > newBarChartData;
  27. for(int i = 0; i < 5; i++)
  28. {
  29. num = rand() % 100;
  30. QVector<double> values;
  31. values.push_back(num);
  32. values.push_back(100-num);
  33.  
  34. newBarChartData.push_back(values);
  35. }
  36. barChart->setSamples(newData);
  37.  
  38. QwtPlot::Axis yAxis = QwtPlot::yLeft,
  39. xAxis = QwtPlot::xBottom;
  40. setAxisScale(yAxis, 0, 100, 20);
  41. setAxisAutoScale(xAxis);
  42. enableAxis(yAxis, true);
  43. enableAxis(xAxis, false);
  44.  
  45. QwtScaleDraw *yAxis_scaleDraw = axisScaleDraw(yAxis);
  46. yAxis_scaleDraw->enableComponent(QwtScaleDraw::Backbone, false);
  47. yAxis_scaleDraw->enableComponent(QwtScaleDraw::Ticks, false);
  48.  
  49. QwtScaleDraw *xAxis_scaleDraw = axisScaleDraw(xAxis);
  50. xAxis_scaleDraw->enableComponent(QwtScaleDraw::Backbone, false);
  51. xAxis_scaleDraw->enableComponent(QwtScaleDraw::Ticks, false);
  52.  
  53. plotLayout()->setCanvasMargin(0);
  54. updateCanvasMargins();
  55. replot();
  56. }
To copy to clipboard, switch view to plain text mode