XAxis标签与MPAndroidChart的时间(mm:ss)

时间:2020-09-14 07:10:57

标签: java mpandroidchart

我想在我的MPAndroidChart LineChart中以分钟和秒mm:ss的形式显示XAxis标签。我尝试创建ValueFormatter,但是似乎IAxisValueFormattergetFormattedValue已过时。

我每秒有40帧,因此,每40帧,标签应从00:01开始增加,并在00:59至01:00时更改。

您能帮我实现这一目标吗?

enter image description here

到目前为止,我对valueformatter的代码是:

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

}

1 个答案:

答案 0 :(得分:1)

尝试一下:

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 "";
        }
    }
}