Mermaid Sankey Diagram Syntax & Examples
Reference for Mermaid sankey diagram syntax — sankey-beta keyword, CSV-style source-target-value rows, and flow magnitude encoding. Copy-paste funnel & resource examples.
Use this page to connect a high-intent search query to the right problem-solution narrative.
A syntax reference for Mermaid's `sankey-beta` grammar — read this when you need the exact way to express flow magnitudes between source and destination nodes, then try the result in the editor.
View Mermaid sourcePlain-text diagram syntax — copy or edit directly.
1sankey-beta2Visitors,Homepage,6003Visitors,Blog,3004Visitors,Pricing,1005Homepage,Signup,1506Blog,Signup,607Pricing,Signup,408Signup,Paid,70What is a sankey diagram
A sankey diagram shows how a quantity flows from sources to destinations through intermediate stages, with the width of each ribbon proportional to the magnitude of that flow. The reader sees both the structure (which sources connect to which destinations) and the volume (how much) in one view. It is the right shape for any quantity-splitting question: marketing funnel conversion (visitors → signups → paid), energy or material flow (input → process → output), budget allocation (revenue → departments → categories), traffic analysis (channels → pages → conversions). If your data is purely categorical with no magnitude dimension, use a flowchart instead.
Basic syntax — sankey-beta + CSV rows
Every Mermaid sankey diagram starts with the `sankey-beta` keyword on its own line. (The `-beta` suffix reflects the diagram type's experimental status in Mermaid releases, but the grammar has been stable for over a year.) Each subsequent line is a CSV-style row with exactly three fields: `Source,Target,Value`. The source is the upstream node, the target is the downstream node, and the value is the numeric magnitude of the flow. Mermaid auto-creates nodes from these rows — declaring nodes separately is not needed and not supported.
- `sankey-beta` — diagram type keyword (always first)
- `Source,Target,Value` — CSV row format (one per line, no header)
- Nodes are auto-created from rows — no separate node declaration
Multi-stage flows — chaining rows
A node can appear as both a target and a source in different rows — that's how multi-stage flows are built. For example, in a funnel: `Visitors,Homepage,600` says 600 visitors went to the homepage, and then `Homepage,Signup,150` says 150 of those continued to signup. The Homepage node serves as a destination for the first row and a source for the second. The renderer automatically positions intermediate nodes between stages, and the ribbon widths flow proportionally through them. Up to three or four stages reads cleanly; beyond that, ribbons cross over and individual flows become hard to track.
Magnitudes & visual encoding
The third field on each row is the flow magnitude — typically a count, a percentage, or a normalised value. Mermaid scales the entire diagram so the largest flow fits, and every smaller flow renders proportionally. This means absolute values don't matter — only ratios do. A funnel with values `1000 / 500 / 100` renders identically to one with `10 / 5 / 1`. Use the values that make sense for your data; the diagram will scale. The most common gotcha here: mixing scales accidentally — putting one flow in percent and another in count produces an unreadable diagram because the relative magnitudes are meaningless.
Node labels & special characters
Node labels can contain spaces, punctuation, and most special characters. If a label contains a comma (which is the field separator), wrap the entire label in double quotes: `"Signup, free plan",Paid,40`. Quoted labels can include commas, escaped quotes (`"He said \"hi\""`), and other punctuation that would otherwise confuse the parser. Avoid extremely long labels — they don't wrap, so a long label can push the diagram wider than expected. Two-to-four-word labels read cleanly.
- Plain labels: `Homepage,Signup,150`
- Labels with commas: `"Signup, free plan",Paid,40` (wrap in double quotes)
- Labels with quotes: `"He said \"hi\"",...`
Common patterns & gotchas
Three patterns dominate real sankey diagrams. Conversion funnels are the most common — visitors split across landing pages, then narrow toward signup, then narrow further to paid (each stage smaller than the last). Resource flows are similar in shape but split-and-merge multiple times (raw input → processes → multiple outputs). Budget allocation is one-to-many (revenue → departments → categories). The most common gotcha: forgetting that nodes are auto-created from rows — typos in node names create separate nodes that don't connect to the rest of the flow (you end up with `Singup` and `Signup` as two different unconnected nodes). Always check the rendered output for unconnected fragments after editing.
- Typos in node names silently create unconnected nodes — `Singup` vs `Signup`
- Diagrams beyond 3-4 stages get noisy; consider splitting into multiple sankeys
- Magnitudes are relative — mixing percentages with counts produces meaningless ratios