Frederik Petri
Frederik Petri

Reputation: 481

XAxis labels to time (mm:ss) with MPAndroidChart

I would like to display the XAxis labels as time in minutes and seconds mm:ss on my MPAndroidChart LineChart. I tried to create a ValueFormatter, however, it seems to that IAxisValueFormatter and getFormattedValue is deprecated.

I have 40 frames per second, so for every 40th frame the labels should increase with 00:01 and change when 00:59 to 01:00.

Can you help me achieve this?

enter image description here

My code so far for the valueformatter is:

xAxis.setValueFormatter(new MyFormatter());


public class MyFormatter implements IAxisValueFormatter {
@Override
public String getFormattedValue(float value, AxisBase axis) {

    int second = (int) value / 40
    return second + "s" //make it a string and return

}

Upvotes: 1

Views: 695

Answers (1)

Masilila
Masilila

Reputation: 524

Try this out:

import android.util.Log;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.formatter.ValueFormatter;

public class XAxisValueFormatter extends ValueFormatter {
    @Override
    public String getAxisLabel(float value, AxisBase axis) {
        Log.d("ADebugTag", "Value: " + Float.toString(value));
        int axisValue = (int) value/40;
        if (axisValue >= 0) {
            String sh = String.format("%02d:%02d", (axisValue / 3600 * 60 + ((axisValue % 3600) / 60)), (axisValue % 60));
            return sh;
        } else {
            return "";
        }
    }
}

Upvotes: 1

Related Questions