Force BarChart Y axis labels to be integers?

后端 未结 8 748
滥情空心
滥情空心 2021-02-05 11:47

I\'ve created a BarChart using MPAndroidChart and I\'m entering the data dynamically. This means that I need my Y axis to also be determined dynamically. All of my data is repre

8条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-05 12:22

    This is how I resolved this issue. It seems like the y axis is always drawn 1 over the max value(if its not force it). So set the label count 2 over the max value(include zero and one over your max) then all you need to do is remove the decimal with the axis formatter. This works if all your y values are integers, not sure how it would work with decimal values.

        barChart.getAxisLeft().setLabelCount(maxYvalue + 2, true);
        barChart.getAxisLeft().setAxisMinValue(0f);
        barChart.getAxisLeft().setAxisMaxValue(maxYvalue + 1);
        YAxisValueFormatter customYaxisFormatter = new YAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, YAxis yAxis) {
                return String.valueOf((int)value);
            }
        };
        barChart.getAxisLeft().setValueFormatter(customYaxisFormatter);
    

提交回复
热议问题