user3099225
user3099225

Reputation: 413

Retain old values, update new values and show animation in mpandroidchart

I have a mpandroidchart in which i am supplying new values from some source, currently as a demo i am providing data from a random number generator and running it using a runnable. It is working fine. New value gets plotted after a time duration which i have set. But i want it to retain old values and plot new values and animate like right to left. (example - ecg machines data).

final Handler handler = new Handler();
        final Random random = new Random();
        final Integer delay = 2000;
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //Do something after 20 seconds
                ChartValue =  10+random.nextInt(90);
                // calling the graph generator function
                generateGraph(ChartValue);
                handler.postDelayed(this, delay);
            }
        }, delay);

mpAndroidChart

 public void generateGraph(Integer val) {
    final Integer delay = 2000;
    ArrayList<Entry> yValues = new ArrayList<>();
    yValues.add(new Entry(0, val));
    LineDataSet set = new LineDataSet(yValues, "Data");
    set.setFillAlpha(110);
    set.setColor(R.color.red);
    set.setLineWidth(3f);
    set.setValueTextSize(10f);
    ArrayList<ILineDataSet> dataSets = new ArrayList<>();
    dataSets.add(set);
    LineData data = new LineData(dataSets);
    mChart.setData(data);
    mChart.notifyDataSetChanged();
    mChart.invalidate();

}

Upvotes: 1

Views: 1040

Answers (1)

Muhammad Saad Rafique
Muhammad Saad Rafique

Reputation: 3189

Before following line:

 mChart.notifyDataSetChanged();

You need to add following line of code:

mChart.animateX(1000);

where 1000 is time in millisecods.

Upvotes: 2

Related Questions