Вопрос

I'm currently developing an android app for reading out multiple sensor values 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 activity 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!


EDIT / UPDATE: Thanks to jjoe64's answer I was able to modify the function to work properly. I was clearly having a mistake in my thinking, since I thought I'd also be changing a GraphViewSeries-object I would handle my function as additional parameter (which I tried before). Of course this does not work. However, with this minor Improvements I managed to make this work using a Graphviewseries Array. To give people struggling with a similar problem an idea of what I had to change, here the quick-and-dirty draft of the solution.

I just changed

private GraphViewSeries graphViewSerie;

to

private GraphViewSeries graphViewSerie[] = new GraphViewSeries[3]; 

and access the right Series using the already given parameter graphId within the function (if-clause) like this:

int graphIndex = graphId - 1;
graphViewSerie[graphIndex] = new GraphViewSeries(data);

In the else-clause I'm updating the series likewise by calling

graphViewSerie[graphIndex].resetData(data);

So, once again many thanks for your support, jjoe64. I'm sorry I wasn't able to update the question earlier, but I did not find time for it.

Это было полезно?

Решение

of course it is not working correct, because you save always the latest graphseries-object in the member graphViewSerie.

First you have to store the 3 different graphviewseries (maybe via array or map) and then you have to access the correct graphviewseries-object in the else clause.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top