1

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?

Caio Mar
  • 2,344
  • 5
  • 31
  • 37
  • did you find any solution? – Radesh Dec 23 '18 at 09:30
  • No, I didn't find a solution. I tried using callback functions to filter the labels on the x-axis but I couldn't get the range that was being displayed at a given moment. If I remember correctly, the callback sends all x values and I couldn't determine which ones where displaying on the axis as labels. – Caio Mar Dec 23 '18 at 09:38
  • Maybe if the pinch-for-zoom feature is disabled and you set the data range according to your data, the dates/time could behave better. I will try when I get a chance. If I find a solution I will post it here. Or maybe a different library is the answer. Couple days ago I did something similar using Chart.JS on a web application. – Caio Mar Dec 23 '18 at 09:56
  • Thanks, there are too many good library in js but i didn't find in android. if i found the solution i will posted too. i think IAxisValueFormatter best chance to do this job , I'm working on it – Radesh Dec 23 '18 at 10:00
  • Did you find solution for this? – Dan Mar 17 '21 at 09:24

0 Answers0