Skip to main content
Version: 1.2.0

Sankey Chart

Sankey is a visualization used to depict a flow from one set of values to another with proportional magnitudes. The things being connected are called nodes and the connections are called links. It is built with d3. Here's a preview:

Live Editor

Usage

1 . Import SankeyChart after installation

import { SankeyChart } from "@k2/d3-viz";

2 . Add data or URL to SankeyChart component.

<SankeyChart data={SankeyChartData} />
// or
<SankeyChart url="server/api/data" />

Examples

Sankey chart with node and link props: Demo

<SankeyChart
data={SankeyChartData}
linkProps={{
mode: "gradient",
}}
nodeProps={{
shape: "rect",
width: 25,
}}
/>

Sankey chart with grid layout: Demo

<SankeyChart data={SankeyChartData} disableGrid={false} />

Sankey chart with animation Demo

<SankeyChart data={SankeyChartData} />

Sankey Chart with tooltip formatter Demo

<SankeyChart
data={SankeyChartData}
tooltip={{
formatter({ data }) {
return `${data.sourceId} to ${data.targetId}`;
},
}}
/>

Sankey Chart with Classes: Demo

<SankeyChart
data={SankeyChartData}
classes={{
chart: "chart-sankey",
header: "header-sankey",
label: "label-sankey",
link: "link-sankey",
node: "node-sankey",
root: "root-sankey",
title: "title-sankey",
tooltip: "tooltip-sankey",
}}
/>

Props

PropertyTypeRequiredDefault valueDescription
dataSankeyChartDatayesData for SankeyChart.
nodePropsNodePropsnoMentioned in NodePropsNode Props API of chart to customize nodes.
linkPropsLinkPropsnoMentioned inLinkPropsLink Props API of chart to customize links.
titlestring | JSX.ElementnoNoneTitle of the chart.
animatebooleannotrueEnable the animation.
disableGridbooleannotrueDisable grid of SankeyChart.
labelboolean | LabelComponentnotrueEnable or customize the labels.
tooltipboolean | TooltipPropsnotrueEnable or customize the tooltip.
colors{[theme-mode: e.g "light" or "dark"] : SankeyColors }noNoneColors of visual in different theme modes
widthnumberno700Width of SankeyChart.
heightnumberno400Height of SankeyChart.
onClick(event: { type: "node" | "link"; data: D3SankeyNode | D3SankeyLink; }) => voidnoNoneCallback for node or link click.
styleCSSPropertiesnoNoneStyles applied to the root element of chart.
classNamestringnoNoneClass applied to the root element of chart.
classesClassesnoNoneClasses of chart.

SankeyChart is wrapped with fetchData HoC, to perform data handling tasks, which adds support for some additional props.

Custom Types

SankeyChartData

Here is the format of the data accepted by SankeyChart.

type SankeyChartData = {
nodes: {
id: string | number;
label: string;
level: string;
color?: string;
}[];
links: {
id: string | number;
sourceId: string | number;
targetId: string | number;
value: number;
color?: string;
}[];
};

D3SankeyNode

Type of the object containing information of a Sankey node.

type D3SankeyNode = {
dx: number;
dy: number;
id: string | number;
label: string;
sourceLinks: D3SankeyLink[];
targetLinks: D3SankeyLink[];
level: string;
value: number;
x: number;
y: number;
color: string;
};

D3SankeyLink

Type of the object containing information of a Sankey link.

type D3SankeyLink = {
dy: number;
id: string | number;
sourceId: string | number;
targetId: string | number;
originalIndex: number;
source: D3SankeyNode;
sy: number;
target: D3SankeyNode;
ty: number;
y0: number;
value: number;
color: string;
};

LabelComponent

This is a type of custom label component. You can create a custom label component by this.

type LabelComponent = (args: {
data: D3SankeyNode,
linkMode: "gradient" | "default",
}) => JSX.Element;

SankeyColors

This is a type of sankey colors props.

type SankeyColors = {
nodeColorScheme: string[],
linkColor: string,
};

NodeProps

NodeProps allows customization of chart nodes.

PropertyTypeRequiredDefault valueDescription
widthnumberno20Width of nodes.
paddingnumberno20Padding between nodes on same level.
shape"rect" | "ellipse"norectShape of NodeProps (rect or eclipse).
colorSchemestring[]no["#BB856F", "#B6B17C", "#66cdaa", "#42C0FB", "#0077ff", "#9090FF", "#B86DFF", "#074700", "#FF99FF", "#F1536A"]Color scheme of nodes.

LinkProps

LinkProps allows customization of chart links.

PropertyTypeRequiredDefault valueDescription
mode"gradient" | "default"nodefaultColor mode of link (default or gradient).
colorstringno#828282Color of links (Only applicable when mode is default).

Classes

Classes enable customization and styling of chart elements through their classes.

ClassNameTypeRequiredDefault valueDescription
rootstringnoNoneApplied to root element of chart.
headerstringnoNoneApplied to header of chart.
titlestringnoNoneApplied to title of chart.
tooltipstringnoNoneApplied to tooltip of chart.
chartstringnoNoneApplied to svg component of chart.
labelstringnoNoneApplied to labels of chart.
nodestringnoNoneApplied to node of chart.
linkstringnoNoneApplied to link of chart.
gridstringnoNoneApplied to grid of chart.

Please see Class Specificity for correctly defining it.