Data labels in EJ2 TypeScript Chart control
22 Aug 202424 minutes to read
Data label can be added to a chart series by enabling the visible option in the dataLabel. By default, the labels will arrange smartly without overlapping.
import { Chart, ColumnSeries,Category, DataLabel } from '@syncfusion/ej2-charts';
import { columnData } from './datasource.ts';
Chart.Inject(ColumnSeries,Category, DataLabel);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category'
},
series: [
{
type: 'Column',
dataSource: columnData, xName: 'country', yName: 'gold',
marker: {
//Data label for chart series
dataLabel: { visible: 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>Note: To use data label feature, we need to inject
DataLabelusingChart.Inject(DataLabel)method.
Position
Using position property, you can place the label either on Top, Middle,Bottom or Outer (outer is applicable for column and bar type series).
import { Chart, ColumnSeries, Category, DataLabel } from '@syncfusion/ej2-charts';
import { columnData } from './datasource.ts';
Chart.Inject(ColumnSeries, Category, DataLabel);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category'
},
primaryYAxis:
{
labelFormat: '{value}°C'
},
series: [
{
type: 'Column',
dataSource: columnData, xName: 'country', yName: 'gold',
marker: {
//Data label position as middle
dataLabel: { visible: true, position: 'Middle' }
}
}
],
}, '#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>Note: The position
Outeris applicable for column and bar type series.
Data Label Template
Label content can be formatted by using the template option. Inside the template, you can add the placeholder text ${point.x} and ${point.y} to display corresponding data points x & y value. Using template property, you can set data label template in chart.
import { Chart, ColumnSeries, Category, DataLabel } from '@syncfusion/ej2-charts';
import { columnData } from './datasource.ts';
Chart.Inject(ColumnSeries, Category, DataLabel);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category'
},
series: [
{
type: 'Column',
dataSource: columnData, xName: 'country', yName: 'gold',
marker: {
dataLabel: { visible: true, template:'<div style="background:#f5f5f5; border: 1px solid black; padding: 3px 3px 3px 3px"><div>${point.x}</div><div>${point.y}</div></div>' }
}
}
],
}, '#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>Text Mapping
Text from the data source can be mapped using name property.
import { Chart, ColumnSeries, Category, DataLabel } from '@syncfusion/ej2-charts';
Chart.Inject(ColumnSeries, Category, DataLabel);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category'
},
series: [
{
type: 'Column',
dataSource: [{ x: 'Jan', y: 12, text: 'January : 12°C' }, { x: 'Feb', y: 8, text: 'February : 8°C' }, { x: 'Mar', y: 11, text: 'March : 11°C' }, { x: 'Apr', y: 6, text: 'April : 6°C' }],
xName: 'x', yName: 'y',
marker: {
visible: true,
dataLabel: { visible: true,name: 'text' }
}
}
],
}, '#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>Format
Data label for the chart can be formatted using format property. You can use the global formatting options, such as ‘n’, ‘p’, and ‘c’.
import { Chart, ColumnSeries, Category, Legend, DataLabel, Tooltip, ISeriesRenderEventArgs,
ITextRenderEventArgs} from '@syncfusion/ej2-charts';
import { Browser } from '@syncfusion/ej2-base';
Chart.Inject(ColumnSeries, DataLabel, Category, Legend, Tooltip);
let total: any = [];
let chart: Chart = new Chart({
//Initializing Primary X and Y Axis
primaryXAxis: {
valueType: 'Category',
interval: 1,
majorGridLines: { width: 0 },
},
chartArea: { border: { width: 0 } },
primaryYAxis: {
majorGridLines: { width: 0 },
majorTickLines: { width: 0 },
lineStyle: { width: 0 },
labelStyle: { color: 'transparent' },
},
//Initializing Chart Series
series: [
{
type: 'Column',
xName: 'x',
width: 2,
yName: 'y',
name: 'Gold',
dataSource: [
{ x: 'USA', y: 46 },
{ x: 'GBR', y: 27 },
{ x: 'CHN', y: 26 },
],
marker: {
dataLabel: {
visible: true,
position: 'Top',
format: 'n2'
},
},
},
{
type: 'Column',
xName: 'x',
width: 2,
yName: 'y',
name: 'Silver',
dataSource: [
{ x: 'USA', y: 37 },
{ x: 'GBR', y: 23 },
{ x: 'CHN', y: 18 },
],
marker: {
dataLabel: {
visible: true,
position: 'Top',
format:'n2'
},
},
},
{
type: 'Column',
xName: 'x',
width: 2,
yName: 'y',
name: 'Bronze',
dataSource: [
{ x: 'USA', y: 38 },
{ x: 'GBR', y: 17 },
{ x: 'CHN', y: 26 },
],
marker: {
dataLabel: {
visible: true,
position: 'Top',
format:'n2'
},
},
},
],
//Initializing Chart Title
width: Browser.isDevice ? '100%' : '60%',
title: 'Olympic Medal Counts - RIO',
tooltip: { enable: true },
});
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="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>| Value | Format | Resultant Value | Description |
|---|---|---|---|
| 1000 | n1 | 1000.0 | The number is rounded to 1 decimal place. |
| 1000 | n2 | 1000.00 | The number is rounded to 2 decimal places. |
| 1000 | n3 | 1000.000 | The number is rounded to 3 decimal place. |
| 0.01 | p1 | 1.0% | The number is converted to percentage with 1 decimal place. |
| 0.01 | p2 | 1.00% | The number is converted to percentage with 2 decimal place. |
| 0.01 | p3 | 1.000% | The number is converted to percentage with 3 decimal place. |
| 1000 | c1 | $1000.0 | The currency symbol is appended to number and number is rounded to 1 decimal place. |
| 1000 | c2 | $1000.00 | The currency symbol is appended to number and number is rounded to 2 decimal place. |
Margin
margin for data label can be applied to using left, right, bottom and top properties.
import { Chart, ColumnSeries, Category, DataLabel } from '@syncfusion/ej2-charts';
import { columnData } from './datasource.ts';
Chart.Inject(ColumnSeries, Category, DataLabel);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category'
},
series: [
{
type: 'Column',
dataSource: columnData, xName: 'country', yName: 'gold',
marker: {
dataLabel: { visible: true,
border:{width: 1, color : 'red'},
margin:{
left:5,
right:5,
top:5,
bottom:5
}
}
}
}
],
}, '#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>Customization
stroke and border of data label can be customized using fill and border properties. Rounded corners can be customized using rx and ry properties.
import { Chart, ColumnSeries, Category, DataLabel } from '@syncfusion/ej2-charts';
import { columnData } from './datasource.ts';
Chart.Inject(ColumnSeries, Category, DataLabel);
let chart: Chart = new Chart({
primaryXAxis: {
valueType: 'Category'
},
series: [
{
type: 'Column',
dataSource: columnData, xName: 'country', yName: 'gold',
marker: {
dataLabel: { visible: true,
border:{width: 2, color : 'red'},
rx:10, ry: 10
}
}
}
],
}, '#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>Note:
rxandryproperties requiresbordervalues not to be null.
Customizing Specific Point
You can also customize the specific marker and label using pointRender and textRender event. pointRender event allows you to change the shape, color and border for a point, whereas the textRender event allows you to change the text for the point.
import { Chart, LineSeries, DataLabel, IPointRenderEventArgs, ITextRenderEventArgs } from '@syncfusion/ej2-charts';
import { numData } from './datasource.ts';
Chart.Inject(LineSeries, DataLabel);
let chart: Chart = new Chart({
series: [
{
type: 'Line',
dataSource: numData, xName: 'x', yName: 'y',
marker: {
visible: true,
height: 10, width: 10,
dataLabel: { visible: true }
}
}
],
// pointRender event for chart
pointRender: (args: IPointRenderEventArgs) => {
if (args.point.index === 3) {
args.fill = 'red'
}
},
textRender: (args: ITextRenderEventArgs) => {
if (args.point.index === 3) {
args.text = 'Maximum Temperature';
args.color = 'red';
}
else {
args.cancel = 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>Show percentage based on each series points
You can calculate the percentage value based on the sum for each series using the seriesRender and textRender events in the chart. In seriesRender calculate the sum of each series y values and In textRender calculate percentage value based on the sum value and modify the text.
import { Chart, ColumnSeries, Category, Legend, DataLabel, Tooltip, ISeriesRenderEventArgs,
ITextRenderEventArgs} from '@syncfusion/ej2-charts';
import { Browser } from '@syncfusion/ej2-base';
Chart.Inject(ColumnSeries, DataLabel, Category, Legend, Tooltip);
let total: any = [];
let chart: Chart = new Chart({
//Initializing Primary X and Y Axis
primaryXAxis: {
valueType: 'Category',
interval: 1,
majorGridLines: { width: 0 },
},
chartArea: { border: { width: 0 } },
primaryYAxis: {
majorGridLines: { width: 0 },
majorTickLines: { width: 0 },
lineStyle: { width: 0 },
labelStyle: { color: 'transparent' },
},
//Initializing Chart Series
series: [
{
type: 'Column',
xName: 'x',
width: 2,
yName: 'y',
name: 'Gold',
dataSource: [
{ x: 'USA', y: 46 },
{ x: 'GBR', y: 27 },
{ x: 'CHN', y: 26 },
],
marker: {
dataLabel: {
visible: true,
position: 'Top',
font: { fontWeight: '600', color: '#ffffff' },
},
},
},
{
type: 'Column',
xName: 'x',
width: 2,
yName: 'y',
name: 'Silver',
dataSource: [
{ x: 'USA', y: 37 },
{ x: 'GBR', y: 23 },
{ x: 'CHN', y: 18 },
],
marker: {
dataLabel: {
visible: true,
position: 'Top',
font: { fontWeight: '600', color: '#ffffff' },
},
},
},
{
type: 'Column',
xName: 'x',
width: 2,
yName: 'y',
name: 'Bronze',
dataSource: [
{ x: 'USA', y: 38 },
{ x: 'GBR', y: 17 },
{ x: 'CHN', y: 26 },
],
marker: {
dataLabel: {
visible: true,
position: 'Top',
font: { fontWeight: '600', color: '#ffffff' },
},
},
},
],
//Initializing Chart title
width: Browser.isDevice ? '100%' : '60%',
title: 'Olympic Medal Counts - RIO',
tooltip: { enable: true },
seriesRender: (args: ISeriesRenderEventArgs) => {
for (let i = 0; i < args.data.length; i++) {
if (!total[args.data[i].x]) total[args.data[i].x] = 0;
total[args.data[i].x] += parseInt(args.data[i].y);
}
},
textRender: (args: ITextRenderEventArgs) => {
let percentage: number | string = (parseInt(args.text) / total[args.point.x]) * 100;
percentage = percentage % 1 === 0 ? percentage : percentage.toFixed(2);
args.text = percentage + '%';
},
});
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="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>