SciChart Android 3D Charts API > Xamarin 3D Tutorials > Xamarin 3D Tutorial 05 - Plotting Realtime Data
Xamarin 3D Tutorial 05 - Plotting Realtime Data

In this tutorial we are going to show how to update data displayed by 3D chart in realtime. Assuming you have completed previous tutorial, we will now make some changes to update the data dynamically.

Updating Data Values

In our 3D DataSeries, we have some static data so far. Lets animate it now.

We are going to add a Timer and schedule updating the data on timer tick. To update data in a 3D DataSeries, we have to call one of the available UpdateXXX methods on that DataSeries, but first of all we need to adjust some previously created code and save DataSeries instance and data used in private fields:

Example Title
Copy Code
private readonly XyzDataSeries3D<double, double, double> _dataSeries3D = new XyzDataSeries3D<double, double, double>();
private readonly List<double> _xData = new List<double>();
private readonly List<double> _yData = new List<double>();
private readonly List<double> _zData = new List<double>();
protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    // ...
    var dataManager = DataManager.Instance;
    for (int i = 0; i < 1000; i++)
    {
        _xData.Add(dataManager.GetGaussianRandomNumber(5, 1.5));
        _yData.Add(dataManager.GetGaussianRandomNumber(5, 1.5));
        _zData.Add(dataManager.GetGaussianRandomNumber(5, 1.5));
    }
    _dataSeries3D.Append(_xData, _yData, _zData);
    // ...
   
    // start timer here
    Start();
}

After this we can add Timer which updates _dataSeries3D.

Example Title
Copy Code
private const int TimerInterval = 10;
private volatile bool _isRunning = false;
private readonly object _syncRoot = new object();
private Timer _timer;
private void Start()
{
    if (_isRunning) return;
    _isRunning = true;
    _timer = new Timer(TimerInterval);
    _timer.Elapsed += OnTick;
    _timer.AutoReset = true;
    _timer.Start();
}
private void OnTick(object sender, ElapsedEventArgs e)
{
    lock (_syncRoot)
    {
        if (!_isRunning) return;
        var random = new Random();
        for (int i = 0, size = _dataSeries3D.Count; i < size; i++)
        {
            _xData[i] += random.NextDouble() - 0.5;
            _yData[i] += random.NextDouble() - 0.5;
            _zData[i] += random.NextDouble() - 0.5;
        }
        _dataSeries3D.UpdateRangeXyzAt(0, _xData, _yData, _zData);
    }
}

This should result next output of chart

ReatimeUpdate3D