user13207116
user13207116

Reputation:

How to remove decimals from above of the bar in android

Hi in the below code want to change left yaxis values want to display multiplies of 2 and want to display interger number on above of the bar .

Can any one help me

From the below graph how to change float to integer number from above of the graph

private void setData_chart(int count, float range) {

        float barWidth = 0.9f;
        ArrayList<BarEntry> values = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            float val = Math.round((float) (Math.random() * range));
            values.add(new BarEntry(i, val,
                    getResources().getDrawable(R.drawable.ic_launcher)));
        }

        BarDataSet set1;
        if (chart1.getData() != null &&
                chart1.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) chart1.getData().getDataSetByIndex(0);
            set1.setValues(values);
            chart1.getData().notifyDataChanged();
            chart1.notifyDataSetChanged();
        }
        else {
            set1 = new BarDataSet(values, "");

            int endColor1 = ContextCompat.getColor(getContext(), R.color.linecolor);


            set1.setColors(endColor1);
            set1.setDrawIcons(false);

            dataSets = new ArrayList<>();
            dataSets.add(set1);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(12f);
            data.setValueTypeface(tfLight);
            data.setBarWidth(barWidth);
            chart1.setData(data);
        }
    }

Upvotes: 0

Views: 844

Answers (1)

Mehul Kabaria
Mehul Kabaria

Reputation: 6622

For that, you have to create separate ValueFormatter and bind it to your chart data.

public class MyDecimalValueFormatter implements ValueFormatter {

        private DecimalFormat mFormat;

        public MyDecimalValueFormatter() {
            mFormat = new DecimalFormat("#");
        }

        @Override
        public String getFormattedValue(float value) {
            return mFormat.format(value);
        }
    }

and set it like below

MyDecimalValueFormatter formatter = new MyDecimalValueFormatter();

    BarData data = new BarData(dataSets);
    data.setValueTextSize(12f);
    data.setValueTypeface(tfLight);
    data.setBarWidth(barWidth);
    data.setValueFormatter(formatter)
    chart1.setData(data);

Upvotes: 2

Related Questions