| 1 | // https://d3js.org/d3-chord/ v3.0.1 Copyright 2010-2021 Mike Bostock
|
|---|
| 2 | (function (global, factory) {
|
|---|
| 3 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-path')) :
|
|---|
| 4 | typeof define === 'function' && define.amd ? define(['exports', 'd3-path'], factory) :
|
|---|
| 5 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}, global.d3));
|
|---|
| 6 | }(this, (function (exports, d3Path) { 'use strict';
|
|---|
| 7 |
|
|---|
| 8 | var abs = Math.abs;
|
|---|
| 9 | var cos = Math.cos;
|
|---|
| 10 | var sin = Math.sin;
|
|---|
| 11 | var pi = Math.PI;
|
|---|
| 12 | var halfPi = pi / 2;
|
|---|
| 13 | var tau = pi * 2;
|
|---|
| 14 | var max = Math.max;
|
|---|
| 15 | var epsilon = 1e-12;
|
|---|
| 16 |
|
|---|
| 17 | function range(i, j) {
|
|---|
| 18 | return Array.from({length: j - i}, (_, k) => i + k);
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | function compareValue(compare) {
|
|---|
| 22 | return function(a, b) {
|
|---|
| 23 | return compare(
|
|---|
| 24 | a.source.value + a.target.value,
|
|---|
| 25 | b.source.value + b.target.value
|
|---|
| 26 | );
|
|---|
| 27 | };
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | function chord() {
|
|---|
| 31 | return chord$1(false, false);
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | function chordTranspose() {
|
|---|
| 35 | return chord$1(false, true);
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | function chordDirected() {
|
|---|
| 39 | return chord$1(true, false);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | function chord$1(directed, transpose) {
|
|---|
| 43 | var padAngle = 0,
|
|---|
| 44 | sortGroups = null,
|
|---|
| 45 | sortSubgroups = null,
|
|---|
| 46 | sortChords = null;
|
|---|
| 47 |
|
|---|
| 48 | function chord(matrix) {
|
|---|
| 49 | var n = matrix.length,
|
|---|
| 50 | groupSums = new Array(n),
|
|---|
| 51 | groupIndex = range(0, n),
|
|---|
| 52 | chords = new Array(n * n),
|
|---|
| 53 | groups = new Array(n),
|
|---|
| 54 | k = 0, dx;
|
|---|
| 55 |
|
|---|
| 56 | matrix = Float64Array.from({length: n * n}, transpose
|
|---|
| 57 | ? (_, i) => matrix[i % n][i / n | 0]
|
|---|
| 58 | : (_, i) => matrix[i / n | 0][i % n]);
|
|---|
| 59 |
|
|---|
| 60 | // Compute the scaling factor from value to angle in [0, 2pi].
|
|---|
| 61 | for (let i = 0; i < n; ++i) {
|
|---|
| 62 | let x = 0;
|
|---|
| 63 | for (let j = 0; j < n; ++j) x += matrix[i * n + j] + directed * matrix[j * n + i];
|
|---|
| 64 | k += groupSums[i] = x;
|
|---|
| 65 | }
|
|---|
| 66 | k = max(0, tau - padAngle * n) / k;
|
|---|
| 67 | dx = k ? padAngle : tau / n;
|
|---|
| 68 |
|
|---|
| 69 | // Compute the angles for each group and constituent chord.
|
|---|
| 70 | {
|
|---|
| 71 | let x = 0;
|
|---|
| 72 | if (sortGroups) groupIndex.sort((a, b) => sortGroups(groupSums[a], groupSums[b]));
|
|---|
| 73 | for (const i of groupIndex) {
|
|---|
| 74 | const x0 = x;
|
|---|
| 75 | if (directed) {
|
|---|
| 76 | const subgroupIndex = range(~n + 1, n).filter(j => j < 0 ? matrix[~j * n + i] : matrix[i * n + j]);
|
|---|
| 77 | if (sortSubgroups) subgroupIndex.sort((a, b) => sortSubgroups(a < 0 ? -matrix[~a * n + i] : matrix[i * n + a], b < 0 ? -matrix[~b * n + i] : matrix[i * n + b]));
|
|---|
| 78 | for (const j of subgroupIndex) {
|
|---|
| 79 | if (j < 0) {
|
|---|
| 80 | const chord = chords[~j * n + i] || (chords[~j * n + i] = {source: null, target: null});
|
|---|
| 81 | chord.target = {index: i, startAngle: x, endAngle: x += matrix[~j * n + i] * k, value: matrix[~j * n + i]};
|
|---|
| 82 | } else {
|
|---|
| 83 | const chord = chords[i * n + j] || (chords[i * n + j] = {source: null, target: null});
|
|---|
| 84 | chord.source = {index: i, startAngle: x, endAngle: x += matrix[i * n + j] * k, value: matrix[i * n + j]};
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | groups[i] = {index: i, startAngle: x0, endAngle: x, value: groupSums[i]};
|
|---|
| 88 | } else {
|
|---|
| 89 | const subgroupIndex = range(0, n).filter(j => matrix[i * n + j] || matrix[j * n + i]);
|
|---|
| 90 | if (sortSubgroups) subgroupIndex.sort((a, b) => sortSubgroups(matrix[i * n + a], matrix[i * n + b]));
|
|---|
| 91 | for (const j of subgroupIndex) {
|
|---|
| 92 | let chord;
|
|---|
| 93 | if (i < j) {
|
|---|
| 94 | chord = chords[i * n + j] || (chords[i * n + j] = {source: null, target: null});
|
|---|
| 95 | chord.source = {index: i, startAngle: x, endAngle: x += matrix[i * n + j] * k, value: matrix[i * n + j]};
|
|---|
| 96 | } else {
|
|---|
| 97 | chord = chords[j * n + i] || (chords[j * n + i] = {source: null, target: null});
|
|---|
| 98 | chord.target = {index: i, startAngle: x, endAngle: x += matrix[i * n + j] * k, value: matrix[i * n + j]};
|
|---|
| 99 | if (i === j) chord.source = chord.target;
|
|---|
| 100 | }
|
|---|
| 101 | if (chord.source && chord.target && chord.source.value < chord.target.value) {
|
|---|
| 102 | const source = chord.source;
|
|---|
| 103 | chord.source = chord.target;
|
|---|
| 104 | chord.target = source;
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | groups[i] = {index: i, startAngle: x0, endAngle: x, value: groupSums[i]};
|
|---|
| 108 | }
|
|---|
| 109 | x += dx;
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | // Remove empty chords.
|
|---|
| 114 | chords = Object.values(chords);
|
|---|
| 115 | chords.groups = groups;
|
|---|
| 116 | return sortChords ? chords.sort(sortChords) : chords;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | chord.padAngle = function(_) {
|
|---|
| 120 | return arguments.length ? (padAngle = max(0, _), chord) : padAngle;
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 | chord.sortGroups = function(_) {
|
|---|
| 124 | return arguments.length ? (sortGroups = _, chord) : sortGroups;
|
|---|
| 125 | };
|
|---|
| 126 |
|
|---|
| 127 | chord.sortSubgroups = function(_) {
|
|---|
| 128 | return arguments.length ? (sortSubgroups = _, chord) : sortSubgroups;
|
|---|
| 129 | };
|
|---|
| 130 |
|
|---|
| 131 | chord.sortChords = function(_) {
|
|---|
| 132 | return arguments.length ? (_ == null ? sortChords = null : (sortChords = compareValue(_))._ = _, chord) : sortChords && sortChords._;
|
|---|
| 133 | };
|
|---|
| 134 |
|
|---|
| 135 | return chord;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | var slice = Array.prototype.slice;
|
|---|
| 139 |
|
|---|
| 140 | function constant(x) {
|
|---|
| 141 | return function() {
|
|---|
| 142 | return x;
|
|---|
| 143 | };
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | function defaultSource(d) {
|
|---|
| 147 | return d.source;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | function defaultTarget(d) {
|
|---|
| 151 | return d.target;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | function defaultRadius(d) {
|
|---|
| 155 | return d.radius;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | function defaultStartAngle(d) {
|
|---|
| 159 | return d.startAngle;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | function defaultEndAngle(d) {
|
|---|
| 163 | return d.endAngle;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | function defaultPadAngle() {
|
|---|
| 167 | return 0;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | function defaultArrowheadRadius() {
|
|---|
| 171 | return 10;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | function ribbon(headRadius) {
|
|---|
| 175 | var source = defaultSource,
|
|---|
| 176 | target = defaultTarget,
|
|---|
| 177 | sourceRadius = defaultRadius,
|
|---|
| 178 | targetRadius = defaultRadius,
|
|---|
| 179 | startAngle = defaultStartAngle,
|
|---|
| 180 | endAngle = defaultEndAngle,
|
|---|
| 181 | padAngle = defaultPadAngle,
|
|---|
| 182 | context = null;
|
|---|
| 183 |
|
|---|
| 184 | function ribbon() {
|
|---|
| 185 | var buffer,
|
|---|
| 186 | s = source.apply(this, arguments),
|
|---|
| 187 | t = target.apply(this, arguments),
|
|---|
| 188 | ap = padAngle.apply(this, arguments) / 2,
|
|---|
| 189 | argv = slice.call(arguments),
|
|---|
| 190 | sr = +sourceRadius.apply(this, (argv[0] = s, argv)),
|
|---|
| 191 | sa0 = startAngle.apply(this, argv) - halfPi,
|
|---|
| 192 | sa1 = endAngle.apply(this, argv) - halfPi,
|
|---|
| 193 | tr = +targetRadius.apply(this, (argv[0] = t, argv)),
|
|---|
| 194 | ta0 = startAngle.apply(this, argv) - halfPi,
|
|---|
| 195 | ta1 = endAngle.apply(this, argv) - halfPi;
|
|---|
| 196 |
|
|---|
| 197 | if (!context) context = buffer = d3Path.path();
|
|---|
| 198 |
|
|---|
| 199 | if (ap > epsilon) {
|
|---|
| 200 | if (abs(sa1 - sa0) > ap * 2 + epsilon) sa1 > sa0 ? (sa0 += ap, sa1 -= ap) : (sa0 -= ap, sa1 += ap);
|
|---|
| 201 | else sa0 = sa1 = (sa0 + sa1) / 2;
|
|---|
| 202 | if (abs(ta1 - ta0) > ap * 2 + epsilon) ta1 > ta0 ? (ta0 += ap, ta1 -= ap) : (ta0 -= ap, ta1 += ap);
|
|---|
| 203 | else ta0 = ta1 = (ta0 + ta1) / 2;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | context.moveTo(sr * cos(sa0), sr * sin(sa0));
|
|---|
| 207 | context.arc(0, 0, sr, sa0, sa1);
|
|---|
| 208 | if (sa0 !== ta0 || sa1 !== ta1) {
|
|---|
| 209 | if (headRadius) {
|
|---|
| 210 | var hr = +headRadius.apply(this, arguments), tr2 = tr - hr, ta2 = (ta0 + ta1) / 2;
|
|---|
| 211 | context.quadraticCurveTo(0, 0, tr2 * cos(ta0), tr2 * sin(ta0));
|
|---|
| 212 | context.lineTo(tr * cos(ta2), tr * sin(ta2));
|
|---|
| 213 | context.lineTo(tr2 * cos(ta1), tr2 * sin(ta1));
|
|---|
| 214 | } else {
|
|---|
| 215 | context.quadraticCurveTo(0, 0, tr * cos(ta0), tr * sin(ta0));
|
|---|
| 216 | context.arc(0, 0, tr, ta0, ta1);
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 | context.quadraticCurveTo(0, 0, sr * cos(sa0), sr * sin(sa0));
|
|---|
| 220 | context.closePath();
|
|---|
| 221 |
|
|---|
| 222 | if (buffer) return context = null, buffer + "" || null;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | if (headRadius) ribbon.headRadius = function(_) {
|
|---|
| 226 | return arguments.length ? (headRadius = typeof _ === "function" ? _ : constant(+_), ribbon) : headRadius;
|
|---|
| 227 | };
|
|---|
| 228 |
|
|---|
| 229 | ribbon.radius = function(_) {
|
|---|
| 230 | return arguments.length ? (sourceRadius = targetRadius = typeof _ === "function" ? _ : constant(+_), ribbon) : sourceRadius;
|
|---|
| 231 | };
|
|---|
| 232 |
|
|---|
| 233 | ribbon.sourceRadius = function(_) {
|
|---|
| 234 | return arguments.length ? (sourceRadius = typeof _ === "function" ? _ : constant(+_), ribbon) : sourceRadius;
|
|---|
| 235 | };
|
|---|
| 236 |
|
|---|
| 237 | ribbon.targetRadius = function(_) {
|
|---|
| 238 | return arguments.length ? (targetRadius = typeof _ === "function" ? _ : constant(+_), ribbon) : targetRadius;
|
|---|
| 239 | };
|
|---|
| 240 |
|
|---|
| 241 | ribbon.startAngle = function(_) {
|
|---|
| 242 | return arguments.length ? (startAngle = typeof _ === "function" ? _ : constant(+_), ribbon) : startAngle;
|
|---|
| 243 | };
|
|---|
| 244 |
|
|---|
| 245 | ribbon.endAngle = function(_) {
|
|---|
| 246 | return arguments.length ? (endAngle = typeof _ === "function" ? _ : constant(+_), ribbon) : endAngle;
|
|---|
| 247 | };
|
|---|
| 248 |
|
|---|
| 249 | ribbon.padAngle = function(_) {
|
|---|
| 250 | return arguments.length ? (padAngle = typeof _ === "function" ? _ : constant(+_), ribbon) : padAngle;
|
|---|
| 251 | };
|
|---|
| 252 |
|
|---|
| 253 | ribbon.source = function(_) {
|
|---|
| 254 | return arguments.length ? (source = _, ribbon) : source;
|
|---|
| 255 | };
|
|---|
| 256 |
|
|---|
| 257 | ribbon.target = function(_) {
|
|---|
| 258 | return arguments.length ? (target = _, ribbon) : target;
|
|---|
| 259 | };
|
|---|
| 260 |
|
|---|
| 261 | ribbon.context = function(_) {
|
|---|
| 262 | return arguments.length ? ((context = _ == null ? null : _), ribbon) : context;
|
|---|
| 263 | };
|
|---|
| 264 |
|
|---|
| 265 | return ribbon;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | function ribbon$1() {
|
|---|
| 269 | return ribbon();
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | function ribbonArrow() {
|
|---|
| 273 | return ribbon(defaultArrowheadRadius);
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | exports.chord = chord;
|
|---|
| 277 | exports.chordDirected = chordDirected;
|
|---|
| 278 | exports.chordTranspose = chordTranspose;
|
|---|
| 279 | exports.ribbon = ribbon$1;
|
|---|
| 280 | exports.ribbonArrow = ribbonArrow;
|
|---|
| 281 |
|
|---|
| 282 | Object.defineProperty(exports, '__esModule', { value: true });
|
|---|
| 283 |
|
|---|
| 284 | })));
|
|---|