簡體   English   中英

Android GraphView 4.x-條形圖不適合

[英]Android GraphView 4.x - Bar Graph Not Fitting

我在Android應用程序中使用GraphView的4.x版本。 我有一個帶有7個數據點的條形圖,第一個和最后一個條形被切斷。

這是我的代碼

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

這是條形圖的屏幕截圖 這是條形圖的屏幕截圖

似乎在v3.1.3和4.0.0之間有很多更改,無論如何也達到了這一要求。 庫v3中特定於BarGraph的邏輯在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());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM