2

I am using the GraphView Library and I cant get the graph to redraw itself when I update the series (or replace the series with a new one). It only seems to redraw itself when I touch the screen. Here is my update function.

private void updateGraph(Animal a)
{
    Toast.makeText(getApplicationContext(), "Updating Graph", Toast.LENGTH_SHORT).show();
    GraphViewData[] newData = new GraphViewData[2];
    newData[0] = new GraphViewData(0,a.getAvg());
    newData[1] = new GraphViewData(1,0.2);

    GraphViewSeries newSeries = new GraphViewSeries(a.getName(),dStyle,newData);

    graph.removeSeries(exampleSeries);
    graph.addSeries(newSeries);
    //update horizontal value
    //graph.setHorizontalLabels(new String[]{a.getName()});
}
Zachary
  • 183
  • 2
  • 15

2 Answers2

1

I figured it out...

//test random y value
    double rnd = generator.nextDouble();

    // round is an incremental value (1,2,3...)
    GraphViewData newData = new GraphViewData(round,rnd);

    exampleSeries.appendData(newData, false);
    graph.redrawAll();
Zachary
  • 183
  • 2
  • 15
0

Use onDataChanged to let the graph redraw and recalculate the viewport.

graph.onDataChanged(true, true);

Be careful with the two parameters keepLabelsSize and keepViewport . As the Javadoc says:

  • @param keepLabelsSize
    true if you don't want to recalculate the size of the labels. It is recommended to use "true" because this will improve performance and prevent a flickering.
  • @param keepViewport
    true if you don't want that the viewport will be recalculated. It is recommended to use "true" for performance.

Anyway, keep in mind that onDataChanged gets implicitly called when a new series is added or removed and when data is appended via appendData or resetData. Thus in most cases a manual call of onDataChanged in not necessary.

clic
  • 365
  • 1
  • 13