Annotations in TypeScript Charts

Chart annotations allow highlighting specific areas of the chart using text, shapes, images, or custom HTML elements. Annotations can be used to emphasize trends, mark thresholds, show custom notes, or display additional information directly inside the chart area.

Annotations are added using the annotations option. Set the content property to reference the element that should be rendered within the chart.

import { Chart, ColumnSeries, Category, ChartAnnotation } from '@syncfusion/ej2-charts';
import { columnData } from './datasource.ts';
Chart.Inject(ColumnSeries, Category, ChartAnnotation);

let chart: Chart = new Chart({
    primaryXAxis: {
        valueType: 'Category',
    },
    annotations:[{
        content: '70 Gold Medals',
        coordinateUnits: 'Point',
        x: 'France',
        y: 50
    }],
    series:[{
        dataSource: columnData,
        xName: 'country', yName: 'gold',
        type: 'Column'
    }],

}, '#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 id='element1'></div>
    </div>
</body>

</html>

Note: To use the annotation feature, inject ChartAnnotation using Chart.Inject(ChartAnnotation) method.

Positioning annotations

Annotations are positioned using the x and y properties. The interpretation of these values depends on the coordinateUnits setting:

  • When coordinateUnits is Pixel: x and y represent pixel offsets from the chart area origin.
  • When coordinateUnits is Point: x and y represent actual data values on the respective axes (use axis names with xAxisName and yAxisName if multiple axes exist).

Region

Annotations can be positioned relative to either the overall Chart area or a specific Series. When placed relative to the chart, the annotation uses the chart’s coordinate system. When placed relative to a series, the annotation aligns with that series’ data points. By default, annotations are placed with respect to the chart.

import { Chart, ColumnSeries, Category, ChartAnnotation } from '@syncfusion/ej2-charts';
import { columnData } from './datasource.ts';
Chart.Inject(ColumnSeries, Category, ChartAnnotation);

let chart: Chart = new Chart({
    primaryXAxis: {
        valueType: 'Category',
    },
    annotations:[{
        content: '<div>Highest Medal Count</div>',
        region: 'Series',
        coordinateUnits: 'Point',
        x: 'France',
        y: 50
    }],
    series:[{
        dataSource: columnData,
        xName: 'country', yName: 'gold',
        type: 'Column'
    }],

}, '#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 id='element1'></div>
    </div>
</body>

</html>

Coordinate units

Use the coordinateUnits property to define how annotation coordinates are interpreted. Choose between:

  • Pixel – The annotation is positioned using fixed pixel values within the chart area (use x and y pixel offsets).
  • Point – The annotation is positioned based on chart data points (use axis values for x and y positioning).
import { Chart, ColumnSeries, Category, ChartAnnotation } from '@syncfusion/ej2-charts';
import { columnData } from './datasource.ts';
Chart.Inject(ColumnSeries, Category, ChartAnnotation);

let chart: Chart = new Chart({
    primaryXAxis: {
        valueType: 'Category',
    },
    annotations:[{
        content: '<div style="border: 1px solid black; padidng: 5px 5px 5px 5px, backgrund:#f5f5f5">Annotation in Pixel</div>',
        coordinateUnits: 'Pixel',
        x: 150,
        y: 50
    }],
    series:[{
        dataSource: columnData,
        xName: 'country', yName: 'gold',
        type: 'Column'
    }],

}, '#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 id='element1'></div>
    </div>
</body>

</html>

Alignment

Annotations support verticalAlignment and horizontalAlignment properties. The verticalAlignment can be set to Top, Bottom, or Middle, and the horizontalAlignment can be set to Near, Far, or Center. Alignment works with both Pixel and Point coordinate units.

import {
    Chart,
    ColumnSeries,
    Category,
    ChartAnnotation
} from '@syncfusion/ej2-charts';

Chart.Inject(ColumnSeries, Category, ChartAnnotation);

let chart: Chart = new Chart({
    primaryXAxis: {
        valueType: 'Category',
        labelPlacement: 'OnTicks',
        title: 'Countries'
    },
    primaryYAxis: {
        minimum: 0,
        maximum: 80,
        interval: 20,
        title: 'Medals'
    },
    annotations: [
        {
            content: 'USA: 50 Gold Medals',
            region: 'Series',
            coordinateUnits: 'Point',
            x: 'USA',
            y: 50,
            horizontalAlignment: 'Near',
            verticalAlignment: 'Top'
        },
        {
            content: 'Japan: 70 Gold Medals',
            region: 'Series',
            coordinateUnits: 'Point',
            x: 'Japan',
            y: 70,
            horizontalAlignment: 'Center',
            verticalAlignment: 'Middle'
        },
        {
            content: 'Germany: 40 Gold Medals',
            region: 'Series',
            coordinateUnits: 'Point',
            x: 'Germany',
            y: 40,
            horizontalAlignment: 'Far',
            verticalAlignment: 'Bottom'
        }
    ],
    series: [
        {
            dataSource: [
                { country: 'USA', gold: 50, silver: 70, bronze: 45 },
                { country: 'China', gold: 40, silver: 60, bronze: 55 },
                { country: 'Japan', gold: 70, silver: 60, bronze: 50 },
                { country: 'Australia', gold: 60, silver: 56, bronze: 40 },
                { country: 'France', gold: 50, silver: 45, bronze: 35 },
                { country: 'Germany', gold: 40, silver: 30, bronze: 22 },
                { country: 'Italy', gold: 40, silver: 35, bronze: 37 },
                { country: 'Sweden', gold: 30, silver: 25, bronze: 27 }
            ],
            xName: 'country',
            yName: 'gold',
            name: 'Gold',
            type: 'Column'
        },
        {
            dataSource: [
                { country: 'USA', gold: 50, silver: 70, bronze: 45 },
                { country: 'China', gold: 40, silver: 60, bronze: 55 },
                { country: 'Japan', gold: 70, silver: 60, bronze: 50 },
                { country: 'Australia', gold: 60, silver: 56, bronze: 40 },
                { country: 'France', gold: 50, silver: 45, bronze: 35 },
                { country: 'Germany', gold: 40, silver: 30, bronze: 22 },
                { country: 'Italy', gold: 40, silver: 35, bronze: 37 },
                { country: 'Sweden', gold: 30, silver: 25, bronze: 27 }
            ],
            xName: 'country',
            yName: 'silver',
            name: 'Silver',
            type: 'Column'
        },
        {
            dataSource: [
                { country: 'USA', gold: 50, silver: 70, bronze: 45 },
                { country: 'China', gold: 40, silver: 60, bronze: 55 },
                { country: 'Japan', gold: 70, silver: 60, bronze: 50 },
                { country: 'Australia', gold: 60, silver: 56, bronze: 40 },
                { country: 'France', gold: 50, silver: 45, bronze: 35 },
                { country: 'Germany', gold: 40, silver: 30, bronze: 22 },
                { country: 'Italy', gold: 40, silver: 35, bronze: 37 },
                { country: 'Sweden', gold: 30, silver: 25, bronze: 27 }
            ],
            xName: 'country',
            yName: 'bronze',
            name: 'Bronze',
            type: 'Column'
        }
    ],
    title: 'Annotation Alignment'
}, '#element');

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 id='element1'></div>
    </div>
</body>

</html>

Adding Y-Axis subtitle using annotations

You can add a subtitle to the chart y-axis by setting an HTML div element in the content property of the annotation. Set the coordinateUnits to Pixel and customize the x and y position values to place the text at your desired location.

import { Chart, ColumnSeries, Category, ChartAnnotation } from '@syncfusion/ej2-charts';
import { columnData } from './datasource.ts';
Chart.Inject(ColumnSeries, Category, ChartAnnotation);

let chart: Chart = new Chart({
    primaryXAxis: {
        valueType: 'Category',
    },
    primaryYAxis: {
        title: '(m2/min)'
    },
    annotations: [{
        content: '<div id="text" style="transform: rotate(-90deg);">Speed Rate</div>',
        x: 6,
        y: 180,
        coordinateUnits: 'Pixel',
        Region: 'Chart'
    }],
    series: [{
        dataSource: columnData,
        xName: 'country', yName: 'gold',
        type: 'Column'
    }],

}, '#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 id='element1'></div>
    </div>
</body>

</html>

See also