# Sankey Chart

> React SVG sankey diagram: node columns laid out from the links, gradient ribbons, a node's flows lit on hover or focus, and a hidden data table.

- Kind: Component (Charts)
- Docs: https://ui.minidev.pro/docs/sankey-chart
- Source (registry JSON with the full file): https://ui.minidev.pro/r/sankey-chart.json
- License: MIT. One file, yours to edit after install.

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/sankey-chart
```

Writes `components/ui/sankey-chart.tsx` and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/sankey-chart.json`.

### npm package

```bash
npm i minidev-ui-kit
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

```tsx
import { SankeyChart } from "@/components/ui/sankey-chart"
```

From the npm package:

```tsx
import { SankeyChart } from "minidev-ui-kit/ui/sankey-chart"
```

## Usage

```tsx
import { SankeyChart } from "@/components/ui/sankey-chart"

export function Example() {
  return (
    <SankeyChart />
  )
}
```

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `links` | `SankeyLink[]` | `SAMPLE_LINKS` | no | Flows between node ids. Columns are computed from these. |
| `nodes` | `SankeyNodeInput[]` | `SAMPLE_NODES` | no | Optional node order, labels and colours. Unknown ids found in links are added automatically. |
| `align` | `"left" \| "justify"` | `"left"` | no | "left" puts each node one column after its furthest source; "justify" also pushes nodes with no outgoing links to the last column. |
| `title` | `string` | `"Where signups come from"` | no |  |
| `caption` | `string` | `"Last 30 days, visitors to paid plan"` | no |  |
| `height` | `number` | `320` | no | Chart height in px. The width follows the container. |
| `nodeWidth` | `number` | `10` | no |  |
| `nodePadding` | `number` | `14` | no | Vertical gap between nodes in a column, in px. |
| `format` | `(value: number) => string` | `defaultFormat` | no | Formats values in labels, the tooltip text and the table. |
| `unit` | `string` | `"visitors"` | no | Unit word for accessible labels, e.g. "visitors". |
| `className` | `string` | - | no |  |

## Examples

### Custom flows and labels

links reference node ids; nodes adds display labels and colours, and format and unit rewrite every number for a budget in thousands.

```tsx
const budgetLinks: SankeyLink[] = [
  { source: "revenue", target: "payroll", value: 420 },
  { source: "revenue", target: "infra", value: 160 },
  { source: "revenue", target: "marketing", value: 110 },
  { source: "revenue", target: "profit", value: 90 },
  { source: "payroll", target: "eng", value: 260 },
  { source: "payroll", target: "sales", value: 100 },
  { source: "payroll", target: "support", value: 60 },
  { source: "infra", target: "cloud", value: 120 },
  { source: "infra", target: "tooling", value: 40 },
]

const budgetNodes: SankeyNodeInput[] = [
  { id: "revenue", label: "Revenue", color: "var(--chart-1)" },
  { id: "payroll", label: "Payroll", color: "var(--chart-2)" },
  { id: "infra", label: "Infrastructure", color: "var(--chart-3)" },
  { id: "marketing", label: "Marketing", color: "var(--chart-4)" },
  { id: "profit", label: "Profit", color: "var(--chart-5)" },
  { id: "eng", label: "Engineering" },
  { id: "sales", label: "Sales" },
  { id: "support", label: "Support" },
  { id: "cloud", label: "Cloud" },
  { id: "tooling", label: "Tooling" },
]

<SankeyChart
  links={budgetLinks}
  nodes={budgetNodes}
  title="Where October revenue went"
  caption="Northwind, in thousands"
  unit="USD"
  format={(n) => `$${n}k`}
  height={300}
/>
```

### Justified columns

align justify pushes nodes with no outgoing flow to the last column, so visitors who left line up with the plans instead of sitting mid chart.

```tsx
<SankeyChart align="justify" nodeWidth={14} nodePadding={18} caption="Last 30 days, dead ends pushed right" />
```

## Keyboard interactions

| Key | Action |
| --- | --- |
| `Tab` | Moves focus into the diagram. All nodes share one Tab stop. |
| `ArrowRight` or `ArrowDown` | Moves focus to the next node and highlights its flows. |
| `ArrowLeft` or `ArrowUp` | Moves focus to the previous node. |
| `Home` or `End` | Moves focus to the first or last node. |

## Accessibility

- The chart is a `<figure>` labelled by its title; the SVG is `role="group"` named with the node and flow counts.
- Each node is a focusable `role="img"` whose label reads its total and every flow in and out, such as "Signed up: 3,480 visitors. From Organic search 1,400, … To Starter 1,640, …". `unit` supplies the word "visitors".
- A visually hidden `<table>` lists every flow with From, To and Value columns; the ribbons are `aria-hidden`.
- Highlighting is an opacity change on a 140ms CSS transition, which the kit's stylesheet cuts under reduced motion.

## FAQ

### Why do sample nodes like "Starter" show up next to my own data?

`nodes` defaults to the sample node list, and every listed node is drawn even if no link uses it. When you pass your own `links`, also pass `nodes` (to set labels and colours) or `nodes={[]}`.

### How are SankeyChart columns decided?

From the links alone: each node goes one column after its furthest source. With `align="justify"`, nodes that have no outgoing links move to the last column, so every end state lines up on the right. Links from a node to itself and links with a value of 0 or less are dropped.

### Can the data contain a cycle, like users who come back?

The layout will not hang: nodes caught in a cycle are placed one column after their placed sources. A sankey reads best without loops, so model returns as a separate node, such as "Returned".

### How do I size it?

Set `height` in pixels (default 320); the width follows the container through a `ResizeObserver`. Labels in middle columns hide when fewer than five characters fit, but each node keeps its full spoken label.

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Related

- [Funnel Chart](https://ui.minidev.pro/docs/funnel-chart/md): React funnel chart with horizontal or vertical stages, conversion and drop-off between stages, an optional comparison outline and bars that spring to new data.
- [Area Chart](https://ui.minidev.pro/docs/area-chart/md): Dependency free SVG area chart for React with a smoothed line, dashed grid, gradient fill and an optional highlighted point. Strokes stay crisp at any size.
- [Bar Chart](https://ui.minidev.pro/docs/bar-chart/md): Lightweight React bar chart with rounded bars, dashed gridlines and labels. Hovering a bar dims the rest and shows its value; the peak is highlighted first.
- [Chart Card](https://ui.minidev.pro/docs/chart-card/md): Card wrapper for charts with a title, optional description and a minimum height body. Pairs with the area, bar and donut charts in a React dashboard.
- [Chart Legend](https://ui.minidev.pro/docs/chart-legend/md): Wrapping chart legend with small color swatches and labels. Pass colors per series or fall back to accent, success, warning and danger theme tokens.
- [Chart Tooltip Card](https://ui.minidev.pro/docs/chart-tooltip-card/md): Tooltip card for charts showing a title and label and value rows with tabular numbers. Drop it into any chart library's custom tooltip slot.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
