Search results

Visualize current view records of grid in Pie Chart in JavaScript Chart control

30 Mar 2023 / 2 minutes to read

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.

Source
Preview
index.ts
index.html
Copied to clipboard
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');
Copied to clipboard
<!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/21.1.35/material.css" rel="stylesheet" rel="stylesheet">
    <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="control-section">
      <div id="link">
          <a id="category" style="visibility:hidden; display:inline-block">
            Sales by Category 
      </a>
          <p style="visibility:hidden; display:inline-block" id="symbol">&nbsp;&gt;&gt;&nbsp;</p>
          <p id="text" style="display:inline-block;"></p>
      </div>
      <div id="container"></div>
  </div>
  
</body> 
</html>