Android的GraphView 4.x的 - 条形图不恰当 [英] Android GraphView 4.x - Bar Graph Not Fitting

查看:290
本文介绍了Android的GraphView 4.x的 - 条形图不恰当的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Andr​​oid应用程序中使用GraphView的4.x的版本。我有7个数据点与第一个和最后杆的柱状图被切断

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.

下面是我的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

推荐答案

在那里出现过v3.1.3和4.0.0之间有不少的变化,反正也打这一个。在分流视了图书馆的V3和向下的光柱特定的逻辑的xInterval,所以你得到了一个全宽栏在屏幕上的第一个和最后条目显示一半已被丢弃。反正它的琐碎左右例如,code

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

这篇关于Android的GraphView 4.x的 - 条形图不恰当的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆