0

I am using MPAndroidChart I need to display the line chart with real time. But problem is, it's displaying after the entire completed one. Actually it should plot like real time. What I tried is,

private void updateUI(final ArrayList<TimeModel> pJsonObject) {

for(int i=0;i<pJsonObject.size();i++){
            int speedRates = pJsonObject.get(i).getSpeedRate();
            String time = String.valueOf(pJsonObject.get(i).getTotalTime());
            xAxes.add(time);
            //yAxes.add(new Entry(10, 0));
            yAxes.add(new Entry(speedRates,i));
        }
        String[] xaxes = new String[xAxes.size()];
        for (int i = 0; i < xAxes.size(); i++) {
            xaxes[i] = xAxes.get(i).toString();
        }

        LineDataSet lineDataSet = new LineDataSet(yAxes, "Time");

        lineDataSet.setDrawCircles(true);
        lineDataSet.setColor(Color.GREEN);

        lineDataSets.add(lineDataSet);

        linechart.setData(new LineData(xaxes, lineDataSets));
        linechart.setVisibleXRangeMaximum(65f);
//        linechart.setTouchEnabled(true);
//        linechart.setDragEnabled(true);

        linechart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);

        linechart.getAxisLeft().setDrawGridLines(false);
        linechart.getXAxis().setDrawGridLines(false);


        YAxis yAxisRight = linechart.getAxisRight();
        yAxisRight.setEnabled(false);

        linechart.animateXY(3000, 3000);
 }

But this is plotting as completed one. I need to plot as moving real time in mpandroid chart.

James Z
  • 12,209
  • 10
  • 24
  • 44
Shiv
  • 129
  • 3
  • 12
  • please refer this link it might help you: https://android.jlelse.eu/build-a-realtime-graph-in-android-with-pusher-eb46b418427c – Mohsin mithawala May 29 '18 at 10:49
  • Please see my answer for real time graph : https://stackoverflow.com/a/50531511/3101777 – Misha Akopov May 29 '18 at 10:55
  • Thanks @MishaAkopov Let me know what are all the lines need to be added when comparing with my code? Will you please answer it by editing mine. so that let me accept and close it. – Shiv May 29 '18 at 11:03
  • @Shiv In your code you have all points at once, that's why graph is drawn instantly. You want to draw graph one point at a time with some intervals, for example 1 seconds ? – Misha Akopov May 29 '18 at 12:32
  • Yes you are right @MishaAkopov i need to display with 1 second interval. – Shiv May 29 '18 at 16:14
  • But i am clueless how to do. :( kindly help me please. – Shiv May 29 '18 at 16:14

1 Answers1

1

Here is code. It starts thread, then in loop you add entries to chart (500 points in this case), and after each adding, Thread sleeps for 1000 milliseconds (1 sec) and it is your interval:

    new Thread(new Runnable() {

        @Override
        public void run() {
            for(int i = 0; i < 500; i++) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        addEntry();
                    }
                });

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }).start();

For just animation with interval of 1 second for each point use this

mChart.animateX(1000 * pointCount);

for example, in your case:

mChart.animateX(1000 * xAxes.size());
Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
  • what i need to use in add entry? In simple, I need similar to this. https://i.stack.imgur.com/mzS9c.gif i tried using //linechart.animateXY(4000, 4000); linechart.animateY(4000, Easing.EasingOption.EaseInBack); but nothing works perfectly. :( – Shiv May 30 '18 at 04:14
  • See updated answer, 4000 milliseconds is too small number for animating many points, you should set 1000 per point, and will get real time experience for all points – Misha Akopov May 30 '18 at 06:20