4

Hi I am trying to draw a chart with this library but its entries static like:

LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {

                        new DataPoint(0, 1),
                        new DataPoint(1, 5),
                        new DataPoint(2, 3),
                        new DataPoint(3, 2),
                        new DataPoint(4, 6)
                });
                graph.addSeries(series);

How can i fix this that accepts ex. a list view elements that created on runtime ? Basicly I want something like this:

for (int i = 0; i < list.size(); i++) {
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {

                        new DataPoint(i, list.getElement()),
});
}
mesopotamia
  • 393
  • 1
  • 5
  • 19

1 Answers1

8

Try something like this:

DataPoint[] dataPoints = new DataPoint[list.size()]; // declare an array of DataPoint objects with the same size as your list
for (int i = 0; i < list.size(); i++) {
    // add new DataPoint object to the array for each of your list entries
    dataPoints[i] = new DataPoint(i, list.getElement()); // not sure but I think the second argument should be of type double
}

LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(dataPoints); // This one should be obvious right? :)
Slobodan Antonijević
  • 2,533
  • 2
  • 17
  • 27
  • it works greate thanks so much. One more question it seems x axis is based on i. How can I replace it with time? I have a list that completed with some value and value-time. I want y axis is value (it is ok.) and x axis is time how can I arrange this? – mesopotamia Apr 13 '16 at 09:20
  • StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(yourGraphView); staticLabelsFormatter.setHorizontalLabels(new String[]{ time1, time2, time3, etc}); yourGraphView.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter); You can play around with this for example or check here [link](http://www.android-graphview.org/documentation/category/labels-and-label-formatter) – Slobodan Antonijević Apr 13 '16 at 09:29
  • if you want to change the data take a look at the methods appendData and resetData on the Series object – appsthatmatter Jul 22 '16 at 08:58
  • it's been 3 months since you answered, but thank you! Been working on this for 5 hours before your answer! – AndroidDev21921 Nov 30 '16 at 02:49
  • What is that list you called it like list.size()? – Bay Sep 22 '19 at 18:37
  • @Bay Any list of inputs you declare or read from wherever, maybe a list of points (double values). It could also be an array of values, anything that can be iterated through. – Slobodan Antonijević Sep 25 '19 at 08:32