| 1 | # d3-chord
|
|---|
| 2 |
|
|---|
| 3 | Visualize relationships or network flow with an aesthetically-pleasing circular layout.
|
|---|
| 4 |
|
|---|
| 5 | [<img alt="Chord Diagram" src="https://raw.githubusercontent.com/d3/d3-chord/master/img/chord.png" width="480" height="480">](https://observablehq.com/@d3/chord-diagram)
|
|---|
| 6 |
|
|---|
| 7 | ## Installing
|
|---|
| 8 |
|
|---|
| 9 | If you use npm, `npm install d3-chord`. You can also download the [latest release on GitHub](https://github.com/d3/d3-chord/releases/latest). For vanilla HTML in modern browsers, import d3-chord from Skypack:
|
|---|
| 10 |
|
|---|
| 11 | ```html
|
|---|
| 12 | <script type="module">
|
|---|
| 13 |
|
|---|
| 14 | import {chord} from "https://cdn.skypack.dev/d3-chord@3";
|
|---|
| 15 |
|
|---|
| 16 | const c = chord();
|
|---|
| 17 |
|
|---|
| 18 | </script>
|
|---|
| 19 | ```
|
|---|
| 20 |
|
|---|
| 21 | For legacy environments, you can load d3-chord’s UMD bundle from an npm-based CDN such as jsDelivr; a `d3` global is exported:
|
|---|
| 22 |
|
|---|
| 23 | ```html
|
|---|
| 24 | <script src="https://cdn.jsdelivr.net/npm/d3-path@3"></script>
|
|---|
| 25 | <script src="https://cdn.jsdelivr.net/npm/d3-chord@3"></script>
|
|---|
| 26 | <script>
|
|---|
| 27 |
|
|---|
| 28 | const chord = d3.chord();
|
|---|
| 29 |
|
|---|
| 30 | </script>
|
|---|
| 31 | ```
|
|---|
| 32 |
|
|---|
| 33 | ## API Reference
|
|---|
| 34 |
|
|---|
| 35 | <a href="#chord" name="chord">#</a> d3.<b>chord</b>() · [Source](https://github.com/d3/d3-chord/blob/master/src/chord.js)
|
|---|
| 36 |
|
|---|
| 37 | Constructs a new chord layout with the default settings.
|
|---|
| 38 |
|
|---|
| 39 | <a href="#_chord" name="_chord">#</a> <i>chord</i>(<i>matrix</i>) · [Source](https://github.com/d3/d3-chord/blob/master/src/chord.js)
|
|---|
| 40 |
|
|---|
| 41 | Computes the chord layout for the specified square *matrix* of size *n*×*n*, where the *matrix* represents the directed flow amongst a network (a complete digraph) of *n* nodes. The given *matrix* must be an array of length *n*, where each element *matrix*[*i*] is an array of *n* numbers, where each *matrix*[*i*][*j*] represents the flow from the *i*th node in the network to the *j*th node. Each number *matrix*[*i*][*j*] must be nonnegative, though it can be zero if there is no flow from node *i* to node *j*. From the [Circos tableviewer example](http://mkweb.bcgsc.ca/circos/guide/tables/):
|
|---|
| 42 |
|
|---|
| 43 | ```js
|
|---|
| 44 | const matrix = [
|
|---|
| 45 | [11975, 5871, 8916, 2868],
|
|---|
| 46 | [ 1951, 10048, 2060, 6171],
|
|---|
| 47 | [ 8010, 16145, 8090, 8045],
|
|---|
| 48 | [ 1013, 990, 940, 6907]
|
|---|
| 49 | ];
|
|---|
| 50 | ```
|
|---|
| 51 |
|
|---|
| 52 | The return value of *chord*(*matrix*) is an array of *chords*, where each chord represents the combined bidirectional flow between two nodes *i* and *j* (where *i* may be equal to *j*) and is an object with the following properties:
|
|---|
| 53 |
|
|---|
| 54 | * `source` - the source subgroup
|
|---|
| 55 | * `target` - the target subgroup
|
|---|
| 56 |
|
|---|
| 57 | Each source and target subgroup is also an object with the following properties:
|
|---|
| 58 |
|
|---|
| 59 | * `startAngle` - the start angle in radians
|
|---|
| 60 | * `endAngle` - the end angle in radians
|
|---|
| 61 | * `value` - the flow value *matrix*[*i*][*j*]
|
|---|
| 62 | * `index` - the node index *i*
|
|---|
| 63 |
|
|---|
| 64 | The chords are typically passed to [d3.ribbon](#ribbon) to display the network relationships. The returned array includes only chord objects for which the value *matrix*[*i*][*j*] or *matrix*[*j*][*i*] is non-zero. Furthermore, the returned array only contains unique chords: a given chord *ij* represents the bidirectional flow from *i* to *j* *and* from *j* to *i*, and does not contain a duplicate chord *ji*; *i* and *j* are chosen such that the chord’s source always represents the larger of *matrix*[*i*][*j*] and *matrix*[*j*][*i*].
|
|---|
| 65 |
|
|---|
| 66 | The *chords* array also defines a secondary array of length *n*, *chords*.groups, where each group represents the combined outflow for node *i*, corresponding to the elements *matrix*[*i*][0 … *n* - 1], and is an object with the following properties:
|
|---|
| 67 |
|
|---|
| 68 | * `startAngle` - the start angle in radians
|
|---|
| 69 | * `endAngle` - the end angle in radians
|
|---|
| 70 | * `value` - the total outgoing flow value for node *i*
|
|---|
| 71 | * `index` - the node index *i*
|
|---|
| 72 |
|
|---|
| 73 | The groups are typically passed to [d3.arc](https://github.com/d3/d3-shape#arc) to produce a donut chart around the circumference of the chord layout.
|
|---|
| 74 |
|
|---|
| 75 | <a href="#chord_padAngle" name="chord_padAngle">#</a> <i>chord</i>.<b>padAngle</b>([<i>angle</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/chord.js)
|
|---|
| 76 |
|
|---|
| 77 | If *angle* is specified, sets the pad angle between adjacent groups to the specified number in radians and returns this chord layout. If *angle* is not specified, returns the current pad angle, which defaults to zero.
|
|---|
| 78 |
|
|---|
| 79 | <a href="#chord_sortGroups" name="chord_sortGroups">#</a> <i>chord</i>.<b>sortGroups</b>([<i>compare</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/chord.js)
|
|---|
| 80 |
|
|---|
| 81 | If *compare* is specified, sets the group comparator to the specified function or null and returns this chord layout. If *compare* is not specified, returns the current group comparator, which defaults to null. If the group comparator is non-null, it is used to sort the groups by their total outflow. See also [d3.ascending](https://github.com/d3/d3-array/blob/master/README.md#ascending) and [d3.descending](https://github.com/d3/d3-array/blob/master/README.md#descending).
|
|---|
| 82 |
|
|---|
| 83 | <a href="#chord_sortSubgroups" name="chord_sortSubgroups">#</a> <i>chord</i>.<b>sortSubgroups</b>([<i>compare</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/chord.js)
|
|---|
| 84 |
|
|---|
| 85 | If *compare* is specified, sets the subgroup comparator to the specified function or null and returns this chord layout. If *compare* is not specified, returns the current subgroup comparator, which defaults to null. If the subgroup comparator is non-null, it is used to sort the subgroups corresponding to *matrix*[*i*][0 … *n* - 1] for a given group *i* by their total outflow. See also [d3.ascending](https://github.com/d3/d3-array/blob/master/README.md#ascending) and [d3.descending](https://github.com/d3/d3-array/blob/master/README.md#descending).
|
|---|
| 86 |
|
|---|
| 87 | <a href="#chord_sortChords" name="chord_sortChords">#</a> <i>chord</i>.<b>sortChords</b>([<i>compare</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/chord.js)
|
|---|
| 88 |
|
|---|
| 89 | If *compare* is specified, sets the chord comparator to the specified function or null and returns this chord layout. If *compare* is not specified, returns the current chord comparator, which defaults to null. If the chord comparator is non-null, it is used to sort the [chords](#_chord) by their combined flow; this only affects the *z*-order of the chords. See also [d3.ascending](https://github.com/d3/d3-array/blob/master/README.md#ascending) and [d3.descending](https://github.com/d3/d3-array/blob/master/README.md#descending).
|
|---|
| 90 |
|
|---|
| 91 | <a href="#chordDirected" name="chordDirected">#</a> d3.<b>chordDirected</b>() · [Source](https://github.com/d3/d3-chord/blob/master/src/chord.js), [Examples](https://observablehq.com/@d3/directed-chord-diagram)
|
|---|
| 92 |
|
|---|
| 93 | A chord layout for directional flows. The chord from *i* to *j* is generated from the value in *matrix*[*i*][*j*] only.
|
|---|
| 94 |
|
|---|
| 95 | <a href="#chordTranspose" name="chordTranspose">#</a> d3.<b>chordTranspose</b>() · [Source](https://github.com/d3/d3-chord/blob/master/src/chord.js)
|
|---|
| 96 |
|
|---|
| 97 | A transposed chord layout. Useful to highlight outgoing (rather than incoming) flows.
|
|---|
| 98 |
|
|---|
| 99 | <a href="#ribbon" name="ribbon">#</a> d3.<b>ribbon</b>() · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 100 |
|
|---|
| 101 | Creates a new ribbon generator with the default settings.
|
|---|
| 102 |
|
|---|
| 103 | <a href="#_ribbon" name="_ribbon">#</a> <i>ribbon</i>(<i>arguments…</i>) · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 104 |
|
|---|
| 105 | Generates a ribbon for the given *arguments*. The *arguments* are arbitrary; they are simply propagated to the ribbon generator’s accessor functions along with the `this` object. For example, with the default settings, a [chord object](#_chord) expected:
|
|---|
| 106 |
|
|---|
| 107 | ```js
|
|---|
| 108 | const ribbon = d3.ribbon();
|
|---|
| 109 |
|
|---|
| 110 | ribbon({
|
|---|
| 111 | source: {startAngle: 0.7524114, endAngle: 1.1212972, radius: 240},
|
|---|
| 112 | target: {startAngle: 1.8617078, endAngle: 1.9842927, radius: 240}
|
|---|
| 113 | }); // "M164.0162810494058,-175.21032946354026A240,240,0,0,1,216.1595644740915,-104.28347273835429Q0,0,229.9158815306728,68.8381247563705A240,240,0,0,1,219.77316791012538,96.43523560788266Q0,0,164.0162810494058,-175.21032946354026Z"
|
|---|
| 114 | ```
|
|---|
| 115 |
|
|---|
| 116 | Or equivalently if the radius is instead defined as a constant:
|
|---|
| 117 |
|
|---|
| 118 | ```js
|
|---|
| 119 | const ribbon = d3.ribbon()
|
|---|
| 120 | .radius(240);
|
|---|
| 121 |
|
|---|
| 122 | ribbon({
|
|---|
| 123 | source: {startAngle: 0.7524114, endAngle: 1.1212972},
|
|---|
| 124 | target: {startAngle: 1.8617078, endAngle: 1.9842927}
|
|---|
| 125 | }); // "M164.0162810494058,-175.21032946354026A240,240,0,0,1,216.1595644740915,-104.28347273835429Q0,0,229.9158815306728,68.8381247563705A240,240,0,0,1,219.77316791012538,96.43523560788266Q0,0,164.0162810494058,-175.21032946354026Z"
|
|---|
| 126 | ```
|
|---|
| 127 |
|
|---|
| 128 | If the ribbon generator has a context, then the ribbon is rendered to this context as a sequence of path method calls and this function returns void. Otherwise, a path data string is returned.
|
|---|
| 129 |
|
|---|
| 130 | <a href="#ribbon_source" name="ribbon_source">#</a> <i>ribbon</i>.<b>source</b>([<i>source</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 131 |
|
|---|
| 132 | If *source* is specified, sets the source accessor to the specified function and returns this ribbon generator. If *source* is not specified, returns the current source accessor, which defaults to:
|
|---|
| 133 |
|
|---|
| 134 | ```js
|
|---|
| 135 | function source(d) {
|
|---|
| 136 | return d.source;
|
|---|
| 137 | }
|
|---|
| 138 | ```
|
|---|
| 139 |
|
|---|
| 140 | <a href="#ribbon_target" name="ribbon_target">#</a> <i>ribbon</i>.<b>target</b>([<i>target</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 141 |
|
|---|
| 142 | If *target* is specified, sets the target accessor to the specified function and returns this ribbon generator. If *target* is not specified, returns the current target accessor, which defaults to:
|
|---|
| 143 |
|
|---|
| 144 | ```js
|
|---|
| 145 | function target(d) {
|
|---|
| 146 | return d.target;
|
|---|
| 147 | }
|
|---|
| 148 | ```
|
|---|
| 149 |
|
|---|
| 150 | <a href="#ribbon_radius" name="ribbon_radius">#</a> <i>ribbon</i>.<b>radius</b>([<i>radius</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 151 |
|
|---|
| 152 | If *radius* is specified, sets the source and target radius accessor to the specified function and returns this ribbon generator. If *radius* is not specified, returns the current source radius accessor, which defaults to:
|
|---|
| 153 |
|
|---|
| 154 | ```js
|
|---|
| 155 | function radius(d) {
|
|---|
| 156 | return d.radius;
|
|---|
| 157 | }
|
|---|
| 158 | ```
|
|---|
| 159 |
|
|---|
| 160 | <a href="#ribbon_sourceRadius" name="ribbon_sourceRadius">#</a> <i>ribbon</i>.<b>sourceRadius</b>([<i>radius</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 161 |
|
|---|
| 162 | If *radius* is specified, sets the source radius accessor to the specified function and returns this ribbon generator. If *radius* is not specified, returns the current source radius accessor, which defaults to:
|
|---|
| 163 |
|
|---|
| 164 | ```js
|
|---|
| 165 | function radius(d) {
|
|---|
| 166 | return d.radius;
|
|---|
| 167 | }
|
|---|
| 168 | ```
|
|---|
| 169 |
|
|---|
| 170 | <a href="#ribbon_targetRadius" name="ribbon_targetRadius">#</a> <i>ribbon</i>.<b>targetRadius</b>([<i>radius</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 171 |
|
|---|
| 172 | If *radius* is specified, sets the target radius accessor to the specified function and returns this ribbon generator. If *radius* is not specified, returns the current target radius accessor, which defaults to:
|
|---|
| 173 |
|
|---|
| 174 | ```js
|
|---|
| 175 | function radius(d) {
|
|---|
| 176 | return d.radius;
|
|---|
| 177 | }
|
|---|
| 178 | ```
|
|---|
| 179 |
|
|---|
| 180 | By convention, the target radius in asymmetric chord diagrams is typically inset from the source radius, resulting in a gap between the end of the directed link and its associated group arc.
|
|---|
| 181 |
|
|---|
| 182 | <a href="#ribbon_startAngle" name="ribbon_startAngle">#</a> <i>ribbon</i>.<b>startAngle</b>([<i>angle</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 183 |
|
|---|
| 184 | If *angle* is specified, sets the start angle accessor to the specified function and returns this ribbon generator. If *angle* is not specified, returns the current start angle accessor, which defaults to:
|
|---|
| 185 |
|
|---|
| 186 | ```js
|
|---|
| 187 | function startAngle(d) {
|
|---|
| 188 | return d.startAngle;
|
|---|
| 189 | }
|
|---|
| 190 | ```
|
|---|
| 191 |
|
|---|
| 192 | The *angle* is specified in radians, with 0 at -*y* (12 o’clock) and positive angles proceeding clockwise.
|
|---|
| 193 |
|
|---|
| 194 | <a href="#ribbon_endAngle" name="ribbon_endAngle">#</a> <i>ribbon</i>.<b>endAngle</b>([<i>angle</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 195 |
|
|---|
| 196 | If *angle* is specified, sets the end angle accessor to the specified function and returns this ribbon generator. If *angle* is not specified, returns the current end angle accessor, which defaults to:
|
|---|
| 197 |
|
|---|
| 198 | ```js
|
|---|
| 199 | function endAngle(d) {
|
|---|
| 200 | return d.endAngle;
|
|---|
| 201 | }
|
|---|
| 202 | ```
|
|---|
| 203 |
|
|---|
| 204 | The *angle* is specified in radians, with 0 at -*y* (12 o’clock) and positive angles proceeding clockwise.
|
|---|
| 205 |
|
|---|
| 206 | <a href="#ribbon_padAngle" name="ribbon_padAngle">#</a> <i>ribbon</i>.<b>padAngle</b>([<i>angle</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 207 |
|
|---|
| 208 | If *angle* is specified, sets the pad angle accessor to the specified function and returns this ribbon generator. If *angle* is not specified, returns the current pad angle accessor, which defaults to:
|
|---|
| 209 |
|
|---|
| 210 | ```js
|
|---|
| 211 | function padAngle() {
|
|---|
| 212 | return 0;
|
|---|
| 213 | }
|
|---|
| 214 | ```
|
|---|
| 215 |
|
|---|
| 216 | The pad angle specifies the angular gap between adjacent ribbons.
|
|---|
| 217 |
|
|---|
| 218 | <a href="#ribbon_context" name="ribbon_context">#</a> <i>ribbon</i>.<b>context</b>([<i>context</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 219 |
|
|---|
| 220 | If *context* is specified, sets the context and returns this ribbon generator. If *context* is not specified, returns the current context, which defaults to null. If the context is not null, then the [generated ribbon](#_ribbon) is rendered to this context as a sequence of [path method](http://www.w3.org/TR/2dcontext/#canvaspathmethods) calls. Otherwise, a [path data](http://www.w3.org/TR/SVG/paths.html#PathData) string representing the generated ribbon is returned. See also [d3-path](https://github.com/d3/d3-path).
|
|---|
| 221 |
|
|---|
| 222 | <a href="#ribbonArrow" name="ribbonArrow">#</a> d3.<b>ribbonArrow</b>() · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 223 |
|
|---|
| 224 | Creates a new arrow ribbon generator with the default settings.
|
|---|
| 225 |
|
|---|
| 226 | <a href="#ribbonArrow_headRadius" name="ribbonArrow_headRadius">#</a> <i>ribbonArrow</i>.<b>headRadius</b>([<i>radius</i>]) · [Source](https://github.com/d3/d3-chord/blob/master/src/ribbon.js)
|
|---|
| 227 |
|
|---|
| 228 | If *radius* is specified, sets the arrowhead radius accessor to the specified function and returns this ribbon generator. If *radius* is not specified, returns the current arrowhead radius accessor, which defaults to:
|
|---|
| 229 |
|
|---|
| 230 | ```js
|
|---|
| 231 | function headRadius() {
|
|---|
| 232 | return 10;
|
|---|
| 233 | }
|
|---|
| 234 | ```
|
|---|