# Sankey

> Show how a quantity moves from sources through stages to destinations, with ribbon widths in proportion to the flow.

A sankey diagram shows how a quantity moves between stages: energy from its sources to the sectors that use it, money from revenue streams to departments, visitors from a landing page to a paid plan. Each ribbon's width is in proportion to the amount it carries, so the reader sees where most of the flow goes and where it leaks away.

Use a [stacked bar](/docs/comparison/grouped-stacked/) instead when there is one split and no stages in between, and a [graph](/docs/flow/graph/) when the connections matter more than the amounts.

## A basic sankey

Set `type` to `"sankey"` and give it one row per link, with a source, a target and a value. The three encoding channels name those fields. Nodes are placed in columns by their depth in the flow, except that by default every node with no outgoing links moves to the last column.

Labels in the first column sit to the left of their nodes, labels in the last column to the right, and labels in the columns between to the right. Each label is the node name with its value beside it, formatted by the `format` on the value channel.

The figure below is Lawrence Livermore National Laboratory's 2024 US energy flow: fuels feed electricity generation and the four end-use sectors, and each sector's energy ends as either energy services or rejected energy. It uses all 42 labelled links and leaves out only an unlabelled hairline.

```json
{
  "type": "sankey",
  "data": [
    {
      "source": "Solar",
      "target": "Electricity generation",
      "value": 0.75
    },
    {
      "source": "Nuclear",
      "target": "Electricity generation",
      "value": 8.17
    },
    {
      "source": "Hydro",
      "target": "Electricity generation",
      "value": 0.83
    },
    {
      "source": "Wind",
      "target": "Electricity generation",
      "value": 1.54
    },
    {
      "source": "Natural gas",
      "target": "Electricity generation",
      "value": 13.94
    },
    {
      "source": "Coal",
      "target": "Electricity generation",
      "value": 7.01
    },
    {
      "source": "Biomass",
      "target": "Electricity generation",
      "value": 0.32
    },
    {
      "source": "Geothermal",
      "target": "Electricity generation",
      "value": 0.05
    },
    {
      "source": "Petroleum",
      "target": "Electricity generation",
      "value": 0.17
    },
    {
      "source": "Net electricity imports",
      "target": "Electricity generation",
      "value": 0.01
    },
    {
      "source": "Solar",
      "target": "Residential",
      "value": 0.26
    },
    {
      "source": "Geothermal",
      "target": "Residential",
      "value": 0.04
    },
    {
      "source": "Natural gas",
      "target": "Residential",
      "value": 4.58
    },
    {
      "source": "Biomass",
      "target": "Residential",
      "value": 0.37
    },
    {
      "source": "Petroleum",
      "target": "Residential",
      "value": 0.92
    },
    {
      "source": "Natural gas",
      "target": "Commercial",
      "value": 3.46
    },
    {
      "source": "Solar",
      "target": "Commercial",
      "value": 0.08
    },
    {
      "source": "Geothermal",
      "target": "Commercial",
      "value": 0.02
    },
    {
      "source": "Biomass",
      "target": "Commercial",
      "value": 0.17
    },
    {
      "source": "Coal",
      "target": "Commercial",
      "value": 0.01
    },
    {
      "source": "Petroleum",
      "target": "Commercial",
      "value": 0.8
    },
    {
      "source": "Natural gas",
      "target": "Industrial",
      "value": 10.9
    },
    {
      "source": "Solar",
      "target": "Industrial",
      "value": 0.02
    },
    {
      "source": "Coal",
      "target": "Industrial",
      "value": 0.85
    },
    {
      "source": "Biomass",
      "target": "Industrial",
      "value": 2.36
    },
    {
      "source": "Petroleum",
      "target": "Industrial",
      "value": 8.73
    },
    {
      "source": "Natural gas",
      "target": "Transportation",
      "value": 1.39
    },
    {
      "source": "Biomass",
      "target": "Transportation",
      "value": 1.89
    },
    {
      "source": "Petroleum",
      "target": "Transportation",
      "value": 24.97
    },
    {
      "source": "Electricity generation",
      "target": "Residential",
      "value": 5.06
    },
    {
      "source": "Electricity generation",
      "target": "Commercial",
      "value": 4.95
    },
    {
      "source": "Electricity generation",
      "target": "Industrial",
      "value": 3.53
    },
    {
      "source": "Electricity generation",
      "target": "Transportation",
      "value": 0.02
    },
    {
      "source": "Electricity generation",
      "target": "Rejected energy",
      "value": 19.21
    },
    {
      "source": "Residential",
      "target": "Rejected energy",
      "value": 3.93
    },
    {
      "source": "Residential",
      "target": "Energy services",
      "value": 7.3
    },
    {
      "source": "Commercial",
      "target": "Rejected energy",
      "value": 3.32
    },
    {
      "source": "Commercial",
      "target": "Energy services",
      "value": 6.17
    },
    {
      "source": "Industrial",
      "target": "Rejected energy",
      "value": 13.46
    },
    {
      "source": "Industrial",
      "target": "Energy services",
      "value": 12.93
    },
    {
      "source": "Transportation",
      "target": "Rejected energy",
      "value": 22.35
    },
    {
      "source": "Transportation",
      "target": "Energy services",
      "value": 5.94
    }
  ],
  "encoding": {
    "source": {
      "field": "source",
      "type": "nominal"
    },
    "target": {
      "field": "target",
      "type": "nominal"
    },
    "value": {
      "field": "value",
      "type": "quantitative",
      "format": ".2f"
    }
  },
  "chrome": {
    "title": "Rejected energy was 66% of US energy consumption in 2024",
    "subtitle": "Energy from each fuel through electricity generation and end-use sectors, 2024 (quadrillion BTU)"
  }
}
```

The diagram draws once the page's script runs, so the figure reserves its height until then. Hovering a node or a ribbon highlights every link and node upstream and downstream of it and dims the rest.

## A funnel

A funnel is a sankey whose drop-off branches end early. Each stage splits into the readers who go on and the ones who leave, so the losses read as clearly as the conversions. The default `nodeAlign: "justify"` would move Bounce, Drop Off and Churned to the last column; `nodeAlign: "left"` keeps each one beside the stage it leaves. `",.0f"` on the value channel prints whole numbers with thousands separators.

```json
{
  "type": "sankey",
  "data": [
    {
      "source": "Landing Page",
      "target": "Sign Up",
      "value": 3200
    },
    {
      "source": "Landing Page",
      "target": "Bounce",
      "value": 6800
    },
    {
      "source": "Sign Up",
      "target": "Onboarding",
      "value": 2400
    },
    {
      "source": "Sign Up",
      "target": "Drop Off",
      "value": 800
    },
    {
      "source": "Onboarding",
      "target": "Active User",
      "value": 1800
    },
    {
      "source": "Onboarding",
      "target": "Churned",
      "value": 600
    },
    {
      "source": "Active User",
      "target": "Premium",
      "value": 720
    },
    {
      "source": "Active User",
      "target": "Free Tier",
      "value": 1080
    }
  ],
  "encoding": {
    "source": {
      "field": "source",
      "type": "nominal"
    },
    "target": {
      "field": "target",
      "type": "nominal"
    },
    "value": {
      "field": "value",
      "type": "quantitative",
      "format": ",.0f"
    }
  },
  "nodeAlign": "left",
  "chrome": {
    "title": "720 of 10,000 visitors became paying users",
    "subtitle": "One month's visitors from landing page to plan"
  }
}
```

## Link color

Nodes take the palette's colors in turn. `linkStyle` sets how the ribbons between them are colored: `"gradient"` (the default) runs from the source node's color to the target node's color, `"source"` and `"target"` use one end's color all the way, and `"neutral"` uses the theme's gridline color. Coloring by source makes it easy to follow one source, such as petroleum, to every place it goes.

```json
{
  "type": "sankey",
  "data": [
    {
      "source": "Coal",
      "target": "Electricity",
      "value": 46.5
    },
    {
      "source": "Natural Gas",
      "target": "Electricity",
      "value": 38.2
    },
    {
      "source": "Natural Gas",
      "target": "Heating",
      "value": 25.8
    },
    {
      "source": "Nuclear",
      "target": "Electricity",
      "value": 19.7
    },
    {
      "source": "Solar",
      "target": "Electricity",
      "value": 10.3
    },
    {
      "source": "Wind",
      "target": "Electricity",
      "value": 14.1
    },
    {
      "source": "Petroleum",
      "target": "Transport",
      "value": 55.4
    },
    {
      "source": "Petroleum",
      "target": "Industry",
      "value": 12.3
    },
    {
      "source": "Electricity",
      "target": "Residential",
      "value": 38.5
    },
    {
      "source": "Electricity",
      "target": "Commercial",
      "value": 35.8
    },
    {
      "source": "Electricity",
      "target": "Industry",
      "value": 34.5
    },
    {
      "source": "Heating",
      "target": "Residential",
      "value": 15.2
    },
    {
      "source": "Heating",
      "target": "Commercial",
      "value": 10.6
    },
    {
      "source": "Transport",
      "target": "Passenger",
      "value": 32.1
    },
    {
      "source": "Transport",
      "target": "Freight",
      "value": 23.3
    }
  ],
  "encoding": {
    "source": {
      "field": "source",
      "type": "nominal"
    },
    "target": {
      "field": "target",
      "type": "nominal"
    },
    "value": {
      "field": "value",
      "type": "quantitative",
      "format": ".1f"
    }
  },
  "linkStyle": "source",
  "chrome": {
    "title": "Petroleum feeds transport and industry directly",
    "subtitle": "Links take the color of the node they leave (quadrillion BTU)"
  }
}
```

## Merging small nodes

`other` merges nodes that carry less than a share of their column's total into one node named "Other", drawn in a neutral color. Here solar and wind each carry under 7% of the sources column, so they merge. Links into and out of merged nodes move to the new node, so the total flow does not change. A column with only one node under the threshold is left alone, and `{ threshold, label }` renames the merged node. Without `other`, every node is drawn.

```json
{
  "type": "sankey",
  "data": [
    {
      "source": "Coal",
      "target": "Electricity",
      "value": 46.5
    },
    {
      "source": "Natural Gas",
      "target": "Electricity",
      "value": 38.2
    },
    {
      "source": "Natural Gas",
      "target": "Heating",
      "value": 25.8
    },
    {
      "source": "Nuclear",
      "target": "Electricity",
      "value": 19.7
    },
    {
      "source": "Solar",
      "target": "Electricity",
      "value": 10.3
    },
    {
      "source": "Wind",
      "target": "Electricity",
      "value": 14.1
    },
    {
      "source": "Petroleum",
      "target": "Transport",
      "value": 55.4
    },
    {
      "source": "Petroleum",
      "target": "Industry",
      "value": 12.3
    },
    {
      "source": "Electricity",
      "target": "Residential",
      "value": 38.5
    },
    {
      "source": "Electricity",
      "target": "Commercial",
      "value": 35.8
    },
    {
      "source": "Electricity",
      "target": "Industry",
      "value": 34.5
    },
    {
      "source": "Heating",
      "target": "Residential",
      "value": 15.2
    },
    {
      "source": "Heating",
      "target": "Commercial",
      "value": 10.6
    },
    {
      "source": "Transport",
      "target": "Passenger",
      "value": 32.1
    },
    {
      "source": "Transport",
      "target": "Freight",
      "value": 23.3
    }
  ],
  "encoding": {
    "source": {
      "field": "source",
      "type": "nominal"
    },
    "target": {
      "field": "target",
      "type": "nominal"
    },
    "value": {
      "field": "value",
      "type": "quantitative",
      "format": ".1f"
    }
  },
  "other": 0.07,
  "chrome": {
    "title": "Solar and wind merge into one node under a 7% threshold",
    "subtitle": "Sources under 7% of their column's flow become Other (quadrillion BTU)"
  }
}
```

## Formatting values

The value channel's `format` takes a d3-format string and applies to every label. `"$,.1f"` prints a dollar sign, thousands separators and one decimal, which suits amounts in millions.

```json
{
  "type": "sankey",
  "data": [
    {
      "source": "Product Sales",
      "target": "Engineering",
      "value": 18.4
    },
    {
      "source": "Product Sales",
      "target": "Marketing",
      "value": 8.2
    },
    {
      "source": "Product Sales",
      "target": "Operations",
      "value": 6.5
    },
    {
      "source": "Product Sales",
      "target": "Sales",
      "value": 5.1
    },
    {
      "source": "Services",
      "target": "Engineering",
      "value": 7.6
    },
    {
      "source": "Services",
      "target": "Operations",
      "value": 9.3
    },
    {
      "source": "Services",
      "target": "Sales",
      "value": 3.8
    },
    {
      "source": "Licensing",
      "target": "Engineering",
      "value": 4.2
    },
    {
      "source": "Licensing",
      "target": "R&D",
      "value": 12.1
    },
    {
      "source": "Licensing",
      "target": "Admin",
      "value": 2.4
    },
    {
      "source": "Other Revenue",
      "target": "Marketing",
      "value": 3.5
    },
    {
      "source": "Other Revenue",
      "target": "R&D",
      "value": 2.8
    },
    {
      "source": "Other Revenue",
      "target": "Admin",
      "value": 4.1
    }
  ],
  "encoding": {
    "source": {
      "field": "source",
      "type": "nominal"
    },
    "target": {
      "field": "target",
      "type": "nominal"
    },
    "value": {
      "field": "value",
      "type": "quantitative",
      "format": "$,.1f"
    }
  },
  "chrome": {
    "title": "Engineering takes the largest share of revenue",
    "subtitle": "Revenue allocation by department ($ millions)"
  }
}
```

## On a phone

When the labels outside the first and last columns would leave the ribbons less than half the width, which happens on most phones, the labels move onto the flow. Labels in the first and middle columns sit just right of their node, labels in the last column just left of it, and each wraps within the gap to the next column with its value on its own line below.

A halo in the background color keeps each label readable over any ribbon, and labels on small neighboring nodes are nudged apart. Setting `nodeLabelAlign` to `"left"` or `"right"` keeps labels where you put them at every width.

A phone also has less room for a long tail of small nodes. `overrides` changes fields at one breakpoint, `compact` under 400px or `medium` from 400 to 700px: `chrome` merges with the top-level chrome, and `other`, `nodeSort`, `nodeLabelAlign` and `nodePadding` replace their top-level values. This folds every node under 2% of its column into "Other" on phones only:

```json
{
  "overrides": {
    "compact": { "other": 0.02 },
    "medium": { "other": 0.02 }
  }
}
```

## Fields that matter

| Field | What it does |
| --- | --- |
| `type` | `"sankey"` selects this visualization. |
| `data` | One row per link. |
| `encoding.source`, `encoding.target` | The fields that name each link's two nodes. |
| `encoding.value` | The field that sets each ribbon's width; its `format` sets the label numbers. |
| `nodeAlign` | `"justify"` (the default) moves nodes with no outgoing links to the last column; `"left"` keeps each at its depth. `"right"` and `"center"` are also available. |
| `linkStyle` | `"gradient"`, `"source"`, `"target"` or `"neutral"` ribbon color. Default `"gradient"`. |
| `other` | A share of the column total below which nodes merge into "Other". Off unless set. |
| `overrides` | Fields that change at the `compact` or `medium` breakpoint. |
| `chrome` | The title and subtitle above the diagram. |

## Related

- [Graph](/docs/flow/graph/) for networks where the connections matter more than the amounts.
- [Grouped and stacked bars](/docs/comparison/grouped-stacked/) for one split without stages.
- [Stacked to 100%](/docs/part-to-whole/stacked-100/) for the shares of one whole.
- [SankeySpec reference](/docs/spec/sankey-spec/) lists every sankey field.
