如何使用MPAndroidChart在LineChart的轴x中添加日期? 现在我将Date(以毫秒为单位)转换为字符串,但是当我尝试将其放入图表时,x值不会显示。
LineDataSet dataSet = new LineDataSet(entries, getString(R.string.NDVI_trend));
LineData lineData = new LineData(dataSet);
chart.setData(lineData);
Description description = new Description();
description.setText("");
chart.setDescription(description);
chart.getAxisRight().setEnabled(false);
chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
chart.getXAxis().setDrawGridLines(false);
chart.getLegend().setEnabled(false);
String[] dateString = new String[entries.size() +1];
for (int i = 0; i < entries.size(); i++){
dateString[i] = getDate((long)entries.get(i).getX(),"dd MMM yyyy");
}
IAxisValueFormatter axisValueFormatter = new IndexAxisValueFormatter(dateString);
chart.getXAxis().setValueFormatter(axisValueFormatter);
chart.invalidate();
getData方法:
public static String getDate(long milliSeconds, String dateFormat)
{
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}