Sankey Chart

Flows between nodes, each as thick as its value: the nodes in columns, the flows in their source's color. Names and colors come from the chart's config; the keyboard moves through the flows, and the tooltip says their values.

ChartsExperimental

Installation

It has its own entry point, @semantic-components/ui/charts/sankey, built on d3-scale d3-shape d3-sankey — an optional peer of ui: install it where you use the component.

ts
import { ScChartSankey } from '@semantic-components/ui/charts/sankey';

Usage

import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import {  ScChart,  type ScChartConfig,  ScChartPlot,  ScChartTooltip,  ScChartTooltipIndicator,  ScChartTooltipItem,  ScChartTooltipLabel,  ScChartTooltipName,  ScChartTooltipPortal,  ScChartTooltipProvider,  ScChartTooltipValue,} from '@semantic-components/ui/charts';import { ScChartSankey } from '@semantic-components/ui/charts/sankey';
@Component({  selector: 'app-sankey-chart-usage-demo',  imports: [    ScChart,    ScChartPlot,    ScChartSankey,    ScChartTooltip,    ScChartTooltipPortal,    ScChartTooltipProvider,    DecimalPipe,    ScChartTooltipLabel,    ScChartTooltipIndicator,    ScChartTooltipItem,    ScChartTooltipName,    ScChartTooltipValue,  ],  template: `    <div scChart class="aspect-video max-h-80" [config]="config" [data]="data">      <svg        scChartPlot        categoryKey="flow"        aria-label="Where visitors come from and where they go"      >        <g scChartSankey key="visitors" />      </svg>      <div scChartTooltipProvider>        <ng-template scChartTooltipPortal>          <div scChartTooltip #tooltip="scChartTooltip">            <div scChartTooltipLabel>{{ tooltip.label() }}</div>            @for (item of tooltip.payload(); track item.dataKey) {              <div scChartTooltipItem [item]="item">                <span scChartTooltipIndicator></span>                <span scChartTooltipName>{{ item.name }}</span>                <span scChartTooltipValue>{{ item.value | number }}</span>              </div>            }          </div>        </ng-template>      </div>    </div>  `,  host: { class: 'block w-full max-w-xl' },  encapsulation: ViewEncapsulation.None,})export class SankeyChartUsageDemo {  private readonly data = [    {      flow: 'Search → Pricing',      source: 'search',      target: 'pricing',      visitors: 420,    },    { flow: 'Search → Docs', source: 'search', target: 'docs', visitors: 310 },    {      flow: 'Social → Pricing',      source: 'social',      target: 'pricing',      visitors: 180,    },    { flow: 'Social → Docs', source: 'social', target: 'docs', visitors: 90 },    {      flow: 'Direct → Pricing',      source: 'direct',      target: 'pricing',      visitors: 150,    },    {      flow: 'Pricing → Sign up',      source: 'pricing',      target: 'signup',      visitors: 380,    },    {      flow: 'Pricing → Left',      source: 'pricing',      target: 'left',      visitors: 370,    },    { flow: 'Docs → Sign up', source: 'docs', target: 'signup', visitors: 160 },    { flow: 'Docs → Left', source: 'docs', target: 'left', visitors: 240 },  ];  private readonly config: ScChartConfig = {    visitors: { label: 'Visitors' },    search: { label: 'Search', color: 'var(--chart-1)' },    social: { label: 'Social', color: 'var(--chart-2)' },    direct: { label: 'Direct', color: 'var(--chart-3)' },    pricing: { label: 'Pricing', color: 'var(--chart-4)' },    docs: { label: 'Docs', color: 'var(--chart-5)' },    signup: { label: 'Sign up', color: 'var(--chart-1)' },    left: { label: 'Left', color: 'var(--chart-3)' },  };}

Anatomy

A chart (its rows and config), its plot with the series, and the tooltip and legend you write from their state.

import { ScChart, ScChartPlot, ScChartTooltip, ScChartTooltipIndicator, ScChartTooltipItem, ScChartTooltipName, ScChartTooltipPortal, ScChartTooltipProvider, ScChartTooltipValue, ScChartLegend, ScChartLegendIndicator, ScChartLegendItem } from '@semantic-components/ui/charts';import { ScChartSankey } from '@semantic-components/ui/charts/sankey';
<div scChart [config]="…" [data]="…">  <svg scChartPlot categoryKey="…" aria-label="…">    <g scChartSankey key="…" />  </svg>  <div scChartTooltipProvider>    <ng-template scChartTooltipPortal>      <div scChartTooltip #tooltip="scChartTooltip">        <div scChartTooltipItem [item]="…">          <span scChartTooltipIndicator></span>          <span scChartTooltipName>…</span>          <span scChartTooltipValue>…</span>        </div>      </div>    </ng-template>  </div>  <div scChartLegend #legend="scChartLegend">    <div scChartLegendItem [item]="…">      <span scChartLegendIndicator></span>    </div>  </div></div>

Accessibility

  • The plot is one tab stop, a group named by its aria-label: the arrow keys, Home and End move through its flows; Escape closes the tooltip.
  • As the keyboard moves, screen readers hear what the tooltip says, through the CDK's LiveAnnouncer (politely).
  • Right to left, with the app following the direction (charts.followDirection), it is mirrored — flowing from the right — and ← goes to the next.
  • It draws whole at once: Recharts' has no animation.

Examples

RTL

البحث الأسعار التوثيق التواصل الاجتماعي مباشر التسجيل غادر
البحث
الأسعار
التوثيق
التواصل الاجتماعي
مباشر
التسجيل
غادر
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import {  ScChart,  type ScChartConfig,  ScChartLegend,  ScChartLegendIndicator,  ScChartLegendItem,  ScChartPlot,  ScChartTooltip,

API reference

The series' inputs. The chart, its plot, axes, tooltip and legend are on the Chart page.

InputTypeDefaultDescription
keystring (required)— each flow's value
sourceKeystring'source' each flow's source node
targetKeystring'target' each flow's target node
nodeWidthnumber, in px12
nodePaddingnumber16 the space between nodes in a column, in px
classstring'' Merged over its own classes.