5

I am using QwtPlotBarChart to draw a histogram. I have set the chart up so that there is zero spacing between bars, and my xBottom axis has the real range of the data.

Samples are set on the chart using a QVector of QPointF, with the x values corresponding to the midpoints of the bins.

A QwtPlotPicker hovering over the bars shows that they start and end at the actual start and ends of the bins.

However, I am having trouble getting the ticks where the labels are drawn to show up in the correct place along the x axis. I am using a custom scaledraw, similar to the distrowatch example. I have an additional couple of parameters, namely the minimum value of the data range, and the bin size. My label index code looks like this:

virtual QwtText label( double value ) const
{
    QwtText lbl;

    //const int index = qRound( value );
    const int index = (int)(value-_min_val)/_bin_size;
    if ( index >= 0 && index < d_labels.size() )
    {
        lbl = d_labels[ index ];
    }

    return lbl;
}

This seems to be ok; my labels show the min and max values of each bin and a nearly in the right place. I have tried to set the tick positions as follows:

QwtScaleDiv div = _plot->axisScaleDiv(QwtPlot::xBottom);
div.setTicks(QwtScaleDiv::MajorTick, x_data.toList());
_plot->setAxisScaleDiv(QwtPlot::xBottom, div);

here x_data is the vector of bin midpoints used to plot the data. The axis range is set using:

double half_bin = bin_size / 2.0;
_plot->setAxisScale(QwtPlot::xBottom, x_data.first() - half_bin, x_data.last() + half_bin, bin_size);

Any ideas what I'm missing? Depending on the number of bins, the ticks (and subsequently the labels) will occasionally line up with the mid point of the bins (or at least most of them), but as I change the bin count, the ticks will become more or less offset from the center of the bin.

KelvinS
  • 2,870
  • 8
  • 34
  • 67
mike
  • 1,192
  • 9
  • 32

0 Answers0