Skip to content Skip to sidebar Skip to footer

Graphview Is Not Updated When New Data Is Added

I have a GraphView that is not updated when new data is added, it changes the Y value to accommodate for the new data, but it doesn't draw them, here is the code: public class Mai

Solution 1:

For changes to take effect, you need to call:

graphView.onDataChanged(false, false);

instead of graphView.redrawAll() in the View.OnClickListener implementation. You must play with the parameters to ensure the best performance, take a look at the javadoc in the source code:

/**
 * Call this to let the graph redraw and recalculate the viewport.
 *
 * This will be called when a new series was added or removed and when data was appended 
 * via {@link com.jjoe64.graphview.series.BaseSeries#appendData(com.jjoe64.graphview.series.DataPointInterface, boolean, int)}
 * or {@link com.jjoe64.graphview.series.BaseSeries#resetData(com.jjoe64.graphview.series.DataPointInterface[])}.
 *
 * @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.
 */publicvoidonDataChanged(boolean keepLabelsSize, boolean keepViewport) { /*...*/ }

No need for Thread/Runnable at all.

Solution 2:

Right now its chafing values but not changing UI, because you need to do such UI update operations in separate thread. You need to place all your code which is inside onClick in Thread/Runnable. Create Thread object inside onClick & place that code.

Refer below link :

http://karanbalkar.com/2014/05/display-graphs-using-graphview-in-android/

Solution 3:

Wow actually works now, took me quite a while to notice I need a Handler... Thanks, heres my code for other noobs like me:

refreshButton.setOnClickListener(newView.OnClickListener()
    {
        publicvoidonClick(View v)
        {
            graphHandler.post(
            graphRunnable = newRunnable()
            {
                publicvoidrun() 
                {
                    sensorSeries.appendData(newGraphViewData(xgraph, dataAcx), false, 20);
                    graphView.redrawAll();
                    myLabel.setText(""+dataAcx+" "+xgraph);
                    xgraph++;
                    dataAcx++;
                }
            }
            );
        }
    });

Post a Comment for "Graphview Is Not Updated When New Data Is Added"