How To

Create a Live Chart

You can update a chart with live data by using the set interval.

To update live data in a chart, follow the given steps:

Step 1:

Initialize the chart with series.

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');

Step 2:

Update the data to series, and refresh the chart at specified interval by using the set interval.

To refresh the chart, invoke the refresh method.

Source
Preview
index.html
<!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="http://npmci.syncfusion.com/packages/development/material.css" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    
    
<script src="//cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <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>
                  </div>
                </div>
                <div class="col-sm-4" style="width:200px; height:350px;float: right">
                  <div id="Grid">
                  </div>
                </div>
            </div>
    </div>
 
<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Prevent the data label when the data value is 0

To prevent the chart data label when the data value is 0, follow the given steps:

Step 1:

Get the point value and check whether the args.point.y value is zero or not by using the textRender event. If the value is zero, then set the args.cancel to true.

The output will appear as follows,

Source
Preview
index.html
<!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="http://npmci.syncfusion.com/packages/development/material.css" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    
    
<script src="//cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <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>
                  </div>
                </div>
                <div class="col-sm-4" style="width:200px; height:350px;float: right">
                  <div id="Grid">
                  </div>
                </div>
            </div>
    </div>
 
<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Format the tooltip value

Using tooltipRender event, you can able to format the datetime value instead of rendered value.

To format the datetime value,please follow the steps below

Step 1:

By using tooltipRender event we can able to get the current point x value. Using this value to format the tooltip by using formatDate method.

The output will appear as follows,

Source
Preview
index.html
<!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="http://npmci.syncfusion.com/packages/development/material.css" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    
    
<script src="//cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <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>
                  </div>
                </div>
                <div class="col-sm-4" style="width:200px; height:350px;float: right">
                  <div id="Grid">
                  </div>
                </div>
            </div>
    </div>
 
<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Add or remove a series from the chart dynamically

You can add or remove the chart series dynamically by using the addSeries or removeSeries method.

To add or remove the series dynamically, follow the given steps:

Step 1:

To add a new series to chart dynamically, pass the series value to the addSeries method.

To remove the new series from chart dynamically, pass the series index to the removeSeries method.

Source
Preview
index.html
<!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="index.css" rel="stylesheet">
    
    
<script src="//cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="element" style="height: 300px"></div>
        <button id="add" type="button" width="15%" style="float: left">Add</button>
        <button id="remove" type="button" width="15%" style="float: right">Remove</button>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

To add dotted line using annotation

By using annotation, you can add dotted lines in the chart.

To add dotted lines in the chart, follow the given steps:

Step 1:

Initialize the custom elements by using the annotation property.

By setting coordinateUnits value as point in annotation object you can placed dotted lines in the chart based on point x and y values.

Source
Preview
index.js
index.html
var chart = new ej.charts.Chart({
        primaryXAxis: {
            valueType: 'Category',
        },
        primaryYAxis: {
            title: 'Medals'
        },
        annotations: [{
                content: '<div id ="test" style="border-top:3px dashed grey;border-top-width: 2px; width: 10000px"></div>',
                x: 'France',
                y: 50,
                coordinateUnits: 'Point',
                Region: 'Chart'
            }],
        series: [{
                dataSource: datasource.columnData,
                xName: 'country', yName: 'gold',
                type: 'Line'
            }],
    }, '#element');
<!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="index.css" rel="stylesheet">
    
    
<script src="//cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="element"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Display the data filtered from grid in pie chart

You can visualize the filtered data that returned by grid in pie chart.

To visualize the data in pie chart, follow the given steps:

Step 1:

Initialize the grid with datasource.

Step 2:

By using the grid’s actionComplete event and getCurrentViewRecords method, you can get the current page records. By setting allowFiltering value as true, you can filter the data. By using the grid’s databound event, you can update the current page filtered records into the chart’s datasource and display the grid filtered data in chart.

{% tab template= “chart/grid-visual” %}

import { Grid, Filter, Page, Selection, FilterType, ActionEventArgs  } from '@syncfusion/ej2-grids';
import { orderData } from './datasource.ts';
import { AccumulationChart, ColumnSeries, DateTime, Category, AccumulationDataLabel } from '@syncfusion/ej2-charts';
AccumulationChart.Inject(ColumnSeries, DateTime, Category, AccumulationDataLabel);

Grid.Inject(Filter, Page, Selection);
   let chart: AccumulationChart;
    let filtertype: { [key: string]: Object }[] = [
        { id: 'Menu', type: 'Menu' },
        { id: 'CheckBox', type: 'CheckBox' },
        { id: 'Excel', type: 'Excel' }
    ];

    let grid: Grid = new Grid(
        {
            dataSource: orderData,
            allowPaging: true,
            allowFiltering: true,
            filterSettings: { type: 'Menu' },
            columns: [
                { field: 'OrderID', headerText: 'Order ID', width: 120, textAlign: 'Right' },
                { field: 'CustomerName', headerText: 'Customer Name', width: 150 },
                {
                    field: 'OrderDate', headerText: 'Order Date', width: 130,
                    format: { type: 'dateTime', format: 'M/d/y hh:mm a' }, textAlign: 'Right'
                },
                { field: 'Freight', width: 120, format: 'C2', textAlign: 'Right' }
            ],
            pageSettings: { pageCount: 5 },
            dataBound: () => {
        chart = new AccumulationChart({
           series: [
        {
            dataSource: grid.getCurrentViewRecords(),
            type:'Pie',
            xName: 'CustomerName',
            yName: 'Freight',  dataLabel: { visible: true }
        }
    ]
          });
          chart.appendTo('#Chart');
        },
              actionComplete: (args: ActionEventArgs) => {
              if (args.requestType === 'filtering') {
                 chart.series[0].dataSource =  grid.getCurrentViewRecords();
                   chart.refresh();
                }
              }
        });
    grid.appendTo('#Grid');

{%endtab%}