Data label template in EJ2 JavaScript 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.

var datalabelData = [
     { 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' },
 ];
var Chart = new ej.charts.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">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    
    <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>

<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>