Integrate Chart in EJ2 TypeScript Grid
18 Nov 20185 minutes to read
The Grid and Chart Integration feature in Syncfusion allows users to visualize tabular data in a graphical format by linking the Grid with the Chart.This feature allows seamless integration with Charts to visualize selected data interactively. You can generate various Chart types such as bar, line, or pie directly from Grid data using the ContextMenu feature. This feature is particularly useful in scenarios where tabular data can be better understood through graphical representation.
This feature is particularly useful in dashboards, reporting tools, and data-driven applications where both tabular and visual representations of data are required.
Steps for Chart integration
To integrate Charts into your EJ2 TypeScript Grid, follow these steps:
Dependencies
Below is the list of minimum dependencies required to integrate Chart functionality into the EJ2 TypeScript Grid:
|-- dependencies
|-- @syncfusion/ej2-grids
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-buttons
|-- @syncfusion/ej2-dropdowns
|-- @syncfusion/ej2-inputs
|-- @syncfusion/ej2-navigations
|-- @syncfusion/ej2-popups
|-- @syncfusion/ej2-charts
|-- @syncfusion/ej2-grid-chartSetup for local development
Chart integration via Context Menu in Grid
This section explains how to dynamically render Charts based on user interactions within the Syncfusion TypeScript Grid using the context menu. Users can right-click on selected rows and choose from various Chart types such as bar, pie, line Charts etc to visualize the data instantly. This feature enhances interactivity and provides a seamless analytical experience.
For example, in a sales dashboard, you can select sales records for specific products and instantly view a comparative bar or pie Chart illustrating online sales versus retail sales or revenue trends over several month. This functionality helps improve decision-making by providing immediate visual feedback.
Steps to be followed to integrate Chart into Grid via contextmenu:
The following complete example demonstrates how to integrate Charts into a Grid context menu using the contextMenuClick event with gridChart.render method:
Enable export functionality in GridChart
The export functionality in GridChart allows Charts to be saved or shared externally in various formats. This is particularly useful for preserving visualized data or including Charts in reports and presentations.
To enable this feature, set the allowExport property to true when creating the GridChart instance. Once enabled, an export option appears in the Chart popup toolbar, allowing the Chart to be exported in various formats such as PDF, CSV, Print, JPEG, XLSX, SVG, and PNG.
The following code snippet demonstrates how to enable export functionality in GridChart:
gridChart = new GridChart({
allowExport: true
});Enable property panel in GridChart
The property panel in GridChart provides a built-in interface to modify Chart settings dynamically at runtime. By default, the property panel is disabled. To enable it, set the enablePropertyPanel option to true during the instantiation of the GridChart object. Here’s how to do it:
gridChart = new GridChart({
enablePropertyPanel: true
});When enabled, the property panel appears alongside the Chart popup and offers three main configuration sections:
-
Chart Types: Choose from a variety of Chart types, including Column, Line, Pie, and others.
-
Chart Data: Configure the category axis and series fields for data visualization.
-
Formatting Options: Adjust styling properties such as Chart style, title style, legend appearance, series settings, and axes formatting.
-
Chart Style:
- Margin : Adjust the margins around the Chart (top, bottom, left, right) for better layout spacing.
- Color: Set distinct colors for different Chart elements to improve visual clarity.
-
Title Style
- Legend - Toggle the visibility of the legend and customize its font, size, color, and position.
- Series - Enable/disable tooltips and customize Series color and data labels based on categories such as online, retail, or revenue.
-
Axes: - Select axes as either category or value type, and customize:
- Reverse Order to change the data sequence.
- Title: Adjust text, font, size, color, and rotation.
- Label: Modify text, font, size, color, and rotation for better label presentation.
-
customize the Chart model:
You can customize the Chart by defining a chartModel object in the chart property of the model object within the contextMenuClick event .This allows you to configure various Chart properties such as axes, margins, tooltips, titles, and more. Additionally, you can use the events of the Chart within the chartModel configuration to apply further customizations when the Chart is rendered.
To customize Accumulation Charts (such as Pie), use the accumulationChart property of the model object. This property allows you to configure Chart options like titles, legends, data labels, and visual styles tailored for accumulation-type visualizations. You can also use Accumulation Chart events to apply additional customizations when the Chart is rendered.
The following code snippets demonstrate how to achieve this:
You can also programmatically update the Chart’s appearance using the updateChartSettings(args: UpdateChartArgs) event provided by the GridChart integration. This event is triggered when you modify Chart properties through the Chart popup’s property panel. This is useful for applying custom themes, adjusting axis settings, or modifying series styles.
The following code snippet demonstrates how to dynamically customize chart margins and restrict their values to a minimum of 20 using the updateChartSettings event.
The following complete example demonstrates how to enable the property panel by setting enablePropertyPanel to true, customize the chart using the chartModel within contextMenuClick event, and dynamically control or customize the behavior of the updateChartSettings event.