HeisenBerg HeisenBerg - 2 months ago 28
C# Question

How to use MPAndroidChart's ValueFormatter in Xamarin.Android

I'm using the NuGet package MpAndroidChart..

In java,

public class MyValueFormatter implements ValueFormatter {

private DecimalFormat mFormat;

public MyValueFormatter() {
mFormat = new DecimalFormat("###,###,##0.0"); // use one decimal
}

@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
// write your logic here
return mFormat.format(value) + " $"; // e.g. append a dollar-sign
}
}


I tried to implement ValueFormatter, but the package does not contain an interface called
ValueFormatter


How do I implement this in C#?

Edit:

Thank you for your answer, But how do I use this, I tried

QuestionFormatter formatter = new QuestionFormatter();
chart.AxisLeft.ValueFormatter = (IYAxisValueFormatter) formatter;


But I'm getting Invalid Cast Exception

Answer

IValueFormatter is in the MikePhil.Charting.Formatter namespace

Using:

using MikePhil.Charting.Formatter;

Example:

public class CustomFormatter : IValueFormatter
{
    public IntPtr Handle
    {
        get
        {
            throw new NotImplementedException();
        }
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public string GetFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler)
    {
        throw new NotImplementedException();
    }
}

Note: Of course you will need to implement those methods with your own code ;-)

Update:

public class CustomYFormatter : IYAxisValueFormatter
{
    public IntPtr Handle
    {
        get
        {
            throw new NotImplementedException();
        }
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public string GetFormattedValue(float value, YAxis yAxis)
    {
        throw new NotImplementedException();
    }
}
Comments