Sankey Diagram Maker from CSV
Operator: Abdulla Al Fahad. How tools are built and checked
A Sankey diagram draws money, energy or visitors as ribbons whose widths are the amounts, which shows at a glance where a total goes and which branch takes most of it. Each row of source, target and value is one ribbon, and every node is labelled with its value and its share of the total flow.
Nodes are placed in columns by their longest path from a source, their order is relaxed 32 times so that heavy ribbons run nearly straight, and you can drag a node up or down, or use the Up and Down buttons in the node table, to tidy what the relaxation leaves.
Data that loops back, such as a pension payment that flows into salary, has no left-to-right order, so the page names the cycle instead of drawing something misleading.
Key features
- CSV, XLSX or pasted rows with a column picker for source, target and value, up to 300 nodes and 3,000 links
- Repeated source and target pairs are added together and reported; rows with an empty name or a value that is not positive are skipped and listed
- Cycles, including a row that points a node at itself, are found and printed as a path such as Salary → Budget → Savings → Pension → Salary
- Ribbon colour from the source, the target, a gradient between them, or grey
- Values in a unit you choose and a percentage of the total flow in every label, each switchable, plus node reordering by dragging or with buttons
- SVG and PNG export, and node-totals and merged-links CSV
How nodes are placed and what the percentages mean
A node's column is its longest path from a node with no inflow. Nodes with no outflow are moved to the last column by default so that all endings line up, or can stay where the path puts them. A node's size is the larger of its inflow and outflow, and all columns share one scale: the largest column that still fits the chosen height sets pixels per unit of value, with 14 px between nodes. Total flow is the sum of the values of nodes that have no inflow.
The relaxation follows the idea in d3-sankey: right to left, each node moves toward the value-weighted average height of the nodes it feeds, then left to right toward the average height of the nodes that feed it, by a fraction of the distance that is damped in the early rounds and eases toward a full step by the last, and after each column is moved overlaps are resolved by pushing nodes apart. Inside a node, ribbons are stacked in the order of the height of the node at the other end, which keeps crossings down.
How it works
Cycle detection runs a depth-first search over the merged links and reports the first back edge as the cycle. Each of the 32 relaxation rounds visits the columns once in each direction, so layout time grows with nodes and links, not with the numeric values.
A dragged node is pinned at the height you drop it, kept inside the diagram, and the other nodes in its column are packed above and below it, so it stays under the pointer. The remaining columns are relaxed around the pin. Two pinned nodes dropped on the same spot are left overlapping rather than moved; Reset node order returns to the computed layout.
A ribbon runs from the right edge of its source to the left edge of its target as a cubic curve, so a link that skips a column passes behind the nodes in between.
Typical uses
- A facilities manager charts 6 energy sources feeding 4 buildings and then 12 uses, and drags the largest building to the top so its ribbons are read first.
- An analyst charts a signup funnel in which 10,000 visits split into 3,200 signups and 6,800 exits, and the exits are moved to the last column.
Building one
- Paste the rows or drop the file, and check that Source, Target and Value point at the right columns.
- Add a unit and title, choose how ribbons are coloured, and drag nodes where the layout is crowded.
- Download SVG or PNG, and the node-totals CSV if you want the figures behind the picture.
Example
Rows (source, target, value)
Salary,Budget,4200
Freelance,Budget,800
Budget,Housing,1700
Budget,Food,900
Budget,Transport,450
Budget,Savings,1200
Budget,Other,750
Housing,Rent,1500
Housing,Utilities,200
Savings,Emergency fund,400
Savings,Pension,800Diagram
12 nodes in 4 columns and a total flow of 5,000. Salary 4,200 (84%) and Freelance 800 (16%) feed Budget 5,000 (100%), which feeds Housing 1,700 (34%), Savings 1,200 (24%), Food 900 (18%), Other 750 (15%) and Transport 450 (9%). Adding a twelfth row, Pension,Salary,100, replaces the diagram with: Salary → Budget → Savings → Pension → Salary.Tips for best results
- Adding a node in the middle pushes everything after it one column to the right, because columns follow the longest path. Route a branch through an intermediate node, such as Budget in the example, when you want its leaves a column further on.
- Use the percentage-only labels for a slide and keep the values in the node-totals CSV, where they are exact.
- Put a unit prefix such as $ or a suffix such as ' kWh' in the two unit fields; they appear in node labels and ribbon tooltips.
Things to check before you rely on it
Percentages are of the total flow that enters at the sources, so a node deep in the diagram shows its share of that total, not of its parent.
A node's size is the larger of what flows in and out, so a node whose inflow and outflow differ (a loss, a gain) is drawn at the bigger value and the ribbons do not fill it on one side.
Touch screens scroll the page when you drag across the diagram, so on a phone use the Up and Down buttons in the node table.
Common errors and how to fix them
- "These links form a cycle: A → B → C → A"
- Remove or reverse one of the listed rows. A diagram that flows in one direction cannot contain a loop, and the message shows the first loop found, so repeat until no cycle is named.
- "Row 14: the value "abc" is not a positive number, so the row was skipped."
- Fix or delete the cell. Values may carry thousands separators and currency symbols; zero and negative values are skipped because a ribbon cannot have zero width.
Best practices
- Keep node names short and consistent in spelling; 'Savings' and 'savings' are two nodes.
- Check the merged-links CSV against the source if totals look off: it shows each pair after repeated rows were added.
Why a loop is refused
A Sankey diagram's columns run from sources to endings. Pension → Salary points back to the first column, so any placement either draws a ribbon running backwards or hides the loop, and both misreport the flow. Showing the path lets you decide whether the row is a data error or a flow to chart separately.
A self-reference such as Food → Food is also a cycle of length one and is reported the same way.
Colours and what they imply
Colouring ribbons by their source keeps each origin's share visible all the way to the endings. Colouring by target is better when the endings are the categories you want the audience to compare, and the gradient setting blends the two at the cost of a busier picture.
Frequently asked questions
Can a value be a decimal or have a currency symbol?
Yes. Values such as 1,250.50 and $3,000 are read as numbers. Only positive values are drawn.