OLD 3D Tutorial 07 - Plotting Realtime Data

Go back to the code Creating a Chart and Add a Data Series.

Before we add realtime updates to the chart it is necessary to use the UpdateSuspender object to create the chart instead of the approach we used before, which was just to run a series of steps in initExample(). UpdateSuspender is disposable class which allows nested suspend/resume operations on an ISuspendable target.

Then change this:

Copy Code
     surface3d.setXAxis(xAxis);
     surface3d.setYAxis(yAxis);
     surface3d.setZAxis(zAxis);
     surface3d.getRenderableSeries().add(rs);
     surface3d.getChartModifiers().add(sciChart3DBuilder.newModifierGroupWithDefaultModifiers().build()); 

... to this code shown below. Build and run the project to make sure that it still works.

Copy Code
UpdateSuspender.using(surface3d, new Runnable() {
            @Override
            public void run() {
                surface3d.setCamera(camera);
                surface3d.setXAxis(xAxis);
                surface3d.setYAxis(yAxis);
                surface3d.setZAxis(zAxis);
                surface3d.getRenderableSeries().add(rs);
                surface3d.getChartModifiers().add(sciChart3DBuilder.newModifierGroupWithDefaultModifiers().build());
            }
        });

Now we add the code below to update the chart in real time. As you can see updating the chart is as simple as changing the data and running that in a thread.

First add ScheduledExecutorService and ScheduledFuture to the top:

Copy Code
public class CreateScatter3DChartFragment extends ExampleBaseFragment {
    @BindView(R.id.chart3d)
    SciChartSurface3D surface3d;
    private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    private ScheduledFuture<?> schedule;

Now put this at the bottom:

Copy Code
    @Override
    protected void initExample() {
        // ...
       
        schedule = scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
            private final DoubleValues buffer = new DoubleValues();
            @Override
            public void run() {
                UpdateSuspender.using(surface3d, new Runnable() {
                @Override
                    public void run() {
                        dataSeries.clear();
                        double randomDouble = Math.random();
                        randomDouble = randomDouble * 5 + 1;
                        int randomInt = (int) randomDouble;
                        for (int i = 0; i < 5; i++) {
                            final double x = i*randomInt;
                            final double y = i*randomInt;
                            final double z = i*randomInt;
                            dataSeries.append(x, y, z);
                        }
                    }
                });
            }
        }, 0, 33, TimeUnit.MILLISECONDS);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (schedule != null)
            schedule.cancel(true);
    }

The complete code for the modified CreateScatter3DChartFragment is here.

The resulting chart is the same except values plotted on the screen change in real time.

 

See Also

Miscellaneous APIs