Mpandroidchart: How to show int values in chart instead of float?

Created on 17 Oct 2014  路  4Comments  路  Source: PhilJay/MPAndroidChart

I want to show integer values on Pie Chart, but float values shown instead. How to do it?

Most helpful comment

now mChart.setValueFormatter not available..

/[/ usage on whole data object
lineData.setValueFormatter(new MyValueFormatter());

// usage on individual dataset object
lineDataSet.setValueFormatter(new MyValueFormatter());](https://github.com/PhilJay/MPAndroidChart/wiki/The-ValueFormatter-interface)

All 4 comments

You need to create your own Formatter class for the values by implementing the ValueFormatter interface provided by the library.

Then you can return whatever value you want to be displayed.

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

In code:

mChart.setValueFormatter(new YourFormatter());

Also have a look here #77 and search known issues before creating new ones.

now mChart.setValueFormatter not available..

/[/ usage on whole data object
lineData.setValueFormatter(new MyValueFormatter());

// usage on individual dataset object
lineDataSet.setValueFormatter(new MyValueFormatter());](https://github.com/PhilJay/MPAndroidChart/wiki/The-ValueFormatter-interface)

why I can't override getForrmatedValue() method?

Here is the full piechart code I implemented in my app using this library, check it if you find useful...
public void piechart1(int max){
len = max;
pieChart.getDescription().setEnabled(false);
pieChart.setCenterText("Message Format");
pieChart.setTransparentCircleColor(Color.WHITE);
pieChart.setTransparentCircleAlpha(110);
pieChart.setEntryLabelTextSize(0);
pieChart.setUsePercentValues(false);
Legend l = pieChart.getLegend();
l.setEnabled(false);
pieChart.animateY(2500, Easing.EaseInOutQuad);
List entries = new ArrayList<>();

    for(int i = 1;i<=len;i++) {
        entries.add(new PieEntry(10*i,"text"));
    }
    PieDataSet set = new PieDataSet(entries, "Message Types");
    Log.e("data", String.valueOf(entries));
    ArrayList<Integer> colors = new ArrayList<>();
    colors.add(Color.YELLOW);
    colors.add(Color.CYAN);
    colors.add(Color.GRAY);
    colors.add(Color.parseColor("#D661B1EF"));
    colors.add(Color.MAGENTA);
    colors.add(Color.parseColor("#5ef28b"));
    colors.add(Color.LTGRAY);
    colors.add(Color.MAGENTA);
    colors.add(Color.DKGRAY);
    colors.add(Color.BLUE);

    set.setColors(colors);
    set.setSliceSpace(2.5f);
    set.setValueTextSize(5.0f);
    PieData data = new PieData(set);
    ValueFormatter vf = new ValueFormatter() { //value format here, here is the overridden method
        @Override
        public String getFormattedValue(float value) {
            return ""+(int)value;
        }
    };
    data.setValueFormatter(vf);
    pieChart.setData(data);
    pieChart.highlightValues(null);
    pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
        @Override
        public void onValueSelected(Entry e, Highlight h) {
            if (e == null)
                return;
            int x = (int) h.getX()+1;
            set.setValueTextSize(15.0f);
            float n = h.getX();
            Log.e("VAL SELECTED", "Value: " + e.getY() + ", index: " + h.getX() + ", DataSet index: " + h.getDataSetIndex());
            legendtext.setVisibility(View.VISIBLE);
            legendtext.setTextColor(Color.parseColor("#D661B1EF"));
            legendtext.setText("Format-"+x);
        }

        @Override
        public void onNothingSelected() {
            legendtext.setVisibility(View.INVISIBLE);
            set.setValueTextSize(5.0f);
        }
    });
    pieChart.invalidate(); // refresh
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

chathudan picture chathudan  路  3Comments

ChenZeFengHi picture ChenZeFengHi  路  3Comments

mrZizik picture mrZizik  路  3Comments

AndroidJiang picture AndroidJiang  路  3Comments

Giammaofwar picture Giammaofwar  路  3Comments