Skip to content

Usage

Sankey data points define directed flows between named nodes.

{
from: 'Coal',
to: 'Electricity',
flow: 42
}
OptionDescription
alphaOpacity applied to colorFrom and colorTo when rendering a flow. Not scriptable. Defaults to 0.5.
backFlowsHow the flows that return to an earlier column (or to their own node) are drawn: { bundle, side }. See Back flow options. Not scriptable.
colorFromScriptable color for the source node side.
colorModeFlow coloring mode: gradient, from, or to.
colorToScriptable color for the target node side.
columnOptional column assignment by node key.
flowColorScriptable flow color. When set, it overrides colorMode for flows without changing node colors.
flowLabelsOptional flow-value labels with scriptable styling and placement. See Flow Labels.
hoverColorFromScriptable hover color for the source node side. Defaults to colorFrom saturated and darkened.
hoverColorToScriptable hover color for the target node side. Defaults to colorTo saturated and darkened.
labelsOptional display labels by node key.
nodeAlignWhere the nodes of a column sit on the cross axis: auto (default, moved to their flows), start or end (packed at that edge with the requested gaps), or edges (the outermost at the two edges). See Node Align.
nodeLabelsNode label styling and placement. Positions are auto, left, top, right, bottom, or center. See Node Labels.
nodeMinSizeMinimum drawn node bar size, in CSS pixels. Resolved per node; see Node Min Size. Defaults to 0 (no minimum).
nodePaddingGap between nodes within a column, in CSS pixels. Rendered exactly, as long as the gaps leave room for the flows (they may take up to half of the axis; beyond that they all shrink by the same factor). Resolved per node; see Node Padding.
nodePaddingModeDeprecated, removed in 1.0: auto (default) or even, with an effect only with nodeAlign: ‘auto’. See Node Padding Mode.
nodeWidthNode rectangle width in pixels.
orientationFlow direction: horizontal (left to right, the default) or vertical (top to bottom).
priorityOptional ordering priority by node key.
sizeNode size strategy: min or max.

hoverColorFrom and hoverColorTo are never simply unset: leaving them out doesn’t turn off the hover effect, it falls back to a computed color — colorFrom/colorTo run through Chart.js’s own getHoverColor helper (saturated by 0.5, darkened by 0.1). To get no color change on hover, set hoverColorFrom/hoverColorTo explicitly to the same value as colorFrom/colorTo.

Use nodeLabels to place and style labels globally or by node. Position names follow Chart.js layout positions, with auto preserving Sankey’s automatic left/right placement.

nodeLabels: {
position: {
Coal: 'right',
Electricity: 'center',
Energy: 'left'
},
color: (node) => node.key === 'Electricity' ? 'white' : 'black',
backgroundColor: {
Electricity: 'blue'
},
borderRadius: 3,
padding: 4,
font: {
size: 11,
weight: 'normal'
}
}

nodePadding sets the gap between nodes stacked in the same column, in CSS pixels. The layout is done in pixels once the chart area is known, so the gap is exact: the column that determines the scale (the one with the most to fit) renders exactly the requested gap, and other columns never get a smaller one. With nodeAlign: 'auto' (the default) a column with room to spare may show a larger gap where aligning a node with its flows asks for it; with 'start' and 'end' every gap is the requested one, except where a flow that skips columns is held level (see Node Align). If the gaps alone would take more than half of the axis, every gap is shrunk by the same factor so that the flows stay visible. Like nodeLabels, it resolves per node, in three forms:

  • A plain number applies the same gap everywhere: nodePadding: 20.
  • A { before, after } object sets an asymmetric gap: more space above the node than below it, or vice versa. before is the gap above the node and after is the gap below it, regardless of orientation (in a vertical chart, “above”/“below” become “left”/“right” visually, but before/after keep the same meaning). Either side left out falls back to 10.
  • A function receiving the node and returning either form, for full control: nodePadding: (node) => node.key === 'Coal' ? { after: 28 } : 4.

A top-level object is always a Record of node keys (like nodeLabels), never a { before, after } gap directly — that shape would be ambiguous with a keyed Record. Write a function for a global asymmetric gap instead:

// per-node gaps, keyed by node
nodePadding: {
Coal: { after: 28 },
Solar: 4
}
// a global asymmetric gap: more space below every node than above it
nodePadding: () => ({ before: 4, after: 8 })

Gaps collapse like CSS margins instead of summing: the space between two adjacent nodes in a column is max(prevNode.after, nextNode.before), not their sum. A plain nodePadding: 10 is therefore equivalent to every node using { before: 10, after: 10 } — max(10, 10) is still 10.

Deprecated. nodePaddingMode keeps working until 1.0, where it is removed without a replacement. It has an effect only with nodeAlign: 'auto'. nodeAlign 'start' and 'end' give exactly the requested gaps, except where a flow that skips columns is held level.

With nodeAlign: 'auto' and by default (nodePaddingMode: 'auto'), each column is packed with the requested gaps and then every node is moved as close as it can get to where its flows meet the nodes at the other end, without changing the order and without bringing two nodes closer than the gap. Two nodes in the same column can therefore end up with different gaps between them. Setting nodePaddingMode: 'even' instead moves a column as one block with exactly the requested gap between each pair: regular gaps, but the nodes are not aligned individually with their flows, so those flows bend more to reach them. Note that size: 'min' can make a node’s drawn bar smaller than the flows it carries, so flows can still overlap visually in either mode.

See the Node Padding sample for a single wide gap separating groups within a column, and for auto vs. even side by side.

nodeAlign decides where the nodes of a column sit on the cross axis: top to bottom in a horizontal chart, left to right in a vertical one. It does not move a node to another column; that is what column and modeX do (nodeAlign in d3-sankey means that other thing).

  • 'auto' (the default): every column is packed with the requested gaps and then every node is moved as close as it can get to where its flows meet the nodes at the other end, so the flows run as straight as they can, and a gap can grow wider than nodePadding. A drawing that does not fill the axis is centred on it.
  • 'start': every column is packed at the start (the top, or the left) with exactly the requested gaps, and a drawing that does not fill the axis starts at the start edge. The nodes are not moved towards their flows, so the flows bend more: on a dense chart much more.
  • 'end': the same, packed at the end (the bottom, or the right).
  • 'edges': the first and the last node of every column touch the two edges, and the nodes between them are placed as with 'auto', never closer to a neighbour than the gap. When a flow that skips the column is the outermost, the flow runs along the edge and the first node comes after it. A node alone in its column goes to the edge nearer to where its flows meet it. A drawing that does not fill the axis is centred on it. Note that modeX: 'edge' is a different thing on the other axis.

Any other value is drawn as 'auto'. A flow that skips columns is held level across them with every value, so a column it passes can sit off its edge or have one gap wider than requested; with nodeAlign: 'auto' and nodePaddingMode: 'even' it is not held level. The lanes of back flows stay next to the content: with 'start' and lanes before the content, a column under a lane starts after it and a column beside the lanes starts at the edge. When the minimum sizes of a column take more than the axis, what does not fit is past the end edge with 'start', before the start edge with 'end', and on both sides with 'auto' and 'edges'.

datasets: [{
data,
nodeAlign: 'start'
}]

See the Node Align sample for the four values side by side.

nodeMinSize gives a node’s drawn bar a minimum size in CSS pixels, so a node carrying a tiny flow next to a huge one doesn’t disappear into a sub-pixel line. It resolves per node, in the same three forms as nodePadding: a plain number (nodeMinSize: 6), a Record keyed by node key, or a function receiving the node. Defaults to 0, meaning no minimum.

Only the drawn bar is stretched — the underlying data isn’t. When a node’s natural size (in flow units, converted to pixels) is smaller than nodeMinSize, its bar grows to nodeMinSize symmetrically around the node’s real position: half the growth extends above/left, half below/right. The node’s label follows the same stretched rectangle. The flows attached to the node keep their exact proportional height and still connect at the same points they always did — stretching the bar doesn’t move or resize any flow, so a bar that grew to be visible can still have flows that are their own hairline thickness inside it.

Space for the stretch is reserved during layout, so a stretched bar does not overlap a neighboring node — the gap between two stretched bars in the same column still respects the requested nodePadding, with every nodeAlign and nodePaddingMode.

nodeMinSize makes the node visible, not the flow. A flow of 0.37 arriving at a 6px node bar is still a hairline inside that bar — if the flow itself needs to be visible, the data needs a minimum applied before charting (e.g. clamped upstream), and that’s usually worth calling out in a label rather than silently distorting the chart.

See the Node Min Size sample for a huge node next to several tiny ones, with and without a minimum.

A flow whose target is in the same column as its source or in an earlier one is a back flow, and so is a flow from a node to itself. Back flows are drawn around the content instead of between the columns: the band leaves the source, runs along a lane after the last node on the cross axis (the bottom of a horizontal chart, the right side of a vertical one), and enters the target from behind. By default the back flows between the same two nodes share a lane after the content; the backFlows dataset option changes that (see Back flow options).

  • Which flows are back flows is decided from the data, not from its order: the flows between the same two nodes are taken together, from the largest total to the smallest, and a flow that would close a cycle with the flows before it is a back flow (so two flows of 4 and 3 from A to B win over one flow of 5 from B to A). A flow from a node to itself is always one. Use column to pin nodes where you want them; a flow that closes a cycle runs forward like any other when the pinned columns put its target after its source.
  • The lane of a loop is chosen by its length. Short loops go innermost so nested loops do not cross, and loops with a whole column between them share a lane. Loops that only partly overlap have to cross each other.
  • Back flows take part in the order of the nodes. The order within a column is chosen to keep flows from crossing, and a band that would pass between a node and the lane its back flow runs along counts as a crossing, so nodes with a back flow end up next to the lane and the flows that skip columns pass on the other side. A back flow still does not pull its two nodes together.
  • A loop takes room only in the columns it runs under, from its target column to its source column. Loops that do not overlap share a level, so a short loop at one end of the chart does not cost the columns at the other end anything. A lane starts right after the last node or waypoint of the columns it passes, and the loops it sits on.
  • The lanes, the turns and the nodes are laid out together. The lanes take their thickness and the gaps between them off the cross axis of the columns they pass, and the turns take room beside the columns, so the bands are drawn thinner than the axis alone would allow when the chart is short along the columns. When there is not enough room, the gaps, the radii of the turns and the least thickness of a band (1 pixel) shrink together. Every back flow is routed; none is left as a curve between the nodes. With very many loops the bands become hairlines.
  • Labels positioned outside the first or last column (left/right, or top/bottom when vertical) are kept clear of the turns.

backFlows is a dataset option with two settings, both optional. The defaults draw what is described above.

  • backFlows.bundle (default 'pair'): which back flows share a lane. 'pair' puts the back flows between the same two nodes in one lane, 'target' all that return to one node, 'source' all that leave one node, and false gives every back flow a lane of its own. In a shared lane the shortest flow joins first and runs nearest to the content, and the others run outside it. The flows of a lane that share a node turn around each other at it, the shorter one inside, so their turns do not overlap. A flow from a node to itself keeps its own lane, and forward flows are not affected.
  • backFlows.side (default 'end'): the side of the cross axis the lanes run along. 'end' is after the content (the bottom of a horizontal chart, the right side of a vertical one), 'start' is before it (the top, the left side). 'auto' chooses for each lane the side nearer to where its flows begin and end, weighted by flow, and the order of the nodes takes the choice into account, so a loop whose nodes are in the upper half goes along the top. A flow between two nodes of the same column goes round the side of its target in 'auto'.
datasets: [{
data,
backFlows: { bundle: 'target', side: 'auto' }
}]

Changing backFlows and calling chart.update() lays the chart out again, with an animation if animations are on. When a chart is updated, for this or any other reason, a forward flow that starts or stops skipping a column glides to its new shape, but a flow that becomes a back flow, or stops being one, has no shape in between: it changes to its new shape halfway through the animation.

With side: 'start' the order of the nodes does not change to suit the lane: a flow between two nodes pinned to one column with column can run over the node between its ends. Use 'auto' there.

See the Circular Flows sample, where both options can be changed.

A flow is found at the mouse where it is drawn, and the hit test works out the answer from the route of the flow, so it follows curves, runs across skipped columns and the lanes of back flows exactly. A flow thinner than 3 pixels (most flows of a large chart are thinner than 1 pixel) is hit within a pixel and a half of its center line, so that it can be pointed at; a thicker flow is hit only on its band.

The default interaction of a sankey chart is { mode: 'sankey', intersect: true }. The 'sankey' mode registered by the package gives the flow whose band is nearest to the mouse, so that the wider area of a thin flow never takes a point that is on a thick flow beside it. Where flows are painted over each other, the one whose center is nearest wins, as in nearest. Set interaction.mode to another mode, 'nearest' for example, to use Chart.js’s own, which compares the centers of the flows and gives the wider areas as they are. Without intersect the 'sankey' mode is 'nearest'.

When chart.update() moves or resizes nodes, for example after a change of nodeAlign or nodePadding, the bars of the nodes, their labels and the ends of their flows move together over the same animation. A label is drawn next to its node in every frame, on the side that belongs to the final place of the node. An update that comes while the nodes are moving goes on from where they are.

Nothing moves on a first render (the flows are revealed column by column), on an update with the mode 'none' or with animations off, or on a change of orientation: the nodes are at their new places at once, also when a movement was running. A resize puts the nodes at their new places at once when the chart is at rest; a resize during a movement does not stop it, and the nodes and their flows go on together to the new places in the time that was left. A node that an update adds is at its place at once, and one that it removes is gone at once.

The nodes are Chart.js elements, one per node in the order of the nodes, in chart.getDatasetMeta(index).nodes; the element of a node stays the same across updates as long as its key does.

When the data is replaced, for example by a new array with a row inserted, removed or moved, a flow is matched by its source, its target and its place among the rows with the same source and target, in data order. A flow that is matched moves from where it was, attached to its two nodes, except a flow that becomes a back flow or stops being one: it changes shape halfway and does not follow its nodes while they move. The flow of a new row is revealed as on a first render, and the flow of a removed row is gone at once. chart.getDatasetMeta(index).data[i] is always the flow of row i. Rows with the same source and target are told apart only by their order: when they are reordered among themselves, their flows swap places instead of moving.

A scriptable animation option (animation.delay, animations.numbers.duration, …) is called for every node as well as for every flow. For a node the context has type: 'node' and the node in context.node, and no data index or row, so a function that reads the row must check context.type first:

animations: {
numbers: {
delay: (context) => {
if (context.type === 'node') return context.node.x * 100 // by column
if (context.type === 'data') return context.dataIndex * 10
return 0
}
}
}

The context of a node holds type: 'node', node (the SankeyNode: its key, its column in x, its in, out and size), and from the dataset chart, dataset, datasetIndex, mode and active; its index is that of the dataset. It has no dataIndex, parsed or raw. The package exports its type, SankeyNodeScriptableContext, with SankeyNode. Chart.js types the context of an animation function as its own, whose type is any string, so in TypeScript the check on context.type does not narrow it: cast after the check.

import type { ScriptableContext } from 'chart.js'
import type { SankeyNodeScriptableContext } from 'chartjs-chart-sankey'
const delay = (context: ScriptableContext<'sankey'>) => {
if (context.type === 'node') {
const { node } = context as unknown as SankeyNodeScriptableContext
return (node.x ?? 0) * 100
}
return 0
}

Flow labels use the same visual options and display the numeric flow value. Since flows are Chart.js elements, these options use the standard scriptable context.

flowLabels: {
display: (context) => context.raw.flow >= 10,
position: 'center',
color: 'black',
backgroundColor: 'white',
borderRadius: 3,
padding: 4,
font: {
size: 11,
weight: 'normal'
}
}

Which flows run backwards, the column of every node, the order of the nodes within a column and the order of the flows at each node are decided from the data alone. In a cyclic chart the flows between the same two nodes are visited together, from the largest total to the smallest, and the one that would close a cycle is the back flow, so the biggest flows keep running forwards and the order of the data only breaks ties. The order of the nodes within a column is chosen to keep flows from crossing: nodes are first listed as the data mentions them, then moved towards the average position of the nodes they are connected to, a few times forward and backward, and the order with the fewest crossings is kept. priority fixes the relative order of the nodes it names. A flow that skips columns takes room in each column it passes, as a waypoint the size of the flow, so it runs between the nodes of those columns instead of over them; waypoints take part in the order and the alignment, and the gap between two of them is 2 pixels (or nodePadding if that is smaller). The waypoints of a flow are held at one level wherever the columns have the room, the thickest flows first, so the flow runs straight across the columns it skips and bends only next to its two nodes; where a column is too full, or two such flows swap places, the flow keeps a bend between two of its waypoints. This holds with every nodeAlign; only with nodeAlign: 'auto' and nodePaddingMode: 'even' the waypoints are not held level. Because they take room, a chart with many skipping flows is drawn with a smaller scale. Sizes, gaps and positions are computed in pixels when the chart area is known, so the title, the legend and layout.padding are respected and the scales are not used for the layout.

parsed.x and parsed.y of a flow no longer hold positions in value units. On the column axis (x for horizontal charts, y for vertical ones) parsed holds the column of the source node and parsed._custom the column of the target; the other axis is 0. The pixel geometry of a flow is available on the element (x, y, x2, y2, width, height).