MP android chart displaying float values ​​on x-axis of horizontal chart?

I use the MP chart to view reviews, everything works fine, except that the X axis values ​​are displayed as floating values ​​(for a number of a certain type of review).

This is the code I'm using:

     BarData data = new BarData(getXAxisValues(), getDataSet(properties));
            chart.setData(data);
            chart.getXAxis().setEnabled(false); // hides horizontal grid lines inside chart
            YAxis leftAxis = chart.getAxisLeft();
            chart.getAxisRight().setEnabled(false); // hides horizontal grid lines with below line
            leftAxis.setEnabled(false); // hides vertical grid lines  inside chart
            /*chart.animateXY(2000, 2000);*/ // for animating reviews display
            chart.invalidate();
            chart.setClickable(false);
            chart.setDescription("");    // Hide the description
            chart.getLegend().setEnabled(false);
            chart.setDoubleTapToZoomEnabled(false);
            chart.setPinchZoom(false);

            leftAxis.setDrawLabels(true);
private ArrayList<BarDataSet> getDataSet(Properties properties) {
        ArrayList<BarDataSet> dataSets = null;

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

        BarEntry v1e1 = new BarEntry(properties.getRating10().intValue(), 0);
        valueSet1.add(v1e1);
        BarEntry v1e2 = new BarEntry(properties.getRating20().intValue(), 1);
        valueSet1.add(v1e2);
        BarEntry v1e3 = new BarEntry(properties.getRating30().intValue(), 2);
        valueSet1.add(v1e3);
        BarEntry v1e4 = new BarEntry(properties.getRating40().intValue(), 3);
        valueSet1.add(v1e4);
        BarEntry v1e5 = new BarEntry(properties.getRating50().intValue(), 4);
        valueSet1.add(v1e5);

        BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Asset");
        barDataSet1.setColors(ColorTemplate.COLORFUL_COLORS);

        dataSets = new ArrayList<>();
        dataSets.add(barDataSet1);
        return dataSets;
    }

    private ArrayList<String> getXAxisValues() {
        ArrayList<String> xAxis = new ArrayList<>();
        xAxis.add("12.0");
        xAxis.add("4.0");
        xAxis.add("4");
        xAxis.add("4");
        xAxis.add("4");
        return xAxis;
    }

Here I set the data in a horizontal bar chart.

Picture

I want the red circled values ​​to be integers instead of float. Can anybody help me?

+4
source share
2 answers

I have a solution (thanks again to Philip), This is a solution

Create your own value formatting class that implements this ValueFormatter

This is my class:

public class MyValueFormatter implements ValueFormatter{
    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        return Math.round(value)+""; 
    }
}

Formatter BarData:

BarData data = new BarData(getXAxisValues(), getDataSet(properties));
data.setValueFormatter(new MyValueFormatter());

-

decimal notation

+17

ValueFormatter , . , , ,...

+5

Source: https://habr.com/ru/post/1609290/


All Articles