Search code examples
android-graphview

Set width of bars in bar graph


Is there a way to control the width of the bars in a bar graph? Currently I am seeing the first and last bars default to half the width of the remaining (middle) bars.


Solution

  • Note, this is a Duplicate: Anyway appears there have been quite a few changes between v3.1.3 and 4.0.0, as I also hit this one. The BarGraph specific logic in v3 of the library that shunted the viewport up and down half an xInterval, so you got a full width bar for the first and last entry on screen appears to have been dropped, but it's trivial to code around e.g.

    double xInterval=1.0;
    graphView.getViewport().setXAxisBoundsManual(true);
    if (dataSeries instanceof BarGraphSeries ) {
        // Shunt the viewport, per v3.1.3 to show the full width of the first and last bars. 
        graphView.getViewport().setMinX(dataSeries.getLowestValueX() - (xInterval/2.0));
        graphView.getViewport().setMaxX(dataSeries.getHighestValueX() + (xInterval/2.0));
    } else {
        graphView.getViewport().setMinX(dataSeries.getLowestValueX() );
        graphView.getViewport().setMaxX(dataSeries.getHighestValueX());
    }