Grid data chart in EJ2 JavaScript Chart control

13 Dec 20235 minutes to read

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

To visualize the data in 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 using the grid’s databound event, you can update the current page records into the chart’s datasource and visualize the grid data in chart.

var chart = new ej.charts.Chart();
var data = new ej.data.DataManager(orderData).executeLocal(new ej.data.Query().take(100));
var grid = new ej.grids.Grid({

    dataSource: data,
    allowPaging: true,
    pageSettings: { pageSize: 10 },
    columns: [
        { field: 'OrderDate', headerText: 'Order Date', width: 130, format: 'yMd', textAlign: 'Right' },
        { field: 'Freight', width: 120, format: 'C2', textAlign: 'Right' }
    ],
    dataBound: function () {
        chart = new ej.charts.Chart({
            //Initializing Primary X Axis
            primaryXAxis: {
                valueType: 'DateTime',
                intervalType: 'Days'
            },
            series: [
                {
                    type: 'Line',
                    dataSource: grid.getCurrentViewRecords(),
                    xName: 'OrderDate', width: 2, marker: {
                        visible: true
                    },
                    yName: 'Freight', name: 'Germany'
                },
            ],

        }, '#Chart');
    },
    actionComplete: function (args) {
        if (args.requestType === 'paging') {
            chart.series[0].dataSource = grid.getCurrentViewRecords();
            chart.refresh();
        }
    }
}, '#Grid');
<!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://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">


  <script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
  <script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>

  <div class="container">
    <div id="Grid"></div>
    <div id="Chart"></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>