Reputation: 695
So I am using android-graphview (https://github.com/jjoe64/GraphView).
I'm trying to figure out how to add DataPoints
to the series programmatically one by one as i fetch data from source.
Currently the doc says to do:
LineGraphSeries<DataPoint> series2 = new LineGraphSeries<>(new DataPoint[] {
new DataPoint(0, 4),
new DataPoint(1, 5),
new DataPoint(2, 2),
new DataPoint(3, 1),
new DataPoint(4, 10)
});
But i want to be able to add/create dynamically as my for loop gets those data points.
Any suggestions?
Upvotes: 0
Views: 2496
Reputation: 65
The simplest way is to append data to an existing series, that was added to the graph.
GraphView graph = (GraphView) findViewById(R.id.gv);
LineGraphSeries<DataPoint> lineGraphSeries = new LineGraphSeries<>();
graph.addSeries(lineGraphSeries);
DataPoint dataPoint = sth...
lineGraphSeries.appendData(dataPoint);
Upvotes: 1
Reputation: 5269
public class AddSeriesAtRuntime extends BaseExample {
private Activity mActivity;
@Override
public void onCreate(FullscreenActivity activity) {
mActivity = activity;
GraphView graph = (GraphView) activity.findViewById(R.id.graph);
initGraph(graph);
}
@Override
public void initGraph(final GraphView graph) {
Button btn = (Button) mActivity.findViewById(R.id.btnAddSeries);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
graph.addSeries(new LineGraphSeries(generateData()));
}
});
btn = (Button) mActivity.findViewById(R.id.btnClear);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
graph.removeAllSeries();
}
});
}
private DataPoint[] generateData() {
Random rand = new Random();
int count = 30;
DataPoint[] values = new DataPoint[count];
for (int i=0; i<count; i++) {
double x = i;
double f = rand.nextDouble()*0.15+0.3;
double y = Math.sin(i*f+2) + rand.nextDouble()*0.3;
DataPoint v = new DataPoint(x, y);
values[i] = v;
}
return values;
}
}
Upvotes: 0