Results 1 to 7 of 7

Thread: QwtPlotMultiBarChart not equal width

  1. #1
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QwtPlotMultiBarChart not equal width

    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 

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlotMultiBarChart not equal width

    Quote Originally Posted by nanthiran_2005 View Post
    Qt Code:
    1. plotLayout()->setCanvasMargin(0);
    2. updateCanvasMargins();
    To copy to clipboard, switch view to plain text mode 
    The left half of the bars are cut off - guess because your code explicitly removes the margins that are needed for them. Calling QwtPlot::updateCanvasMargins(); manually - outside the replot context - doesn't make much sense too.

    What happens when removing the bad lines above ?

    Uwe

  3. The following user says thank you to Uwe for this useful post:

    Momergil (23rd June 2014)

  4. #3
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QwtPlotMultiBarChart not equal width

    Hi Uwe,

    That actually did solve my problem. Thanks alot.

    I have removed updateCanvasMargin(), and set canvas margin to 20.

    Qt Code:
    1. plotLayout()->setCanvasMargin(20);
    To copy to clipboard, switch view to plain text mode 

    However the bar chart is centred now. I want no margin at the bottom portion of the bar chart.
    The bar chart now looks as shown below.

    barchart.jpg

    Note that the bar chart only becomes equal width when the margin of xBottom axis is set.

    Another workaround is to enable the x-axis, which I do not want to. This change give the outcome that I want but with the x-axis enabled.
    Qt Code:
    1. enableAxis(xAxis, true);
    2. plotLayout()->setCanvasMargin(20, QwtPlot::yLeft);
    3. plotLayout()->setCanvasMargin(20, QwtPlot::yRight);
    To copy to clipboard, switch view to plain text mode 

    So is there anyway where I can disable the x-axis and has no margin at the bottom portion?
    Last edited by nanthiran_2005; 23rd June 2014 at 06:08.

  5. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlotMultiBarChart not equal width

    Not sure what you are trying to do: why setting a margin of 20 pixels if you don't want to have one ?

    The margins to the left/right get auto-adjusted in the replot cycle ( see QwtPlotItem::Margins ) as the bar width depends ( see QwtPlotAbstractBarChart::sampleWidth ). So all you need to do is to add this line once in the beginning to remove the default bottom margin:

    Qt Code:
    1. plotLayout()->setCanvasMargin(0);
    To copy to clipboard, switch view to plain text mode 

    Then better don't interfere by setting the margins manually.

    Uwe

  6. #5
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QwtPlotMultiBarChart not equal width

    Just by removing the updateCanvasMargin() line and setting the canvas margin to 0, does not solve the problem.
    The width of the bar remain the same as I have shown in the first picture.

  7. #6
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlotMultiBarChart not equal width

    Looks like a bug to me - related to the disabled x axis.

    A possible workaround is to use the following code:

    Qt Code:
    1. #if 0
    2. enableAxis(xAxis, false);
    3. #else
    4. // workaround
    5. QwtScaleDraw *scaleDraw = axisScaleDraw( QwtPlot::xBottom );
    6. scaleDraw->enableComponent(QwtScaleDraw::Backbone, false);
    7. xAxis_scaleDraw->enableComponent(QwtScaleDraw::Ticks, false);
    8. xAxis_scaleDraw->enableComponent(QwtScaleDraw::Labels, false);
    9. #endif
    To copy to clipboard, switch view to plain text mode 

    HTH,
    Uwe

  8. The following user says thank you to Uwe for this useful post:

    nanthiran_2005 (27th June 2014)

  9. #7
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QwtPlotMultiBarChart not equal width

    Hi Uwe,

    Thanks for the help. The line which disable the label helps. However, I still have to manually set the margin of yLeft and yRight. Otherwise, the bar chart will not be equal of width.
    Attached below is the portion of code which help me to get the bar chart I want.

    Qt Code:
    1. QwtPlot::Axis yAxis = QwtPlot::yLeft,
    2. xAxis = QwtPlot::xBottom;
    3. setAxisScale(yAxis, 0, 100, 20);
    4. setAxisAutoScale(xAxis);
    5. enableAxis(yAxis, true);
    6.  
    7. QwtScaleDraw *yAxis_scaleDraw = axisScaleDraw(yAxis);
    8. yAxis_scaleDraw->enableComponent(QwtScaleDraw::Backbone, false);
    9. yAxis_scaleDraw->enableComponent(QwtScaleDraw::Ticks, false);
    10.  
    11. QwtScaleDraw *xAxis_scaleDraw = axisScaleDraw(xAxis);
    12. xAxis_scaleDraw->enableComponent(QwtScaleDraw::Backbone, false);
    13. xAxis_scaleDraw->enableComponent(QwtScaleDraw::Ticks, false);
    14. xAxis_scaleDraw->enableComponent(QwtScaleDraw::Labels, false);
    15.  
    16. plotLayout()->setCanvasMargin(0);
    17. plotLayout()->setCanvasMargin(20, QwtPlot::yLeft);
    18. plotLayout()->setCanvasMargin(20, QwtPlot::yRight);
    To copy to clipboard, switch view to plain text mode 
    Last edited by nanthiran_2005; 27th June 2014 at 12:52.

Similar Threads

  1. QwtPlotMultiBarChart with raw data
    By Momergil in forum Qwt
    Replies: 2
    Last Post: 14th May 2014, 17:27
  2. Stacked Curves - QwtPlotMultiBarChart
    By GG2013 in forum Qwt
    Replies: 2
    Last Post: 13th August 2013, 02:58
  3. Greater or equal than and smaller or equal than not working..
    By ruben.rodrigues in forum General Programming
    Replies: 3
    Last Post: 27th April 2011, 08:09
  4. What does Updatedata function in MFC equal with Qt?
    By nthung in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2010, 11:58
  5. Equal scales
    By sukram in forum Qwt
    Replies: 1
    Last Post: 22nd July 2008, 07:44

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.