Search documentation

Search pages, components, blocks, styles and colours.

Get started
Skip to content

Sankey Chart

PreviousNext

Flow volumes between stages, from sources to destinations, as ribbons as thick as each flow.

Revenue flow
Acme Inc, Q2: channel to product to region
Revenue by channel, product and region, Q2
FlowShare of source
Direct → Platform$420K64.6%
Direct → Analytics$160K24.6%
Direct → Support$70K10.8%
Partners → Platform$210K70%
Partners → Analytics$90K30%
Marketplace → Platform$120K75%
Marketplace → Support$40K25%
Platform → Americas$390K52%
Platform → EMEA$250K33.3%
Platform → APAC$110K14.7%
Analytics → Americas$140K56%
Analytics → EMEA$110K44%
Support → Americas$60K54.5%
Support → APAC$50K45.5%

Give it nodes and the links that flow between them. Nodes stand in columns by their longest path from a source, with every node that nothing leaves in the last column. Each node is a 10px bar as tall as the larger of what flows into it and out of it, and each link is a ribbon as thick as its value, washed in the colour of the node it leaves. The layout moves nodes towards the nodes they connect to, so links run as straight as they can, and stacks each node's links in the order of their other ends, so they don't cross where they needn't. Labels sit beside each node, before it in the last column, with the node's total when there is room.

Point at a node: it, every link touching it and the nodes at their other ends stay lit while the rest dims. Point at a link: it and the two nodes it joins stay lit. A tooltip gives a node's totals in and out, or a link's flow and its share of what leaves its source. Tab to the chart and use the arrow keys to step through the links. Links extend from their sources to their targets the first time the diagram is on screen, and nodes and links move to their new sizes when the data changes.

Sankey Chart is part of the Fabricator style. It's drawn in SVG, so the same component ships for every base, and its layout comes from the same model as the React Native version.

Installation

bunx --bun fabricator-ui@latest add sankey-chart

Usage

import { SankeyChart } from "@/components/ui/sankey-chart"
const nodes = [
  { id: "direct", label: "Direct" },
  { id: "partners", label: "Partners" },
  { id: "platform", label: "Platform" },
  { id: "support", label: "Support" },
]
 
const links = [
  { source: "direct", target: "platform", value: 420_000 },
  { source: "direct", target: "support", value: 70_000 },
  { source: "partners", target: "platform", value: 210_000 },
]
 
<SankeyChart nodes={nodes} links={links} label="Revenue by channel and product" />

Each node has an id that links name as source and target, and an optional label and color. Links with a zero, negative or missing value are left out, and so are nodes that no link touches. Links can't loop back: a cycle, a link to an id that isn't in nodes or two nodes with one id throw a SankeyLayoutError, whose code is cycle, unknown-node or duplicate-node and whose nodes names the ids involved. The chart fills its container's width and is 18rem tall until you give it a height with className.

Examples

Budget

One source split over two steps. Each department's links take its colour, so its spending reads as one stream.

Annual budget by department and cost
FlowShare of source
Budget → Engineering$2.4M52.2%
Budget → Go to market$1.5M32.6%
Budget → Operations$700K15.2%
Engineering → Salaries$1.9M79.2%
Engineering → Infrastructure$500K20.8%
Go to market → Salaries$800K53.3%
Go to market → Campaigns$700K46.7%
Operations → Salaries$400K57.1%
Operations → Office$300K42.9%

Colours

Nodes take the chart palette in the order of nodes, and links take the colour of the node they leave, as a light wash so overlapping links stay readable. Give a node its own with color: a palette slot, an upstream chart token (chart-1 … chart-5), a role (primary, info, success, …), a CSS variable (var(--brand)) or any CSS colour.

TokenHueTokenHue
--chart-series-1blue--chart-series-5magenta
--chart-series-2orange--chart-series-6green
--chart-series-3aqua--chart-series-7violet
--chart-series-4amber--chart-series-8red

Slots never repeat: a ninth node is grey. Nodes in the last column colour no link, so a neutral such as muted-foreground suits them and leaves the palette to the nodes that flow. Labels stay in the text colours over the washes.

Motion

  • Extend: the first time the diagram scrolls into view, the links extend from their sources to their targets, left to right, on the reveal role, once per mount. Labels fade in column by column.
  • Morph: when nodes, links or the visible nodes change, each node and link moves and resizes on the move role, matched by its id. A new one grows from its middle; one that leaves thins into it.
  • Highlight: the lit links deepen on the glide role and everything else dims.

All three follow the motion speed of the region they sit in. Reduce Motion and the instant speed draw the diagram at once and keep only the fades; animate={false} does the same for one chart.

API Reference

SankeyChart

All other props go to the root <div>.

PropTypeDefault
nodes{ id: string; label?: string; color?: ChartColor }[]required
links{ source: string; target: string; value: number }[]required
labelstringrequired: the accessible name
formatValue(value: number, key: string) => string1,234
legendbooleanfalse
tooltipbooleantrue
animatebooleantrue
loadingbooleanfalse
emptyReactNode"No data"
onActiveChange(active: ChartActive<TNode | TLink> | null) => void-

formatValue receives "value" as its key. onActiveChange reports the inspected node or link as datum, its place in nodes or links as index, and its id as key (a link's is source→target). Nodes are named on the chart, so the legend is off unless you ask for it; when on, it hides a node and its links when pressed and spotlights it when pointed at.

Data attributes

AttributeElementPresent when
data-slot="sankey-chart"rootalways
data-loadingrootloading is true
data-slot="chart-plot"the plotalways
data-slot="sankey-chart-link"a linkalways; data-dimmed while another mark is lit
data-slot="sankey-chart-node"a nodealways; data-dimmed while another mark is lit
data-slot="sankey-chart-label"its labelthe name fits
data-slot="chart-tooltip"the tooltipa node or link is inspected

Accessibility

  • The plot is a slider named after label ("Revenue by channel and product: inspect values"). Tab to it, then the arrow keys step through the links in reading order, column by column and top to bottom, Home and End jump to the first and last, and Escape lets go. Its value text reads the tooltip: the link's ends, flow and share of its source.
  • Every link is also in a table hidden from sight, with its flow and share.
  • Every node is named on the chart, so no node is told apart by colour alone.
  • Text never takes a node's colour: names and totals stay in the text colours.
  • loading sets aria-busy on the root and dims the last drawing until new data arrives.