如何在 MPAndroidChart 中添加 x 轴作为日期时间标签?
问题描述:
我在我的项目中为温度报告实现了折线图(MPAndroidChart 库).在 X 轴上应绘制日期时间,并应绘制 Y 轴温度.
I implemented line chart (MPAndroidChart library) for temperature report in my project.In X axis datetime should be plotted and Y axis temperature should be plotted.
我刚刚在 X 轴标签中将日期时间添加为字符串,但它已折叠.所以请任何人指导我.
I just added datetime as string in X axis label but it's collapsed. So please anyone guide me.
答
请尝试以下操作.
设置X轴
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setValueFormatter(new MyXAxisValueFormatter());
xAxis.setLabelsToSkip(0);
创建一个新类 MyXAxisValueFormatter
实现 XAxisValueFormatter
Create a new class MyXAxisValueFormatter
implement XAxisValueFormatter
public class MyXAxisValueFormatter implements XAxisValueFormatter {
@Override
public String getXValue(String dateInMillisecons, int index, ViewPortHandler viewPortHandler) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM");
return sdf.format(new Date(Long.parseLong(dateInMillisecons)));
} catch (Exception e) {
return dateInMillisecons;
}
}
希望能帮到你