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 visualizing flow data with nodes and links, featuring gradient colors and glow effects

Basic Chart

Installation

npx shadcn@latest add @evilcharts/sankey-chart

Usage

The sankey chart is a composible compound component: <EvilSankeyChart /> is the container, and <Node />, <Link />, and <Tooltip /> are composed as children. Render only the parts you need.

import {
  EvilSankeyChart,
  Node,
  NodeLabel,
  Link,
  Tooltip,
} from "@/components/evilcharts/charts/sankey-chart";
import type { SankeyData } from "recharts";
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;
 
<EvilSankeyChart data={data} config={chartConfig}>
  <Node isClickable>
    <NodeLabel position="outside" showValues />
  </Node>
  <Link variant="source" />
  <Tooltip />
</EvilSankeyChart>

Interactive Selection

Set isClickable on <Node /> to let nodes be selected by clicking. Use the onSelectionChange callback on the root to handle selection events:

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

Loading State

isLoading='true'

Examples

Below are some examples of the sankey chart with different configurations. You can customize the <Link /> variant, the root nodeWidth, nodePadding, and other properties.

Gradient Colors

gradient colors

Labeled Nodes

Inside Labels

showNodeLabels='inside'
showNodeLabels='inside' - solid colors

Outside Labels

showNodeLabels='outside'
<Link variant='solid' />
<Link variant='source' />

Glowing Nodes

<Node glow={['Solar', 'Wind']} />

API Reference

The sankey chart is composed of a root container and a small set of composible parts. Render the root, then compose the parts you need as children.

EvilSankeyChart

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

PropTypeDefaultDescription
data*SankeyData

Sankey diagram data containing nodes and links. Nodes represent entities, and links represent flows between them. SankeyData is { nodes: SankeyNode[]; links: SankeyLink[] }, where SankeyNode = { name: string; icon?: ReactNode } and SankeyLink = { source: number; target: number; value: number }.

config*ChartConfig

Configuration object that defines the chart's nodes. Each key should match a node name from your data, with corresponding colors.

children*ReactNode

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

classNamestring

Additional CSS classes to apply to 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

Number of iterations for the Sankey layout algorithm. Higher values produce better layouts but take more time.

sortbooleantrue

Whether to sort nodes automatically for optimal layout.

alignleft|justify"justify"

Horizontal alignment strategy for nodes. "left" aligns nodes to the left, "justify" spreads them across the width.

verticalAlignjustify|top"justify"

Vertical alignment strategy 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

Callback function that is called when a node is selected or deselected. Receives an object with dataKey (node name) and value (node value calculated from links), or null when deselected. Fires when a node is clicked while <Node /> has isClickable set.

isLoadingbooleanfalse

Shows a loading placeholder animation when data is being fetched.

sankeyPropsOmit<SankeyProps, "data">

Additional props to pass to the underlying Recharts Sankey component. Read the Recharts Sankey documentation for available props.

Node

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

PropTypeDefaultDescription
radiusnumber0

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

isClickablebooleanfalse

Enables interactive clicking on nodes to select/deselect them. Selected nodes become highlighted while unselected nodes and their links dim.

glowstring[][]

Array of node names that should have a glowing effect applied. Creates a smooth outer glow around the specified nodes.

childrenReactNode

Optional <NodeLabel /> composition.

NodeLabel

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

PropTypeDefaultDescription
positioninside|outside

Position of node labels. "inside" shows labels inside nodes, "outside" shows labels beside nodes. When <NodeLabel /> is not composed, no labels are shown.

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 the sankey links render.

PropTypeDefaultDescription
variantgradient|solid|source|target"gradient"

The coloring strategy for links. "gradient" fades from source to target color, "solid" uses a single 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.

glownumber[][]

Array of link indices that should have a glowing effect applied. Creates a smooth outer glow around the specified links.

Tooltip

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

PropTypeDefaultDescription
variantdefault|frosted-glass"default"

Controls the visual style of the tooltip.

roundnesssm|md|lg|xl"lg"

Controls the border-radius of the tooltip.

defaultIndexnumber

When set, the tooltip will be visible by default at the specified data point index.