Selection in TypeScript Charts
The Chart supports point, series, cluster, rectangular, and lasso selection through click, tap, or drag interactions. When Series or Cluster selection is enabled, selecting a data point also selects the corresponding legend item.
Selection modes
Set the selectionMode property to one of the following values:
| Mode | Description |
|---|---|
None |
Disables selection. This is the default value. |
Point |
Selects an individual data point. |
Series |
Selects an entire series. |
Cluster |
Selects data points that share the same index across all series. |
DragXY |
Selects data points within a rectangular region along both axes. |
DragX |
Selects data points within a rectangular region along the horizontal axis. |
DragY |
Selects data points within a rectangular region along the vertical axis. |
Lasso |
Selects data points within a freehand-drawn region. |
Point
Set selectionMode to Point to select an individual data 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>
</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
Set selectionMode to Series to select an entire 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>
</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
Set selectionMode to Cluster to select data points that share the same index across all 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 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>
</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
Use DragXY, DragX, or DragY to select data points within a rectangular region. On touch devices, double-tap and drag to perform rectangular selection.
-
DragXY: Selects data points along both axes. -
DragX: Selects data points along the horizontal axis. -
DragY: Selects data points along the vertical axis.
Handle the dragComplete event to process the values selected by a drag operation. Refer to the IDragCompleteEventArgs API for the event-argument properties.
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>
</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>Lasso selection
Set selectionMode to Lasso to select data points inside a freehand-drawn region. Lasso selection is useful when the required points cannot be enclosed efficiently by a rectangular region.
Multiple selection
Set the isMultiSelect property to true to retain more than one point, series, or cluster selection. The default value is false.
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>
</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.
Use the selectedDataIndexes property to select points or series when the Chart loads. Each item contains a zero-based series index and point index.
selectedDataIndexes: [
{ series: 0, point: 2 }
]Use this property with Point, Series, or Cluster selection modes.
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>
</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>Legend selection
By default, clicking a legend item toggles the visibility of its series. To select a series through the legend without hiding it, set selectionMode to Series and set legendSettings.toggleVisibility to false.
Use legendSettings.enableHighlight only when hover highlighting is also required.
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>
</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 customization
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>
</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 events
Use the selectionComplete event to retrieve the selected values after point, series, or cluster selection. Use the dragComplete event to retrieve values selected through rectangular or lasso selection.