Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

d3-chord.js 8.9KB

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