How can I help you?
Cross hair and track ball in EJ2 TypeScript Chart control
3 Feb 202624 minutes to read
The crosshair feature displays vertical and horizontal guide lines that intersect at the mouse or touch position, helping to identify the corresponding axis values precisely.
Crosshair lines can be enabled by setting the enable property in the crosshair settings.
import { Chart, LineSeries, Crosshair, DateTime, Legend } from '@syncfusion/ej2-charts';
import { series1 } from './datasource.ts';
Chart.Inject(LineSeries, DateTime, Crosshair, Legend);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'DateTime',
},
series: [
{
type: 'Line', width: 2, name: 'Temperature',
dataSource: series1, xName: 'x', yName: 'y'
}
],
//crosshair for chart
crosshair: { enable: true },
legendSettings: { visible: true },
title: 'Weather Condition'
}, '#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>Tooltip for axis
Axis tooltip labels can be enabled by setting the enable property of crosshairTooltip in the corresponding axis. This tooltip displays the current axis value at the crosshair position.
import { Chart, LineSeries, Crosshair, DateTime, Legend } from '@syncfusion/ej2-charts';
import { series1 } from './datasource.ts';
Chart.Inject(LineSeries, DateTime, Crosshair, Legend);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'DateTime',
crosshairTooltip: { enable: true },
},
primaryYAxis:
{
crosshairTooltip: { enable: true }
},
series: [
{
type: 'Line', width: 2, name: 'Temperature',
dataSource: series1, xName: 'x', yName: 'y'
}
],
//crosshair for chart
crosshair: { enable: true },
legendSettings: { visible: true },
title: 'Weather Condition'
}, '#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
The fill and textStyle properties of crosshairTooltip are used to customize the background color and font style of the crosshair label.
The color and width of the crosshair lines can be customized by using the line property in the crosshair settings.
import { Chart, LineSeries, Crosshair, DateTime, Legend } from '@syncfusion/ej2-charts';
import { series1 } from './datasource.ts';
Chart.Inject(LineSeries, DateTime, Crosshair, Legend);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'DateTime',
crosshairTooltip: { enable: true, fill: 'green' },
},
primaryYAxis:
{
crosshairTooltip: { enable: true, fill: 'green' },
},
series: [
{
type: 'Line', width: 2, name: 'Temperature',
dataSource: series1, xName: 'x', yName: 'y'
}
],
crosshair: {
enable: true,
// customizing crosshair
line: {width: 2, color: 'green'} },
legendSettings: { visible: true },
title: 'Weather Condition'
}, '#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>Note: To use the crosshair feature, inject
CrosshairmoduleChart.Inject(Crosshair)method.
Snap to data
By enabling the snapToData property, the crosshair snaps to the nearest data point instead of following the exact mouse position. This improves accuracy when inspecting values.
import { Tooltip, Crosshair, DateTime } from '@syncfusion/ej2-charts';
import { Chart, LineSeries, Legend } from '@syncfusion/ej2-charts';
import { trackData } from './datasource.ts';
Chart.Inject(LineSeries, DateTime, Tooltip, Crosshair, Legend);
let chart: Chart = new Chart({
primaryXAxis: {
title: 'Years',
minimum: new Date(2000, 1, 1), maximum: new Date(2006, 2, 11),
intervalType: 'Years',
valueType: 'DateTime',
lineStyle: { width: 0 },
majorGridLines: { width: 0 },
edgeLabelPlacement: 'Shift',
},
primaryYAxis:
{
title: 'Revenue in Millions',
labelFormat: '{value}M',
majorTickLines: { width: 0 },
minimum: 10, maximum: 60,
lineStyle: { width: 0 }
},
series: [
{
dataSource: trackData, name: 'John', xName: 'x',
marker: { visible: true },
type: 'Line', width: 2,
yName: 'y'
}
],
// trackball for chart
tooltip: { enable: true, shared: true, format: '${series.name} : ${point.x} : ${point.y}' },
crosshair: { enable: true, lineType: 'Horizontal', snapToData: true },
title: 'Average Sales per Person',
legendSettings: { visible: false }
}, '#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>Trackball
The trackball feature tracks the data point closest to the mouse or touch position. A trackball marker highlights the nearest point, and the trackball tooltip displays detailed information about that point.
To use the trackball feature, inject both Crosshair and Tooltip into the provide.
Trackball functionality can be enabled by setting the enable property of the crosshair to true and the shared property of the tooltip to true.
import { Tooltip, Crosshair, DateTime } from '@syncfusion/ej2-charts';
import { Chart, LineSeries, Legend } from '@syncfusion/ej2-charts';
import { trackData } from './datasource.ts';
Chart.Inject(LineSeries, DateTime, Tooltip, Crosshair, Legend);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'DateTime',
},
series: [
{
dataSource: trackData, name: 'John', xName: 'x',
marker: { visible: true },
type: 'Line', width: 2,
yName: 'y'
},
{
dataSource: trackData, name: 'Andrew', xName: 'x',
marker: { visible: true },
type: 'Line', width: 2,
yName: 'y1'
},
{
dataSource: trackData, name: 'Thomas', xName: 'x',
marker: { visible: true },
type: 'Line', width: 2,
yName: 'y2'
},
{
dataSource: trackData, name: 'Mark', xName: 'x',
marker: { visible: true },
type: 'Line', width: 2,
yName: 'y3'
},
{
dataSource: trackData, name: 'William', xName: 'x',
marker: { visible: true },
type: 'Line', width: 2,
yName: 'y4'
}
],
// trackball for chart
tooltip: { enable: true, shared: true, format: '${series.name} : ${point.x} : ${point.y}' },
crosshair: { enable: true, lineType: 'Vertical' },
title: 'Average Sales per Person'
}, '#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>Crosshair highlight
The highlightCategory property highlights the background of the entire category when the crosshair is moved over the chart.
The crosshair line color can be customized using the color property within the line configuration.
import { Chart, ColumnSeries, Category, Crosshair } from '@syncfusion/ej2-charts';
Chart.Inject(ColumnSeries, Category, Crosshair);
let chartData: Object[] = [
{ country: "USA", gold: 50 },
{ country: "China", gold: 40 },
{ country: "Japan", gold: 70 },
{ country: "Australia", gold: 60 },
{ country: "France", gold: 50 },
{ country: "Germany", gold: 40 },
{ country: "Italy", gold: 40 },
{ country: "Sweden", gold: 30 }
];
let chart: Chart = new Chart({
primaryXAxis: {
//Category in primary X Axis
valueType: 'Category',
},
series:[{
dataSource: chartData,
xName: 'country', yName: 'gold',
type: 'Column'
}],
crosshair: { enable: true, line: { color: 'red' }, lineType: 'Vertical', highlightCategory: true },
}, '#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>
</body>
</html>