MPAndroidChart:XAxis值与数据值不对应 [英] MPAndroidChart: XAxis values not corresponding to Data values

查看:790
本文介绍了MPAndroidChart:XAxis值与数据值不对应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用MPAndroidChart时遇到问题:我在条形图中显示了一些输入数据,但是xaxis标签未显示在我想要的位置上:我想在每个图表值中立即向其显示一个酒吧".

I've a problem using MPAndroidChart: I have some entry data to display in a Bar Chart, but the xaxis labels aren't displaying on the position I want: I want to show them one per chart value, immediatly under the "bar".

这就是现在正在发生的事情:

This is what is happening right now:

这是我的代码:

    ArrayList<BarEntry> entries1 = my entries...;
    final ArrayList<String> stringList1 = my strings...;

    XAxis xAxis1 = chartCost.getXAxis();
    xAxis1.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis1.setDrawGridLines(false);
    xAxis1.setCenterAxisLabels(true);
    /*
    xAxis1.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return stringList1.get((int)value);
        }
    });
    */

    BarDataSet set = new BarDataSet(entries1, "myData");
    BarData data = new BarData(set);
    chartCost.setData(data);
    chartCost.setFitBars(true);
    chartCost.invalidate();

entries1和stringList1已正确填充.这是一个示例:

entries1 and stringList1 are being correctly populated. Here's an example:

- x: 0, y: 22,90
- x: 1, y: 21,53
- x: 2, y: 18,49

如果我对IAxisValueFormatter进行分解,则浮点值并不对应于条目的x值,但是,正如您所看到的,所有十进制值都可以!

If I decomment the IAxisValueFormatter, the float value is not corresponding at the x value of my entries, but, as you can see, there are all decimal values!

我想获得此标签(将标签放在条形下方):

I want to obtain this (place labels under bars):

但是我该怎么做呢? 谢谢!

But how can I do that? Thanks!

推荐答案

在代码中包含以下几行

xAxis1.setCenterAxisLabels(false);
xAxis1.setGranularity(1f);

setCenterAxisLabels,默认值为false.因此这不是强制性的.如果将其设置为true,则将其设置为false.否则,您的小节将位于连续的x值之间

setCenterAxisLabels, default value is false. So it is not mandatory. If you have set it true, make it false.Else your bar will lie between the successive x values

这篇关于MPAndroidChart:XAxis值与数据值不对应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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