[英]How can I use custom data in LineChart MPAndroidChart?
How can I put a Date in the axis x in LineChart with MPAndroidChart ? 如何使用MPAndroidChart在LineChart中的x轴上放置日期? Now I'm convert the Date (in milliseconds) to string, but when i try to put is in the chart the x values don't display.
现在,我将日期(以毫秒为单位)转换为字符串,但是当我尝试在图表中放置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();
The getData method: 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());
}
If you know how to convert your float
x-values into a String
representation of a Date
you can use a IAxisValueFormatter
: 如果您知道如何将
float
x值转换为Date
的String
表示形式,则可以使用IAxisValueFormatter
:
mChart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
private SimpleDateFormat mFormat = new SimpleDateFormat("dd MMM HH:mm");
@Override
public String getFormattedValue(float value, AxisBase axis) {
long millis = (long) value;
return mFormat.format(new Date(millis));
}
});
chart.getXAxis().setValueFormatter(axisValueFormatter);
chart.setGranularity(1.0f);
chart.setGranularityEnabled(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.