Set X-axis range for QStackedBarSeries

908 Views Asked by At

I'm using following code to create a vertical stacked chart:

QStackedBarSeries* series = new QStackedBarSeries();
m_errorSet[0] = new QBarSet("");
m_errorSet[1] = new QBarSet("");
m_errorSet[0]->setColor(QColor("red"));
m_errorSet[1]->setColor(QColor(0x00FF00));
for (i = 0; i < MAX_ERROR_COLS; i++)
{
   *m_errorSet[0] << 1;
   *m_errorSet[1] << 4;
}
series->setBarWidth(1);

series->append(m_errorSet[0]);
series->append(m_errorSet[1]);

QChart* chart = new QChart();
chart->addSeries(series);
chart->setAnimationOptions(QChart::NoAnimation);
chart->legend()->setVisible(false);

QValueAxis* axisX = new QValueAxis();
//    axisX->setMin(-4.0);
//    axisX->setMax(0.0);
chart->addAxis(axisX, Qt::AlignBottom);
series->attachAxis(axisX);

This works well, it creates a set of 80 bars spread over the full available size in X. For some reason the X-axis is marked with a range of -0.5 .. 79.5.

What I want to have is a X-axis range of -4.0 .. 0.0. But as soon as I enable

axisX->setMin(-4.0);
axisX->setMax(0.0);

in my code above, I get only one bar - the remaining ones are out of the valid range.

So my question: how can I tell the bars to always be located within the X-axis-range? Or in other words: where can I set the X-axis-values for my bars?

Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

It's a problem of the order: First attach the axis, then the series so that QStackedBarSeries knows how to locate the bar sets:

QValueAxis* axisX = new QValueAxis();
axisX->setRange(-4.0,0.0);

QChart* chart = new QChart();
chart->addAxis(axisX, Qt::AlignBottom);
series->attachAxis(axisX);

chart->addSeries(series);
chart->setAnimationOptions(QChart::NoAnimation);
chart->legend()->setVisible(false);