Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JJOE64 Android graphview set LineGraphSeries Dynamically

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()),
});
}
like image 246
mesopotamia Avatar asked Feb 08 '23 06:02

mesopotamia


1 Answers

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? :)
like image 191
Slobodan Antonijević Avatar answered Feb 09 '23 19:02

Slobodan Antonijević