Sankey
Overview
The Sankey component draws flows between nodes, each link as wide as the amount that runs along it.
It wraps Recharts' Sankey and comes from the
@allxsmith/bestax-charts package, which the Charts guide
shows how to install. You give it data of nodes and the links that flow between them, and
it draws them with Bulma's colors, each link in the color of the node it leaves, with node names
beside the nodes, a tooltip in a Bulma box, and a data table of every flow that screen readers
read.
Import
import { Sankey } from '@allxsmith/bestax-charts';
Usage
Basic Usage
data.nodes lists the nodes by name, and each of data.links runs from a source node to a
target node, both given by their index in nodes, carrying a value. label names the
chart; it's required, because it's what a screen reader announces.
function example() { const energy = { nodes: [ { name: 'Solar', color: 'warning' }, { name: 'Wind', color: 'info' }, { name: 'Gas', color: 'danger' }, { name: 'Grid', color: 'link' }, { name: 'Homes', color: 'success' }, { name: 'Industry', color: 'primary' }, ], links: [ { source: 0, target: 3, value: 120 }, { source: 1, target: 3, value: 180 }, { source: 2, target: 3, value: 260 }, { source: 3, target: 4, value: 250 }, { source: 3, target: 5, value: 310 }, ], }; return ( <Sankey label="Electricity, from source to use" data={energy} unit=" TWh" /> ); }
More Stages
Nodes line up in columns by how far along the flows they sit, so a node reached through others sits further right, and the nodes the flows end at line up at the right edge.
function example() { const visits = { nodes: [ { name: 'Search' }, { name: 'Social' }, { name: 'Home' }, { name: 'Pricing' }, { name: 'Docs' }, { name: 'Sign-up' }, { name: 'Left' }, ], links: [ { source: 0, target: 2, value: 5400 }, { source: 1, target: 2, value: 2100 }, { source: 2, target: 3, value: 3200 }, { source: 2, target: 4, value: 2800 }, { source: 2, target: 6, value: 1500 }, { source: 3, target: 5, value: 900 }, { source: 3, target: 6, value: 2300 }, { source: 4, target: 5, value: 600 }, { source: 4, target: 6, value: 2200 }, ], }; return <Sankey label="Where visitors go" data={visits} height={360} />; }
Nodes and Links
nodeWidth sets how wide each node's bar is and nodePadding the gap between the nodes of one
column, in pixels. linkCurvature bends the links, from 0 (straight) to 1, and linkOpacity
sets how opaque they are, so the links they cross show through.
function example() { const budget = { nodes: [ { name: 'Budget', color: 'link' }, { name: 'Staff' }, { name: 'Tools' }, { name: 'Travel' }, ], links: [ { source: 0, target: 1, value: 600 }, { source: 0, target: 2, value: 250 }, { source: 0, target: 3, value: 150 }, ], }; return ( <Sankey label="Budget" data={budget} nodeWidth={24} nodePadding={24} linkCurvature={0.2} linkOpacity={0.7} height={240} /> ); }
Node Names
Each node's name is written beside it, on its right, or on the left of a node the flows end at,
so the names stay inside the chart. showLabels={false} leaves them off.
function example() { return ( <Sankey label="Budget" showLabels={false} data={{ nodes: [{ name: 'Budget' }, { name: 'Staff' }, { name: 'Tools' }], links: [ { source: 0, target: 1, value: 600 }, { source: 0, target: 2, value: 250 }, ], }} height={200} /> ); }
Formatting Values
Values are written as useNumberFormat writes a number, in the
LocaleProvider's locale. valueFormatter writes them your way instead, in the tooltip and the
data table, and unit is written after each one.
function example() { const dollars = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0, }); return ( <Sankey label="Where the money goes" data={{ nodes: [ { name: 'Revenue', color: 'success' }, { name: 'Salaries' }, { name: 'Hosting' }, { name: 'Profit', color: 'primary' }, ], links: [ { source: 0, target: 1, value: 420000 }, { source: 0, target: 2, value: 90000 }, { source: 0, target: 3, value: 150000 }, ], }} valueFormatter={value => dollars.format(value)} showTable height={240} /> ); }
Colors
A node's color takes a Bulma color name, a shade of one of Bulma's six palette colors
(primary-30, info-dark), or any CSS color, and a node with no color takes the next of Bulma's
palette colors. Each link takes the color of the node it leaves, so a flow keeps its color from
where it starts.
function example() { return ( <Sankey label="Traffic by channel" data={{ nodes: [ { name: 'Email', color: 'primary-40' }, { name: 'Ads', color: '#e76f51' }, { name: 'Site', color: 'grey-dark' }, ], links: [ { source: 0, target: 2, value: 300 }, { source: 1, target: 2, value: 500 }, ], }} height={200} /> ); }
Showing the Data Table
The data table is always there for screen readers: a row per link, headed by the node it runs
from, with the node it runs to and its value. showTable puts it on screen too, below the chart,
and showLabel shows the chart's label above it as a caption. labels translates the table's
headers and the tooltip's words.
function example() { return ( <Sankey label="Strom, von der Quelle zur Nutzung" showLabel showTable data={{ nodes: [ { name: 'Sonne', color: 'warning' }, { name: 'Wind', color: 'info' }, { name: 'Netz', color: 'link' }, { name: 'Haushalte', color: 'success' }, ], links: [ { source: 0, target: 2, value: 120 }, { source: 1, target: 2, value: 180 }, { source: 2, target: 3, value: 300 }, ], }} labels={{ source: 'Von', target: 'Nach', value: 'Menge', total: 'Summe' }} unit=" TWh" height={220} /> ); }
Recharts Props and Children
chartProps goes to Recharts' Sankey after the chart's own props (here, sort={false} keeps
the nodes of each column in the order nodes lists them), and tooltipProps to its Tooltip.
children adds more elements inside the drawing.
function example() { return ( <Sankey label="Budget" data={{ nodes: [ { name: 'Budget', color: 'link' }, { name: 'Travel' }, { name: 'Tools' }, { name: 'Staff' }, ], links: [ { source: 0, target: 1, value: 150 }, { source: 0, target: 2, value: 250 }, { source: 0, target: 3, value: 600 }, ], }} chartProps={{ sort: false }} height={220} /> ); }
Size
The chart fills its container's width and is 300 pixels tall. width and height take pixels
or a percentage of the container. A chart sized in pixels draws on the server as well; one sized
in percent draws once the browser has measured its container, and follows it as it changes. Its
data table renders on the server either way.
<Sankey label="Two flows" data={{ nodes: [{ name: 'In' }, { name: 'Kept' }, { name: 'Spent' }], links: [ { source: 0, target: 1, value: 40 }, { source: 0, target: 2, value: 60 }, ], }} width={320} height={180} />
Theming
The node names are written in --bulma-chart-text-color, which falls back to Bulma's
--bulma-text-weak. The BarChart's Theming section lists the other
variables the charts draw with.
:root {
--bulma-chart-text-color: var(--bulma-text-weak); /* node names */
}
Accessibility
- The chart is a
<figure>named bylabel, which is also its data table's caption.descriptionis read with the name. The drawing is a picture (role="img") named and described by the same caption and description. - A data table of every link renders beside the drawing, visually hidden with Bulma's
is-sr-only, so a screen reader reads the amounts the links show (WAI: complex images). Each row is one flow: headed by the node it runs from, then the node it runs to and its value, so reading along a row follows the flow, and each cell down a column is announced with the node the flow leaves. A node's total, which its height shows, is the larger of what flows into it and what flows out.showTableputs the table on screen. - Recharts gives a Sankey a keyboard layer that makes the drawing a tab stop, but its arrow keys can't reach the nodes or links and show an empty tooltip, so the chart turns it off: the drawing is not a tab stop, and its tooltip follows the pointer only. The data table holds every value the link tooltips show.
- Node names are written beside the nodes in the chart's text color.
- Each link takes the color of the node it leaves, so color says where a flow comes from in the drawing. The data table's first column says it in words.
Related Components
Treemap: A tree of values drawn as nested tiles.SunburstChart: A tree of values drawn as rings.BarChart: Values drawn as bars.Table: The Bulma table the chart's data table is drawn with.LocaleProvider: The locale the chart writes its numbers in.- Helper Props: Bulma helper props for spacing, color, etc.
Additional Resources
Props
| Prop | Type | Default | Description |
|---|---|---|---|
data | { nodes: readonly SankeyChartNode[]; links: readonly SankeyChartLink[] } | — | Required. The nodes, and the links that flow between them. A link whose source or target isn't the index of a node is left out, and so is one that runs from a node to itself or back to a node its flow came from: a Sankey draws flows that only move forward, and a loop leaves Recharts nowhere to put its nodes. |
nodeWidth | number | 10 | How wide each node's bar is, in pixels. |
nodePadding | number | 10 | The gap between the nodes of one column, in pixels. |
linkCurvature | number | 0.5 | How much the links curve, from 0 (straight) to 1. |
linkOpacity | number | 0.4 | How opaque the links are, from 0 to 1. Each takes its source node's color, and see-through links let the nodes and the links they cross show. |
showLabels | boolean | true | Writes each node's name beside it: on the right, or on the left of a node the flows end at. |
labels | { source?: string; target?: string; value?: string; total?: string } | — | The header text of the data table and the tooltip, to translate. |
chartProps | Partial<React.ComponentProps<typeof RechartsSankey>> | — | Props passed to Recharts' Sankey, after the chart's own, such as iterations. |
tooltipProps | Partial<TooltipProps> | — | Props passed to Recharts' Tooltip, after the chart's own. |
className | string | — | Additional CSS classes to apply. |
label | string | — | Required. What the chart shows, such as "Monthly sales". It names the chart for assistive tech, and captions its data table. Shown above the chart with showLabel. |
description | string | — | A sentence or two on what the chart says, such as the trend it shows. A screen reader reads it with the chart's name. Not shown on screen. |
showLabel | boolean | false | Shows label above the chart as its caption. |
showTable | boolean | false | Shows the chart's data table below it. The table is always there for screen readers, which read the numbers from it; this puts it on screen as well. |
hasTooltip | boolean | true | Shows a tooltip with the values under the pointer, or under the keyboard focus as the arrow keys move through the chart. |
valueFormatter | ChartValueFormatter | — | Writes each value on the axes, in the tooltip, on the labels and in the data table. Without one, values are written as useNumberFormat writes a number, in the LocaleProvider's locale. |
unit | string | — | Written after every value, such as % or kg. |
width | ChartSize | '100%' | The chart's width, in pixels or as a percentage of its container. A percentage makes the chart follow its container's size as the browser measures it. Recharts draws in the browser either way: a server render holds the figure, its caption, legend and data table, and the drawing appears once the page hydrates. |
height | ChartSize | 300 | The chart's height, in pixels or as a percentage of its container, which then needs a height of its own. |
children | React.ReactNode | — | More Recharts elements to draw inside the chart, such as a Brush, a ReferenceArea, or <defs> for a pattern a color names with url(#id). |
ref | React.Ref<HTMLElement> | — | Ref forwarded to the root <figure>. |
... | All standard HTML attributes and Bulma helper props | — | See Helper Props |
Types:
ChartColor: Bulma color |`${(typeof paletteColors)[number]}-${(typeof validColorShades)[number]}`|(string & {})— A color for a series, a slice or a mark. A Bulma color name such asprimaryorgrey-light, a shade of one of Bulma's six palette colors such asprimary-60orinfo-dark, or any CSS color, such as#3e8ed0,var(--brand)orurl(#stripes)for a pattern the chart's children define. A Bulma name draws with Bulma's own CSS variable, so it followsThemeand dark mode.ChartSize:number|`${number}%`— A chart's width or height: pixels, or a percentage of its container.ChartValueFormatter:(value: number) => string— Writes a value for the axes, the tooltip, the labels and the data table.SankeyChartLink: One flow of a Sankey diagram, from one node to another. Its fields:source:number— Required. The index innodesof the node it runs from.target:number— Required. The index innodesof the node it runs to.value:number— Required. How much flows, which sets the link's width.
SankeyChartNode: One node of a Sankey diagram: a place the flows run from, to or through. Its fields:name:string— Required. What the node is called, written beside it and in the data table.color:ChartColor— Its color, which the flows leaving it take too. Without one, it takes the next of Bulma's palette colors.