Shivam Bhalla
Shivam Bhalla

Reputation: 1919

MPAndroidGraph How to make the graph auto-scroll

I am using MPAndroidChart in my application and I am generating points in real-time and adding them to the graph. How do I make the view "follow" the graph and move as new values are added ? In the sense, that once the graph reaches the right end of the screen(x value is maximum), the graph should move forward and allow the user to swipe back to see the older part of the graph.

Hope my question is clear.

Thanks !

Upvotes: 3

Views: 2470

Answers (1)

Philipp Jahoda
Philipp Jahoda

Reputation: 51421

That's possible, please have a look at the realtime-chart-demo, it explains how to do it.

  • Set an empty LineData (or whatever data) object for the chart:

    chart.setData(new LineData())

  • Call this method everytime you add an entry:

    private void addEntry(Entry entry) {
    
        LineData data = mChart.getData();
    
        if (data != null) {
    
            // get the dataset where you want to add the entry
            LineDataSet set = data.getDataSetByIndex(0);
    
            if (set == null) {
                // create a new DataSet if there is none yet
                set = createSet();
                data.addDataSet(set);
            }
    
            // add a new x-value first
            data.addXValue("somestring");
            data.addEntry(entry, 0);
    
            // let the chart know it's data has changed
            mChart.notifyDataSetChanged();
    
            // limit the number of visible entries
            mChart.setVisibleXRange(6);
    
            // move to the latest entry
            mChart.moveViewToX(data.getXValCount()-7);
        }
    }
    

Upvotes: 4

Related Questions