Sunday, 15 September 2013

Using GraphView Library for funtions to display multiple graphs

Using GraphView Library for funtions to display multiple graphs

i'm currently developing an android app for reading out multiple
sensorvalues via bluetooth and display them in a graph. When i stumbled
upon jjoe64's GraphViewLibrary, i knew this would fit my purposes
perfectly. But now I'm kind of stuck. Basically, i wrote a little function
that would generate and display the values of three sensors in 3 different
graphs one under the other. This works just fine when the acitivity is
started first, all three graphs a nicely rendered and displayed. But when
i want to update the graphs with different values using the
resetData()-method to render the new values in each graph, only the last
of the three graphs is updated. Obviously, because it's the last graph
generated using this rather simple function. My question is: Is there any
other elegant way to use a function like mine for generating and updating
all three graphs one after the other? I already tried to set the GraphView
variable back to null and different combinations of removing and adding
the view. Passing the function a individual GraphView-variable like
graphView1, graphView2... does also not work.
Here is the function:
private GraphView graphView;
private GraphViewSeries graphViewSerie;
private Boolean graphExisting = false;
...
public void makeGraphs (float[] valueArray, String heading, int graphId) {
String graphNumber = "graph"+graphId;
int resId = getResources().getIdentifier(graphNumber,"id",
getPackageName());
LinearLayout layout = (LinearLayout) findViewById(resId);
int numElements = valueArray.length;
GraphViewData[] data = new GraphViewData[numElements];
for (int c = 0; c<numElements; c++) {
data[c] = new GraphViewData(c+1, valueArray[c]);
Log.i(tag, "GraphView Graph"+graphId+": ["+(c+1)+"]
["+valueArray[c]+"].");
}
if (!graphExisting) {
// init temperature series data
graphView = new LineGraphView(
this // context
, heading // heading
);
graphViewSerie = new GraphViewSeries(data);
graphView.addSeries(graphViewSerie);
((LineGraphView) graphView).setDrawBackground(true);
graphView.getGraphViewStyle().setNumHorizontalLabels(numElements);
graphView.getGraphViewStyle().setNumVerticalLabels(5);
graphView.getGraphViewStyle().setTextSize(10);
layout.addView(graphView);
}
else {
//graphViewSerie = new GraphViewSeries(data);
//graphViewSerie.resetData(data);
graphViewSerie.resetData(new GraphViewData[] {
new GraphViewData(1, 1.2f)
, new GraphViewData(2, 1.4f)
, new GraphViewData(2.5, 1.5f) // another frequency
, new GraphViewData(3, 1.7f)
, new GraphViewData(4, 1.3f)
, new GraphViewData(5, 1.0f)
});
}
And this is the function-call depending on an previously generated array
(which is being monitored to be filled with the right values):
makeGraphs(graphData[0], "TempHistory", 1);
makeGraphs(graphData[1], "AirHistory", 2);
makeGraphs(graphData[2], "SensHistory", 3);
graphExisting = true;
Any help and / or any feedback in general is greatly appreciated! Lots of
thanks in advance!

No comments:

Post a Comment