โœฆ StealThis .dev

Sankey Chart

A complex flow diagram (Sankey) built with D3.js. Features splitting and merging flows, automatic node positioning, and interactive link highlighting. Perfect for visualizing income statements or resource allocations.

Open in Lab
vanilla-js d3 svg react tailwind vue svelte
Targets: TS JS HTML React Vue Svelte

Code

Features

  • Complex Flows โ€” visualize multiple stages of data splitting and merging
  • D3.js Powered โ€” uses the industrial-standard D3-Sankey plugin for high performance
  • Automatic Layout โ€” node positions and link widths are calculated mathematically
  • Interactive Links โ€” hover over any branch to highlight the entire path
  • Responsive โ€” automatic redraw on window resize with viewport scaling
  • Dark Mode Support โ€” styled with CSS variables for seamless theme switching

How it works

  1. Data Prep โ€” Define an array of nodes (entities) and links (connections with value)
  2. Sankey Engine โ€” D3-Sankey computes the x0, x1, y0, y1 coordinates for every rectangle
  3. SVG Paths โ€” d3.sankeyLinkHorizontal() generates the smooth bezier curves between nodes
  4. Color Mapping โ€” Nodes inject their specific brand colors into the outgoing links
  5. Dynamic Scaling โ€” The SVG viewBox ensures the chart remains readable on any screen size

Live Example

The included snippet demonstrates a real-world use case: Alphabetโ€™s (Google) Q2 FY24 Income Statement, showing exactly how revenue filters down through gross profit and operating expenses to net earnings.