| [e4c61dd] | 1 | # d3-sankey
|
|---|
| 2 |
|
|---|
| 3 | Sankey diagrams visualize the directed flow between nodes in an acyclic network. For example, this diagram shows a possible scenario of UK energy production and consumption in 2050:
|
|---|
| 4 |
|
|---|
| 5 | [<img alt="Sankey diagram" src="https://raw.githubusercontent.com/d3/d3-sankey/master/img/energy.png" width="960">](https://observablehq.com/@d3/sankey-diagram)
|
|---|
| 6 |
|
|---|
| 7 | Source: Department of Energy & Climate Change, Tom Counsell.
|
|---|
| 8 |
|
|---|
| 9 | **For an interactive editor, see [Flow-o-Matic](https://observablehq.com/@mbostock/flow-o-matic).**
|
|---|
| 10 |
|
|---|
| 11 | ## Installing
|
|---|
| 12 |
|
|---|
| 13 | If you use NPM, `npm install d3-sankey`. Otherwise, download the [latest release](https://github.com/d3/d3-sankey/releases/latest). You can also load directly from [unpkg.com](https://unpkg.com/d3-sankey/). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported:
|
|---|
| 14 |
|
|---|
| 15 | ```html
|
|---|
| 16 | <script src="https://unpkg.com/d3-array@1"></script>
|
|---|
| 17 | <script src="https://unpkg.com/d3-collection@1"></script>
|
|---|
| 18 | <script src="https://unpkg.com/d3-path@1"></script>
|
|---|
| 19 | <script src="https://unpkg.com/d3-shape@1"></script>
|
|---|
| 20 | <script src="https://unpkg.com/d3-sankey@0"></script>
|
|---|
| 21 | <script>
|
|---|
| 22 |
|
|---|
| 23 | var sankey = d3.sankey();
|
|---|
| 24 |
|
|---|
| 25 | </script>
|
|---|
| 26 | ```
|
|---|
| 27 |
|
|---|
| 28 | ## API Reference
|
|---|
| 29 |
|
|---|
| 30 | <a href="#sankey" name="sankey">#</a> d3.<b>sankey</b>() [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 31 |
|
|---|
| 32 | Constructs a new Sankey generator with the default settings.
|
|---|
| 33 |
|
|---|
| 34 | <a href="#_sankey" name="_sankey">#</a> <i>sankey</i>(<i>arguments</i>…) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 35 |
|
|---|
| 36 | Computes the node and link positions for the given *arguments*, returning a *graph* representing the Sankey layout. The returned *graph* has the following properties:
|
|---|
| 37 |
|
|---|
| 38 | * *graph*.nodes - the array of [nodes](#sankey_nodes)
|
|---|
| 39 | * *graph*.links - the array of [links](#sankey_links)
|
|---|
| 40 |
|
|---|
| 41 | <a href="#sankey_update" name="sankey_update">#</a> <i>sankey</i>.<b>update</b>(<i>graph</i>) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 42 |
|
|---|
| 43 | Recomputes the specified *graph*’s links’ positions, updating the following properties of each *link*:
|
|---|
| 44 |
|
|---|
| 45 | * *link*.y0 - the link’s vertical starting position (at source node)
|
|---|
| 46 | * *link*.y1 - the link’s vertical end position (at target node)
|
|---|
| 47 |
|
|---|
| 48 | This method is intended to be called after computing the initial [Sankey layout](#_sankey), for example when the diagram is repositioned interactively.
|
|---|
| 49 |
|
|---|
| 50 | <a name="sankey_nodes" href="#sankey_nodes">#</a> <i>sankey</i>.<b>nodes</b>([<i>nodes</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 51 |
|
|---|
| 52 | If *nodes* is specified, sets the Sankey generator’s nodes accessor to the specified function or array and returns this Sankey generator. If *nodes* is not specified, returns the current nodes accessor, which defaults to:
|
|---|
| 53 |
|
|---|
| 54 | ```js
|
|---|
| 55 | function nodes(graph) {
|
|---|
| 56 | return graph.nodes;
|
|---|
| 57 | }
|
|---|
| 58 | ```
|
|---|
| 59 |
|
|---|
| 60 | If *nodes* is specified as a function, the function is invoked when the Sankey layout is [generated](#_sankey), being passed any arguments passed to the Sankey generator. This function must return an array of nodes. If *nodes* is not a function, it must be a constant array of *nodes*.
|
|---|
| 61 |
|
|---|
| 62 | Each *node* must be an object. The following properties are assigned by the [Sankey generator](#_sankey):
|
|---|
| 63 |
|
|---|
| 64 | * *node*.sourceLinks - the array of outgoing [links](#sankey_links) which have this node as their source
|
|---|
| 65 | * *node*.targetLinks - the array of incoming [links](#sankey_links) which have this node as their target
|
|---|
| 66 | * *node*.value - the node’s value; this is the sum of *link*.value for the node’s incoming [links](#sankey_links), or *node*.fixedValue if defined
|
|---|
| 67 | * *node*.index - the node’s zero-based index within the array of nodes
|
|---|
| 68 | * *node*.depth - the node’s zero-based graph depth, derived from the graph topology
|
|---|
| 69 | * *node*.height - the node’s zero-based graph height, derived from the graph topology
|
|---|
| 70 | * *node*.layer - the node’s zero-based column index, corresponding to its horizontal position
|
|---|
| 71 | * *node*.x0 - the node’s minimum horizontal position, derived from *node*.depth
|
|---|
| 72 | * *node*.x1 - the node’s maximum horizontal position (*node*.x0 + [*sankey*.nodeWidth](#sankey_nodeWidth))
|
|---|
| 73 | * *node*.y0 - the node’s minimum vertical position
|
|---|
| 74 | * *node*.y1 - the node’s maximum vertical position (*node*.y1 - *node*.y0 is proportional to *node*.value)
|
|---|
| 75 |
|
|---|
| 76 | See also [*sankey*.links](#sankey_links).
|
|---|
| 77 |
|
|---|
| 78 | <a name="sankey_links" href="#sankey_links">#</a> <i>sankey</i>.<b>links</b>([<i>links</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 79 |
|
|---|
| 80 | If *links* is specified, sets the Sankey generator’s links accessor to the specified function or array and returns this Sankey generator. If *links* is not specified, returns the current links accessor, which defaults to:
|
|---|
| 81 |
|
|---|
| 82 | ```js
|
|---|
| 83 | function links(graph) {
|
|---|
| 84 | return graph.links;
|
|---|
| 85 | }
|
|---|
| 86 | ```
|
|---|
| 87 |
|
|---|
| 88 | If *links* is specified as a function, the function is invoked when the Sankey layout is [generated](#_sankey), being passed any arguments passed to the Sankey generator. This function must return an array of links. If *links* is not a function, it must be a constant array of *links*.
|
|---|
| 89 |
|
|---|
| 90 | Each *link* must be an object with the following properties:
|
|---|
| 91 |
|
|---|
| 92 | * *link*.source - the link’s source [node](#sankey_nodes)
|
|---|
| 93 | * *link*.target - the link’s target [node](#sankey_nodes)
|
|---|
| 94 | * *link*.value - the link’s numeric value
|
|---|
| 95 |
|
|---|
| 96 | For convenience, a link’s source and target may be initialized using numeric or string identifiers rather than object references; see [*sankey*.nodeId](#sankey_nodeId). The following properties are assigned to each link by the [Sankey generator](#_sankey):
|
|---|
| 97 |
|
|---|
| 98 | * *link*.y0 - the link’s vertical starting position (at source node)
|
|---|
| 99 | * *link*.y1 - the link’s vertical end position (at target node)
|
|---|
| 100 | * *link*.width - the link’s width (proportional to *link*.value)
|
|---|
| 101 | * *link*.index - the zero-based index of *link* within the array of links
|
|---|
| 102 |
|
|---|
| 103 | <a name="sankey_linkSort" href="#sankey_linkSort">#</a> <i>sankey</i>.<b>linkSort</b>([<i>sort</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 104 |
|
|---|
| 105 | If *sort* is specified, sets the link sort method and returns this Sankey generator. If *sort* is not specified, returns the current link sort method, which defaults to *undefined*, indicating that vertical order of links within each node will be determined automatically by the layout. If *sort* is null, the order is fixed by the input. Otherwise, the specified *sort* function determines the order; the function is passed two links, and must return a value less than 0 if the first link should be above the second, and a value greater than 0 if the second link should be above the first, or 0 if the order is not specified.
|
|---|
| 106 |
|
|---|
| 107 | <a name="sankey_nodeId" href="#sankey_nodeId">#</a> <i>sankey</i>.<b>nodeId</b>([<i>id</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 108 |
|
|---|
| 109 | If *id* is specified, sets the node id accessor to the specified function and returns this Sankey generator. If *id* is not specified, returns the current node id accessor, which defaults to the numeric *node*.index:
|
|---|
| 110 |
|
|---|
| 111 | ```js
|
|---|
| 112 | function id(d) {
|
|---|
| 113 | return d.index;
|
|---|
| 114 | }
|
|---|
| 115 | ```
|
|---|
| 116 |
|
|---|
| 117 | The default id accessor allows each link’s source and target to be specified as a zero-based index into the [nodes](#sankey_nodes) array. For example:
|
|---|
| 118 |
|
|---|
| 119 | ```js
|
|---|
| 120 | var nodes = [
|
|---|
| 121 | {"id": "Alice"},
|
|---|
| 122 | {"id": "Bob"},
|
|---|
| 123 | {"id": "Carol"}
|
|---|
| 124 | ];
|
|---|
| 125 |
|
|---|
| 126 | var links = [
|
|---|
| 127 | {"source": 0, "target": 1}, // Alice → Bob
|
|---|
| 128 | {"source": 1, "target": 2} // Bob → Carol
|
|---|
| 129 | ];
|
|---|
| 130 | ```
|
|---|
| 131 |
|
|---|
| 132 | Now consider a different id accessor that returns a string:
|
|---|
| 133 |
|
|---|
| 134 | ```js
|
|---|
| 135 | function id(d) {
|
|---|
| 136 | return d.id;
|
|---|
| 137 | }
|
|---|
| 138 | ```
|
|---|
| 139 |
|
|---|
| 140 | With this accessor, you can use named sources and targets:
|
|---|
| 141 |
|
|---|
| 142 | ```js
|
|---|
| 143 | var nodes = [
|
|---|
| 144 | {"id": "Alice"},
|
|---|
| 145 | {"id": "Bob"},
|
|---|
| 146 | {"id": "Carol"}
|
|---|
| 147 | ];
|
|---|
| 148 |
|
|---|
| 149 | var links = [
|
|---|
| 150 | {"source": "Alice", "target": "Bob"},
|
|---|
| 151 | {"source": "Bob", "target": "Carol"}
|
|---|
| 152 | ];
|
|---|
| 153 | ```
|
|---|
| 154 |
|
|---|
| 155 | This is particularly useful when representing graphs in JSON, as JSON does not allow references. See [this example](https://bl.ocks.org/mbostock/f584aa36df54c451c94a9d0798caed35).
|
|---|
| 156 |
|
|---|
| 157 | <a name="sankey_nodeAlign" href="#sankey_nodeAlign">#</a> <i>sankey</i>.<b>nodeAlign</b>([<i>align</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 158 |
|
|---|
| 159 | If *align* is specified, sets the node [alignment method](#alignments) to the specified function and returns this Sankey generator. If *align* is not specified, returns the current node alignment method, which defaults to [d3.sankeyJustify](#sankeyJustify). The specified function is evaluated for each input *node* in order, being passed the current *node* and the total depth *n* of the graph (one plus the maximum *node*.depth), and must return an integer between 0 and *n* - 1 that indicates the desired horizontal position of the node in the generated Sankey diagram.
|
|---|
| 160 |
|
|---|
| 161 | <a name="sankey_nodeSort" href="#sankey_nodeSort">#</a> <i>sankey</i>.<b>nodeSort</b>([<i>sort</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 162 |
|
|---|
| 163 | If *sort* is specified, sets the node sort method and returns this Sankey generator. If *sort* is not specified, returns the current node sort method, which defaults to *undefined*, indicating that vertical order of nodes within each column will be determined automatically by the layout. If *sort* is null, the order is fixed by the input. Otherwise, the specified *sort* function determines the order; the function is passed two nodes, and must return a value less than 0 if the first node should be above the second, and a value greater than 0 if the second node should be above the first, or 0 if the order is not specified.
|
|---|
| 164 |
|
|---|
| 165 | <a name="sankey_nodeWidth" href="#sankey_nodeWidth">#</a> <i>sankey</i>.<b>nodeWidth</b>([<i>width</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 166 |
|
|---|
| 167 | If *width* is specified, sets the node width to the specified number and returns this Sankey generator. If *width* is not specified, returns the current node width, which defaults to 24.
|
|---|
| 168 |
|
|---|
| 169 | <a name="sankey_nodePadding" href="#sankey_nodePadding">#</a> <i>sankey</i>.<b>nodePadding</b>([<i>padding</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 170 |
|
|---|
| 171 | If *padding* is specified, sets the vertical separation between nodes at each column to the specified number and returns this Sankey generator. If *padding* is not specified, returns the current node padding, which defaults to 8.
|
|---|
| 172 |
|
|---|
| 173 | <a name="sankey_extent" href="#sankey_extent">#</a> <i>sankey</i>.<b>extent</b>([<i>extent</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 174 |
|
|---|
| 175 | If *extent* is specified, sets the extent of the Sankey layout to the specified bounds and returns the layout. The *extent* bounds are specified as an array \[\[<i>x0</i>, <i>y0</i>\], \[<i>x1</i>, <i>y1</i>\]\], where *x0* is the left side of the extent, *y0* is the top, *x1* is the right and *y1* is the bottom. If *extent* is not specified, returns the current extent which defaults to [[0, 0], [1, 1]].
|
|---|
| 176 |
|
|---|
| 177 | <a name="sankey_size" href="#sankey_size">#</a> <i>sankey</i>.<b>size</b>([<i>size</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 178 |
|
|---|
| 179 | An alias for [*sankey*.extent](#sankey_extent) where the minimum *x* and *y* of the extent are ⟨0,0⟩. Equivalent to:
|
|---|
| 180 |
|
|---|
| 181 | ```js
|
|---|
| 182 | sankey.extent([[0, 0], size]);
|
|---|
| 183 | ```
|
|---|
| 184 |
|
|---|
| 185 | <a name="sankey_iterations" href="#sankey_iterations">#</a> <i>sankey</i>.<b>iterations</b>([<i>iterations</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source")
|
|---|
| 186 |
|
|---|
| 187 | If *iterations* is specified, sets the number of relaxation iterations when [generating the layout](#_sankey) and returns this Sankey generator. If *iterations* is not specified, returns the current number of relaxation iterations, which defaults to 6.
|
|---|
| 188 |
|
|---|
| 189 | ### Alignments
|
|---|
| 190 |
|
|---|
| 191 | See [*sankey*.nodeAlign](#sankey_nodeAlign).
|
|---|
| 192 |
|
|---|
| 193 | <a name="sankeyLeft" href="#sankeyLeft">#</a> d3.<b>sankeyLeft</b>(<i>node</i>, <i>n</i>) [<>](https://github.com/d3/d3-sankey/blob/master/src/align.js "Source")
|
|---|
| 194 |
|
|---|
| 195 | [<img alt="left" src="https://raw.githubusercontent.com/d3/d3-sankey/master/img/align-left.png" width="480">](https://observablehq.com/@d3/sankey-diagram?align=left)
|
|---|
| 196 |
|
|---|
| 197 | Returns *node*.depth.
|
|---|
| 198 |
|
|---|
| 199 | <a name="sankeyRight" href="#sankeyRight">#</a> d3.<b>sankeyRight</b>(<i>node</i>, <i>n</i>) [<>](https://github.com/d3/d3-sankey/blob/master/src/align.js "Source")
|
|---|
| 200 |
|
|---|
| 201 | [<img alt="right" src="https://raw.githubusercontent.com/d3/d3-sankey/master/img/align-right.png" width="480">](https://observablehq.com/@d3/sankey-diagram?align=right)
|
|---|
| 202 |
|
|---|
| 203 | Returns *n* - 1 - *node*.height.
|
|---|
| 204 |
|
|---|
| 205 | <a name="sankeyCenter" href="#sankeyCenter">#</a> d3.<b>sankeyCenter</b>(<i>node</i>, <i>n</i>) [<>](https://github.com/d3/d3-sankey/blob/master/src/align.js "Source")
|
|---|
| 206 |
|
|---|
| 207 | [<img alt="center" src="https://raw.githubusercontent.com/d3/d3-sankey/master/img/align-center.png" width="480">](https://observablehq.com/@d3/sankey-diagram?align=center)
|
|---|
| 208 |
|
|---|
| 209 | Like [d3.sankeyLeft](#sankeyLeft), except that nodes without any incoming links are moved as right as possible.
|
|---|
| 210 |
|
|---|
| 211 | <a name="sankeyJustify" href="#sankeyJustify">#</a> d3.<b>sankeyJustify</b>(<i>node</i>, <i>n</i>) [<>](https://github.com/d3/d3-sankey/blob/master/src/align.js "Source")
|
|---|
| 212 |
|
|---|
| 213 | [<img alt="justify" src="https://raw.githubusercontent.com/d3/d3-sankey/master/img/energy.png" width="480">](https://observablehq.com/@d3/sankey-diagram)
|
|---|
| 214 |
|
|---|
| 215 | Like [d3.sankeyLeft](#sankeyLeft), except that nodes without any outgoing links are moved to the far right.
|
|---|
| 216 |
|
|---|
| 217 | ### Links
|
|---|
| 218 |
|
|---|
| 219 | <a name="sankeyLinkHorizontal" href="#sankeyLinkHorizontal">#</a> d3.<b>sankeyLinkHorizontal</b>() [<>](https://github.com/d3/d3-sankey/blob/master/src/sankeyLinkHorizontal.js "Source")
|
|---|
| 220 |
|
|---|
| 221 | Returns a [horizontal link shape](https://github.com/d3/d3-shape/blob/master/README.md#linkHorizontal) suitable for a Sankey diagram. The [source accessor](https://github.com/d3/d3-shape/blob/master/README.md#link_source) is defined as:
|
|---|
| 222 |
|
|---|
| 223 | ```js
|
|---|
| 224 | function source(d) {
|
|---|
| 225 | return [d.source.x1, d.y0];
|
|---|
| 226 | }
|
|---|
| 227 | ```
|
|---|
| 228 |
|
|---|
| 229 | The [target accessor](https://github.com/d3/d3-shape/blob/master/README.md#link_target) is defined as:
|
|---|
| 230 |
|
|---|
| 231 | ```js
|
|---|
| 232 | function target(d) {
|
|---|
| 233 | return [d.target.x0, d.y1];
|
|---|
| 234 | }
|
|---|
| 235 | ```
|
|---|
| 236 |
|
|---|
| 237 | For example, to render the links of a Sankey diagram in SVG, you might say:
|
|---|
| 238 |
|
|---|
| 239 | ```js
|
|---|
| 240 | svg.append("g")
|
|---|
| 241 | .attr("fill", "none")
|
|---|
| 242 | .attr("stroke", "#000")
|
|---|
| 243 | .attr("stroke-opacity", 0.2)
|
|---|
| 244 | .selectAll("path")
|
|---|
| 245 | .data(graph.links)
|
|---|
| 246 | .join("path")
|
|---|
| 247 | .attr("d", d3.sankeyLinkHorizontal())
|
|---|
| 248 | .attr("stroke-width", function(d) { return d.width; });
|
|---|
| 249 | ```
|
|---|