Как исправить значение с плавающей точкой для int в YAxis в BarChart? - PullRequest
0 голосов
/ 16 марта 2020
I have created one ValueFormatter class.
public class MyValueFormatter extends ValueFormatter
{

    private final DecimalFormat mFormat;
    private String suffix;

    public MyValueFormatter(String suffix) {
        mFormat = new DecimalFormat("###,###,###,##0.0");
        this.suffix = suffix;
    }

    @Override
    public String getFormattedValue(float value) {
         return "" + ((int) value);
    }

    @Override
    public String getAxisLabel(float value, AxisBase axis) {
        if (axis instanceof XAxis) {
            return mFormat.format(Math.round(value));
        } else if (value > 0) {
            return mFormat.format(Math.round(value)) + suffix;
        } else {
            return mFormat.format(Math.round(value));
        }
    }
}

Создан один ArrayList.

    ArrayList<BarEntry> NoOfEmp = new ArrayList<BarEntry>();
    NoOfEmp.add(new BarEntry( 0f,jan));

наконец вызовите ValueFormatter:

        ValueFormatter custom = new MyValueFormatter("");

        YAxis leftAxis = chart.getAxisLeft();
//        leftAxis.setTypeface(tfLight);
//        leftAxis.setLabelCount(8, false);
        leftAxis.setValueFormatter(custom);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(15f);
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setDrawGridLines(false);
//        rightAxis.setTypeface(tfLight);
//        rightAxis.setLabelCount(8, false);
        rightAxis.setValueFormatter(custom);
        rightAxis.setSpaceTop(15f);
        rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

        chart.setData(data);

Я использовал метод getFormatted для setValueFormatter (custom). но он не работает

вывод:

0.0
0.0
1.0
1.0
2.0
2.0

нужно только целое число.

Я учусь реализовывать диаграмму в android. Мне нужно преобразовать значение с плавающей точкой в ​​целое число.

...