Search code examples
androidarraysandroid-graphview

GraphView always start on zero, and dynamically data


How can I make my GraphView always start on zero and not on the lowest number of my data ? For example if my data received is {10,44,1,15}, the bottom one will be the 1 and I wanted to be zero. How can I do that ?

And how can I make the GraphViewSeries dynamically ? For example I receive an array with data and I want depending the size of the array to make the GraphViewSeries, like if I receive an array with 3 elements the GraphViewSeries will be like this:

GraphViewSeries exampleSeries = new GraphViewSeries(
                new GraphViewData[] { 
                        new GraphViewData(1, total[0]),
                        new GraphViewData(2, total[1]),
                        new GraphViewData(3, total[2])});

If the array has 5 elements with would be like this:

GraphViewSeries exampleSeries = new GraphViewSeries(
                new GraphViewData[] { 
                        new GraphViewData(1, total[0]),
                        new GraphViewData(2, total[1]),
                        new GraphViewData(3, total[2]),
                        new GraphViewData(4, total[3]),
                        new GraphViewData(5, total[4]) });

How can I do this ?


Solution

  • You can set the min and max values like this:

    GraphView graphView = new LineGraphView(context, "Graph name")
    graphView.setManualYAxisBounds((double) max, (double) min);
    

    For dynamic series you can do this:

    GraphViewData[] data = new GraphViewData[total.length];
    for (int a = 0; a < total.length; a++) {
        data[a] = new GraphView.GraphViewData(a, total[a]);
    }
    

    then you add your data to a series like this:

    GraphViewSeriesStyle style = new GraphViewSeriesStyle(Color.rgb(150, 150, 150), 2);
    GraphViewSeries series = new GraphViewSeries(driverName.substring(0, 3).toUpperCase(), style, data);
    

    for the official documentation refer to here.