Skip to content

Sankey API

Flows between nodes laid out in columns, with band thickness proportional to the flow value (built on d3-sankey) - see the Sankey demo.

Import

ts
import "@michi-vz/wc/sankey-chart";
// <michi-vz-sankey-chart> is now defined
ts
import { mountSankeyChart } from "@michi-vz/core";

const chart = mountSankeyChart(el, props);

Props

PropTypeDefaultDescription
nodes*SankeyNodeItem[]Flow nodes; each needs a unique id that links reference
links*SankeyLinkItem[]Directed flows between nodes (source -> target); band thickness is proportional to value
titlestringOptional chart title rendered above the plot
nodeWidthnumber18Node rect width in px (default 18).
nodePaddingnumber12Vertical gap between nodes in a column in px (default 12).
nodeRadiusnumber2Corner radius of the node rects in px (default 2; 0 = square corners). Clamped to half the node's shorter side.
linkRadiusnumber2Corner radius (px) of the filled flow ribbons where they meet the nodes (default 2; 0 = sharp corners). Clamped to half the band's thickness.
linkColorMode"source" | "target""source"Colour links by their "source" (default) or "target" node.
linkOpacitynumber0.45Link opacity in [0,1] (default 0.45).
showLabelsbooleantrueDraw node labels (default true).
valueFormatter(n: number) => stringFormats a numeric value for labels and tooltips
tooltipFormatter(mark: SankeyNodeContext | SankeyLinkContext) => stringReturns custom tooltip HTML for a hovered datum/mark (sanitized before it is inserted)
onHighlightItem(labels: string[]) => voidCalled when the hovered/highlighted label(s) change
Common props — shared by every chart (14)
PropTypeDefaultDescription
widthnumber800Chart width in pixels
heightnumber500Chart height in pixels
marginMargin{ top: 36, right: 12, bottom: 12, left: 12 }Inner margins (top/right/bottom/left, in px) reserved for axes, titles, and labels
colorsstring[]Categorical palette for series/labels without an explicit colour or colorsMapping entry
colorsMappingRecord<string, string>Explicit label -> colour map; takes precedence over the palette and per-item colours
highlightItemsstring[]Labels to emphasise; all other marks dim
disabledItemsstring[]Labels to hide and exclude from scales/stacks
renderer"svg" | "canvas""svg"Render as inline SVG (default) or to a canvas (faster for large datasets); getContext() is identical either way
localestringBCP-47 locale used for number and date formatting
skipColorMappingDispatchbooleanfalseExternal-CSS mode: unmapped labels resolve to transparent and onColorMappingGenerated is not emitted, so mark colours come from your CSS via the data-label-safe contract
enableTransitionsbooleantrueAnimate updates with CSS transitions (default true)
onColorMappingGenerated(mapping: Record<string, string>) => voidCalled with the resolved label -> colour map after the chart assigns colours
onChartDataProcessed(context: ChartContext) => voidCalled with the renderer-agnostic ChartContext whenever the data is (re)processed
onDataWarning(warnings: DataWarning[]) => voidCalled with any non-fatal data warnings (duplicate labels, non-finite values, gaps, ...)

Data is two arrays

Unlike the other charts, a Sankey takes nodes ({ id, label?, color? }[]) and links ({ source, target, value }[]) instead of a single dataSet.

Events

The web component dispatches these bubbling CustomEvents (the engine exposes the same via the on* callbacks in the table above):

EventDetailFires when
michi-vz:highlightstring[]hover highlight changes (node id, or a link's source + target)
michi-vz:colormappingRecord<string, string>a color mapping is generated
michi-vz:dataprocessedChartContextdata is (re)processed
michi-vz:datawarningDataWarning[]input warnings are detected (e.g. a link to an unknown node)

getContext()

mountSankeyChart(el, props).getContext() returns a renderer-agnostic SankeyChartContext: the nodes (id / label / color / value / column depth), the links (source / target / value / color), summary stats (node & link counts, column count, total flow, largest link, busiest node), a deterministic natural-language summary, and an a11y table listing the flows. See LLM context.

Source

Props are typed as SankeyChartProps in @michi-vz/core.

Free and open source. MIT licensed.