@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,110 @@
|
|
|
1
|
+
import { assertNonNegative } from "@ac-kit/core";
|
|
2
|
+
import { buildHierarchyTable } from "./_hierarchy-table.js";
|
|
3
|
+
import { buildSiblingLinks, createCheckedSeparation, resolveTreeSize, } from "./_tree-common.js";
|
|
4
|
+
/**
|
|
5
|
+
* Leaves evenly spaced in pre-order along x (`separationOf` apart), each parent
|
|
6
|
+
* at the mean x of its children — the layout of a dendrogram or a cluster tree.
|
|
7
|
+
* With `width`/`height` the leaves are inset half a separation from the box
|
|
8
|
+
* edges; with `nodeWidth`/`nodeHeight` the root sits at x = 0 and the highest
|
|
9
|
+
* row at y = 0, `nodeHeight` per unit of height (or per level without
|
|
10
|
+
* `heightOf`). All heights 0 put every row at y = 0.
|
|
11
|
+
*
|
|
12
|
+
* The arrangement is the one scipy's `dendrogram` and d3's `cluster` draw.
|
|
13
|
+
*
|
|
14
|
+
* Complexity: O(n) time and memory, no recursion on depth. Separations and
|
|
15
|
+
* sizes are meant to lie within [1e−150, 1e150]; beyond that sums and quotients
|
|
16
|
+
* can underflow or overflow to NaN or ∞.
|
|
17
|
+
*
|
|
18
|
+
* @throws {RangeError} A size that is not finite and > 0, a height that is not
|
|
19
|
+
* finite and ≥ 0, or a separation that is not finite and > 0.
|
|
20
|
+
*/
|
|
21
|
+
export function dendrogramLayout(root, traits, options = {}) {
|
|
22
|
+
const size = resolveTreeSize(options, "dendrogramLayout");
|
|
23
|
+
const table = buildHierarchyTable(root, traits.childrenOf, { budget: options.budget }, "dendrogramLayout");
|
|
24
|
+
const { nodes, parentIndices, depths } = table;
|
|
25
|
+
const count = nodes.length;
|
|
26
|
+
const positions = new Float64Array(count * 2);
|
|
27
|
+
const separation = createCheckedSeparation(table, traits.separationOf, "dendrogramLayout");
|
|
28
|
+
const { firstChild, lastChild } = buildSiblingLinks(parentIndices);
|
|
29
|
+
// Height of each row above the base line, in the units of the y axis.
|
|
30
|
+
const heights = new Float64Array(count);
|
|
31
|
+
const { heightOf } = traits;
|
|
32
|
+
if (heightOf === undefined) {
|
|
33
|
+
// Children have higher rows than their parent: one reverse pass.
|
|
34
|
+
for (let row = count - 1; row > 0; row--) {
|
|
35
|
+
const parent = parentIndices[row];
|
|
36
|
+
if (heights[row] + 1 > heights[parent]) {
|
|
37
|
+
heights[parent] = heights[row] + 1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
for (let row = 0; row < count; row++) {
|
|
43
|
+
const height = heightOf(nodes[row], row);
|
|
44
|
+
assertNonNegative(height, `dendrogramLayout: height of row ${row}`);
|
|
45
|
+
heights[row] = height;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
let highest = 0;
|
|
49
|
+
for (let row = 0; row < count; row++) {
|
|
50
|
+
if (heights[row] > highest) {
|
|
51
|
+
highest = heights[row];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const xs = new Float64Array(count);
|
|
55
|
+
const childSums = new Float64Array(count);
|
|
56
|
+
const childCounts = new Uint32Array(count);
|
|
57
|
+
for (let row = 1; row < count; row++) {
|
|
58
|
+
childCounts[parentIndices[row]]++;
|
|
59
|
+
}
|
|
60
|
+
let previousLeaf = -1;
|
|
61
|
+
for (let row = 0; row < count; row++) {
|
|
62
|
+
if (firstChild[row] < 0) {
|
|
63
|
+
xs[row] =
|
|
64
|
+
previousLeaf < 0
|
|
65
|
+
? 0
|
|
66
|
+
: xs[previousLeaf] + separation(row, previousLeaf);
|
|
67
|
+
previousLeaf = row;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
for (let row = count - 1; row >= 0; row--) {
|
|
71
|
+
if (firstChild[row] >= 0) {
|
|
72
|
+
xs[row] = childSums[row] / childCounts[row];
|
|
73
|
+
}
|
|
74
|
+
if (row > 0) {
|
|
75
|
+
childSums[parentIndices[row]] += xs[row];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Margins matter only inside a box; a lone root is centred whatever the separation.
|
|
79
|
+
let lowEdge = -0.5;
|
|
80
|
+
let highEdge = 0.5;
|
|
81
|
+
if (!size.isPerNode && count > 1) {
|
|
82
|
+
const firstLeaf = descend_(0, firstChild);
|
|
83
|
+
const lastLeaf = descend_(0, lastChild);
|
|
84
|
+
lowEdge = xs[firstLeaf] - separation(firstLeaf, lastLeaf) / 2;
|
|
85
|
+
highEdge = xs[lastLeaf] + separation(lastLeaf, firstLeaf) / 2;
|
|
86
|
+
}
|
|
87
|
+
const rootX = xs[0];
|
|
88
|
+
for (let row = 0; row < count; row++) {
|
|
89
|
+
const level = heights[row];
|
|
90
|
+
if (size.isPerNode) {
|
|
91
|
+
positions[row * 2] = (xs[row] - rootX) * size.horizontal;
|
|
92
|
+
positions[row * 2 + 1] = (highest - level) * size.vertical;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
positions[row * 2] =
|
|
96
|
+
((xs[row] - lowEdge) / (highEdge - lowEdge)) * size.horizontal;
|
|
97
|
+
positions[row * 2 + 1] =
|
|
98
|
+
highest > 0 ? (1 - level / highest) * size.vertical : 0;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return { nodes, parentIndices, depths, positions };
|
|
102
|
+
}
|
|
103
|
+
function descend_(row, next) {
|
|
104
|
+
let current = row;
|
|
105
|
+
while (next[current] >= 0) {
|
|
106
|
+
current = next[current];
|
|
107
|
+
}
|
|
108
|
+
return current;
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=dendrogram-layout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
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AAO,GAAG,IAAI,CAAC,OAAO,CAAE,CAAC;IAC1B,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { WorkBudget } from "@ac-kit/core";
|
|
2
|
+
import type { ForceOptions, ForceTraits } from "./create-force-step.js";
|
|
3
|
+
import type { AsyncLayoutOptions } from "./types.js";
|
|
4
|
+
export type ForceDirectedLayoutOptions = ForceOptions & {
|
|
5
|
+
/** 2 per node; NaN entries take `phyllotaxisLayout`'s position (d3). */
|
|
6
|
+
readonly initialPositions?: Float64Array;
|
|
7
|
+
/** 2 per node; NaN = free on that axis. */
|
|
8
|
+
readonly fixedPositions?: Float64Array;
|
|
9
|
+
/** Steps run. Default ⌈log(alphaMin) / log(1 − alphaDecay)⌉ = 300. */
|
|
10
|
+
readonly iterations?: number;
|
|
11
|
+
/** Default 1, in [0, 1]. */
|
|
12
|
+
readonly alpha?: number;
|
|
13
|
+
/** Default 0.001, in (0, 1). */
|
|
14
|
+
readonly alphaMin?: number;
|
|
15
|
+
/** Default 1 − alphaMin^(1/300), in [0, 1]. */
|
|
16
|
+
readonly alphaDecay?: number;
|
|
17
|
+
/** Default 0, in [0, 1]. */
|
|
18
|
+
readonly alphaTarget?: number;
|
|
19
|
+
/** Charged n + m before every step. */
|
|
20
|
+
readonly budget?: WorkBudget;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* `createForceStep` run for `iterations` steps from rest, alpha updated before
|
|
24
|
+
* each step as d3 (`alpha += (alphaTarget − alpha)·alphaDecay`):
|
|
25
|
+
* `simulation.stop().tick(300)`. Pinned axes start at their pin; the other NaN
|
|
26
|
+
* initial coordinates take the phyllotaxis position of their node (both axes
|
|
27
|
+
* together, as d3). Returns new positions, 2 per node.
|
|
28
|
+
*
|
|
29
|
+
* Complexity: O(iterations · (n log n + m)), O(iterations · n²) exact.
|
|
30
|
+
*
|
|
31
|
+
* @throws {RangeError} The conditions of `createForceStep`, a position buffer
|
|
32
|
+
* of the wrong length or holding ±Infinity, an option out of range. Whatever
|
|
33
|
+
* `budget.spend` throws propagates.
|
|
34
|
+
*/
|
|
35
|
+
export declare function forceDirectedLayout<TNode, TLink>(nodes: readonly TNode[], links: readonly TLink[], traits: ForceTraits<TNode, TLink>, options: ForceDirectedLayoutOptions): Float64Array;
|
|
36
|
+
/**
|
|
37
|
+
* {@link forceDirectedLayout} yielding to the event loop after every step; the
|
|
38
|
+
* result equals the sync form's. Validation errors reject. Aborting rejects
|
|
39
|
+
* with `signal.reason` before the next step and discards the work.
|
|
40
|
+
*/
|
|
41
|
+
export declare function forceDirectedLayoutAsync<TNode, TLink>(nodes: readonly TNode[], links: readonly TLink[], traits: ForceTraits<TNode, TLink>, options: ForceDirectedLayoutOptions & AsyncLayoutOptions): Promise<Float64Array>;
|
|
42
|
+
//# sourceMappingURL=force-directed-layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"force-directed-layout.d.ts","sourceRoot":"","sources":["../../src/layout/force-directed-layout.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG/C,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAExE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAErD,MAAM,MAAM,0BAA0B,GAAG,YAAY,GAAG;IACvD,wEAAwE;IACxE,QAAQ,CAAC,gBAAgB,CAAC,EAAE,YAAY,CAAC;IACzC,2CAA2C;IAC3C,QAAQ,CAAC,cAAc,CAAC,EAAE,YAAY,CAAC;IACvC,sEAAsE;IACtE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,4BAA4B;IAC5B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,gCAAgC;IAChC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,+CAA+C;IAC/C,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,4BAA4B;IAC5B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,uCAAuC;IACvC,QAAQ,CAAC,MAAM,CAAC,EAAE,UAAU,CAAC;CAC7B,CAAC;AAuHF;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,KAAK,EAC/C,KAAK,EAAE,SAAS,KAAK,EAAE,EACvB,KAAK,EAAE,SAAS,KAAK,EAAE,EACvB,MAAM,EAAE,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,EACjC,OAAO,EAAE,0BAA0B,GACjC,YAAY,CAEd;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,KAAK,EACpD,KAAK,EAAE,SAAS,KAAK,EAAE,EACvB,KAAK,EAAE,SAAS,KAAK,EAAE,EACvB,MAAM,EAAE,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,EACjC,OAAO,EAAE,0BAA0B,GAAG,kBAAkB,GACtD,OAAO,CAAC,YAAY,CAAC,CAMvB"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { assertInteger, describeValue, runInTimeSlices, runToCompletion, } from "@ac-kit/core";
|
|
2
|
+
import { createForceStep } from "./create-force-step.js";
|
|
3
|
+
import { phyllotaxisLayout } from "./phyllotaxis-layout.js";
|
|
4
|
+
function checkPositions(positions, nodeCount, name) {
|
|
5
|
+
if (positions === undefined) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
if (positions.length !== nodeCount * 2) {
|
|
9
|
+
throw new RangeError(`forceDirectedLayout: ${name} must hold ${nodeCount * 2} numbers, got ${positions.length}`);
|
|
10
|
+
}
|
|
11
|
+
for (let index = 0; index < positions.length; index++) {
|
|
12
|
+
const value = positions[index];
|
|
13
|
+
if (!Number.isNaN(value) && !Number.isFinite(value)) {
|
|
14
|
+
throw new RangeError(`forceDirectedLayout: ${name}[${index}] must be finite or NaN, got ${describeValue(value)}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function unitInterval(value, name, isOpen) {
|
|
19
|
+
const isValid = isOpen ? value > 0 && value < 1 : value >= 0 && value <= 1;
|
|
20
|
+
if (!isValid) {
|
|
21
|
+
throw new RangeError(`forceDirectedLayout: ${name} must be in ${isOpen ? "(0, 1)" : "[0, 1]"}, got ${describeValue(value)}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function* forceSteps_(nodes, links, traits, options) {
|
|
25
|
+
const { initialPositions, fixedPositions, alphaMin = 0.001, alphaTarget = 0, budget, ...stepOptions } = options;
|
|
26
|
+
let alpha = options.alpha ?? 1;
|
|
27
|
+
const alphaDecay = options.alphaDecay ?? 1 - Math.pow(alphaMin, 1 / 300);
|
|
28
|
+
unitInterval(alphaMin, "alphaMin", true);
|
|
29
|
+
unitInterval(alpha, "alpha", false);
|
|
30
|
+
unitInterval(alphaDecay, "alphaDecay", false);
|
|
31
|
+
unitInterval(alphaTarget, "alphaTarget", false);
|
|
32
|
+
const nodeCount = nodes.length;
|
|
33
|
+
checkPositions(initialPositions, nodeCount, "initialPositions");
|
|
34
|
+
checkPositions(fixedPositions, nodeCount, "fixedPositions");
|
|
35
|
+
let iterations = options.iterations;
|
|
36
|
+
if (iterations === undefined) {
|
|
37
|
+
// The default decay gives exactly 300 ticks; rounding must not make it 301.
|
|
38
|
+
iterations =
|
|
39
|
+
options.alphaDecay === undefined
|
|
40
|
+
? 300
|
|
41
|
+
: Math.ceil(Math.log(alphaMin) / Math.log1p(-alphaDecay));
|
|
42
|
+
if (!Number.isFinite(iterations)) {
|
|
43
|
+
throw new RangeError("forceDirectedLayout: iterations is required when alphaDecay is 0");
|
|
44
|
+
}
|
|
45
|
+
// alphaDecay = 1 reaches the target in one tick, which the log gives as 0.
|
|
46
|
+
if (iterations === 0 && alpha > alphaTarget) {
|
|
47
|
+
iterations = 1;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
assertInteger(iterations, "forceDirectedLayout: iterations", 0);
|
|
51
|
+
const step = createForceStep(nodes, links, traits, stepOptions);
|
|
52
|
+
// Snapshots: the run may be suspended while the caller reuses its buffers.
|
|
53
|
+
const pins = fixedPositions?.slice();
|
|
54
|
+
const positions = initialPositions?.slice() ?? new Float64Array(nodeCount * 2);
|
|
55
|
+
if (initialPositions === undefined) {
|
|
56
|
+
positions.fill(NaN);
|
|
57
|
+
}
|
|
58
|
+
let placed;
|
|
59
|
+
for (let node = 0; node < nodeCount; node++) {
|
|
60
|
+
if (Number.isNaN(positions[2 * node]) ||
|
|
61
|
+
Number.isNaN(positions[2 * node + 1])) {
|
|
62
|
+
placed ??= phyllotaxisLayout(nodeCount);
|
|
63
|
+
positions[2 * node] = placed[2 * node];
|
|
64
|
+
positions[2 * node + 1] = placed[2 * node + 1];
|
|
65
|
+
}
|
|
66
|
+
for (let axis = 0; axis < 2; axis++) {
|
|
67
|
+
const pin = pins === undefined ? NaN : pins[2 * node + axis];
|
|
68
|
+
if (!Number.isNaN(pin)) {
|
|
69
|
+
positions[2 * node + axis] = pin;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const velocities = new Float64Array(nodeCount * 2);
|
|
74
|
+
const cost = nodeCount + links.length;
|
|
75
|
+
for (let iteration = 0; iteration < iterations; iteration++) {
|
|
76
|
+
// Before the charge, so that an abort seen here costs no tick.
|
|
77
|
+
yield;
|
|
78
|
+
budget?.spend(cost);
|
|
79
|
+
alpha = Math.min(1, Math.max(0, alpha + (alphaTarget - alpha) * alphaDecay));
|
|
80
|
+
step(positions, velocities, alpha, pins);
|
|
81
|
+
}
|
|
82
|
+
return positions;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* `createForceStep` run for `iterations` steps from rest, alpha updated before
|
|
86
|
+
* each step as d3 (`alpha += (alphaTarget − alpha)·alphaDecay`):
|
|
87
|
+
* `simulation.stop().tick(300)`. Pinned axes start at their pin; the other NaN
|
|
88
|
+
* initial coordinates take the phyllotaxis position of their node (both axes
|
|
89
|
+
* together, as d3). Returns new positions, 2 per node.
|
|
90
|
+
*
|
|
91
|
+
* Complexity: O(iterations · (n log n + m)), O(iterations · n²) exact.
|
|
92
|
+
*
|
|
93
|
+
* @throws {RangeError} The conditions of `createForceStep`, a position buffer
|
|
94
|
+
* of the wrong length or holding ±Infinity, an option out of range. Whatever
|
|
95
|
+
* `budget.spend` throws propagates.
|
|
96
|
+
*/
|
|
97
|
+
export function forceDirectedLayout(nodes, links, traits, options) {
|
|
98
|
+
return runToCompletion(forceSteps_(nodes, links, traits, options));
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* {@link forceDirectedLayout} yielding to the event loop after every step; the
|
|
102
|
+
* result equals the sync form's. Validation errors reject. Aborting rejects
|
|
103
|
+
* with `signal.reason` before the next step and discards the work.
|
|
104
|
+
*/
|
|
105
|
+
export function forceDirectedLayoutAsync(nodes, links, traits, options) {
|
|
106
|
+
const { signal, sliceMs, ...layoutOptions } = options;
|
|
107
|
+
return runInTimeSlices(forceSteps_(nodes, links, traits, layoutOptions), {
|
|
108
|
+
signal,
|
|
109
|
+
sliceMs,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=force-directed-layout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"force-directed-layout.js","sourceRoot":"","sources":["../../src/layout/force-directed-layout.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,aAAa,EACb,aAAa,EACb,eAAe,EACf,eAAe,GACf,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAsB5D,SAAS,cAAc,CACtB,SAAmC,EACnC,SAAiB,EACjB,IAAY;IAEZ,IAAI,SAAS,KAAK,SAAS,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AM;QACN,OAAO;KACP,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Callable, WorkBudget } from "@ac-kit/core";
|
|
2
|
+
import type { CycleRemovalMethod } from "./_layered-acyclic.js";
|
|
3
|
+
import type { AsyncLayoutOptions, NetworkTraits } from "./types.js";
|
|
4
|
+
export type LayeredTraits<TNode, TLink> = NetworkTraits<TNode, TLink> & {
|
|
5
|
+
/** Node box size (default 0: points); finite, ≥ 0. */
|
|
6
|
+
readonly widthOf?: Callable<[node: TNode, index: number], number>;
|
|
7
|
+
readonly heightOf?: Callable<[node: TNode, index: number], number>;
|
|
8
|
+
/** Ranking and ordering weight. Default 1, finite ≥ 0. */
|
|
9
|
+
readonly weightOf?: Callable<[link: TLink, index: number], number>;
|
|
10
|
+
/** Minimum rank span. Default 1; a positive integer. */
|
|
11
|
+
readonly minLengthOf?: Callable<[link: TLink, index: number], number>;
|
|
12
|
+
};
|
|
13
|
+
export type LayeredLayoutOptions = {
|
|
14
|
+
/** Rank direction. Default "down" (ranks grow with y). */
|
|
15
|
+
readonly direction?: "down" | "up" | "right" | "left";
|
|
16
|
+
/** Gap between nodes of a rank, beyond their sizes. Default 50; finite, ≥ 0. */
|
|
17
|
+
readonly nodeSeparation?: number;
|
|
18
|
+
/** Gap between ranks, beyond their sizes. Default 50; finite, ≥ 0. */
|
|
19
|
+
readonly rankSeparation?: number;
|
|
20
|
+
/** Gap around the bends of long links. Default 10; finite, ≥ 0. */
|
|
21
|
+
readonly edgeSeparation?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Default "networkSimplex" (Gansner et al. 1993, minimises Σ weight·span);
|
|
24
|
+
* "longestPath" is O(V + E) and stacks sinks on the last rank.
|
|
25
|
+
*/
|
|
26
|
+
readonly ranking?: "networkSimplex" | "longestPath";
|
|
27
|
+
/**
|
|
28
|
+
* Links reversed to break cycles: "depthFirst" (default) or "greedy"
|
|
29
|
+
* (Eades–Lin–Smyth 1993).
|
|
30
|
+
*/
|
|
31
|
+
readonly cycleRemoval?: CycleRemovalMethod;
|
|
32
|
+
/** Ordering stops after this many sweeps without fewer crossings. Default 4. */
|
|
33
|
+
readonly orderingPatience?: number;
|
|
34
|
+
/** Charged per network-simplex pivot (V + E) and per ordering sweep (V′ + E′). */
|
|
35
|
+
readonly budget?: WorkBudget;
|
|
36
|
+
};
|
|
37
|
+
export type LayeredLayout = {
|
|
38
|
+
/** Node centres, 2 per node. */
|
|
39
|
+
readonly positions: Float64Array;
|
|
40
|
+
/** Rank per node, 0 first. */
|
|
41
|
+
readonly ranks: Uint32Array;
|
|
42
|
+
/** Position within its rank, 0 first. */
|
|
43
|
+
readonly orders: Uint32Array;
|
|
44
|
+
/**
|
|
45
|
+
* Polyline vertices, 2 each: per link, from the source's box border through
|
|
46
|
+
* each bend to the target's border.
|
|
47
|
+
*/
|
|
48
|
+
readonly edgePoints: Float64Array;
|
|
49
|
+
/**
|
|
50
|
+
* `linkCount + 1` offsets: link i is vertices `[edgeStarts[i], edgeStarts[i
|
|
51
|
+
*
|
|
52
|
+
* - 1])`. Self-loops are empty.
|
|
53
|
+
*/
|
|
54
|
+
readonly edgeStarts: Uint32Array;
|
|
55
|
+
/**
|
|
56
|
+
* 1 where link i was reversed to break a cycle; its polyline still runs
|
|
57
|
+
* source → target.
|
|
58
|
+
*/
|
|
59
|
+
readonly reversed: Uint8Array;
|
|
60
|
+
/** Size of the box holding every node and bend, origin (0, 0). */
|
|
61
|
+
readonly width: number;
|
|
62
|
+
readonly height: number;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Layered drawing of a directed graph (Sugiyama, Tagawa & Toda 1981): cycle
|
|
66
|
+
* removal, ranking, long links split by dummy vertices, crossing reduction by
|
|
67
|
+
* barycentric layer sweeps with exact bilayer crossing counts (Barth, Jünger &
|
|
68
|
+
* Mutzel 2004), coordinates by Brandes & Köpf 2001 (balanced median of four
|
|
69
|
+
* alignments). Disconnected parts share the ranks. Parallel links each get a
|
|
70
|
+
* path. Deterministic.
|
|
71
|
+
*
|
|
72
|
+
* The result is exactly the "down" layout turned: "up" flips the rank axis,
|
|
73
|
+
* "right" and "left" swap the axes (and flip, for "left") with the node sizes
|
|
74
|
+
* swapped, so widths always read along x and heights along y.
|
|
75
|
+
*
|
|
76
|
+
* Complexity: cycle removal O(V + E); ranking O(V + E) longest path, network
|
|
77
|
+
* simplex polynomial and near-linear in practice (cap with `budget`); with D
|
|
78
|
+
* dummy vertices, V′ = V + D and E′ = E + D: ordering O(sweeps · E′ log V′),
|
|
79
|
+
* coordinates O(V′ + E′). D grows with the rank span of the links, bounded by
|
|
80
|
+
* `minLengthOf`.
|
|
81
|
+
*
|
|
82
|
+
* @throws {RangeError} A link index outside `[0, nodes.length)` or not an
|
|
83
|
+
* integer, an invalid size, weight, minimum length or option, naming the
|
|
84
|
+
* row.
|
|
85
|
+
*/
|
|
86
|
+
export declare function layeredLayout<TNode, TLink>(nodes: readonly TNode[], links: readonly TLink[], traits: LayeredTraits<TNode, TLink>, options?: LayeredLayoutOptions): LayeredLayout;
|
|
87
|
+
/**
|
|
88
|
+
* {@link layeredLayout} yielding to the event loop after cycle removal, after
|
|
89
|
+
* every network-simplex round and pivot, after every ordering sweep and
|
|
90
|
+
* transpose pass, and before the coordinates; the result equals the sync
|
|
91
|
+
* form's. Validation errors reject. Aborting rejects with `signal.reason` at
|
|
92
|
+
* the next of those points and discards the work. A single pivot or sweep of a
|
|
93
|
+
* very large graph can still overshoot `sliceMs`.
|
|
94
|
+
*/
|
|
95
|
+
export declare function layeredLayoutAsync<TNode, TLink>(nodes: readonly TNode[], links: readonly TLink[], traits: LayeredTraits<TNode, TLink>, options?: LayeredLayoutOptions & AsyncLayoutOptions): Promise<LayeredLayout>;
|
|
96
|
+
//# sourceMappingURL=layered-layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layered-layout.d.ts","sourceRoot":"","sources":["../../src/layout/layered-layout.ts"],"names":[],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B,GACrD,OAAO,CAAC,aAAa,CAAC,CAMxB"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { assertNonNegative, describeValue, isIntegerInRange, runInTimeSlices, runToCompletion, } from "@ac-kit/core";
|
|
2
|
+
import { buildIndexGraph } from "./_index-graph.js";
|
|
3
|
+
import { orderLayerSteps } from "./_layer-order.js";
|
|
4
|
+
import { findReversedLinks } from "./_layered-acyclic.js";
|
|
5
|
+
import { placeLayers } from "./_layered-position.js";
|
|
6
|
+
import { longestPathRanks, networkSimplexRankSteps } from "./_layered-rank.js";
|
|
7
|
+
const DIRECTIONS_ = ["down", "up", "right", "left"];
|
|
8
|
+
const RANKINGS_ = ["networkSimplex", "longestPath"];
|
|
9
|
+
const CYCLE_REMOVALS_ = ["depthFirst", "greedy"];
|
|
10
|
+
function readNumbers_(items, read, isValid, describe) {
|
|
11
|
+
if (read === undefined) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
const values = new Float64Array(items.length);
|
|
15
|
+
for (let index = 0; index < items.length; index++) {
|
|
16
|
+
const value = read(items[index], index);
|
|
17
|
+
if (!isValid(value)) {
|
|
18
|
+
throw new RangeError(describe(index, value));
|
|
19
|
+
}
|
|
20
|
+
values[index] = value;
|
|
21
|
+
}
|
|
22
|
+
return values;
|
|
23
|
+
}
|
|
24
|
+
function isFiniteNonNegative_(value) {
|
|
25
|
+
return Number.isFinite(value) && value >= 0;
|
|
26
|
+
}
|
|
27
|
+
function* layeredSteps_(nodes, links, traits, options) {
|
|
28
|
+
const { direction = "down", nodeSeparation = 50, rankSeparation = 50, edgeSeparation = 10, ranking = "networkSimplex", cycleRemoval = "depthFirst", orderingPatience, budget, } = options;
|
|
29
|
+
if (!DIRECTIONS_.includes(direction)) {
|
|
30
|
+
throw new RangeError(`layeredLayout: direction must be "down", "up", "right" or "left", got ${describeValue(direction)}`);
|
|
31
|
+
}
|
|
32
|
+
if (!RANKINGS_.includes(ranking)) {
|
|
33
|
+
throw new RangeError(`layeredLayout: ranking must be "networkSimplex" or "longestPath", got ${describeValue(ranking)}`);
|
|
34
|
+
}
|
|
35
|
+
if (!CYCLE_REMOVALS_.includes(cycleRemoval)) {
|
|
36
|
+
throw new RangeError(`layeredLayout: cycleRemoval must be "depthFirst" or "greedy", got ${describeValue(cycleRemoval)}`);
|
|
37
|
+
}
|
|
38
|
+
assertNonNegative(nodeSeparation, "layeredLayout: nodeSeparation");
|
|
39
|
+
assertNonNegative(rankSeparation, "layeredLayout: rankSeparation");
|
|
40
|
+
assertNonNegative(edgeSeparation, "layeredLayout: edgeSeparation");
|
|
41
|
+
if (orderingPatience !== undefined &&
|
|
42
|
+
!isIntegerInRange(orderingPatience, 0, 2 ** 32)) {
|
|
43
|
+
throw new RangeError(`layeredLayout: orderingPatience must be a non-negative integer, got ${describeValue(orderingPatience)}`);
|
|
44
|
+
}
|
|
45
|
+
const nodeCount = nodes.length;
|
|
46
|
+
const graph = buildIndexGraph(nodeCount, links, traits, "layeredLayout");
|
|
47
|
+
const widths = readNumbers_(nodes, traits.widthOf, isFiniteNonNegative_, (index, value) => `layeredLayout: width of node ${index} must be finite and ≥ 0, got ${describeValue(value)}`);
|
|
48
|
+
const heights = readNumbers_(nodes, traits.heightOf, isFiniteNonNegative_, (index, value) => `layeredLayout: height of node ${index} must be finite and ≥ 0, got ${describeValue(value)}`);
|
|
49
|
+
const weights = readNumbers_(links, traits.weightOf, isFiniteNonNegative_, (index, value) => `layeredLayout: weight of link ${index} must be finite and ≥ 0, got ${describeValue(value)}`);
|
|
50
|
+
const minLengths = readNumbers_(links, traits.minLengthOf, (value) => isIntegerInRange(value, 1, 2 ** 32), (index, value) => `layeredLayout: minimum length of link ${index} must be a positive integer, got ${describeValue(value)}`);
|
|
51
|
+
yield;
|
|
52
|
+
const reversed = findReversedLinks(graph, cycleRemoval, weights);
|
|
53
|
+
yield;
|
|
54
|
+
const ranks = ranking === "longestPath"
|
|
55
|
+
? longestPathRanks(graph, reversed, minLengths)
|
|
56
|
+
: yield* networkSimplexRankSteps(graph, reversed, weights, minLengths, budget);
|
|
57
|
+
yield;
|
|
58
|
+
const order = yield* orderLayerSteps(graph, ranks, {
|
|
59
|
+
weights,
|
|
60
|
+
patience: orderingPatience,
|
|
61
|
+
budget,
|
|
62
|
+
});
|
|
63
|
+
yield;
|
|
64
|
+
// Ranks run along y for "down" and "up", along x for the other two, so the
|
|
65
|
+
// rank-axis extent of a node is its height, or its width when turned.
|
|
66
|
+
const turned = direction === "right" || direction === "left";
|
|
67
|
+
const empty = new Float64Array(nodeCount);
|
|
68
|
+
const placed = placeLayers(graph, order, reversed, (turned ? heights : widths) ?? empty, (turned ? widths : heights) ?? empty, direction, { nodeSeparation, rankSeparation, edgeSeparation });
|
|
69
|
+
return {
|
|
70
|
+
positions: placed.positions,
|
|
71
|
+
ranks,
|
|
72
|
+
orders: order.orders.slice(0, nodeCount),
|
|
73
|
+
edgePoints: placed.edgePoints,
|
|
74
|
+
edgeStarts: placed.edgeStarts,
|
|
75
|
+
reversed,
|
|
76
|
+
width: placed.width,
|
|
77
|
+
height: placed.height,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Layered drawing of a directed graph (Sugiyama, Tagawa & Toda 1981): cycle
|
|
82
|
+
* removal, ranking, long links split by dummy vertices, crossing reduction by
|
|
83
|
+
* barycentric layer sweeps with exact bilayer crossing counts (Barth, Jünger &
|
|
84
|
+
* Mutzel 2004), coordinates by Brandes & Köpf 2001 (balanced median of four
|
|
85
|
+
* alignments). Disconnected parts share the ranks. Parallel links each get a
|
|
86
|
+
* path. Deterministic.
|
|
87
|
+
*
|
|
88
|
+
* The result is exactly the "down" layout turned: "up" flips the rank axis,
|
|
89
|
+
* "right" and "left" swap the axes (and flip, for "left") with the node sizes
|
|
90
|
+
* swapped, so widths always read along x and heights along y.
|
|
91
|
+
*
|
|
92
|
+
* Complexity: cycle removal O(V + E); ranking O(V + E) longest path, network
|
|
93
|
+
* simplex polynomial and near-linear in practice (cap with `budget`); with D
|
|
94
|
+
* dummy vertices, V′ = V + D and E′ = E + D: ordering O(sweeps · E′ log V′),
|
|
95
|
+
* coordinates O(V′ + E′). D grows with the rank span of the links, bounded by
|
|
96
|
+
* `minLengthOf`.
|
|
97
|
+
*
|
|
98
|
+
* @throws {RangeError} A link index outside `[0, nodes.length)` or not an
|
|
99
|
+
* integer, an invalid size, weight, minimum length or option, naming the
|
|
100
|
+
* row.
|
|
101
|
+
*/
|
|
102
|
+
export function layeredLayout(nodes, links, traits, options = {}) {
|
|
103
|
+
return runToCompletion(layeredSteps_(nodes, links, traits, options));
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* {@link layeredLayout} yielding to the event loop after cycle removal, after
|
|
107
|
+
* every network-simplex round and pivot, after every ordering sweep and
|
|
108
|
+
* transpose pass, and before the coordinates; the result equals the sync
|
|
109
|
+
* form's. Validation errors reject. Aborting rejects with `signal.reason` at
|
|
110
|
+
* the next of those points and discards the work. A single pivot or sweep of a
|
|
111
|
+
* very large graph can still overshoot `sliceMs`.
|
|
112
|
+
*/
|
|
113
|
+
export function layeredLayoutAsync(nodes, links, traits, options = {}) {
|
|
114
|
+
const { signal, sliceMs, ...layoutOptions } = options;
|
|
115
|
+
return runInTimeSlices(layeredSteps_(nodes, links, traits, layoutOptions), {
|
|
116
|
+
signal,
|
|
117
|
+
sliceMs,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=layered-layout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layered-layout.js","sourceRoot":"","sources":["../../src/layout/layered-layout.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,iBAAiB,EACjB,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,eAAe,GACf,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACp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,GAA8C,EAAE;IAEvD,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,aAAa,EAAE,GAAG,OAAO,CAAC;IACtD,OAAO,eAAe,CAAC,aAAa,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE;QAC1E,MAAM;QACN,OAAO;KACP,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { HierarchyBoxLayout, HierarchyOrder, HierarchyWalkOptions, ValuedHierarchyTraits } from "./types.js";
|
|
2
|
+
export type PartitionLayoutOptions = HierarchyWalkOptions & {
|
|
3
|
+
/** Layout box; default 1 × 1. Breadth (value) along x, depth along y. */
|
|
4
|
+
readonly width?: number;
|
|
5
|
+
readonly height?: number;
|
|
6
|
+
/** Gap removed from each box's right and bottom edges. Default 0. */
|
|
7
|
+
readonly padding?: number;
|
|
8
|
+
/** Round box edges to integers (halves round up). Default false. */
|
|
9
|
+
readonly round?: boolean;
|
|
10
|
+
readonly order?: HierarchyOrder;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Adjacency layout: each row's box spans its value along x and one band of
|
|
14
|
+
* `height / (treeHeight + 1)` along y — what an icicle, a flame graph (flipped
|
|
15
|
+
* by the caller) and a sunburst (x as angle, y as radius) draw. A node's
|
|
16
|
+
* children share its x extent in proportion to their values (the slice-and-dice
|
|
17
|
+
* cut of Shneiderman's tree-maps applied to one band per depth, as in Kruskal &
|
|
18
|
+
* Landis's icicle plots).
|
|
19
|
+
*
|
|
20
|
+
* A zero-value child gets a zero-width box at its position; a box whose padding
|
|
21
|
+
* exceeds its extent collapses to its midpoint. The root box starts at
|
|
22
|
+
* `(padding, padding)`. Values and sizes are meant to lie within [1e−150,
|
|
23
|
+
* 1e150].
|
|
24
|
+
*
|
|
25
|
+
* Complexity: O(n), O(n log n) with `order: "valueDescending"`.
|
|
26
|
+
*
|
|
27
|
+
* @throws {RangeError} A negative or non-finite value, width, height or
|
|
28
|
+
* padding.
|
|
29
|
+
*/
|
|
30
|
+
export declare function partitionLayout<TNode>(root: TNode, traits: ValuedHierarchyTraits<TNode>, options?: PartitionLayoutOptions): HierarchyBoxLayout<TNode>;
|
|
31
|
+
//# sourceMappingURL=partition-layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"partition-layout.d.ts","sourceRoot":"","sources":["../../src/layout/partition-layout.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACX,kBAAkB,EAClB,cAAc,EACd,oBAAoB,EACpB,qBAAqB,EACrB,MAAM,YAAY,CAAC;AAEpB,MAAM,MAAM,sBAAsB,GAAG,oBAAoB,GAAG;IAC3D,yEAAyE;IACzE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,qEAAqE;IACrE,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,oEAAoE;IACpE,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,KAAK,CAAC,EAAE,cAAc,CAAC;CAChC,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,eAAe,CAAC,KAAK,EACpC,IAAI,EAAE,KAAK,EACX,MAAM,EAAE,qBAAqB,CAAC,KAAK,CAAC,EACpC,OAAO,GAAE,sBAA2B,GAClC,kBAAkB,CAAC,KAAK,CAAC,CA0E3B"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { groupIndices } from "@ac-kit/algo";
|
|
2
|
+
import { assertNonNegative } from "@ac-kit/core";
|
|
3
|
+
import { buildValuedHierarchyTable } from "./_hierarchy-table.js";
|
|
4
|
+
import { tileDice } from "./_treemap-tiling.js";
|
|
5
|
+
/**
|
|
6
|
+
* Adjacency layout: each row's box spans its value along x and one band of
|
|
7
|
+
* `height / (treeHeight + 1)` along y — what an icicle, a flame graph (flipped
|
|
8
|
+
* by the caller) and a sunburst (x as angle, y as radius) draw. A node's
|
|
9
|
+
* children share its x extent in proportion to their values (the slice-and-dice
|
|
10
|
+
* cut of Shneiderman's tree-maps applied to one band per depth, as in Kruskal &
|
|
11
|
+
* Landis's icicle plots).
|
|
12
|
+
*
|
|
13
|
+
* A zero-value child gets a zero-width box at its position; a box whose padding
|
|
14
|
+
* exceeds its extent collapses to its midpoint. The root box starts at
|
|
15
|
+
* `(padding, padding)`. Values and sizes are meant to lie within [1e−150,
|
|
16
|
+
* 1e150].
|
|
17
|
+
*
|
|
18
|
+
* Complexity: O(n), O(n log n) with `order: "valueDescending"`.
|
|
19
|
+
*
|
|
20
|
+
* @throws {RangeError} A negative or non-finite value, width, height or
|
|
21
|
+
* padding.
|
|
22
|
+
*/
|
|
23
|
+
export function partitionLayout(root, traits, options = {}) {
|
|
24
|
+
const { width = 1, height = 1, padding = 0, round = false } = options;
|
|
25
|
+
assertNonNegative(width, "partitionLayout: width");
|
|
26
|
+
assertNonNegative(height, "partitionLayout: height");
|
|
27
|
+
assertNonNegative(padding, "partitionLayout: padding");
|
|
28
|
+
const table = buildValuedHierarchyTable(root, traits, { budget: options.budget, order: options.order }, "partitionLayout");
|
|
29
|
+
const { nodes, depths, values } = table;
|
|
30
|
+
const count = nodes.length;
|
|
31
|
+
const boxes = new Float64Array(count * 4);
|
|
32
|
+
let bandCount = 1;
|
|
33
|
+
for (let row = 0; row < count; row++) {
|
|
34
|
+
if (depths[row] + 1 > bandCount) {
|
|
35
|
+
bandCount = depths[row] + 1;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const children = groupIndices(table.parentIndices, (parent) => parent, count);
|
|
39
|
+
const context = {
|
|
40
|
+
boxes,
|
|
41
|
+
values,
|
|
42
|
+
depths,
|
|
43
|
+
children,
|
|
44
|
+
aspectRatio: 1,
|
|
45
|
+
};
|
|
46
|
+
boxes[0] = padding;
|
|
47
|
+
boxes[1] = padding;
|
|
48
|
+
boxes[2] = width;
|
|
49
|
+
boxes[3] = height / bandCount;
|
|
50
|
+
for (let row = 0; row < count; row++) {
|
|
51
|
+
const offset = row * 4;
|
|
52
|
+
const depth = depths[row];
|
|
53
|
+
if (children.offsets[row + 1] > children.offsets[row]) {
|
|
54
|
+
tileDice(context, row, boxes[offset], (height * (depth + 1)) / bandCount, boxes[offset + 2], (height * (depth + 2)) / bandCount);
|
|
55
|
+
}
|
|
56
|
+
let x0 = boxes[offset];
|
|
57
|
+
let y0 = boxes[offset + 1];
|
|
58
|
+
let x1 = boxes[offset + 2] - padding;
|
|
59
|
+
let y1 = boxes[offset + 3] - padding;
|
|
60
|
+
if (x1 < x0) {
|
|
61
|
+
x0 = x1 = (x0 + x1) / 2;
|
|
62
|
+
}
|
|
63
|
+
if (y1 < y0) {
|
|
64
|
+
y0 = y1 = (y0 + y1) / 2;
|
|
65
|
+
}
|
|
66
|
+
boxes[offset] = x0;
|
|
67
|
+
boxes[offset + 1] = y0;
|
|
68
|
+
boxes[offset + 2] = x1;
|
|
69
|
+
boxes[offset + 3] = y1;
|
|
70
|
+
}
|
|
71
|
+
if (round) {
|
|
72
|
+
for (let index = 0; index < boxes.length; index++) {
|
|
73
|
+
boxes[index] = Math.round(boxes[index]);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { ...table, boxes };
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=partition-layout.js.map
|