2015-02-24 84 views
1

我使用GraphView的4.x的版本在我的Android應用程序。我有一個包含7個數據點的條形圖,第一個和最後一個小節正在被切斷。的Android 4.x的GraphView - 條形圖無法適應

這裏是我的代碼

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

回答

2

似乎已經出現了v3.1.3和4.0.0之間有不少變化的屏幕截圖,反正也打這個。在分流視了圖書館的V3和向下的光柱特定的邏輯的xInterval,讓你有一個完整的寬度巴屏幕上的第一個和最後一個條目有一半顯示已被刪除。無論如何,它在代碼周圍的代碼很簡單。

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