Android drawing graph using graph view

时间:2016-04-07 10:47:40

标签: android graph

I have a graph that is not a real time graph, i want to add the x-axis and y-axis from the data that i have.

GraphView line_graph = (GraphView) findViewById(R.id.graph3);

    LineGraphSeries<DataPoint> line_series =
            new LineGraphSeries<DataPoint>(new DataPoint[] {
                    new DataPoint(0, 0),
                    new DataPoint(1, 5),
                    new DataPoint(2, 3),
                    new DataPoint(3, 2),
                    new DataPoint(4, 6)
            });
    line_graph.addSeries(line_series);

i was just testing the graph with the above points. And now i have an array received from database using JSON, i want to use them as the x-axis data and y-axis data. Just like array[oddNumber] as x-axis data, array[evenNumber] as y-axis data. Is there any way to do that?

1 个答案:

答案 0 :(得分:0)

获取您的JSON数据并将其转换为整数数组,如xAxis和yAxis。

现在这样做,

Integer[] xAxis = new Integer[]{0, 1, 2, 3, 4};
    Integer[] yAxis = new Integer[]{0, 5, 3, 2, 6};

    GraphView line_graph = (GraphView) findViewById(R.id.graph3);

    DataPoint[] dataPoints = new DataPoint[xAxis.length];
    for (int i = 0; i < xAxis.length; i++)
    {
        dataPoints[i] = new DataPoint(xAxis[i],yAxis[i]);
    }

    LineGraphSeries<DataPoint> line_series =
            new LineGraphSeries<DataPoint>(dataPoints);

    line_graph.addSeries(line_series);

注意:xAxis和yAxis数组的长度必须相同。