Sankey Chart

Documentation Index

Fetch the complete documentation index at: /llms.txt. Use this file to discover all available pages before exploring further.

Sankey charts for flows between nodes, with labels and gradient links

Installation

npx shadcn-svelte@latest add https://evilcharts-sv.vercel.app/r/layerchart-sankey-chart.json

Usage

A compound component: <EvilSankeyChart /> is the container; <EvilSankeyChart.Node />, <EvilSankeyChart.Link />, and <EvilSankeyChart.Tooltip /> compose as children. Render only the parts you need.

<script lang="ts">
	import {
		EvilSankeyChart,
		type SankeyData
	} from '$lib/components/evilcharts/charts/layerchart-sankey-chart/index.js';
	import { type ChartConfig } from '$lib/components/evilcharts/ui/layerchart-chart';
</script>
<script lang="ts">
	const data: SankeyData = {
		nodes: [
			{ name: 'Visit' },
			{ name: 'Direct-Favourite' },
			{ name: 'Page-Click' },
			{ name: 'Detail-Favourite' },
			{ name: 'Lost' }
		],
		links: [
			{ source: 0, target: 1, value: 3728 },
			{ source: 0, target: 2, value: 354170 },
			{ source: 2, target: 3, value: 62429 },
			{ source: 2, target: 4, value: 291741 }
		]
	};

	const chartConfig = {
		Visit: {
			label: 'Visit',
			colors: { light: ['#3b82f6'], dark: ['#60a5fa'] }
		},
		'Page-Click': {
			label: 'Page Click',
			colors: { light: ['#f59e0b'], dark: ['#fbbf24'] }
		}
		// ... more node configs
	} satisfies ChartConfig;
</script>

<EvilSankeyChart {data} config={chartConfig}>
	<EvilSankeyChart.Node isClickable>
		<EvilSankeyChart.NodeLabel position="outside" showValues />
	</EvilSankeyChart.Node>
	<EvilSankeyChart.Link variant="source" />
	<EvilSankeyChart.Tooltip />
</EvilSankeyChart>

Interactive Selection

Set isClickable on <Node /> to select nodes on click. Handle events with the root’s onSelectionChange callback:

<EvilSankeyChart
	{data}
	config={chartConfig}
	onSelectionChange={(selection) => {
		if (selection) {
			console.log('Selected:', selection.dataKey, 'Value:', selection.value);
		} else {
			console.log('Deselected');
		}
	}}
>
	<EvilSankeyChart.Node isClickable />
	<EvilSankeyChart.Link variant="source" />
	<EvilSankeyChart.Tooltip />
</EvilSankeyChart>

Loading State

Examples

Customize the <Link /> variant, the root nodeWidth, nodePadding, and more.

Gradient Colors

Labeled Nodes

Inside Labels

Outside Labels

API Reference

A root container plus a few composable parts. Render the root, then compose the parts you need.

EvilSankeyChart

The root container. Owns the flow data, layout config, shared context, and the loading skeleton.

PropTypeDefaultDescription
data*SankeyData

Nodes and links for the diagram — nodes are entities, links are flows between them. SankeyData is { nodes: SankeyNode[]; links: SankeyLink[] }, where SankeyNode = { name: string; icon?: Snippet } and SankeyLink = { source: number; target: number; value: number }.

config*ChartConfig

Defines the chart’s nodes. Each key matches a node name from your data and sets its colors.

children*Snippet

The composed parts — <Node />, <Link />, and <Tooltip />.

accessibilityChartAccessibility

Names and optionally describes the chart wrapper. It remains a group, so interactive legends and marks stay available to assistive technology.

classNamestring

Extra CSS classes for the chart container.

nodeWidthnumber10

The width of each node in pixels.

nodePaddingnumber10

The vertical padding between nodes in pixels.

linkCurvaturenumber0.5

The curvature of links between nodes. Value between 0 (straight) and 1 (maximum curve).

iterationsnumber32

Iterations for the Sankey layout algorithm. Higher values improve the layout but take more time.

sortbooleantrue

Whether to sort nodes automatically for optimal layout.

align left| justify"justify"

Horizontal alignment for nodes. "left" aligns left; "justify" spreads them across the width.

verticalAlign justify| top"justify"

Vertical alignment for nodes. "top" aligns to top; "justify" distributes vertically.

backgroundVariantBackgroundVariant

Background pattern variant to display behind the chart.

defaultSelectedNodestring | nullnull

The node name selected on first render.

onSelectionChange(selection: { dataKey: string; value: number } | null) => void

Called when a node is selected or deselected. Receives an object with dataKey (node name) and value (calculated from links), or null when deselected. Fires on node click when <Node /> has isClickable set.

isLoadingbooleanfalse

Shows a loading placeholder animation when data is being fetched.

sankeyPropsOmit<SankeyProps, "data">

Extra props for the underlying LayerChart Sankey component. See the LayerChart Sankey documentation for available props.

Node

Configures how nodes render. Compose a <NodeLabel /> inside to show labels and values.

PropTypeDefaultDescription
radiusnumber0

The corner radius of node rectangles in pixels. Set to 0 for square nodes.

isClickablebooleanfalse

Lets nodes be clicked to select or deselect them. Selected nodes highlight while the rest, and their links, dim.

childrenSnippet

Optional <NodeLabel /> composition.

NodeLabel

Declares labels for the <Node /> it is composed inside.

PropTypeDefaultDescription
position inside| outside

Label position. "inside" shows labels inside nodes; "outside" shows them beside nodes. Without <NodeLabel />, no labels render.

showValuesbooleanfalse

Whether to display the total flow value alongside each node label.

valueFormatter(value: number) => string(value) => value.toLocaleString()

Function to format node values when showValues is enabled.

Link

Configures how links render.

PropTypeDefaultDescription
variant gradient| solid| source| target"gradient"

Coloring strategy for links. "gradient" fades source to target; "solid" uses one color; "source" uses the source node color; "target" uses the target node color.

verticalPaddingnumber0

Vertical padding where links connect to nodes in pixels. Useful when using node labels.

Tooltip

The hover tooltip. Hidden automatically while the chart is loading.

PropTypeDefaultDescription
variant default| frosted-glass"default"

Controls the visual style of the tooltip.

roundness sm| md| lg| xl"lg"

Controls the border-radius of the tooltip.

defaultIndexnumber

When set, shows the tooltip by default at this data point index.