@ac-kit/math-graph 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +91 -0
- package/LICENSE +28 -0
- package/README.md +92 -2
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/layout/_force-forces.d.ts +103 -0
- package/dist/layout/_force-forces.d.ts.map +1 -0
- package/dist/layout/_force-forces.js +490 -0
- package/dist/layout/_force-forces.js.map +1 -0
- package/dist/layout/_hierarchy-table.d.ts +34 -0
- package/dist/layout/_hierarchy-table.d.ts.map +1 -0
- package/dist/layout/_hierarchy-table.js +131 -0
- package/dist/layout/_hierarchy-table.js.map +1 -0
- package/dist/layout/_index-graph.d.ts +32 -0
- package/dist/layout/_index-graph.d.ts.map +1 -0
- package/dist/layout/_index-graph.js +51 -0
- package/dist/layout/_index-graph.js.map +1 -0
- package/dist/layout/_layer-order.d.ts +96 -0
- package/dist/layout/_layer-order.d.ts.map +1 -0
- package/dist/layout/_layer-order.js +462 -0
- package/dist/layout/_layer-order.js.map +1 -0
- package/dist/layout/_layered-acyclic.d.ts +28 -0
- package/dist/layout/_layered-acyclic.d.ts.map +1 -0
- package/dist/layout/_layered-acyclic.js +179 -0
- package/dist/layout/_layered-acyclic.js.map +1 -0
- package/dist/layout/_layered-position.d.ts +56 -0
- package/dist/layout/_layered-position.d.ts.map +1 -0
- package/dist/layout/_layered-position.js +369 -0
- package/dist/layout/_layered-position.js.map +1 -0
- package/dist/layout/_layered-rank.d.ts +54 -0
- package/dist/layout/_layered-rank.d.ts.map +1 -0
- package/dist/layout/_layered-rank.js +436 -0
- package/dist/layout/_layered-rank.js.map +1 -0
- package/dist/layout/_tree-common.d.ts +38 -0
- package/dist/layout/_tree-common.d.ts.map +1 -0
- package/dist/layout/_tree-common.js +73 -0
- package/dist/layout/_tree-common.js.map +1 -0
- package/dist/layout/_treemap-tiling.d.ts +62 -0
- package/dist/layout/_treemap-tiling.d.ts.map +1 -0
- package/dist/layout/_treemap-tiling.js +187 -0
- package/dist/layout/_treemap-tiling.js.map +1 -0
- package/dist/layout/circular-layout.d.ts +27 -0
- package/dist/layout/circular-layout.d.ts.map +1 -0
- package/dist/layout/circular-layout.js +35 -0
- package/dist/layout/circular-layout.js.map +1 -0
- package/dist/layout/create-force-step.d.ts +112 -0
- package/dist/layout/create-force-step.d.ts.map +1 -0
- package/dist/layout/create-force-step.js +134 -0
- package/dist/layout/create-force-step.js.map +1 -0
- package/dist/layout/dendrogram-layout.d.ts +32 -0
- package/dist/layout/dendrogram-layout.d.ts.map +1 -0
- package/dist/layout/dendrogram-layout.js +110 -0
- package/dist/layout/dendrogram-layout.js.map +1 -0
- package/dist/layout/force-directed-layout.d.ts +42 -0
- package/dist/layout/force-directed-layout.d.ts.map +1 -0
- package/dist/layout/force-directed-layout.js +112 -0
- package/dist/layout/force-directed-layout.js.map +1 -0
- package/dist/layout/layered-layout.d.ts +96 -0
- package/dist/layout/layered-layout.d.ts.map +1 -0
- package/dist/layout/layered-layout.js +120 -0
- package/dist/layout/layered-layout.js.map +1 -0
- package/dist/layout/partition-layout.d.ts +31 -0
- package/dist/layout/partition-layout.d.ts.map +1 -0
- package/dist/layout/partition-layout.js +78 -0
- package/dist/layout/partition-layout.js.map +1 -0
- package/dist/layout/phyllotaxis-layout.d.ts +17 -0
- package/dist/layout/phyllotaxis-layout.d.ts.map +1 -0
- package/dist/layout/phyllotaxis-layout.js +32 -0
- package/dist/layout/phyllotaxis-layout.js.map +1 -0
- package/dist/layout/sankey-layout.d.ts +104 -0
- package/dist/layout/sankey-layout.d.ts.map +1 -0
- package/dist/layout/sankey-layout.js +636 -0
- package/dist/layout/sankey-layout.js.map +1 -0
- package/dist/layout/tidy-tree-layout.d.ts +25 -0
- package/dist/layout/tidy-tree-layout.d.ts.map +1 -0
- package/dist/layout/tidy-tree-layout.js +197 -0
- package/dist/layout/tidy-tree-layout.js.map +1 -0
- package/dist/layout/treemap-layout.d.ts +54 -0
- package/dist/layout/treemap-layout.d.ts.map +1 -0
- package/dist/layout/treemap-layout.js +121 -0
- package/dist/layout/treemap-layout.js.map +1 -0
- package/dist/layout/types.d.ts +90 -0
- package/dist/layout/types.d.ts.map +1 -0
- package/dist/layout/types.js +2 -0
- package/dist/layout/types.js.map +1 -0
- package/package.json +66 -3
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
import { findCycle } from "@ac-kit/algo";
|
|
2
|
+
import { assertInteger, assertNonNegative, clamp, describeValue, runInTimeSlices, runToCompletion, } from "@ac-kit/core";
|
|
3
|
+
import { buildIndexGraph } from "./_index-graph.js";
|
|
4
|
+
import { countBilayerCrossings, orderLayerSteps } from "./_layer-order.js";
|
|
5
|
+
import { longestPathRanks } from "./_layered-rank.js";
|
|
6
|
+
const FUNCTION_NAME_ = "sankeyLayout";
|
|
7
|
+
const DEFAULT_NODE_WIDTH_ = 24;
|
|
8
|
+
const DEFAULT_NODE_PADDING_ = 8;
|
|
9
|
+
const DEFAULT_ITERATIONS_ = 6;
|
|
10
|
+
// A collision shift below this is not worth applying (as the reference); it
|
|
11
|
+
// scales down with a height under 1, so tiny layouts still resolve overlaps.
|
|
12
|
+
const MINIMUM_SHIFT_ = 1e-6;
|
|
13
|
+
const FIXED_VALUE_TOLERANCE_ = 1e-9;
|
|
14
|
+
// Sums, column totals and span-weighted averages of values up to this stay far
|
|
15
|
+
// below Number.MAX_VALUE for any realistic link count.
|
|
16
|
+
const MAXIMUM_VALUE_ = 1e150;
|
|
17
|
+
function assertAtMost_(value, name) {
|
|
18
|
+
if (value > MAXIMUM_VALUE_) {
|
|
19
|
+
throw new RangeError(`${FUNCTION_NAME_}: ${name} must be at most ${MAXIMUM_VALUE_}, got ${value}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const DAMPING_ = 0.99;
|
|
23
|
+
/**
|
|
24
|
+
* Weighted crossings of the links drawn as straight lines between node slots (a
|
|
25
|
+
* node's slot is its relative position in its column), for the column orders in
|
|
26
|
+
* `columnNodes`. Each link is cut at every column it spans, as in the layered
|
|
27
|
+
* layout, at the height the line has there; per band the crossings come from
|
|
28
|
+
* `countBilayerCrossings` over the ranks of those heights.
|
|
29
|
+
*
|
|
30
|
+
* Complexity: O(Σ span · log) over the links.
|
|
31
|
+
*/
|
|
32
|
+
function straightCrossings_(graph, linkValues, columns, columnStarts, columnNodes) {
|
|
33
|
+
const { nodeCount, linkCount, sources, targets } = graph;
|
|
34
|
+
const columnCount = columnStarts.length - 1;
|
|
35
|
+
const fractions = new Float64Array(nodeCount);
|
|
36
|
+
for (let column = 0; column < columnCount; column++) {
|
|
37
|
+
const begin = columnStarts[column];
|
|
38
|
+
const count = columnStarts[column + 1] - begin;
|
|
39
|
+
for (let slot = 0; slot < count; slot++) {
|
|
40
|
+
fractions[columnNodes[begin + slot]] = (slot + 0.5) / count;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const bandStarts = new Uint32Array(columnCount + 1);
|
|
44
|
+
for (let link = 0; link < linkCount; link++) {
|
|
45
|
+
for (let column = columns[sources[link]]; column < columns[targets[link]]; column++) {
|
|
46
|
+
bandStarts[column + 1]++;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
for (let band = 0; band < columnCount; band++) {
|
|
50
|
+
bandStarts[band + 1] = bandStarts[band + 1] + bandStarts[band];
|
|
51
|
+
}
|
|
52
|
+
const lower = new Float64Array(bandStarts[columnCount]);
|
|
53
|
+
const upper = new Float64Array(bandStarts[columnCount]);
|
|
54
|
+
const weights = new Float64Array(bandStarts[columnCount]);
|
|
55
|
+
const filled = bandStarts.slice(0, columnCount);
|
|
56
|
+
for (let link = 0; link < linkCount; link++) {
|
|
57
|
+
const first = columns[sources[link]];
|
|
58
|
+
const last = columns[targets[link]];
|
|
59
|
+
const start = fractions[sources[link]];
|
|
60
|
+
const end = fractions[targets[link]];
|
|
61
|
+
const heightAt = (column) => column === last
|
|
62
|
+
? end
|
|
63
|
+
: start + ((end - start) * (column - first)) / (last - first);
|
|
64
|
+
for (let column = first; column < last; column++) {
|
|
65
|
+
const at = filled[column]++;
|
|
66
|
+
lower[at] = heightAt(column);
|
|
67
|
+
upper[at] = heightAt(column + 1);
|
|
68
|
+
weights[at] = linkValues[link];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
let total = 0;
|
|
72
|
+
for (let band = 0; band < columnCount; band++) {
|
|
73
|
+
const begin = bandStarts[band];
|
|
74
|
+
const end = bandStarts[band + 1];
|
|
75
|
+
if (end - begin < 2) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const lowerRanks = denseRanks_(lower.subarray(begin, end));
|
|
79
|
+
const upperRanks = denseRanks_(upper.subarray(begin, end));
|
|
80
|
+
total += countBilayerCrossings(lowerRanks.ranks, upperRanks.ranks, lowerRanks.distinct, upperRanks.distinct, weights.subarray(begin, end));
|
|
81
|
+
}
|
|
82
|
+
return total;
|
|
83
|
+
}
|
|
84
|
+
/** Rank of each value among the distinct values, equal values sharing one. */
|
|
85
|
+
function denseRanks_(values) {
|
|
86
|
+
const sorted = Float64Array.from(values).sort();
|
|
87
|
+
let distinct = 0;
|
|
88
|
+
for (let index = 0; index < sorted.length; index++) {
|
|
89
|
+
if (index === 0 || sorted[index] !== sorted[index - 1]) {
|
|
90
|
+
sorted[distinct++] = sorted[index];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const ranks = new Uint32Array(values.length);
|
|
94
|
+
for (let index = 0; index < values.length; index++) {
|
|
95
|
+
let low = 0;
|
|
96
|
+
let high = distinct - 1;
|
|
97
|
+
while (low < high) {
|
|
98
|
+
const middle = (low + high) >> 1;
|
|
99
|
+
if (sorted[middle] < values[index]) {
|
|
100
|
+
low = middle + 1;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
high = middle;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
ranks[index] = low;
|
|
107
|
+
}
|
|
108
|
+
return { ranks, distinct };
|
|
109
|
+
}
|
|
110
|
+
function nonNegativeOption_(name, value, fallback) {
|
|
111
|
+
const result = value ?? fallback;
|
|
112
|
+
assertNonNegative(result, `${FUNCTION_NAME_}: ${name}`);
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
function choiceOption_(name, value, choices) {
|
|
116
|
+
const result = value ?? choices[0];
|
|
117
|
+
if (!choices.includes(result)) {
|
|
118
|
+
throw new RangeError(`${FUNCTION_NAME_}: ${name} must be one of ${choices.join(", ")}, got ${describeValue(result)}`);
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
function settingsOf_(options) {
|
|
123
|
+
const iterations = options.iterations ?? DEFAULT_ITERATIONS_;
|
|
124
|
+
assertInteger(iterations, `${FUNCTION_NAME_}: iterations`, 0, 2 ** 32);
|
|
125
|
+
return {
|
|
126
|
+
width: nonNegativeOption_("width", options.width, 0),
|
|
127
|
+
height: nonNegativeOption_("height", options.height, 0),
|
|
128
|
+
nodeWidth: nonNegativeOption_("nodeWidth", options.nodeWidth, DEFAULT_NODE_WIDTH_),
|
|
129
|
+
nodePadding: nonNegativeOption_("nodePadding", options.nodePadding, DEFAULT_NODE_PADDING_),
|
|
130
|
+
alignment: choiceOption_("alignment", options.alignment, [
|
|
131
|
+
"justify",
|
|
132
|
+
"left",
|
|
133
|
+
"right",
|
|
134
|
+
"center",
|
|
135
|
+
]),
|
|
136
|
+
ordering: choiceOption_("ordering", options.ordering, [
|
|
137
|
+
"relaxation",
|
|
138
|
+
"input",
|
|
139
|
+
"crossings",
|
|
140
|
+
]),
|
|
141
|
+
linkOrder: choiceOption_("linkOrder", options.linkOrder, [
|
|
142
|
+
"breadth",
|
|
143
|
+
"input",
|
|
144
|
+
]),
|
|
145
|
+
iterations,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/** A link that lies on a cycle. */
|
|
149
|
+
function linkOnCycle_(graph) {
|
|
150
|
+
const { nodeCount, targets, outOffsets, outLinks } = graph;
|
|
151
|
+
const cycle = findCycle(Array.from({ length: nodeCount }, (_, node) => node), (node) => Array.from(outLinks.subarray(outOffsets[node], outOffsets[node + 1]), (link) => targets[link]));
|
|
152
|
+
if (cycle === undefined) {
|
|
153
|
+
throw new RangeError(`${FUNCTION_NAME_}: the links contain a cycle`);
|
|
154
|
+
}
|
|
155
|
+
const from = cycle[0];
|
|
156
|
+
const to = cycle[1 % cycle.length];
|
|
157
|
+
for (let item = outOffsets[from]; item < outOffsets[from + 1]; item++) {
|
|
158
|
+
if (targets[outLinks[item]] === to) {
|
|
159
|
+
return outLinks[item];
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
throw new RangeError(`${FUNCTION_NAME_}: the links contain a cycle`);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Longest path from a source (`depths`) and to a sink (`heights`) of every
|
|
166
|
+
* node, and the number of columns. Self-loops and cycles are rejected.
|
|
167
|
+
*/
|
|
168
|
+
function columnDepths_(graph) {
|
|
169
|
+
const { nodeCount, linkCount, sources, targets } = graph;
|
|
170
|
+
for (let link = 0; link < linkCount; link++) {
|
|
171
|
+
if (sources[link] === targets[link]) {
|
|
172
|
+
throw new RangeError(`${FUNCTION_NAME_}: link ${link} is a self-loop on node ${sources[link]}; a circular flow is not supported`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
let sinkRanks;
|
|
176
|
+
try {
|
|
177
|
+
sinkRanks = longestPathRanks(graph, new Uint8Array(linkCount));
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
if (!(error instanceof RangeError)) {
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
const link = linkOnCycle_(graph);
|
|
184
|
+
throw new RangeError(`${FUNCTION_NAME_}: link ${link} (node ${sources[link]} to node ${targets[link]}) is on a cycle; a circular flow is not supported`);
|
|
185
|
+
}
|
|
186
|
+
const sourceRanks = longestPathRanks(graph, new Uint8Array(linkCount).fill(1));
|
|
187
|
+
let longest = 0;
|
|
188
|
+
for (let node = 0; node < nodeCount; node++) {
|
|
189
|
+
longest = Math.max(longest, sinkRanks[node]);
|
|
190
|
+
}
|
|
191
|
+
const depths = new Uint32Array(nodeCount);
|
|
192
|
+
const heights = new Uint32Array(nodeCount);
|
|
193
|
+
for (let node = 0; node < nodeCount; node++) {
|
|
194
|
+
depths[node] = longest - sourceRanks[node];
|
|
195
|
+
heights[node] = longest - sinkRanks[node];
|
|
196
|
+
}
|
|
197
|
+
return { depths, heights, columnCount: nodeCount === 0 ? 0 : longest + 1 };
|
|
198
|
+
}
|
|
199
|
+
function columnOf_(alignment, graph, node, depths, heights, columnCount) {
|
|
200
|
+
const hasOut = graph.outDegrees[node] > 0;
|
|
201
|
+
switch (alignment) {
|
|
202
|
+
case "left":
|
|
203
|
+
return depths[node];
|
|
204
|
+
case "right":
|
|
205
|
+
return columnCount - 1 - heights[node];
|
|
206
|
+
case "center": {
|
|
207
|
+
if (graph.inDegrees[node] > 0) {
|
|
208
|
+
return depths[node];
|
|
209
|
+
}
|
|
210
|
+
if (!hasOut) {
|
|
211
|
+
return 0;
|
|
212
|
+
}
|
|
213
|
+
let shallowest = Infinity;
|
|
214
|
+
for (let item = graph.outOffsets[node]; item < graph.outOffsets[node + 1]; item++) {
|
|
215
|
+
shallowest = Math.min(shallowest, depths[graph.targets[graph.outLinks[item]]]);
|
|
216
|
+
}
|
|
217
|
+
return shallowest - 1;
|
|
218
|
+
}
|
|
219
|
+
default:
|
|
220
|
+
return hasOut ? depths[node] : columnCount - 1;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
function sortRange_(items, begin, end, compare) {
|
|
224
|
+
if (end - begin > 1) {
|
|
225
|
+
items.subarray(begin, end).sort(compare);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Sorts the outgoing links of every source, and the incoming links of every
|
|
230
|
+
* target, of `node` by the other end's position.
|
|
231
|
+
*/
|
|
232
|
+
function reorderLinksOf_(state, node) {
|
|
233
|
+
if (state.settings.linkOrder !== "breadth") {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const { graph, top, outLinks, inLinks, stamps } = state;
|
|
237
|
+
const { sources, targets, outOffsets, inOffsets } = graph;
|
|
238
|
+
let stamp = ++state.stamp;
|
|
239
|
+
for (let item = inOffsets[node]; item < inOffsets[node + 1]; item++) {
|
|
240
|
+
const source = sources[inLinks[item]];
|
|
241
|
+
if (stamps[source] !== stamp) {
|
|
242
|
+
stamps[source] = stamp;
|
|
243
|
+
sortRange_(outLinks, outOffsets[source], outOffsets[source + 1], (first, second) => top[targets[first]] - top[targets[second]] || first - second);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
stamp = ++state.stamp;
|
|
247
|
+
for (let item = outOffsets[node]; item < outOffsets[node + 1]; item++) {
|
|
248
|
+
const target = targets[outLinks[item]];
|
|
249
|
+
if (stamps[target] !== stamp) {
|
|
250
|
+
stamps[target] = stamp;
|
|
251
|
+
sortRange_(inLinks, inOffsets[target], inOffsets[target + 1], (first, second) => top[sources[first]] - top[sources[second]] || first - second);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Scale from value to height that fits the fullest column, then the initial
|
|
257
|
+
* stacking of every column, left to right. Each column's incoming links are
|
|
258
|
+
* ordered by the position of their source, which is placed; the outgoing links
|
|
259
|
+
* keep the input order, as their targets are not placed yet (d3-sankey does the
|
|
260
|
+
* same).
|
|
261
|
+
*/
|
|
262
|
+
function initializeBreadths_(state) {
|
|
263
|
+
const { settings, graph, columnCount, columnStarts, columnNodes, nodeValues, padding, top, bottom, } = state;
|
|
264
|
+
let scale = Infinity;
|
|
265
|
+
for (let column = 0; column < columnCount; column++) {
|
|
266
|
+
const begin = columnStarts[column];
|
|
267
|
+
const end = columnStarts[column + 1];
|
|
268
|
+
let total = 0;
|
|
269
|
+
for (let slot = begin; slot < end; slot++) {
|
|
270
|
+
total += nodeValues[columnNodes[slot]];
|
|
271
|
+
}
|
|
272
|
+
if (total > 0) {
|
|
273
|
+
scale = Math.min(scale,
|
|
274
|
+
// Rounding can leave the padding a hair over the height.
|
|
275
|
+
Math.max(0, settings.height - (end - begin - 1) * padding) / total);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (!Number.isFinite(scale)) {
|
|
279
|
+
scale = 0;
|
|
280
|
+
}
|
|
281
|
+
for (let link = 0; link < graph.linkCount; link++) {
|
|
282
|
+
state.linkWidths[link] = state.linkValues[link] * scale;
|
|
283
|
+
}
|
|
284
|
+
for (let column = 0; column < columnCount; column++) {
|
|
285
|
+
const begin = columnStarts[column];
|
|
286
|
+
const end = columnStarts[column + 1];
|
|
287
|
+
let y = 0;
|
|
288
|
+
for (let slot = begin; slot < end; slot++) {
|
|
289
|
+
const node = columnNodes[slot];
|
|
290
|
+
top[node] = y;
|
|
291
|
+
bottom[node] = y + nodeValues[node] * scale;
|
|
292
|
+
y = bottom[node] + padding;
|
|
293
|
+
}
|
|
294
|
+
const gap = (settings.height - y + padding) / (end - begin + 1);
|
|
295
|
+
for (let slot = begin; slot < end; slot++) {
|
|
296
|
+
const node = columnNodes[slot];
|
|
297
|
+
const shift = gap * (slot - begin + 1);
|
|
298
|
+
top[node] = top[node] + shift;
|
|
299
|
+
bottom[node] = bottom[node] + shift;
|
|
300
|
+
}
|
|
301
|
+
if (settings.linkOrder === "breadth") {
|
|
302
|
+
for (let slot = begin; slot < end; slot++) {
|
|
303
|
+
const node = columnNodes[slot];
|
|
304
|
+
sortRange_(state.inLinks, graph.inOffsets[node], graph.inOffsets[node + 1], (first, second) => top[graph.sources[first]] - top[graph.sources[second]] ||
|
|
305
|
+
first - second);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* The top of `target` at which the link from `source` would leave level, the
|
|
312
|
+
* other links being stacked where they are.
|
|
313
|
+
*/
|
|
314
|
+
function idealTargetTop_(state, source, target) {
|
|
315
|
+
const { graph, linkWidths, outLinks, inLinks } = state;
|
|
316
|
+
let y = state.top[source] - ((graph.outDegrees[source] - 1) * state.padding) / 2;
|
|
317
|
+
for (let item = graph.outOffsets[source]; item < graph.outOffsets[source + 1]; item++) {
|
|
318
|
+
const link = outLinks[item];
|
|
319
|
+
if (graph.targets[link] === target) {
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
y += linkWidths[link] + state.padding;
|
|
323
|
+
}
|
|
324
|
+
for (let item = graph.inOffsets[target]; item < graph.inOffsets[target + 1]; item++) {
|
|
325
|
+
const link = inLinks[item];
|
|
326
|
+
if (graph.sources[link] === source) {
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
y -= linkWidths[link];
|
|
330
|
+
}
|
|
331
|
+
return y;
|
|
332
|
+
}
|
|
333
|
+
/** The top of `source` at which the link to `target` would arrive level. */
|
|
334
|
+
function idealSourceTop_(state, source, target) {
|
|
335
|
+
const { graph, linkWidths, outLinks, inLinks } = state;
|
|
336
|
+
let y = state.top[target] - ((graph.inDegrees[target] - 1) * state.padding) / 2;
|
|
337
|
+
for (let item = graph.inOffsets[target]; item < graph.inOffsets[target + 1]; item++) {
|
|
338
|
+
const link = inLinks[item];
|
|
339
|
+
if (graph.sources[link] === source) {
|
|
340
|
+
break;
|
|
341
|
+
}
|
|
342
|
+
y += linkWidths[link] + state.padding;
|
|
343
|
+
}
|
|
344
|
+
for (let item = graph.outOffsets[source]; item < graph.outOffsets[source + 1]; item++) {
|
|
345
|
+
const link = outLinks[item];
|
|
346
|
+
if (graph.targets[link] === target) {
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
y -= linkWidths[link];
|
|
350
|
+
}
|
|
351
|
+
return y;
|
|
352
|
+
}
|
|
353
|
+
function shiftTopToBottom_(state, end, from, slot, strength) {
|
|
354
|
+
let y = from;
|
|
355
|
+
for (; slot < end; slot++) {
|
|
356
|
+
const node = state.columnNodes[slot];
|
|
357
|
+
const shift = (y - state.top[node]) * strength;
|
|
358
|
+
if (shift > state.shiftThreshold) {
|
|
359
|
+
state.top[node] = state.top[node] + shift;
|
|
360
|
+
state.bottom[node] = state.bottom[node] + shift;
|
|
361
|
+
}
|
|
362
|
+
y = state.bottom[node] + state.padding;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
function shiftBottomToTop_(state, from, slot, strength, begin) {
|
|
366
|
+
let y = from;
|
|
367
|
+
for (; slot >= begin; slot--) {
|
|
368
|
+
const node = state.columnNodes[slot];
|
|
369
|
+
const shift = (state.bottom[node] - y) * strength;
|
|
370
|
+
if (shift > state.shiftThreshold) {
|
|
371
|
+
state.top[node] = state.top[node] - shift;
|
|
372
|
+
state.bottom[node] = state.bottom[node] - shift;
|
|
373
|
+
}
|
|
374
|
+
y = state.top[node] - state.padding;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Pushes the overlapping nodes of a column apart, starting from the middle one
|
|
379
|
+
* and moving outward, then from the edges inward, each shift scaled by
|
|
380
|
+
* `strength`.
|
|
381
|
+
*/
|
|
382
|
+
function resolveCollisions_(state, column, strength) {
|
|
383
|
+
const begin = state.columnStarts[column];
|
|
384
|
+
const end = state.columnStarts[column + 1];
|
|
385
|
+
if (end === begin) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
const middle = begin + ((end - begin) >> 1);
|
|
389
|
+
const subject = state.columnNodes[middle];
|
|
390
|
+
shiftBottomToTop_(state, state.top[subject] - state.padding, middle - 1, strength, begin);
|
|
391
|
+
shiftTopToBottom_(state, end, state.bottom[subject] + state.padding, middle + 1, strength);
|
|
392
|
+
shiftBottomToTop_(state, state.settings.height, end - 1, strength, begin);
|
|
393
|
+
shiftTopToBottom_(state, end, 0, begin, strength);
|
|
394
|
+
}
|
|
395
|
+
function relaxColumn_(state, column, toTarget, alpha, beta) {
|
|
396
|
+
const { graph, linkValues, columns, columnStarts, columnNodes, top, bottom } = state;
|
|
397
|
+
const { sources, targets } = graph;
|
|
398
|
+
const offsets = toTarget ? graph.inOffsets : graph.outOffsets;
|
|
399
|
+
const links = toTarget ? state.inLinks : state.outLinks;
|
|
400
|
+
const begin = columnStarts[column];
|
|
401
|
+
const end = columnStarts[column + 1];
|
|
402
|
+
for (let slot = begin; slot < end; slot++) {
|
|
403
|
+
const node = columnNodes[slot];
|
|
404
|
+
let weighted = 0;
|
|
405
|
+
let total = 0;
|
|
406
|
+
for (let item = offsets[node]; item < offsets[node + 1]; item++) {
|
|
407
|
+
const link = links[item];
|
|
408
|
+
const source = sources[link];
|
|
409
|
+
const target = targets[link];
|
|
410
|
+
// A link spanning several columns pulls harder.
|
|
411
|
+
const weight = linkValues[link] * (columns[target] - columns[source]);
|
|
412
|
+
weighted +=
|
|
413
|
+
(toTarget
|
|
414
|
+
? idealTargetTop_(state, source, target)
|
|
415
|
+
: idealSourceTop_(state, source, target)) * weight;
|
|
416
|
+
total += weight;
|
|
417
|
+
}
|
|
418
|
+
if (total > 0) {
|
|
419
|
+
const shift = (weighted / total - top[node]) * alpha;
|
|
420
|
+
top[node] = top[node] + shift;
|
|
421
|
+
bottom[node] = bottom[node] + shift;
|
|
422
|
+
reorderLinksOf_(state, node);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
if (state.settings.ordering === "relaxation") {
|
|
426
|
+
sortRange_(columnNodes, begin, end, (first, second) => top[first] - top[second]);
|
|
427
|
+
}
|
|
428
|
+
resolveCollisions_(state, column, beta);
|
|
429
|
+
}
|
|
430
|
+
function relax_(state, pass) {
|
|
431
|
+
const { iterations } = state.settings;
|
|
432
|
+
const alpha = DAMPING_ ** pass;
|
|
433
|
+
const beta = Math.max(1 - alpha, (pass + 1) / iterations);
|
|
434
|
+
for (let column = state.columnCount - 2; column >= 0; column--) {
|
|
435
|
+
relaxColumn_(state, column, false, alpha, beta);
|
|
436
|
+
}
|
|
437
|
+
for (let column = 1; column < state.columnCount; column++) {
|
|
438
|
+
relaxColumn_(state, column, true, alpha, beta);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
function* sankeySteps_(nodes, links, traits, options) {
|
|
442
|
+
const settings = settingsOf_(options);
|
|
443
|
+
const budget = options.budget;
|
|
444
|
+
const graph = buildIndexGraph(nodes.length, links, traits, FUNCTION_NAME_);
|
|
445
|
+
const { nodeCount, linkCount } = graph;
|
|
446
|
+
const linkValues = new Float64Array(linkCount);
|
|
447
|
+
for (let link = 0; link < linkCount; link++) {
|
|
448
|
+
const value = traits.linkValueOf(links[link], link);
|
|
449
|
+
assertNonNegative(value, `${FUNCTION_NAME_}: value of link ${link}`);
|
|
450
|
+
assertAtMost_(value, `value of link ${link}`);
|
|
451
|
+
linkValues[link] = value;
|
|
452
|
+
}
|
|
453
|
+
const fixedValues = new Float64Array(nodeCount).fill(Number.NaN);
|
|
454
|
+
if (traits.nodeValueOf !== undefined) {
|
|
455
|
+
for (let node = 0; node < nodeCount; node++) {
|
|
456
|
+
const value = traits.nodeValueOf(nodes[node], node);
|
|
457
|
+
if (value === undefined) {
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
assertNonNegative(value, `${FUNCTION_NAME_}: value of node ${node}`);
|
|
461
|
+
assertAtMost_(value, `value of node ${node}`);
|
|
462
|
+
fixedValues[node] = value;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
const { depths, heights, columnCount } = columnDepths_(graph);
|
|
466
|
+
if (columnCount > 0 && settings.nodeWidth > settings.width) {
|
|
467
|
+
throw new RangeError(`${FUNCTION_NAME_}: nodeWidth ${settings.nodeWidth} exceeds width ${settings.width}`);
|
|
468
|
+
}
|
|
469
|
+
const columns = new Uint32Array(nodeCount);
|
|
470
|
+
for (let node = 0; node < nodeCount; node++) {
|
|
471
|
+
columns[node] = clamp(columnOf_(settings.alignment, graph, node, depths, heights, columnCount), 0, columnCount - 1);
|
|
472
|
+
}
|
|
473
|
+
const nodeValues = new Float64Array(nodeCount);
|
|
474
|
+
for (let node = 0; node < nodeCount; node++) {
|
|
475
|
+
let incoming = 0;
|
|
476
|
+
for (let item = graph.inOffsets[node]; item < graph.inOffsets[node + 1]; item++) {
|
|
477
|
+
incoming += linkValues[graph.inLinks[item]];
|
|
478
|
+
}
|
|
479
|
+
let outgoing = 0;
|
|
480
|
+
for (let item = graph.outOffsets[node]; item < graph.outOffsets[node + 1]; item++) {
|
|
481
|
+
outgoing += linkValues[graph.outLinks[item]];
|
|
482
|
+
}
|
|
483
|
+
const flow = Math.max(incoming, outgoing);
|
|
484
|
+
const fixed = fixedValues[node];
|
|
485
|
+
if (Number.isNaN(fixed)) {
|
|
486
|
+
nodeValues[node] = flow;
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
// Bands thicker than their node would run out of it (d3-sankey draws them).
|
|
490
|
+
if (fixed < flow * (1 - FIXED_VALUE_TOLERANCE_)) {
|
|
491
|
+
throw new RangeError(`${FUNCTION_NAME_}: value of node ${node} (${fixed}) is below the ${flow} that flows through it`);
|
|
492
|
+
}
|
|
493
|
+
nodeValues[node] = Math.max(fixed, flow);
|
|
494
|
+
}
|
|
495
|
+
yield;
|
|
496
|
+
const columnStarts = new Uint32Array(columnCount + 1);
|
|
497
|
+
for (let node = 0; node < nodeCount; node++) {
|
|
498
|
+
columnStarts[columns[node] + 1]++;
|
|
499
|
+
}
|
|
500
|
+
for (let column = 0; column < columnCount; column++) {
|
|
501
|
+
columnStarts[column + 1] =
|
|
502
|
+
columnStarts[column + 1] + columnStarts[column];
|
|
503
|
+
}
|
|
504
|
+
const columnNodes = new Uint32Array(nodeCount);
|
|
505
|
+
let longestColumn = 0;
|
|
506
|
+
for (let column = 0; column < columnCount; column++) {
|
|
507
|
+
longestColumn = Math.max(longestColumn, columnStarts[column + 1] - columnStarts[column]);
|
|
508
|
+
}
|
|
509
|
+
const next = columnStarts.slice(0, columnCount);
|
|
510
|
+
for (let node = 0; node < nodeCount; node++) {
|
|
511
|
+
columnNodes[next[columns[node]]++] = node;
|
|
512
|
+
}
|
|
513
|
+
if (settings.ordering === "crossings") {
|
|
514
|
+
const order = yield* orderLayerSteps(graph, columns, {
|
|
515
|
+
weights: linkValues,
|
|
516
|
+
budget,
|
|
517
|
+
});
|
|
518
|
+
const swept = new Uint32Array(nodeCount);
|
|
519
|
+
let slot = 0;
|
|
520
|
+
for (let at = 0; at < order.layerVertices.length; at++) {
|
|
521
|
+
const vertex = order.layerVertices[at];
|
|
522
|
+
if (vertex < nodeCount) {
|
|
523
|
+
swept[slot++] = vertex;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
// The sweeps only beat the order they start from, not the input order.
|
|
527
|
+
const inputCrossings = straightCrossings_(graph, linkValues, columns, columnStarts, columnNodes);
|
|
528
|
+
if (straightCrossings_(graph, linkValues, columns, columnStarts, swept) <
|
|
529
|
+
inputCrossings) {
|
|
530
|
+
columnNodes.set(swept);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
yield;
|
|
534
|
+
const state = {
|
|
535
|
+
settings,
|
|
536
|
+
graph,
|
|
537
|
+
linkValues,
|
|
538
|
+
nodeValues,
|
|
539
|
+
padding: longestColumn > 1
|
|
540
|
+
? Math.min(settings.nodePadding, settings.height / (longestColumn - 1))
|
|
541
|
+
: settings.nodePadding,
|
|
542
|
+
shiftThreshold: MINIMUM_SHIFT_ * Math.min(1, settings.height),
|
|
543
|
+
columnCount,
|
|
544
|
+
columnStarts,
|
|
545
|
+
columnNodes,
|
|
546
|
+
top: new Float64Array(nodeCount),
|
|
547
|
+
bottom: new Float64Array(nodeCount),
|
|
548
|
+
outLinks: graph.outLinks.slice(),
|
|
549
|
+
inLinks: graph.inLinks.slice(),
|
|
550
|
+
stamps: new Uint32Array(nodeCount),
|
|
551
|
+
stamp: 0,
|
|
552
|
+
linkWidths: new Float64Array(linkCount),
|
|
553
|
+
columns,
|
|
554
|
+
};
|
|
555
|
+
initializeBreadths_(state);
|
|
556
|
+
for (let pass = 0; pass < settings.iterations; pass++) {
|
|
557
|
+
budget?.spend(nodeCount + linkCount);
|
|
558
|
+
yield;
|
|
559
|
+
relax_(state, pass);
|
|
560
|
+
}
|
|
561
|
+
yield;
|
|
562
|
+
const columnSpacing = columnCount > 1
|
|
563
|
+
? (settings.width - settings.nodeWidth) / (columnCount - 1)
|
|
564
|
+
: 0;
|
|
565
|
+
const nodeBoxes = new Float64Array(4 * nodeCount);
|
|
566
|
+
for (let node = 0; node < nodeCount; node++) {
|
|
567
|
+
const left = columns[node] * columnSpacing;
|
|
568
|
+
nodeBoxes[4 * node] = left;
|
|
569
|
+
nodeBoxes[4 * node + 1] = state.top[node];
|
|
570
|
+
nodeBoxes[4 * node + 2] = left + settings.nodeWidth;
|
|
571
|
+
nodeBoxes[4 * node + 3] = state.bottom[node];
|
|
572
|
+
}
|
|
573
|
+
const linkWidths = state.linkWidths;
|
|
574
|
+
const linkEnds = new Float64Array(4 * linkCount);
|
|
575
|
+
for (let link = 0; link < linkCount; link++) {
|
|
576
|
+
linkEnds[4 * link] = nodeBoxes[4 * graph.sources[link] + 2];
|
|
577
|
+
linkEnds[4 * link + 2] = nodeBoxes[4 * graph.targets[link]];
|
|
578
|
+
}
|
|
579
|
+
for (let node = 0; node < nodeCount; node++) {
|
|
580
|
+
let y = state.top[node];
|
|
581
|
+
for (let item = graph.outOffsets[node]; item < graph.outOffsets[node + 1]; item++) {
|
|
582
|
+
const link = state.outLinks[item];
|
|
583
|
+
linkEnds[4 * link + 1] = y + linkWidths[link] / 2;
|
|
584
|
+
y += linkWidths[link];
|
|
585
|
+
}
|
|
586
|
+
y = state.top[node];
|
|
587
|
+
for (let item = graph.inOffsets[node]; item < graph.inOffsets[node + 1]; item++) {
|
|
588
|
+
const link = state.inLinks[item];
|
|
589
|
+
linkEnds[4 * link + 3] = y + linkWidths[link] / 2;
|
|
590
|
+
y += linkWidths[link];
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return { nodeBoxes, nodeValues, columns, linkWidths, linkEnds };
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Flow diagram. Columns follow the longest path from the sources, then
|
|
597
|
+
* `alignment`; node heights are proportional to their value; the link bands are
|
|
598
|
+
* stacked at each node. The vertical positions follow the iterative relaxation
|
|
599
|
+
* that Mike Bostock's d3-sankey 0.12 applies to the layered flow diagrams of
|
|
600
|
+
* Riehmann, Hanfler and Froehlich ("Interactive Sankey Diagrams", IEEE InfoVis
|
|
601
|
+
* 2005), implemented here from that description: `iterations` passes, each
|
|
602
|
+
* moving every node, column by column from the right then from the left, to the
|
|
603
|
+
* position that would level its links (weighted by value × columns spanned,
|
|
604
|
+
* strength 0.99^pass), reordering the bands by the position of their other end,
|
|
605
|
+
* then pushing overlapping nodes apart. Isolated nodes get value 0, and the
|
|
606
|
+
* last column under `"justify"`. Where d3-sankey yields NaN, a single column
|
|
607
|
+
* starts at x = 0 and a column of zero-valued nodes takes no height. Collisions
|
|
608
|
+
* are resolved down to 1e-6 × min(1, height), so two nodes of a column may
|
|
609
|
+
* overlap by that much (d3-sankey's own cutoff for heights ≥ 1).
|
|
610
|
+
*
|
|
611
|
+
* Complexity: O(V + E) columns; each pass O(V log V + E log E + Σ d²) for the
|
|
612
|
+
* node degrees d; `"crossings"` adds the cost of the layered ordering and of
|
|
613
|
+
* comparing it with the input order, O(Σ span · log E) over the links.
|
|
614
|
+
*
|
|
615
|
+
* @throws {RangeError} A cycle or a self-loop (naming a link on it: a circular
|
|
616
|
+
* flow is not supported), a link end that is not a node index, a negative,
|
|
617
|
+
* non-finite or over-1e150 link or node value (so no sum overflows), a node
|
|
618
|
+
* value below its flow, a `nodeWidth` larger than `width`, an option out of
|
|
619
|
+
* range.
|
|
620
|
+
*/
|
|
621
|
+
export function sankeyLayout(nodes, links, traits, options) {
|
|
622
|
+
return runToCompletion(sankeySteps_(nodes, links, traits, options));
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* {@link sankeyLayout} in time slices: yields to the event loop every `sliceMs`
|
|
626
|
+
* (after the columns, after each relaxation pass), and rejects with
|
|
627
|
+
* `signal.reason` once aborted. The result equals the sync form's.
|
|
628
|
+
*/
|
|
629
|
+
export function sankeyLayoutAsync(nodes, links, traits, options) {
|
|
630
|
+
const { signal, sliceMs, ...layoutOptions } = options;
|
|
631
|
+
return runInTimeSlices(sankeySteps_(nodes, links, traits, layoutOptions), {
|
|
632
|
+
signal,
|
|
633
|
+
sliceMs,
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
//# sourceMappingURL=sankey-layout.js.map
|