Search code examples
androiddatetimechartsandroid-graphview

Using dates with the Graphview library


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?


Solution

  • GraphView is a great library to use, i find it the easiest as well. The first step in doing this would be to add a String Variable in the GraphViewData Class within GraphView.java. Like So:

    static public class GraphViewData {
        public final double valueX;
        public final double valueY;
        public final String valueDate;
    
        public GraphViewData(double valueX, double valueY,String valueDate) {
            super();
            this.valueX = valueX;
            this.valueY = valueY;
            this.valueDate = valueDate;
        }
    }
    

    When you create your GraphViewData object when creating a GraphView Graph, you will need to add the date data in string form (along with the X and Y).

    Lets say you have 80 data points in your graph (index 0 - 79). There is a method within GraphView that is responsible for generating and returning the horizontal labels, i believe its called generateHorLabels. Instead of just returning the X Value (0-79), Use the X value to get the String from the GraphData object.

    In the code you have now, it should have the following in a for loop

    labels[i] = formatLabel(min + ((max-min)*i/numLabels), true);
    

    instead of the above, you could do something like this.

    Double temp =  Double.valueOf(formatLabel(min + ((max-min)*i/numLabels), true));
    int rounded =(int)Math.round(temp); 
    labels[i] = values[rounded].valueDate;
    

    Hope this helped!