IndexAxisValueFormatter无法按预期工作 [英] IndexAxisValueFormatter not working as expected

查看:186
本文介绍了IndexAxisValueFormatter无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MPAndroidChart 创建条形图.我的配置:

I'm using the MPAndroidChart to create a bar chart. My configurations:

<string-array name="months_initials">
       <item>J</item>
       <item>F</item>
       <item>M</item>
       <item>A</item>
       <item>M</item>
       <item>J</item>
       <item>J</item>
       <item>A</item>
       <item>S</item>
       <item>O</item>
       <item>N</item>
       <item>D</item>
   </string-array>

...

String[] months = getResources().getStringArray(R.array.months_initials);
chart.getXAxis().setCenterAxisLabels(true);
chart.getXAxis().setLabelCount(months.length, true);
chart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(months) {
    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        Timber.i("index = %s", value);
        return super.getFormattedValue(value, axis);
    }
});

指数= 0.5
指数= 1.5909091
指数= 2.6818182
指数= 3.7727275
指数= 4.8636365
指数= 5.9545455
指数= 7.0454545
索引= 8.136364
索引= 9.227273
指数= 10.318182
索引= 11.409091

index = 0.5
index = 1.5909091
index = 2.6818182
index = 3.7727275
index = 4.8636365
index = 5.9545455
index = 7.0454545
index = 8.136364
index = 9.227273
index = 10.318182
index = 11.409091

这是结果:

And this is the result:

现在,如果我将其更改为:return super.getFormattedValue(value-0.5f, axis);

Now, if I change it to: return super.getFormattedValue(value-0.5f, axis);

结果是:

同样,如果我添加另一个更改:chart.getXAxis().setLabelCount(Integer.MAX_VALUE, true);

Again, if I add another change: chart.getXAxis().setLabelCount(Integer.MAX_VALUE, true);

指数= 0.5
指数= 1.0
指数= 1.5
指数= 2.0
指数= 2.5
指数= 3.0
指数= 3.5
指数= 4.0
指数= 4.5
指数= 5.0
指数= 5.5
指数= 6.0
指数= 6.5
指数= 7.0
指数= 7.5
指数= 8.0
指数= 8.5
指数= 9.0
指数= 9.5
索引= 10.0
指数= 10.5
索引= 11.0
指数= 11.5
索引= 12.0
指数= 12.5

index = 0.5
index = 1.0
index = 1.5
index = 2.0
index = 2.5
index = 3.0
index = 3.5
index = 4.0
index = 4.5
index = 5.0
index = 5.5
index = 6.0
index = 6.5
index = 7.0
index = 7.5
index = 8.0
index = 8.5
index = 9.0
index = 9.5
index = 10.0
index = 10.5
index = 11.0
index = 11.5
index = 12.0
index = 12.5

结果是:

有点锤击时间",但是对我来说有用,不幸的是标签没有正确居中. 那么,这里发生了什么,我想念什么?如何获得最终结果?

A bit "hammer time" but it would work for me, unfortunately the labels are not correctly centered. So, what's happening here, what am I missing? How can I achieve my final result?

感谢您的时间.

ps:也打开了问题.

编辑完整的设置代码:

    String[] months = getResources().getStringArray(R.array.months_initials);
    BarChart chart = binding.barChart;

    chart.setTouchEnabled(false);
    chart.getDescription().setEnabled(false);
    chart.getLegend().setEnabled(false);
    chart.getAxisLeft().setEnabled(false);
    chart.getAxisRight().setEnabled(false);
    chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
    chart.getXAxis().setDrawAxisLine(false);
    chart.getXAxis().setDrawGridLines(false);
    chart.getXAxis().setCenterAxisLabels(true);
    chart.getXAxis().setLabelCount(Integer.MAX_VALUE, true);
    chart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(months) {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            Timber.i("index = %s", value);
            return super.getFormattedValue(value - 0.5f, axis);
        }
    });
    chart.getXAxis().setTextColor(ContextCompat.getColor(this, R.color.grey));
    chart.getXAxis().setTextSize(12);

    BarDataSet barData = new BarDataSet(data, "data");
    barData.setColor(ContextCompat.getColor(this, R.color.chart_bar));
    barData.setDrawValues(false);

    ArrayList<IBarDataSet> dataSets = new ArrayList<>();
    dataSets.add(barData);
    binding.barChart.setData(new BarData(dataSets));
    binding.barChart.animateY(1000, Easing.EasingOption.Linear);

推荐答案

我无法在您的代码中准确指出问题所在,但可能与图表的粒度有关. 但是,这是一个粗略的工作示例.希望这会有所帮助!

I can't exactly pin point the issue in your code but it is probably something do with the granularity of the chart. But here's a rough working example. Hope this helps!

        String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun"};

        BarChart mChart = (BarChart) findViewById(R.id.barChart);
        mChart.setDrawBarShadow(false);
        mChart.setDrawValueAboveBar(false);
        mChart.getDescription().setEnabled(false);
        mChart.setDrawGridBackground(false);

        XAxis xaxis = mChart.getXAxis();
        xaxis.setDrawGridLines(false);
        xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xaxis.setGranularity(1f);
        xaxis.setDrawLabels(true);
        xaxis.setDrawAxisLine(false);
        xaxis.setValueFormatter(new IndexAxisValueFormatter(months));

        YAxis yAxisLeft = mChart.getAxisLeft();
        yAxisLeft.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        yAxisLeft.setDrawGridLines(false);
        yAxisLeft.setDrawAxisLine(false);
        yAxisLeft.setEnabled(false);

        mChart.getAxisRight().setEnabled(false);

        Legend legend = mChart.getLegend();
        legend.setEnabled(false);

        ArrayList<BarEntry> valueSet1 = new ArrayList<BarEntry>();

        for (int i = 0; i < 6; ++i) {
            BarEntry entry = new BarEntry(i, (i+1)*10);
            valueSet1.add(entry);
        }

        List<IBarDataSet> dataSets = new ArrayList<>();
        BarDataSet barDataSet = new BarDataSet(valueSet1, " ");
        barDataSet.setColor(Color.CYAN);
        barDataSet.setDrawValues(false);
        dataSets.add(barDataSet);

        BarData data = new BarData(dataSets);
        mChart.setData(data);
        mChart.invalidate();

结果

这篇关于IndexAxisValueFormatter无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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