ToolTip in EJ2 TypeScript Sankey Chart component

15 Mar 202616 minutes to read

The Sankey Chart provides tooltips that surface contextual details for hovered elements without cluttering the diagram. Tooltips display additional information when users hover over nodes or links in the Sankey Chart. You can enable and customize tooltips using the tooltip property and by injecting the SankeyTooltip module.

This guide outlines how to enable and customize tooltips in the EJ2 TypeScript Sankey Chart.

Tooltip Settings Properties

The following table lists the main tooltip configuration properties:

Property Type Default Description
enable boolean true Enables or disables the tooltip display.
fill string null Background fill color of the tooltip.
opacity number 0.75 Opacity of the tooltip container (0 to 1).
textStyle object null Text styling for the tooltip content.
nodeFormat string ‘$name : $value’ Format string for node tooltips.
linkFormat string ‘$start.name $start.value → $target.name $target.value’ Format string for link tooltips.
enableAnimation boolean true Toggles tooltip animation.
duration number 300 Animation duration in milliseconds.
fadeOutDuration number 1000 Fade-out duration in milliseconds.
fadeOutMode string ‘Move’ Fade-out animation mode (‘Move’, ‘Fade’, ‘Delay’).

Basic Tooltip Configuration

Enable tooltips with default formatting:

import { Sankey, SankeyNodeModel, SankeyLinkModel, SankeyExport, SankeyTooltip, SankeyLegend } from '@syncfusion/ej2-charts';

Sankey.Inject(SankeyTooltip, SankeyLegend, SankeyExport);

const nodes: SankeyNodeModel[] = [
  { id: 'Agricultural Waste' },
  { id: 'Biomass Residues' },
  { id: 'Bio-conversion' },
  { id: 'Liquid Biofuel' },
  { id: 'Electricity' },
  { id: 'Heat' }
];

const links: SankeyLinkModel[] = [
  { sourceId: 'Agricultural Waste', targetId: 'Bio-conversion', value: 84.152 },
  { sourceId: 'Biomass Residues', targetId: 'Bio-conversion', value: 24.152 },
  { sourceId: 'Bio-conversion', targetId: 'Liquid Biofuel', value: 10.597 },
  { sourceId: 'Bio-conversion', targetId: 'Electricity', value: 36.862 },
  { sourceId: 'Bio-conversion', targetId: 'Heat', value: 60.845 }
];

const sankey: Sankey = new Sankey(
  {
     width:  '90%',
    height: '450px',
    tooltip: { enable: true },
    nodes: nodes,
    links: links,
    legendSettings: { visible: true }
  },
  '#sankey-container'
);
<!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='sankey-container'></div>
    </div>
</body>

</html>

Customizing Tooltip Appearance

Adjust tooltip appearance and behavior using tooltip configuration properties:

  • Enable: Shows or hides tooltips.
  • Fill: Sets background color.
  • Opacity: Controls transparency (0 to 1). The default value is 0.75.
  • TextStyle: Configures font size, family, weight, and color for the tooltip text.
  • EnableAnimation: Toggles animation. Default: true.
  • Duration: Animation duration in milliseconds. The default value is 300.
  • FadeOutDuration: Fade-out duration in milliseconds. The default value is 1000.

Example customization:

import { Sankey, SankeyNodeModel, SankeyLinkModel, SankeyExport, SankeyTooltip, SankeyLegend } from '@syncfusion/ej2-charts';

Sankey.Inject(SankeyTooltip, SankeyLegend, SankeyExport);

const nodes: SankeyNodeModel[] = [
  { id: 'Agricultural Waste' },
  { id: 'Biomass Residues' },
  { id: 'Bio-conversion' },
  { id: 'Liquid Biofuel' },
  { id: 'Electricity' },
  { id: 'Heat' }
];

const links: SankeyLinkModel[] = [
  { sourceId: 'Agricultural Waste', targetId: 'Bio-conversion', value: 84.152 },
  { sourceId: 'Biomass Residues', targetId: 'Bio-conversion', value: 24.152 },
  { sourceId: 'Bio-conversion', targetId: 'Liquid Biofuel', value: 10.597 },
  { sourceId: 'Bio-conversion', targetId: 'Electricity', value: 36.862 },
  { sourceId: 'Bio-conversion', targetId: 'Heat', value: 60.845 }
];

const sankey: Sankey = new Sankey(
  {
     width:  '90%',
    height: '450px',
    tooltip: {
      enable: true,
      textStyle: {
        fontFamily: 'Arial',
        fontStyle: 'normal',
        fontWeight: '500',
        fontSize: '14px',
        color: '#000'
      },
      fill: '#F3F3F3'
    },
    nodes: nodes,
    links: links,
    legendSettings: { visible: true }
  },
  '#sankey-container'
);
<!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='sankey-container'></div>
    </div>
</body>

</html>

Format Strings

Format strings provide a simple way to customize tooltip content without requiring custom templates.

Node Tooltip Format

Format string placeholders for node tooltips:

  • $name - Node name/label
  • $value - Node value (sum of incoming links)

Format string placeholders for link tooltips:

  • $start.name - Source node name
  • $start.value - Source node value
  • $target.name - Target node name
  • $target.value - Target node value
  • $value - Link value

Example with custom format strings:

import { Sankey, SankeyNodeModel, SankeyLinkModel, SankeyTooltip, SankeyLegend, SankeyExport } from '@syncfusion/ej2-charts';

Sankey.Inject(SankeyTooltip, SankeyLegend, SankeyExport);

const nodes: SankeyNodeModel[] = [
  { id: 'Agricultural Waste' },
  { id: 'Biomass Residues' },
  { id: 'Bio-conversion' },
  { id: 'Liquid Biofuel' },
  { id: 'Electricity' },
  { id: 'Heat' }
];

const links: SankeyLinkModel[] = [
  { sourceId: 'Agricultural Waste', targetId: 'Bio-conversion', value: 84.152 },
  { sourceId: 'Biomass Residues', targetId: 'Bio-conversion', value: 24.152 },
  { sourceId: 'Bio-conversion', targetId: 'Liquid Biofuel', value: 10.597 },
  { sourceId: 'Bio-conversion', targetId: 'Electricity', value: 36.862 },
  { sourceId: 'Bio-conversion', targetId: 'Heat', value: 60.845 }
];

const sankey: Sankey = new Sankey(
  {
     width:  '90%',
    height: '450px',
    tooltip: {
      enable: true,
      nodeTemplate: '${name}: ${value} TBtu',
      linkTemplate: '${start.name}: ${start.out} TBtu → ${target.name}: ${target.in} TBtu'
    },
    nodes: nodes,
    links: links,
    legendSettings: { visible: true }
  },
  '#sankey-container'
);
<!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='sankey-container'></div>
    </div>
</body>

</html>

Advanced Tooltip Configuration

Tooltip Rendering Event

Use the tooltipRendering event to customize tooltip content dynamically and enable custom logic based on specific conditions:

import { Sankey, SankeyNodeModel, SankeyLinkModel, SankeyTooltip, SankeyLegend, SankeyExport, SankeyTooltipRenderEventArgs } from '@syncfusion/ej2-charts';

Sankey.Inject(SankeyTooltip, SankeyLegend, SankeyExport);

const nodes: SankeyNodeModel[] = [
  { id: 'Agricultural Waste' },
  { id: 'Biomass Residues' },
  { id: 'Bio-conversion' },
  { id: 'Liquid Biofuel' },
  { id: 'Electricity' },
  { id: 'Heat' }
];

const links: SankeyLinkModel[] = [
  { sourceId: 'Agricultural Waste', targetId: 'Bio-conversion', value: 84.152 },
  { sourceId: 'Biomass Residues', targetId: 'Bio-conversion', value: 24.152 },
  { sourceId: 'Bio-conversion', targetId: 'Liquid Biofuel', value: 10.597 },
  { sourceId: 'Bio-conversion', targetId: 'Electricity', value: 36.862 },
  { sourceId: 'Bio-conversion', targetId: 'Heat', value: 60.845 }
];

const onTooltipRendering = (args: SankeyTooltipRenderEventArgs) => {
  if (args.text === 'link') {
    args.text = `Flow: ${args.link.sourceId}${args.link.targetId} (${args.link.value})`;
  } else if (args.text) {
    args.text = `Node: ${args.text}`;
  }
};

const sankey: Sankey = new Sankey(
  {
    width: '90%',
    height: '450px',
    nodes: nodes,
    links: links,
    tooltip: { enable: true },
    tooltipRendering: onTooltipRendering,
    legendSettings: { visible: true }
  },
  '#sankey-container'
);
<!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='sankey-container'></div>
    </div>
</body>

</html>

Disabling Tooltips

To disable tooltips, set the enable property to false:

const tooltip = { enable: false };

Key Considerations

  • Keep Text Concise: Keep tooltip text concise and readable.
  • Ensure Contrast: Ensure sufficient contrast for tooltip text and background.
  • Use Format Strings: Prefer nodeFormat and linkFormat for simple content customization without requiring custom rendering logic.
  • Animation Tuning: Tune animation durations to balance responsiveness and polish.
  • Performance: Use format strings instead of complex rendering logic for better performance.
  • Relevant Information: Show only relevant and helpful information.
  • Consistent Styling: Maintain consistent tooltip styling across your application.