How to show a table in a tooltip in TypeScript Charts

Use the Chart tooltip template property to display point information in an HTML table. Define the template in the HTML document, enable the tooltip, and assign the template element selector to tooltip.template.

Define the tooltip table template

Create a script template with a unique ID. Use ${x} and ${y} to display the current point’s X and Y values.

Initialize the tooltip template div as shown in the following html page,

    <div id='templateWrap'>
        <table style="width:100%;  border: 1px solid black;">
        <tr><th colspan="2" bgcolor="#00FFFF">Female</th></tr>
        <tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
        </table>
    </div>

The template must exist in the document before the Chart is initialized.

Enable and assign the template

Set tooltip.enable to true and assign the template selector to tooltip.template.

tooltip: {
    enable: true,
    template:'#Female-Material'
}

Use tooltip inline format when plain text with supported placeholders is sufficient. Use template when the tooltip requires a table, custom HTML, or richer styling.

Complete example

import {
    Chart, LineSeries, Legend, Category, Tooltip
} from '@syncfusion/ej2-charts';
Chart.Inject(LineSeries, Category, Legend,Tooltip);
import { Browser } from '@syncfusion/ej2-base';

let chart: Chart = new Chart({
        title: 'Population of India ( 2010 - 2016 )',
        // Initialize the chart axes
        primaryXAxis: {
            minimum: 2010, maximum: 2016,
            edgeLabelPlacement: 'Shift',

        },
        primaryYAxis: {
            minimum: 900, maximum: 1300,
            labelFormat: '{value}M',
        },
        // Initialize the chart series
        series: [
           {
                name: 'Female',
                dataSource: [
                    { x: 2010, y: 990 }, { x: 2011, y: 1010 },
                    { x: 2012, y: 1030 }, { x: 2013, y: 1070 },
                    { x: 2014, y: 1105 }, { x: 2015, y: 1138 },
                    { x: 2016, y: 1155 }
                ], xName: 'x', yName: 'y',
                marker: {
                    visible: true,
                    shape: 'Rectangle',
                    width: 2
            }
        ],
        tooltip: {
            enable: true,
            template:'#Female-Material'
        },
    width:'650px',
    height: '350px'
    });
    chart.appendTo('#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 id='container'>
        <div id='element'></div>
    </div>  
    <script id="Female-Material" type="text/x-template">
      <div id='templateWrap'>
     <table style="width:100%;  border: 1px solid black;">
     <tr><th colspan="2" bgcolor="#00FFFF">Female</th></tr>
     <tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
    </table>
    </div>
  </script>
</body>
</html>

Template behavior and security

  • ${x} and ${y} represent the current point’s mapped X and Y values.
  • The template value must be a selector that matches an existing template element.
  • A template can include additional static labels, styles, and supported point values.
  • Keep template IDs unique when a page contains multiple Charts.
  • When the data requires custom conversion or conditional text, combine the template with preformatted data fields or use tooltipRender where appropriate.

Troubleshooting

  • If the tooltip does not appear, verify that tooltip.enable is true.
  • If the default tooltip appears, verify that tooltip.template exactly matches the template element ID.
  • If ${x} or ${y} appears as literal text, verify that the template is assigned through the Chart tooltip configuration and is not rendered directly as normal page content.
  • If the tooltip is incorrectly styled, verify that the template CSS is loaded and that its selectors match the template classes.
  • If values are undefined, verify that xName and yName match fields in the series data source.

See also