Data label template in EJ2 TypeScript Chart control

8 May 20234 minutes to read

You can bind text and interior information for a point from dataSource other than x and y value. To change color for the background in the datalabel template, you can use ${point.text}.

To use point.text, you have to bind the property from dataSource to name in the datalabel options.

Follow the given steps to show the table tooltip,

Step 1:

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

    <script id="index" type="text/x-template">
    <div id='templateWrap' style="background-color: ${point.text}; border-radius: 3px;"><span>${point.y}</span></div>
    </script>

Step 2:

To show that datalabel template, set the element id to the template property in datalabel.

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

let datalabelData: Object[] = [
     { x: 10, y: 7000, color: 'red' },
     { x: 20, y: 1000, color: 'yellow' },
     { x: 30, y: 12000, color: 'orange' },
     { x: 40, y: 14000, color: 'skyblue' },
     { x: 50, y: 11000, color: 'blue' },
     { x: 60, y: 5000, color: 'green' },
     { x: 70, y: 7300, color: 'pink' },
     { x: 80, y: 9000, color: 'white' },
     { x: 90, y: 12000, color: 'magenta' },
     { x: 100, y: 14000, color: 'purple' },
     { x: 110, y: 11000, color: 'teal' },
     { x: 120, y: 5000, color: 'gray' },
 ];

let chart: Chart = new Chart({
    series:[{
        dataSource: datalabelData,
        xName: 'x', yName: 'y',
        type: 'Line',
        marker: { visible: true, dataLabel: { visible: true, name: 'color', template: '#index'}}
    }],
}, '#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://cdn.syncfusion.com/ej2/material.css" rel="stylesheet" rel="stylesheet">
    <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>
<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="index" type="text/x-template">
    <div id='templateWrap' style="background-color: ${point.text}; border-radius: 3px;"> <span>${point.y}</span></div>
    </script>
</body>
</html>