Drunken Daddy
Drunken Daddy

Reputation: 7991

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

Upvotes: 0

Views: 740

Answers (2)

cjmurph
cjmurph

Reputation: 869

In addition to @SushiHangover's answer, you have to inherit from Java.Lang.Object when you implement a java interface, then the handle property and dispose method are taken care of by the base class for you and you can just write your formatting method

public class MyValueFormatter : Java.Lang.Object, IValueFormatter
{
    public string GetFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler)
    {
        return value.ToString("F0");
    }
}

Upvotes: 0

SushiHangover
SushiHangover

Reputation: 74164

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();
    }
}

Upvotes: 1

Related Questions