Using dates with the Graphview library

Luciano picture Luciano · May 16, 2012 · Viewed 13.9k times · Source

I'm using GraphView library (see: https://github.com/jjoe64/GraphView or http://www.jjoe64.com/p/graphview-library.html)

But I would like to use Date/Time for the X-as. Does anyone knows a easy way to accomplish this or can anyone push me in the right direction?

Answer

appsthatmatter picture appsthatmatter · Sep 17, 2012

This is the correct way to do this

You just have to use the unix timestamp (seconds from 1.01.1970) as x value.

Then you can set a custom label formatter and convert the unix timestamp to a String:

final java.text.DateFormat dateTimeFormatter = DateFormat.getTimeFormat(mActivity);

LineGraphView graphView = new LineGraphView(mActivity, entry.getValue()) {
    @Override
    protected String formatLabel(double value, boolean isValueX) {
        if (isValueX) {
            // transform number to time
            return dateTimeFormatter.format(new Date((long) value*1000));
        } else {
            return super.formatLabel(value, isValueX);
        }
    }
};