How can I help you?
Selection in EJ2 TypeScript Chart control
3 Feb 202624 minutes to read
The chart provides selection support for both series and individual data points when users interact with the chart using mouse clicks.
When a data point is clicked, the corresponding series legend item is also selected.
Multiple selection modes are available to help select and analyze chart data effectively. The supported selection modes are:
- None
- Point
- Series
- Cluster
- DragXY
- DragX
- DragY
Point
A single data point can be selected by setting the selectionMode property to Point.
import { Chart, ColumnSeries, Category, Legend, Selection } from '@syncfusion/ej2-charts';
import { selectionData } from './datasource.ts';
Chart.Inject(ColumnSeries, Category, Legend, Selection);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category',
},
series:[{
dataSource: selectionData,
xName: 'country', yName: 'gold',
name: 'Gold', type: 'Column'
}, {
dataSource: selectionData,
xName: 'country', yName: 'silver',
name: 'Silver', type: 'Column'
}, {
dataSource: selectionData,
xName: 'country', yName: 'bronze',
name: 'Bronze', type: 'Column'
}],
// selection mode as point
selectionMode: 'Point',
title: 'Olympic Medals'
}, '#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="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
</body>
</html>Series
An entire series can be selected by setting the selectionMode property to Series.
import { Chart, ColumnSeries, Category, Legend, Selection } from '@syncfusion/ej2-charts';
import { selectionData } from './datasource.ts';
Chart.Inject(ColumnSeries, Category, Legend, Selection);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category',
},
series:[{
dataSource: selectionData ,
xName: 'country', yName: 'gold',
name: 'Gold', type: 'Column'
}, {
dataSource: selectionData ,
xName: 'country', yName: 'silver',
name: 'Silver', type: 'Column'
}, {
dataSource: selectionData ,
xName: 'country', yName: 'bronze',
name: 'Bronze', type: 'Column'
}],
// selection mode as series
selectionMode: 'Series',
title: 'Olympic Medals'
}, '#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="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
</body>
</html>Cluster
Cluster selection allows selection of data points that share the same index across all series. This can be enabled by setting the selectionMode property to Cluster.
import { Chart, ColumnSeries, Category, Legend, Selection } from '@syncfusion/ej2-charts';
import { selectionData } from './datasource.ts';
Chart.Inject(ColumnSeries, Category, Legend, Selection);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category',
},
series:[{
dataSource: selectionData ,
xName: 'country', yName: 'gold',
name: 'Gold', type: 'Column'
}, {
dataSource: selectionData ,
xName: 'country', yName: 'silver',
name: 'Silver', type: 'Column'
}, {
dataSource: selectionData ,
xName: 'country', yName: 'bronze',
name: 'Bronze', type: 'Column'
}],
// selection mode as cluster
selectionMode: 'Cluster',
title: 'Olympic Medals'
}, '#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="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
</body>
</html>Rectangular selection
DragXY, DragX and DragY
Rectangular selection enables users to select a group of data points within a defined region by setting the selectionMode property accordingly.
- DragXY – Selects data points along both the horizontal and vertical axes.
- DragX – Selects data points along the horizontal axis.
- DragY – Selects data points along the vertical axis.
The selected data points are returned as an array collection through the dragComplete event.
import { Chart, ScatterSeries, Legend, Selection } from '@syncfusion/ej2-charts';
import { rectData } from './datasource.ts';
Chart.Inject(ScatterSeries, Legend, Selection);
let chart: Chart = new Chart({
series: [
{
type: 'Scatter',
dataSource: rectData,
xName: 'x',
yName: 'y', name: 'Product A',
marker: {
shape: 'Triangle',
width: 10, height: 10
}
},
],
// selection mode as dragxy
selectionMode: 'DragXY',
title: 'Height Vs Weight'
}, '#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="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
</body>
</html>Selection type
Multiple data points or series can be selected simultaneously by enabling the isMultiSelect property.
import { Chart, ColumnSeries, Category, Legend, Selection } from '@syncfusion/ej2-charts';
import { selectionData } from './datasource.ts';
Chart.Inject(ColumnSeries, Category, Legend, Selection);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category',
},
series:[{
dataSource: selectionData ,
xName: 'country', yName: 'gold',
name: 'Gold', type: 'Column'
}, {
dataSource: selectionData ,
xName: 'country', yName: 'silver',
name: 'Silver', type: 'Column'
}, {
dataSource: selectionData ,
xName: 'country', yName: 'bronze',
name: 'Bronze', type: 'Column'
}],
selectionMode: 'Series',
// multipselect forselection
isMultiSelect: true,
title: 'Olympic Medals'
}, '#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="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
</body>
</html>Selection on load
Data points or series can be selected programmatically when the chart is loaded by using the selectedDataIndexes property.
import { Chart, ColumnSeries, Category, Legend, Selection } from '@syncfusion/ej2-charts';
import { selectionData } from './datasource.ts'
Chart.Inject(ColumnSeries, Category, Legend, Selection);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category',
},
series:[{
dataSource: selectionData ,
xName: 'country', yName: 'gold',
name: 'Gold', type: 'Column',
animation: {enable: false},
selectionStyle: 'chartSelection1'
}, {
dataSource: selectionData ,
xName: 'country', yName: 'silver',
name: 'Silver', type: 'Column',
animation: {enable: false},
selectionStyle: 'chartSelection2'
}, {
dataSource: selectionData ,
xName: 'country', yName: 'bronze',
name: 'Bronze', type: 'Column',
animation: {enable: false},
selectionStyle: 'chartSelection3'
}],
selectionMode: 'Point',
isMultiSelect: true,
// Selcted data indexes for chart series
selectedDataIndexes: [
{ series: 0, point: 1}, { series: 2, point: 3}
],
title: 'Olympic Medals'
}, '#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="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
</body>
</html>Selection through on legend
Points or series can also be selected through the legend by enabling the toggleVisibility property.
To visually emphasize the selected series, use the enableHighlight property.
When
highlightModeis set toSeries,Cluster, orPoint, legend highlighting occurs even ifenableHighlightis set to false. In this case,highlightModetakes precedence, and hovering over legend items highlights the corresponding series.
import { Chart, ColumnSeries, Category, Legend, Selection, Highlight } from '@syncfusion/ej2-charts';
import { selectionData } from './datasource.ts'
Chart.Inject(ColumnSeries, Category, Legend, Selection, Highlight);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category',
},
series:[{
dataSource: selectionData,
xName: 'country', yName: 'gold',
name: 'Gold', type: 'Column',
animation: {enable: false},
}, {
dataSource: selectionData,
xName: 'country', yName: 'silver',
name: 'Silver', type: 'Column',
animation: {enable: false},
}, {
dataSource: selectionData,
xName: 'country', yName: 'bronze',
name: 'Bronze', type: 'Column',
animation: {enable: false},
}],
// Selection through on legend
legendSettings: { visible: true, toggleVisibility: false, enableHighlight: true},
title: 'Olympic Medals'
}, '#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="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
</body>
</html>Customization for selection
Custom styles can be applied to selected points or series by using the selectionStyle property.
import { Chart, ColumnSeries, Category, Legend, Selection } from '@syncfusion/ej2-charts';
import { selectionData } from './datasource.ts'
Chart.Inject(ColumnSeries, Category, Legend, Selection);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category',
},
series:[{
dataSource: selectionData,
xName: 'country', yName: 'gold',
name: 'Gold', type: 'Column',
//Selection style for chart series selection
selectionStyle: 'chartSelection1'
}, {
dataSource: selectionData,
xName: 'country', yName: 'silver',
name: 'Silver', type: 'Column',
selectionStyle: 'chartSelection2'
}, {
dataSource: selectionData,
xName: 'country', yName: 'bronze',
name: 'Bronze', type: 'Column',
selectionStyle: 'chartSelection3'
}],
selectionMode: 'Point',
isMultiSelect: true,
title: 'Olympic Medals'
}, '#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="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
</body>
</html>