X轴标签转换为时间(mm:ss),使用MPAndroidChart。

huangapple go评论69阅读模式
英文:

XAxis labels to time (mm:ss) with MPAndroidChart

问题

我想在我的MPAndroidChart LineChart上将X轴标签显示为分钟和秒数的时间格式 mm:ss。我尝试创建了一个ValueFormatter,然而,IAxisValueFormattergetFormattedValue 似乎已经被弃用。

我每秒有40帧,所以每隔40帧,标签应该增加 00:01,并且在 00:59 时变为 01:00。

你能帮我实现这个吗?

xAxis.setValueFormatter(new MyFormatter());

public class MyFormatter implements IAxisValueFormatter {
    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        int seconds = (int) value / 40;
        int minutes = seconds / 60;
        seconds %= 60;
        
        String formattedTime = String.format("%02d:%02d", minutes, seconds);
        return formattedTime;
    }
}

X轴标签转换为时间(mm:ss),使用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?

X轴标签转换为时间(mm:ss),使用MPAndroidChart。

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

}

答案1

得分: 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 "";
            }
        }
    }
英文:

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

huangapple
  • 本文由 发表于 2020年9月14日 15:10:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63879620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定