PDA

View Full Version : QwtPlotMultiBarChart not equal width



nanthiran_2005
20th June 2014, 05:38
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.

10434



BarChart::BarChart(QWidget *parent)
: QwtPlot(parent)
{
setAutoFillBackground(true);
setPalette(Qt::white);
canvas()->setPalette(QColor ("Black")); //background color

barChart = new QwtPlotMultiBarChart;
barChart->setLayoutPolicy(QwtPlotMultiBarChart::AutoAdjustSa mples);
barChart->setSpacing(20);
barChart->setMargin(20);
barChart->setStyle(QwtPlotMultiBarChart::Stacked);
barChart->attach(this);


for(int i = 0; i < 2; ++i)
{
QwtColumnSymbol *symbol = new QwtColumnSymbol(QwtColumnSymbol::Box);
QPalette barColor = (i == 0) ? Qt::green : Qt::red ;
symbol->setPalette(barColor);
symbol->setFrameStyle(QwtColumnSymbol::Raised);
symbol->setLineWidth(1);
barChart->setSymbol(i, symbol);
}

QVector<QVector<double> > newBarChartData;
for(int i = 0; i < 5; i++)
{
num = rand() % 100;
QVector<double> values;
values.push_back(num);
values.push_back(100-num);

newBarChartData.push_back(values);
}
barChart->setSamples(newData);

QwtPlot::Axis yAxis = QwtPlot::yLeft,
xAxis = QwtPlot::xBottom;
setAxisScale(yAxis, 0, 100, 20);
setAxisAutoScale(xAxis);
enableAxis(yAxis, true);
enableAxis(xAxis, false);

QwtScaleDraw *yAxis_scaleDraw = axisScaleDraw(yAxis);
yAxis_scaleDraw->enableComponent(QwtScaleDraw::Backbone, false);
yAxis_scaleDraw->enableComponent(QwtScaleDraw::Ticks, false);

QwtScaleDraw *xAxis_scaleDraw = axisScaleDraw(xAxis);
xAxis_scaleDraw->enableComponent(QwtScaleDraw::Backbone, false);
xAxis_scaleDraw->enableComponent(QwtScaleDraw::Ticks, false);

plotLayout()->setCanvasMargin(0);
updateCanvasMargins();
replot();
}

Uwe
20th June 2014, 10:41
plotLayout()->setCanvasMargin(0);
updateCanvasMargins();


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

nanthiran_2005
23rd June 2014, 05:43
Hi Uwe,

That actually did solve my problem. Thanks alot.

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


plotLayout()->setCanvasMargin(20);

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.

10438

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.


enableAxis(xAxis, true);
plotLayout()->setCanvasMargin(20, QwtPlot::yLeft);
plotLayout()->setCanvasMargin(20, QwtPlot::yRight);


So is there anyway where I can disable the x-axis and has no margin at the bottom portion?

Uwe
23rd June 2014, 06:53
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:


plotLayout()->setCanvasMargin(0);

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

Uwe

nanthiran_2005
23rd June 2014, 07:59
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.

Uwe
25th June 2014, 18:13
Looks like a bug to me - related to the disabled x axis.

A possible workaround is to use the following code:


#if 0
enableAxis(xAxis, false);
#else
// workaround
QwtScaleDraw *scaleDraw = axisScaleDraw( QwtPlot::xBottom );
scaleDraw->enableComponent(QwtScaleDraw::Backbone, false);
xAxis_scaleDraw->enableComponent(QwtScaleDraw::Ticks, false);
xAxis_scaleDraw->enableComponent(QwtScaleDraw::Labels, false);
#endif


HTH,
Uwe

nanthiran_2005
27th June 2014, 12:39
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.



QwtPlot::Axis yAxis = QwtPlot::yLeft,
xAxis = QwtPlot::xBottom;
setAxisScale(yAxis, 0, 100, 20);
setAxisAutoScale(xAxis);
enableAxis(yAxis, true);

QwtScaleDraw *yAxis_scaleDraw = axisScaleDraw(yAxis);
yAxis_scaleDraw->enableComponent(QwtScaleDraw::Backbone, false);
yAxis_scaleDraw->enableComponent(QwtScaleDraw::Ticks, false);

QwtScaleDraw *xAxis_scaleDraw = axisScaleDraw(xAxis);
xAxis_scaleDraw->enableComponent(QwtScaleDraw::Backbone, false);
xAxis_scaleDraw->enableComponent(QwtScaleDraw::Ticks, false);
xAxis_scaleDraw->enableComponent(QwtScaleDraw::Labels, false);

plotLayout()->setCanvasMargin(0);
plotLayout()->setCanvasMargin(20, QwtPlot::yLeft);
plotLayout()->setCanvasMargin(20, QwtPlot::yRight);