Skip to content
OpenChart

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 instead when there is one split and no stages in between, and a 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.

US energy from source to end use in 2024. This chart is drawn in the browser.

US energy from source to end use in 2024. Source: Lawrence Livermore National Laboratory, Estimated U.S. Energy Consumption in 2024 (data: DOE/EIA).
{
  "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
    },
    // … 36 more rows; Copy includes them
  ],
  "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)"
  }
}
import { Sankey } from "@opendata-ai/openchart-react";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 42 rows; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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)"
  }
};

export function Example() {
  return <Sankey spec={spec} />;
}
<script setup lang="ts">
import { Sankey } from "@opendata-ai/openchart-vue";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 42 rows; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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)"
  }
};
</script>

<template>
  <Sankey :spec="spec" />
</template>
<script lang="ts">
import { Sankey } from "@opendata-ai/openchart-svelte";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 42 rows; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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)"
  }
};
</script>

<Sankey {spec} />
import { createSankey } from "@opendata-ai/openchart-vanilla";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 42 rows; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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)"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createSankey(container, spec);

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.

A product funnel from landing page to paid plan. This chart is drawn in the browser.

A product funnel from landing page to paid plan. Source: Illustrative data.
{
  "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"
  }
}
import { Sankey } from "@opendata-ai/openchart-react";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 8 rows, as in the Spec tab; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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"
  }
};

export function Example() {
  return <Sankey spec={spec} />;
}
<script setup lang="ts">
import { Sankey } from "@opendata-ai/openchart-vue";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 8 rows, as in the Spec tab; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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"
  }
};
</script>

<template>
  <Sankey :spec="spec" />
</template>
<script lang="ts">
import { Sankey } from "@opendata-ai/openchart-svelte";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 8 rows, as in the Spec tab; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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"
  }
};
</script>

<Sankey {spec} />
import { createSankey } from "@opendata-ai/openchart-vanilla";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 8 rows, as in the Spec tab; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createSankey(container, spec);

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.

Energy flow with links colored by source. This chart is drawn in the browser.

Energy flow with links colored by source. Source: Illustrative data, after the EIA annual energy flow.

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.

Energy flow with small sources merged. This chart is drawn in the browser.

Energy flow with small sources merged. Source: Illustrative data, after the EIA annual energy flow.
{
  "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
    },
    // … 9 more rows; Copy includes them
  ],
  "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)"
  }
}
import { Sankey } from "@opendata-ai/openchart-react";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 15 rows; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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)"
  }
};

export function Example() {
  return <Sankey spec={spec} />;
}
<script setup lang="ts">
import { Sankey } from "@opendata-ai/openchart-vue";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 15 rows; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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)"
  }
};
</script>

<template>
  <Sankey :spec="spec" />
</template>
<script lang="ts">
import { Sankey } from "@opendata-ai/openchart-svelte";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 15 rows; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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)"
  }
};
</script>

<Sankey {spec} />
import { createSankey } from "@opendata-ai/openchart-vanilla";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 15 rows; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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)"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createSankey(container, spec);

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.

Revenue streams allocated to departments. This chart is drawn in the browser.

Revenue streams allocated to departments. Source: Illustrative data.
{
  "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
    },
    // … 7 more rows; Copy includes them
  ],
  "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)"
  }
}
import { Sankey } from "@opendata-ai/openchart-react";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 13 rows; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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)"
  }
};

export function Example() {
  return <Sankey spec={spec} />;
}
<script setup lang="ts">
import { Sankey } from "@opendata-ai/openchart-vue";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 13 rows; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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)"
  }
};
</script>

<template>
  <Sankey :spec="spec" />
</template>
<script lang="ts">
import { Sankey } from "@opendata-ai/openchart-svelte";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 13 rows; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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)"
  }
};
</script>

<Sankey {spec} />
import { createSankey } from "@opendata-ai/openchart-vanilla";
import type { SankeySpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 13 rows; Copy includes them */];

const spec: SankeySpec = {
  data,
  "type": "sankey",
  "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)"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createSankey(container, spec);

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:

{
  "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.