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.

Відкрити в Lab
vanilla-js d3 svg react tailwind vue svelte
Цілі: TS JS HTML React Vue Svelte

Код

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 Pathsd3.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.