简体   繁体   中英

Android GraphView 4.x - Bar Graph Not Fitting

I am using the 4.x version of GraphView in my Android app. I have a bar graph with 7 data points and the first and last bars are being cut off.

Here is my code

private static final String[] WEEK_DAYS = {"Sun", "Mon", "Tue", "Wed",  "Thu", "Fri", "Sat"};

private void showChartData() {

    ChartData[] data = new ChartData[7];
    for (int x = 0; x < 7; x++) {
        data[x] = new ChartData();
    }
    data[0].setEnrollmentCount(2);
    data[1].setEnrollmentCount(4);
    data[2].setEnrollmentCount(6);
    data[3].setEnrollmentCount(8);
    data[4].setEnrollmentCount(10);
    data[5].setEnrollmentCount(12);
    data[6].setEnrollmentCount(14);

    chartHolder.removeAllViews();

    DataPoint[] graphViewData = new DataPoint[7];

    for (int i = 0; i < data.length; i++) {
        graphViewData[i] = new DataPoint(i,
                data[i].getEnrollmentCount());
    }

    BarGraphSeries<DataPoint> series = new BarGraphSeries<DataPoint>(graphViewData);
    series.setColor(getResources().getColor(R.color.custom_red));

    GraphView graphView = new GraphView(this);
    graphView.addSeries(series); // data
    graphView.setBackgroundColor(Color.WHITE);
    graphView.setTitle("Sales This Week");

    StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graphView);
    staticLabelsFormatter.setHorizontalLabels(WEEK_DAYS);
    graphView.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);          
    graphView.getGridLabelRenderer().setGridStyle(GridStyle.NONE);
    chartHolder.addView(graphView);
}

Here is a screen shot of the bar chart 这是条形图的屏幕截图

Appears there have been quite a few changes between v3.1.3 and 4.0.0, anyway 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. Anyway its trivial to code around eg

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM