I'm using the MP Android Chart library to plot data, on the x-axis I have Unix timestamps. Even thought the data itself is rounded up to the nearest day or hour (depending on the data range), my x-axis is all broken down to minutes. It shows values like '22.07 19:37'. Too bad MpAndroidChart doesn't take care of pretty dates.
What I would like the x-axis to do:
- at 1st of month in case of year span;
- at start of day in case of month or week span;
- on any full hour (##:00) (not perse all) in case of a day span;
- on any 5 minute point on an hour span.
(These requirements were copied from this question never solved.)
This is what I have so far:
onCreate (or whatever):
LineDataSet dataSet = new LineDataSet(entries, "label x"); // add entries to dataset
LineData lineData = new LineData(dataSet);
chart.setData(lineData);
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new DateAxisFormatter(chart));
chart.invalidate(); // refresh
DateAxisFormatter class:
public class DateAxisFormatter implements IAxisValueFormatter {
private Chart chart;
public DateAxisFormatter( Chart chart ) {
this.chart = chart;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
String result = "";
Date date = new Date((long) value);
SimpleDateFormat prettyFormat = new SimpleDateFormat("dd.MM H:mm");
prettyFormat.setTimeZone(TimeZone.getDefault());
result = prettyFormat.format(date);
return result;
}
}
How do I go about making pretty dates with this?