英文:
XAxis labels to time (mm:ss) with MPAndroidChart
问题
我想在我的MPAndroidChart LineChart上将X轴标签显示为分钟和秒数的时间格式 mm:ss
。我尝试创建了一个ValueFormatter,然而,IAxisValueFormatter
和 getFormattedValue
似乎已经被弃用。
我每秒有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;
}
}
英文:
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?
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 "";
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论