How to format tooltip in TypeScript Charts
Use the Chart tooltipRender event to replace the default tooltip text with a formatted DateTime value. Retrieve the current point’s X value from args.point.x, format it using the Internationalization class, and assign the formatted result to args.text.
Enable the tooltip
Set tooltip.enable to true so the Chart raises tooltipRender before displaying a tooltip.
tooltip: {
enable: true
}Format the DateTime value
Create an Internationalization instance once and reuse it inside the event handler. Convert args.point.x to a valid Date, format the value, and set args.text.
tooltipRender: function(args) {
var intl = new ej.base.Internationalization();
var formattedString = intl.formatDate(new Date(args.point.x), { skeleton: 'yMd' });
args.text = formattedString;
}The yMd skeleton produces a locale-aware year, month, and day representation. Use a different supported skeleton when the tooltip requires time or another date granularity.
This formatting is also available through the tooltip’s inline format property when the required output can be expressed with supported tooltip placeholders; use tooltipRender when custom date conversion or conditional formatting is required.
Complete example
import { Chart, LineSeries, DateTime, ITooltipRenderEventArgs, DataLabel, Tooltip } from '@syncfusion/ej2-charts';
import { Internationalization } from '@syncfusion/ej2-base';
Chart.Inject(LineSeries, DateTime, DataLabel, Tooltip);
let chart: Chart = new Chart({
//Initializing Primary X Axis
primaryXAxis: {
valueType: 'DateTime',
},
//Initializing Chart Series
series: [
{
type: 'Line',
dataSource: [
{ x: new Date(2005, 0, 1), y: 21 }, { x: new Date(2006, 0, 1), y: 24 },
{ x: new Date(2007, 0, 1), y: 30 }, { x: new Date(2008, 0, 1), y: 38 },
{ x: new Date(2009, 0, 1), y: 54 }, { x: new Date(2010, 0, 1), y: 57 },
],
xName: 'x', width: 2, marker: {
dataLabel : { visible: true },
visible: true,
width: 10,
height: 10
},
yName: 'y', name: 'Germany',
}
],
//Initializing Chart title
title: 'Inflation - Consumer Price',
tooltip: {enable: true},
tooltipRender:(args: ITooltipRenderEventArgs) => { // To format the current point x value
let intl: Internationalization = new Internationalization();
let formattedString: string = intl.formatDate(new Date(args.point.x), { skeleton: 'yMd' });
args.text = formattedString;
},
height: '350px'
},'#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="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="col-sm-8">
<div class="row">
<div class="col-sm-4">
<div id='container'>
<div id='element' style="width:350px; height:350px;float:left">
</div>
<label id="lbl"></label>
</div>
</div>
<div class="col-sm-4" style="width:200px; height:350px;float: right">
<div id='Grid'>
</div>
</div>
</div>
</div>
</body>
</html>Formatting considerations
-
args.point.xcontains the current point’s mapped X value. -
args.textreplaces the tooltip text for the current point. -
formatDate()formats a JavaScriptDateaccording to the selected locale and options. -
skeleton: 'yMd'formats a date; time-focused skeletons can be used when the X value includes time. - JavaScript
Dateparsing can apply local-time or UTC conversions, so use one time-zone convention consistently in the data source. - When
tooltip.sharedis enabled, verify the event argument shape and desired output for every series before replacing the complete tooltip text.
Troubleshooting
- If no tooltip appears, verify that
tooltip.enableistrueand Tooltip support is available in the loaded EJ2 ES5 bundle. - If the date is invalid, verify that
args.point.xcontains a valid Date, timestamp, or valid date value. - If the tooltip shows only the date, append the point value or series name when both are required.
- If formatting does not follow the expected locale, configure and load the required localization data before creating the Chart.
- If the axis uses DateTime values but labels are incorrect, verify that
primaryXAxis.valueTypeisDateTime.