Series label in TypeScript Chart control

18 Nov 201824 minutes to read

The series label displays the name of each series inline, positioned near the end of the series path or the last visible data point. This helps identify each series without referring to the legend.

Overview

Enable series labels using the labelSettings property within the series configuration. Set the visible property to true to display the label.

import { Chart, LineSeries, Double, Legend, Tooltip, Highlight, SeriesLabel, Category } from '@syncfusion/ej2-charts';
import { franceData, indonesiaData, mexicoData, polandData, vietnamData } from './datasource.ts';
Chart.Inject(LineSeries, Double, Legend, Tooltip, Highlight, SeriesLabel, Category);

let chart: Chart = new Chart({
    primaryXAxis: { valueType: 'Category' }, legendSettings: { visible: true }, primaryYAxis: { title: 'Value' },
    series: [
        {
            type: 'Line',
            dataSource: vietnamData,
            xName: 'x', width: 2, marker: {
                visible: true,
                width: 7,
                height: 7,
                shape: 'Circle',
                isFilled: true
            },
            yName: 'y', name: 'Vietnam',
            labelSettings: { visible: true }
        },
        {
            type: 'Line',
            dataSource: indonesiaData,
            xName: 'x', width: 2, marker: {
                visible: true,
                width: 5,
                height: 5,
                shape: 'Rectangle',
                isFilled: true
            },
            yName: 'y', name: 'Indonesia',
            labelSettings: { visible: true }
        },
        {
            type: 'Line',
            dataSource: franceData,
            xName: 'x', width: 2, marker: {
                visible: true,
                width: 5,
                height: 5,
                shape: 'Rectangle',
                isFilled: true
            },
            yName: 'y', name: 'France',
            labelSettings: { visible: true }
        },
        {
            type: 'Line',
            dataSource: polandData,
            xName: 'x', width: 2, marker: {
                visible: true,
                width: 5,
                height: 5,
                shape: 'Rectangle',
                isFilled: true
            },
            yName: 'y', name: 'Poland',
            labelSettings: { visible: true }
        },
        {
            type: 'Line',
            dataSource: mexicoData,
            xName: 'x', width: 2, marker: {
                visible: true,
                width: 5,
                height: 5,
                shape: 'Rectangle',
                isFilled: true
            },
            yName: 'y', name: 'Mexico',
            labelSettings: { visible: 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>
</head>

<body>
    <div id='loader'>Loading....</div>
    <div id='container'>
        <div id='element'></div>
    </div>
</body>

</html>
export let vietnamData: { x: number; y: number }[] = [
  { x: 2016, y: 7.8 },
  { x: 2017, y: 10.3 },
  { x: 2018, y: 15.5 },
  { x: 2019, y: 17.5 },
  { x: 2020, y: 19.5 },
  { x: 2021, y: 23.0 },
  { x: 2022, y: 20.0 },
  { x: 2023, y: 19.0 },
  { x: 2024, y: 22.1 }
];

export let indonesiaData: { x: number; y: number }[] = [
  { x: 2016, y: 4.8 },
  { x: 2017, y: 5.2 },
  { x: 2018, y: 6.2 },
  { x: 2019, y: 7.8 },
  { x: 2020, y: 9.3 },
  { x: 2021, y: 14.3 },
  { x: 2022, y: 15.6 },
  { x: 2023, y: 16.0 },
  { x: 2024, y: 17.0 }
];

export let franceData: { x: number; y: number }[] = [
  { x: 2016, y: 14.6 },
  { x: 2017, y: 15.5 },
  { x: 2018, y: 15.4 },
  { x: 2019, y: 14.4 },
  { x: 2020, y: 11.6 },
  { x: 2021, y: 13.9 },
  { x: 2022, y: 12.1 },
  { x: 2023, y: 10.0 },
  { x: 2024, y: 10.8 }
];

export let polandData: { x: number; y: number }[] = [
  { x: 2016, y: 8.9 },
  { x: 2017, y: 10.3 },
  { x: 2018, y: 10.8 },
  { x: 2019, y: 9.0 },
  { x: 2020, y: 7.9 },
  { x: 2021, y: 8.5 },
  { x: 2022, y: 7.4 },
  { x: 2023, y: 6.4 },
  { x: 2024, y: 7.1 }
];

export let mexicoData: { x: number; y: number }[] = [
  { x: 2016, y: 19.0 },
  { x: 2017, y: 20.0 },
  { x: 2018, y: 20.2 },
  { x: 2019, y: 18.4 },
  { x: 2020, y: 16.8 },
  { x: 2021, y: 18.5 },
  { x: 2022, y: 18.4 },
  { x: 2023, y: 16.3 },
  { x: 2024, y: 13.7 }
];

Note: To use the series label feature, inject the SeriesLabel module into the services.

Customization

Customize the appearance of series labels using the following properties:

  • text – Custom text for the label. Defaults to the series name.
  • font – Font customization options including color, size, family, and weight.
  • background – Background color of the label.
  • border – Border width and color.
  • opacity – Label transparency (default: 1).
  • showOverlapText – When false, overlapping labels are hidden.
import { Chart, LineSeries, Double, Legend, Tooltip, Highlight, SeriesLabel, Category } from '@syncfusion/ej2-charts';
import { franceData, indonesiaData, mexicoData, polandData, vietnamData } from './datasource.ts';
Chart.Inject(LineSeries, Double, Legend, Tooltip, Highlight, SeriesLabel, Category);

let chart: Chart = new Chart({
    primaryXAxis: { valueType: 'Category' }, legendSettings: { visible: true }, primaryYAxis: { title: 'Value' },
    series: [
        {
            type: 'Line',
            dataSource: vietnamData,
            xName: 'x', width: 2, marker: {
                visible: true,
                width: 7,
                height: 7,
                shape: 'Circle',
                isFilled: true
            },
            yName: 'y', name: 'Vietnam',
            labelSettings: { visible: true, text: 'Vietnam', background: '#E8F5E9', border: { width: 2, color: '#2E7D32' }, opacity: 0.9, font: { size: '12px', fontWeight: '600', color: '#2E7D32' }, showOverlapText: true }
        },
        {
            type: 'Line',
            dataSource: indonesiaData,
            xName: 'x', width: 2, marker: {
                visible: true,
                width: 5,
                height: 5,
                shape: 'Rectangle',
                isFilled: true
            },
            yName: 'y', name: 'Indonesia',
            labelSettings: { visible: true, text: 'Indonesia', background: '#E8F5E9', border: { width: 2, color: '#2E7D32' }, opacity: 0.9, font: { size: '12px', fontWeight: '600', color: '#2E7D32' }, showOverlapText: true }
        },
        {
            type: 'Line',
            dataSource: franceData,
            xName: 'x', width: 2, marker: {
                visible: true,
                width: 5,
                height: 5,
                shape: 'Rectangle',
                isFilled: true
            },
            yName: 'y', name: 'France',
            labelSettings: { visible: true, text: 'France', background: '#E8F5E9', border: { width: 2, color: '#2E7D32' }, opacity: 0.9, font: { size: '12px', fontWeight: '600', color: '#2E7D32' }, showOverlapText: true }
        },
        {
            type: 'Line',
            dataSource: polandData,
            xName: 'x', width: 2, marker: {
                visible: true,
                width: 5,
                height: 5,
                shape: 'Rectangle',
                isFilled: true
            },
            yName: 'y', name: 'Poland',
            labelSettings: { visible: true, text: 'Poland', background: '#E8F5E9', border: { width: 2, color: '#2E7D32' }, opacity: 0.9, font: { size: '12px', fontWeight: '600', color: '#2E7D32' }, showOverlapText: true }
        },
        {
            type: 'Line',
            dataSource: mexicoData,
            xName: 'x', width: 2, marker: {
                visible: true,
                width: 5,
                height: 5,
                shape: 'Rectangle',
                isFilled: true
            },
            yName: 'y', name: 'Mexico',
            labelSettings: { visible: true, text: 'Mexico', background: '#E8F5E9', border: { width: 2, color: '#2E7D32' }, opacity: 0.9, font: { size: '12px', fontWeight: '600', color: '#2E7D32' }, showOverlapText: 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>
</head>

<body>
    <div id='loader'>Loading....</div>
    <div id='container'>
        <div id='element'></div>
    </div>
</body>

</html>
export let vietnamData: { x: number; y: number }[] = [
  { x: 2016, y: 7.8 },
  { x: 2017, y: 10.3 },
  { x: 2018, y: 15.5 },
  { x: 2019, y: 17.5 },
  { x: 2020, y: 19.5 },
  { x: 2021, y: 23.0 },
  { x: 2022, y: 20.0 },
  { x: 2023, y: 19.0 },
  { x: 2024, y: 22.1 }
];

export let indonesiaData: { x: number; y: number }[] = [
  { x: 2016, y: 4.8 },
  { x: 2017, y: 5.2 },
  { x: 2018, y: 6.2 },
  { x: 2019, y: 7.8 },
  { x: 2020, y: 9.3 },
  { x: 2021, y: 14.3 },
  { x: 2022, y: 15.6 },
  { x: 2023, y: 16.0 },
  { x: 2024, y: 17.0 }
];

export let franceData: { x: number; y: number }[] = [
  { x: 2016, y: 14.6 },
  { x: 2017, y: 15.5 },
  { x: 2018, y: 15.4 },
  { x: 2019, y: 14.4 },
  { x: 2020, y: 11.6 },
  { x: 2021, y: 13.9 },
  { x: 2022, y: 12.1 },
  { x: 2023, y: 10.0 },
  { x: 2024, y: 10.8 }
];

export let polandData: { x: number; y: number }[] = [
  { x: 2016, y: 8.9 },
  { x: 2017, y: 10.3 },
  { x: 2018, y: 10.8 },
  { x: 2019, y: 9.0 },
  { x: 2020, y: 7.9 },
  { x: 2021, y: 8.5 },
  { x: 2022, y: 7.4 },
  { x: 2023, y: 6.4 },
  { x: 2024, y: 7.1 }
];

export let mexicoData: { x: number; y: number }[] = [
  { x: 2016, y: 19.0 },
  { x: 2017, y: 20.0 },
  { x: 2018, y: 20.2 },
  { x: 2019, y: 18.4 },
  { x: 2020, y: 16.8 },
  { x: 2021, y: 18.5 },
  { x: 2022, y: 18.4 },
  { x: 2023, y: 16.3 },
  { x: 2024, y: 13.7 }
];

Note: To use the series label feature, inject the SeriesLabel module into the services.

See also