Nisar Ahmad Nisar Ahmad - 3 months ago 29
Android Question

How to round percentage of barchart in MPAndroidChart Xamarin?

public class YourValueFormatter : IValueFormatter
{


public IntPtr Handle
{
get;
}
public void Dispose()
{
throw new NotImplementedException();
}



private DecimalFormat mFormat;

public YourValueFormatter() {
mFormat = new DecimalFormat("###,###,###"); // use no decimals
}

public String GetFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {

// This method never called.
return Math.Round(value)+"";
}


}


This is how I am setting formatter to data

data.SetValueFormatter(new YourValueFormatter());


also tried to set it on dataset like this

dataset.ValueFormatter=new YourValueFormatter();


Tried in many ways but nothing getting work for me.

I want to round bar value like this

2.00 TO 2% (Predefined percentage formatter formate like this 2.0% but I need 2%)

or

420.0 to 420

lib version :
2.2.3.2

Answer

You have to inherit from Java.Lang.Object, when you implement java interfaces.If you have a look at the build output, you'll find something like:

Type 'YourValueFormatter' implements IValueFormatter but does not inherit from Java.Lang.Object. It is not supported.

Correct

public class YourValueFormatter : Java.Lang.Object, IValueFormatter
{
    private DecimalFormat mFormat;

    public YourValueFormatter() {
        mFormat = new DecimalFormat("###,###,###"); // use no decimals
    }

    public String GetFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) 
    {
        // This method never called.
        return Math.Round(value)+""; 
    }
}

Your GetFormattedValue should now get called.