How to create a live chart in TypeScript Charts

Use setInterval() to add data at a fixed interval, remove older points, and refresh the Chart. Keep the interval identifier so the timer can be stopped when the Chart is no longer needed.

Initialize the Chart

Create an initial data array and bind it to the series through dataSource, xName, and yName. Disable animation when updates occur frequently to avoid repeated transition effects.

import { Chart } from '@syncfusion/ej2-charts';

// initialize Chart component
let chart: Chart = new Chart(
    //Initializing Chart Series
    series:[
               type: 'Line',
    ]
);
// render initialized Chart
chart.appendTo('#container');

Update the data at an interval

On each timer tick:

  1. Generate or receive the next data point.
  2. Append the point to the data array.
  3. Remove the oldest point when the array exceeds the maximum length.
  4. Reassign the series data source and call refresh().
setInterval(
    function() {
        if (document.getElementById('container') === null) {
            clearInterval(intervalId);
        } else {
            if (Math.random() > .5) {
                value += Math.random() * 2.0;
            }
            series1.push({ x: i, y: value });
            i++;
            series1.shift(); // Used to remove the first element
            chart.series[0].dataSource = series1;
            chart.refresh();
        }
    }, setTimeoutValue);

Complete example

import { Chart, LineSeries, getElement } from '@syncfusion/ej2-charts';
Chart.Inject(LineSeries);
let series1: Object[] = [];
let value: number = 10;
let i: number;
let intervalId: number;

for (i = 0; i < 50; i++) { // Data update
    if (Math.random() > .5) {
        value += Math.random() * 2.0;
    }
    series1[i] = { x: i, y: value };
}

let chart: Chart = new Chart({
        series: [
            {
                type: 'Line',
                dataSource: series1,
                xName: 'x',
                yName: 'y', animation: { enable: false }
            },
        ],
    height: '350px'
    },'#element');

    let setTimeoutValue: number = 100;
    intervalId = setInterval(
        (): void => {
if (getElement('container') === null) {
        clearInterval(intervalId);
    } else {
    if (Math.random() > .5) {
        value += Math.random() * 2.0;
    }
    series1.push({ x: i, y: value });
    i++;
    series1.shift(); // Used to remove the first element
    chart.series[0].dataSource = series1;
    chart.refresh();
    }
    }, setTimeoutValue);
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 Animation</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    <div id='loader'>Loading....</div>
    <div class="col-sm-8">
            <div class="row">
                <div class="col-sm-4">  
                  <div id='container'>
                    <div id='element'  style="width:350px; height:350px;float:left">
                    </div>
                    <label id="lbl"></label>
                  </div>
                </div>
                <div class="col-sm-4" style="width:200px; height:350px;float: right">
                  <div id='Grid'>
                  </div>
                </div>
            </div>
    </div>
</body> 
</html>

Update frequency and performance

  • Choose an interval appropriate for the source data and user interface; very short intervals can increase CPU usage.
  • Keep a fixed maximum number of displayed points to prevent unbounded memory growth.
  • Disable animation for frequent updates.
  • Stop the timer when the Chart is hidden, removed, or no longer required.
  • For high-frequency streams, batch multiple incoming values into one visual update.
  • Use requestAnimationFrame() for frame-driven visual updates and setInterval() for time-based sampling.
  • For remote data, handle connection failures and avoid starting duplicate timers or subscriptions.

Troubleshooting

  • If the Chart does not update, verify that the timer is running and that the series data source is reassigned before refresh().
  • If memory usage increases continuously, enforce a maximum point count and stop unused timers.
  • If multiple updates occur per interval, verify that startUpdates() cannot create duplicate timers.
  • If the Chart updates after it is removed, clear the interval during component or page cleanup.
  • If values render incorrectly, ensure that the Y values are numbers rather than formatted strings.

See also