@fengwk/kchart 0.0.0-stage → 0.1.1
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 +17 -0
- package/LICENSE +21 -0
- package/NOTICE.md +11 -0
- package/README.md +68 -2
- package/README.zh-CN.md +69 -0
- package/dist/LICENSE +21 -0
- package/dist/NOTICE.md +11 -0
- package/dist/assets/ICONS-LICENSE.txt +53 -0
- package/dist/assets/maps/LICENSE.md +75 -0
- package/dist/assets/maps/METADATA.md +9 -0
- package/dist/assets/maps/REGIONS.md +28 -0
- package/dist/assets/maps/us-states.geojson +1 -0
- package/dist/assets/maps/world.geojson +1 -0
- package/dist/esm/_virtual/_kchart-families.js +48 -0
- package/dist/esm/_virtual/_rolldown/runtime.js +12 -0
- package/dist/esm/assets/icons.js +11 -0
- package/dist/esm/assets/maps/us-states.js +3 -0
- package/dist/esm/assets/maps/world.js +3 -0
- package/dist/esm/assets/themes/claude.js +69 -0
- package/dist/esm/assets/themes/cloudmusic.js +62 -0
- package/dist/esm/assets/themes/kanagawa.js +67 -0
- package/dist/esm/assets/themes/obsidian.js +67 -0
- package/dist/esm/assets/themes/paper.js +61 -0
- package/dist/esm/assets/themes/terminal.js +60 -0
- package/dist/esm/assets/themes/tokyo.js +62 -0
- package/dist/esm/chart/basic.js +1313 -0
- package/dist/esm/chart/catalog.js +476 -0
- package/dist/esm/chart/composition.js +844 -0
- package/dist/esm/chart/context.js +189 -0
- package/dist/esm/chart/distribution.js +442 -0
- package/dist/esm/chart/fields.js +67 -0
- package/dist/esm/chart/maps.js +563 -0
- package/dist/esm/chart/mindmap.js +629 -0
- package/dist/esm/chart/multivariate.js +1229 -0
- package/dist/esm/chart/registry.js +108 -0
- package/dist/esm/chart/relations.js +949 -0
- package/dist/esm/chart/spec.js +1415 -0
- package/dist/esm/chart/stat-compare.js +302 -0
- package/dist/esm/chart/stat-trend.js +181 -0
- package/dist/esm/chart/statistics.js +1007 -0
- package/dist/esm/chart/unit-variants.js +645 -0
- package/dist/esm/chart/variants.js +552 -0
- package/dist/esm/core/color.js +197 -0
- package/dist/esm/core/context.js +256 -0
- package/dist/esm/core/diagnostics.js +23 -0
- package/dist/esm/core/format.js +167 -0
- package/dist/esm/core/geometry.js +130 -0
- package/dist/esm/core/icon-data.js +5 -0
- package/dist/esm/core/icons.js +173 -0
- package/dist/esm/core/scale.js +112 -0
- package/dist/esm/core/scene.js +234 -0
- package/dist/esm/core/svg.js +89 -0
- package/dist/esm/core/text.js +184 -0
- package/dist/esm/core/theme.js +318 -0
- package/dist/esm/diagram/agentflow.js +596 -0
- package/dist/esm/diagram/architecture.js +729 -0
- package/dist/esm/diagram/block.js +988 -0
- package/dist/esm/diagram/blocks.js +687 -0
- package/dist/esm/diagram/c4.js +1002 -0
- package/dist/esm/diagram/class.js +1158 -0
- package/dist/esm/diagram/cynefin.js +749 -0
- package/dist/esm/diagram/emphasis.js +7 -0
- package/dist/esm/diagram/er.js +657 -0
- package/dist/esm/diagram/eventmodeling.js +948 -0
- package/dist/esm/diagram/gantt-date.js +323 -0
- package/dist/esm/diagram/gantt.js +661 -0
- package/dist/esm/diagram/gitgraph.js +472 -0
- package/dist/esm/diagram/graph-layout.js +4046 -0
- package/dist/esm/diagram/graph-parse.js +1366 -0
- package/dist/esm/diagram/graph-route.js +490 -0
- package/dist/esm/diagram/graph.js +553 -0
- package/dist/esm/diagram/ishikawa.js +496 -0
- package/dist/esm/diagram/journey.js +512 -0
- package/dist/esm/diagram/kanban.js +280 -0
- package/dist/esm/diagram/label.js +56 -0
- package/dist/esm/diagram/layers.js +385 -0
- package/dist/esm/diagram/map-label.js +44 -0
- package/dist/esm/diagram/mindmap.js +277 -0
- package/dist/esm/diagram/numeric-composition.js +78 -0
- package/dist/esm/diagram/numeric-model.js +86 -0
- package/dist/esm/diagram/numeric-parse.js +205 -0
- package/dist/esm/diagram/numeric-radar.js +355 -0
- package/dist/esm/diagram/numeric-sankey.js +201 -0
- package/dist/esm/diagram/numeric-xy.js +422 -0
- package/dist/esm/diagram/packet.js +175 -0
- package/dist/esm/diagram/presentation.js +252 -0
- package/dist/esm/diagram/quadrant.js +297 -0
- package/dist/esm/diagram/railroad-parse.js +465 -0
- package/dist/esm/diagram/railroad.js +294 -0
- package/dist/esm/diagram/registry.js +296 -0
- package/dist/esm/diagram/relations.js +966 -0
- package/dist/esm/diagram/requirement.js +341 -0
- package/dist/esm/diagram/sequence.js +1228 -0
- package/dist/esm/diagram/shapes.js +366 -0
- package/dist/esm/diagram/source.js +538 -0
- package/dist/esm/diagram/support.js +153 -0
- package/dist/esm/diagram/swimlane-mermaid.js +140 -0
- package/dist/esm/diagram/swimlane.js +1186 -0
- package/dist/esm/diagram/time.js +748 -0
- package/dist/esm/diagram/timeline.js +376 -0
- package/dist/esm/diagram/tree-family-icon.js +22 -0
- package/dist/esm/diagram/treeview.js +293 -0
- package/dist/esm/diagram/usecase.js +852 -0
- package/dist/esm/diagram/venn.js +1038 -0
- package/dist/esm/diagram/wardley.js +838 -0
- package/dist/esm/index.d.ts +133 -0
- package/dist/esm/index.js +92 -0
- package/dist/esm/interaction.js +561 -0
- package/dist/esm/mermaid.d.ts +28 -0
- package/dist/esm/mermaid.js +70 -0
- package/dist/esm/node.d.ts +68 -0
- package/dist/esm/node.js +373 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/page-api.js +65 -0
- package/dist/esm/runtime.js +352 -0
- package/dist/esm/src/families.js +209 -0
- package/dist/page-api.js +1 -0
- package/dist/units/chart/basic.js +1 -0
- package/dist/units/chart/catalog.js +1 -0
- package/dist/units/chart/composition.js +1 -0
- package/dist/units/chart/context.js +1 -0
- package/dist/units/chart/distribution.js +1 -0
- package/dist/units/chart/fields.js +1 -0
- package/dist/units/chart/maps.js +1 -0
- package/dist/units/chart/mindmap.js +1 -0
- package/dist/units/chart/multivariate.js +1 -0
- package/dist/units/chart/registry.js +1 -0
- package/dist/units/chart/relations.js +1 -0
- package/dist/units/chart/spec.js +3 -0
- package/dist/units/chart/stat-compare.js +1 -0
- package/dist/units/chart/stat-trend.js +1 -0
- package/dist/units/chart/statistics.js +1 -0
- package/dist/units/chart/unit-variants.js +1 -0
- package/dist/units/chart/variants.js +1 -0
- package/dist/units/core/color.js +1 -0
- package/dist/units/core/context.js +1 -0
- package/dist/units/core/diagnostics.js +1 -0
- package/dist/units/core/format.js +1 -0
- package/dist/units/core/geometry.js +1 -0
- package/dist/units/core/icon-data.js +1 -0
- package/dist/units/core/icons.js +1 -0
- package/dist/units/core/scale.js +1 -0
- package/dist/units/core/scene.js +1 -0
- package/dist/units/core/svg.js +1 -0
- package/dist/units/core/text.js +2 -0
- package/dist/units/core/theme.js +1 -0
- package/dist/units/diagram/agentflow.js +12 -0
- package/dist/units/diagram/architecture.js +4 -0
- package/dist/units/diagram/block.js +2 -0
- package/dist/units/diagram/blocks.js +2 -0
- package/dist/units/diagram/c4.js +6 -0
- package/dist/units/diagram/class.js +5 -0
- package/dist/units/diagram/cynefin.js +4 -0
- package/dist/units/diagram/emphasis.js +1 -0
- package/dist/units/diagram/er.js +9 -0
- package/dist/units/diagram/eventmodeling.js +5 -0
- package/dist/units/diagram/gantt-date.js +1 -0
- package/dist/units/diagram/gantt.js +3 -0
- package/dist/units/diagram/gitgraph.js +2 -0
- package/dist/units/diagram/graph-layout.js +4 -0
- package/dist/units/diagram/graph-parse.js +9 -0
- package/dist/units/diagram/graph-route.js +1 -0
- package/dist/units/diagram/graph.js +1 -0
- package/dist/units/diagram/ishikawa.js +2 -0
- package/dist/units/diagram/journey.js +5 -0
- package/dist/units/diagram/kanban.js +2 -0
- package/dist/units/diagram/label.js +4 -0
- package/dist/units/diagram/layers.js +1 -0
- package/dist/units/diagram/map-label.js +1 -0
- package/dist/units/diagram/mindmap.js +2 -0
- package/dist/units/diagram/numeric-composition.js +1 -0
- package/dist/units/diagram/numeric-model.js +1 -0
- package/dist/units/diagram/numeric-parse.js +2 -0
- package/dist/units/diagram/numeric-radar.js +3 -0
- package/dist/units/diagram/numeric-sankey.js +2 -0
- package/dist/units/diagram/numeric-xy.js +2 -0
- package/dist/units/diagram/packet.js +2 -0
- package/dist/units/diagram/presentation.js +2 -0
- package/dist/units/diagram/quadrant.js +2 -0
- package/dist/units/diagram/railroad-parse.js +4 -0
- package/dist/units/diagram/railroad.js +1 -0
- package/dist/units/diagram/registry.js +2 -0
- package/dist/units/diagram/relations.js +3 -0
- package/dist/units/diagram/requirement.js +3 -0
- package/dist/units/diagram/sequence.js +2 -0
- package/dist/units/diagram/shapes.js +60 -0
- package/dist/units/diagram/source.js +20 -0
- package/dist/units/diagram/support.js +3 -0
- package/dist/units/diagram/swimlane-mermaid.js +3 -0
- package/dist/units/diagram/swimlane.js +2 -0
- package/dist/units/diagram/time.js +3 -0
- package/dist/units/diagram/timeline.js +2 -0
- package/dist/units/diagram/tree-family-icon.js +1 -0
- package/dist/units/diagram/treeview.js +2 -0
- package/dist/units/diagram/usecase.js +3 -0
- package/dist/units/diagram/venn.js +2 -0
- package/dist/units/diagram/wardley.js +4 -0
- package/dist/units/interaction.js +1 -0
- package/dist/units/runtime.js +1 -0
- package/dist/units.json +712 -0
- package/package.json +30 -4
|
@@ -0,0 +1,949 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { register } from "./registry.js";
|
|
3
|
+
import { sizeBudget } from "./spec.js";
|
|
4
|
+
// src/chart/relations.js
|
|
5
|
+
var relations_exports = /* @__PURE__ */ __exportAll({
|
|
6
|
+
install: () => install,
|
|
7
|
+
sankeyPresentation: () => sankeyPresentation
|
|
8
|
+
});
|
|
9
|
+
var clamp = (n, lo, hi) => Math.max(lo, Math.min(hi, n));
|
|
10
|
+
var idOf = (record, fallback) => String(record.id ?? fallback);
|
|
11
|
+
var textOf = (record) => String(record.label ?? record.id ?? "");
|
|
12
|
+
var stable = (a, b) => String(a) < String(b) ? -1 : String(a) > String(b) ? 1 : 0;
|
|
13
|
+
var num = (v, fallback = 0) => Number.isFinite(v) ? v : fallback;
|
|
14
|
+
var pair = (x, y) => `${x.toFixed(2)},${y.toFixed(2)}`;
|
|
15
|
+
var withDescription = (ctx, height, children, spec, width = ctx.width) => ({
|
|
16
|
+
...ctx.scene(height, children),
|
|
17
|
+
width,
|
|
18
|
+
description: spec.note || void 0
|
|
19
|
+
});
|
|
20
|
+
function stackLabels(items, heightOf, gap, floor, ceiling) {
|
|
21
|
+
const ordered = [...items].sort((a, b) => a.target - b.target || stable(a.id, b.id));
|
|
22
|
+
let cursor = floor;
|
|
23
|
+
for (const item of ordered) {
|
|
24
|
+
item.top = Math.max(item.target, cursor);
|
|
25
|
+
cursor = item.top + heightOf(item) + gap;
|
|
26
|
+
}
|
|
27
|
+
if (cursor - gap - ceiling > 0) for (let i = ordered.length - 1; i >= 0; i--) {
|
|
28
|
+
const item = ordered[i];
|
|
29
|
+
const nextLimit = i < ordered.length - 1 ? ordered[i + 1].top - gap - heightOf(item) : ceiling - heightOf(item);
|
|
30
|
+
item.top = Math.min(item.top, nextLimit);
|
|
31
|
+
}
|
|
32
|
+
if (ordered.length && ordered[0].top < floor) {
|
|
33
|
+
const shift = floor - ordered[0].top;
|
|
34
|
+
for (const item of ordered) item.top += shift;
|
|
35
|
+
}
|
|
36
|
+
return ordered;
|
|
37
|
+
}
|
|
38
|
+
var paletteColor = (ctx, group) => {
|
|
39
|
+
if (group == null || group === "") return ctx.color("tone1");
|
|
40
|
+
const tones = [
|
|
41
|
+
"tone1",
|
|
42
|
+
"tone2",
|
|
43
|
+
"tone3",
|
|
44
|
+
"tone4",
|
|
45
|
+
"tone5",
|
|
46
|
+
"tone6"
|
|
47
|
+
];
|
|
48
|
+
const hash = parseInt(ctx.hash(String(group)), 36) || 0;
|
|
49
|
+
return ctx.color(tones[hash % tones.length]);
|
|
50
|
+
};
|
|
51
|
+
function forcePositions(nodes, edges, box, radiusFor) {
|
|
52
|
+
const ordered = [...nodes].sort((a, b) => stable(a.id, b.id));
|
|
53
|
+
const positions = /* @__PURE__ */ new Map();
|
|
54
|
+
const ideal = Math.sqrt(box.w * box.h / Math.max(1, nodes.length));
|
|
55
|
+
const goldenAngle = Math.PI * (3 - Math.sqrt(5));
|
|
56
|
+
ordered.forEach((node, index) => {
|
|
57
|
+
const angle = index * goldenAngle;
|
|
58
|
+
const radius = Math.sqrt((index + .5) / ordered.length) * Math.min(box.w, box.h) * .42;
|
|
59
|
+
positions.set(node.id, {
|
|
60
|
+
x: Math.cos(angle) * radius,
|
|
61
|
+
y: Math.sin(angle) * radius
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
for (let iteration = 0; iteration < 240; iteration++) {
|
|
65
|
+
const forces = new Map(ordered.map((node) => [node.id, {
|
|
66
|
+
x: 0,
|
|
67
|
+
y: 0
|
|
68
|
+
}]));
|
|
69
|
+
for (let i = 0; i < ordered.length; i++) for (let j = i + 1; j < ordered.length; j++) {
|
|
70
|
+
const a = positions.get(ordered[i].id), b = positions.get(ordered[j].id);
|
|
71
|
+
const dx = a.x - b.x, dy = a.y - b.y;
|
|
72
|
+
const distance = Math.max(.1, Math.hypot(dx, dy));
|
|
73
|
+
const strength = ideal * ideal / distance;
|
|
74
|
+
const fx = dx / distance * strength, fy = dy / distance * strength;
|
|
75
|
+
forces.get(ordered[i].id).x += fx;
|
|
76
|
+
forces.get(ordered[i].id).y += fy;
|
|
77
|
+
forces.get(ordered[j].id).x -= fx;
|
|
78
|
+
forces.get(ordered[j].id).y -= fy;
|
|
79
|
+
}
|
|
80
|
+
for (const edge of edges) {
|
|
81
|
+
if (edge.source === edge.target) continue;
|
|
82
|
+
const a = positions.get(edge.source), b = positions.get(edge.target);
|
|
83
|
+
const dx = b.x - a.x, dy = b.y - a.y;
|
|
84
|
+
const distance = Math.max(.1, Math.hypot(dx, dy));
|
|
85
|
+
const strength = distance * distance / ideal;
|
|
86
|
+
const fx = dx / distance * strength, fy = dy / distance * strength;
|
|
87
|
+
forces.get(edge.source).x += fx;
|
|
88
|
+
forces.get(edge.source).y += fy;
|
|
89
|
+
forces.get(edge.target).x -= fx;
|
|
90
|
+
forces.get(edge.target).y -= fy;
|
|
91
|
+
}
|
|
92
|
+
const temperature = Math.min(box.w, box.h) * .08 * (1 - iteration / 240) ** 2 + .08;
|
|
93
|
+
for (const node of ordered) {
|
|
94
|
+
const point = positions.get(node.id), force = forces.get(node.id);
|
|
95
|
+
force.x -= point.x * .35;
|
|
96
|
+
force.y -= point.y * .35;
|
|
97
|
+
const length = Math.max(.1, Math.hypot(force.x, force.y));
|
|
98
|
+
const step = Math.min(length, temperature) / length;
|
|
99
|
+
point.x += force.x * step;
|
|
100
|
+
point.y += force.y * step;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const xs = [...positions.values()].map((point) => point.x);
|
|
104
|
+
const ys = [...positions.values()].map((point) => point.y);
|
|
105
|
+
const minX = Math.min(...xs), maxX = Math.max(...xs);
|
|
106
|
+
const minY = Math.min(...ys), maxY = Math.max(...ys);
|
|
107
|
+
const scale = Math.min(box.w / Math.max(1, maxX - minX), box.h / Math.max(1, maxY - minY));
|
|
108
|
+
for (const point of positions.values()) {
|
|
109
|
+
point.x = box.x + box.w / 2 + (point.x - (minX + maxX) / 2) * scale;
|
|
110
|
+
point.y = box.y + box.h / 2 + (point.y - (minY + maxY) / 2) * scale;
|
|
111
|
+
}
|
|
112
|
+
for (let iteration = 0; iteration < 40; iteration++) {
|
|
113
|
+
for (let i = 0; i < ordered.length; i++) for (let j = i + 1; j < ordered.length; j++) {
|
|
114
|
+
const a = positions.get(ordered[i].id), b = positions.get(ordered[j].id);
|
|
115
|
+
const dx = b.x - a.x, dy = b.y - a.y, distance = Math.max(.01, Math.hypot(dx, dy));
|
|
116
|
+
const required = radiusFor(ordered[i]) + radiusFor(ordered[j]) + 2;
|
|
117
|
+
if (distance >= required) continue;
|
|
118
|
+
const shift = (required - distance) / 2;
|
|
119
|
+
a.x -= dx / distance * shift;
|
|
120
|
+
a.y -= dy / distance * shift;
|
|
121
|
+
b.x += dx / distance * shift;
|
|
122
|
+
b.y += dy / distance * shift;
|
|
123
|
+
}
|
|
124
|
+
for (const point of positions.values()) {
|
|
125
|
+
point.x = clamp(point.x, box.x, box.x + box.w);
|
|
126
|
+
point.y = clamp(point.y, box.y, box.y + box.h);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return positions;
|
|
130
|
+
}
|
|
131
|
+
function network(spec, ctx) {
|
|
132
|
+
const data = spec.data;
|
|
133
|
+
const nodes = data.nodes, edges = data.edges;
|
|
134
|
+
if (nodes.length > 200 || edges.length > 500) ctx.fit("network超过200个节点或500条边", "缩小数据范围后重试");
|
|
135
|
+
const force = (spec.layout || "circular") === "force";
|
|
136
|
+
const labelExtent = Math.max(72, ...nodes.map((node) => ctx.measure(textOf(node), 12, 400)));
|
|
137
|
+
const sidePad = Math.min(168, labelExtent + 24);
|
|
138
|
+
const maxLabelLines = Math.max(1, ...nodes.map((n) => ctx.wrap(textOf(n), force && nodes.length > 80 ? 150 : 96, 12, 400).length));
|
|
139
|
+
const denseForce = force && nodes.length > 80;
|
|
140
|
+
let W = ctx.width;
|
|
141
|
+
const labelStackHeight = Math.ceil(nodes.length / 2) * (maxLabelLines * 14 + 4);
|
|
142
|
+
const narrowCircular = !force && W <= 360;
|
|
143
|
+
const narrowRadius = Math.min(W * .32, 90) * .77;
|
|
144
|
+
const H = denseForce ? Math.max(240, Math.round(W * .82)) : narrowCircular ? Math.ceil(2 * (narrowRadius + 17) + labelStackHeight + 64) : Math.max(440, 330 + Math.sqrt(nodes.length) * 24, 180 + (force ? labelStackHeight : nodes.length * (maxLabelLines * 14 + 4)));
|
|
145
|
+
const nodeDegrees = new Map(nodes.map((node) => [node.id, 0]));
|
|
146
|
+
for (const edge of edges) {
|
|
147
|
+
nodeDegrees.set(edge.source, nodeDegrees.get(edge.source) + 1);
|
|
148
|
+
nodeDegrees.set(edge.target, nodeDegrees.get(edge.target) + 1);
|
|
149
|
+
}
|
|
150
|
+
const denseLabelLimit = W <= 360 ? 4 : W <= 600 ? 8 : 12;
|
|
151
|
+
const labelledNodes = denseForce ? new Set([...nodes].sort((a, b) => nodeDegrees.get(b.id) - nodeDegrees.get(a.id) || stable(a.id, b.id)).slice(0, denseLabelLimit).map((node) => node.id)) : null;
|
|
152
|
+
const unvaluedRadius = denseForce ? Math.min(4, Math.max(2.5, W / 160)) : 5.5;
|
|
153
|
+
const maxV = Math.max(0, ...nodes.map((n) => Math.max(0, num(n.value))));
|
|
154
|
+
const radiusFor = (node) => node.value == null ? unvaluedRadius : maxV ? 17 * Math.sqrt(Math.max(0, num(node.value)) / maxV) : 0;
|
|
155
|
+
const labelWeight = (node) => node.value != null && node.value > 0 ? 600 : 400;
|
|
156
|
+
const minLabelWidth = Math.max(0, ...nodes.flatMap((node) => ctx.wrap(textOf(node), 72, 12, labelWeight(node)).map((line) => ctx.measure(line, 12, labelWeight(node)))));
|
|
157
|
+
const labelRoom = Math.max(0, ...nodes.map(radiusFor)) + 10 + minLabelWidth;
|
|
158
|
+
const minRadius = 48;
|
|
159
|
+
let horizontalPadding = sidePad;
|
|
160
|
+
if (denseForce) horizontalPadding = Math.max(28, W * .06);
|
|
161
|
+
else if (force) {
|
|
162
|
+
horizontalPadding = Math.max(sidePad, labelRoom);
|
|
163
|
+
if (W - 2 * horizontalPadding < 96) horizontalPadding = Math.max(labelRoom, W / 2 - minRadius);
|
|
164
|
+
if (W - 2 * horizontalPadding < 96) W = Math.ceil(2 * (horizontalPadding + minRadius));
|
|
165
|
+
} else if (W / 2 - labelRoom < minRadius) W = Math.ceil(2 * (minRadius + labelRoom));
|
|
166
|
+
const cx = W / 2, cy = H / 2;
|
|
167
|
+
const rx = Math.min(W * .32, Math.max(90, H * .36), cx - labelRoom), ry = Math.min(H * .34, rx * .77);
|
|
168
|
+
const positions = force ? forcePositions(nodes, edges, {
|
|
169
|
+
x: horizontalPadding,
|
|
170
|
+
y: 36,
|
|
171
|
+
w: Math.max(1, W - 2 * horizontalPadding),
|
|
172
|
+
h: H - 84
|
|
173
|
+
}, radiusFor) : /* @__PURE__ */ new Map();
|
|
174
|
+
if (!force) nodes.forEach((n, i) => {
|
|
175
|
+
const angle = -Math.PI / 2 + 2 * Math.PI * i / Math.max(1, nodes.length);
|
|
176
|
+
positions.set(n.id, {
|
|
177
|
+
x: cx + rx * Math.cos(angle),
|
|
178
|
+
y: cy + ry * Math.sin(angle)
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
const maxEdge = Math.max(0, ...edges.map((e) => Math.abs(num(e.value))));
|
|
182
|
+
const children = [];
|
|
183
|
+
const incident = new Map(nodes.map((n) => [n.id, edges.filter((e) => e.source === n.id || e.target === n.id)]));
|
|
184
|
+
const arrows = [];
|
|
185
|
+
for (const [i, e] of edges.entries()) {
|
|
186
|
+
const a = positions.get(e.source), b = positions.get(e.target);
|
|
187
|
+
if (!a || !b) continue;
|
|
188
|
+
const dx = b.x - a.x, dy = b.y - a.y, d = Math.max(1, Math.hypot(dx, dy));
|
|
189
|
+
const bend = (parseInt(ctx.hash(idOf(e, i)), 36) % 17 - 8) * 2.2;
|
|
190
|
+
const mx = (a.x + b.x) / 2 - dy / d * bend, my = (a.y + b.y) / 2 + dx / d * bend;
|
|
191
|
+
const width = e.value == null ? 1.4 : 1 + 3 * Math.sqrt(Math.abs(e.value) / Math.max(1e-12, maxEdge));
|
|
192
|
+
const ident = {
|
|
193
|
+
kind: "edge",
|
|
194
|
+
edge: idOf(e, `edge-${i}`),
|
|
195
|
+
source: String(e.source),
|
|
196
|
+
target: String(e.target),
|
|
197
|
+
value: e.value
|
|
198
|
+
};
|
|
199
|
+
children.push(ctx.path(`M${pair(a.x, a.y)} Q${pair(mx, my)} ${pair(b.x, b.y)}`, {
|
|
200
|
+
fill: "none",
|
|
201
|
+
stroke: ctx.color("tone3"),
|
|
202
|
+
"stroke-width": spec.variant === "membrane" ? Math.max(3, width * 3) : width,
|
|
203
|
+
"stroke-opacity": spec.variant === "membrane" ? ".24" : ".58",
|
|
204
|
+
"stroke-linecap": "round",
|
|
205
|
+
meta: ident
|
|
206
|
+
}));
|
|
207
|
+
if (spec.directed) {
|
|
208
|
+
const ux = (b.x - mx) / Math.max(1, Math.hypot(b.x - mx, b.y - my)), uy = (b.y - my) / Math.max(1, Math.hypot(b.x - mx, b.y - my));
|
|
209
|
+
const radius = radiusFor(nodes.find((n) => n.id === e.target));
|
|
210
|
+
const tipX = b.x - ux * radius, tipY = b.y - uy * radius, baseX = tipX - ux * 8, baseY = tipY - uy * 8;
|
|
211
|
+
arrows.push(ctx.path(`M${pair(tipX, tipY)} L${pair(baseX - uy * 3.5, baseY + ux * 3.5)} L${pair(baseX + uy * 3.5, baseY - ux * 3.5)} Z`, {
|
|
212
|
+
fill: ctx.color("tone3"),
|
|
213
|
+
stroke: "none",
|
|
214
|
+
meta: {
|
|
215
|
+
...ident,
|
|
216
|
+
kind: "arrow"
|
|
217
|
+
}
|
|
218
|
+
}));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const labelGroups = {
|
|
222
|
+
start: [],
|
|
223
|
+
end: []
|
|
224
|
+
};
|
|
225
|
+
const labelFill = spec.variant === "membrane" ? ctx.textOn("tone3", {
|
|
226
|
+
opacity: .24,
|
|
227
|
+
over: "bg"
|
|
228
|
+
}) : ctx.textOn("tone3", {
|
|
229
|
+
opacity: .58,
|
|
230
|
+
over: "bg"
|
|
231
|
+
});
|
|
232
|
+
for (const n of nodes) {
|
|
233
|
+
const p = positions.get(n.id), radius = radiusFor(n);
|
|
234
|
+
children.push(ctx.circle(p.x, p.y, radius, {
|
|
235
|
+
fill: paletteColor(ctx, n.group),
|
|
236
|
+
stroke: ctx.color("bg"),
|
|
237
|
+
"stroke-width": radius ? 2 : 0,
|
|
238
|
+
meta: {
|
|
239
|
+
kind: "node",
|
|
240
|
+
node: String(n.id),
|
|
241
|
+
value: n.value,
|
|
242
|
+
related: incident.get(n.id).map((e) => idOf(e, `edge-${edges.indexOf(e)}`))
|
|
243
|
+
}
|
|
244
|
+
}));
|
|
245
|
+
if (denseForce && !labelledNodes.has(n.id)) continue;
|
|
246
|
+
const weight = labelWeight(n);
|
|
247
|
+
const leftRoom = Math.max(72, p.x - radius - 20);
|
|
248
|
+
const rightRoom = Math.max(72, W - p.x - radius - 20);
|
|
249
|
+
const side = denseForce ? leftRoom >= rightRoom ? "start" : "end" : p.x < cx ? "start" : "end";
|
|
250
|
+
const maxWidth = Math.max(48, Math.min(160, side === "start" ? leftRoom : rightRoom));
|
|
251
|
+
const lines = ctx.wrap(textOf(n), maxWidth, 12, weight);
|
|
252
|
+
labelGroups[side].push({
|
|
253
|
+
n,
|
|
254
|
+
p,
|
|
255
|
+
radius,
|
|
256
|
+
lines,
|
|
257
|
+
weight,
|
|
258
|
+
top: p.y - (lines.length - 1) * 7
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
const occupiedLabels = [];
|
|
262
|
+
let labelFloor = 0;
|
|
263
|
+
for (const side of ["start", "end"]) {
|
|
264
|
+
const labels = labelGroups[side].sort((a, b) => a.top - b.top || stable(a.n.id, b.n.id));
|
|
265
|
+
let previousBottom = 16;
|
|
266
|
+
for (const item of labels) {
|
|
267
|
+
const weight = item.weight;
|
|
268
|
+
const measured = Math.max(12, ...item.lines.map((line) => ctx.measure(line, 12, weight)));
|
|
269
|
+
const x = item.p.x + (side === "start" ? -item.radius - 8 : item.radius + 8);
|
|
270
|
+
const anchor = side === "start" ? "end" : "start";
|
|
271
|
+
const w = measured + 8;
|
|
272
|
+
const h = item.lines.length * 14 + 4;
|
|
273
|
+
let y0 = Math.max(item.top, previousBottom + 4);
|
|
274
|
+
const left = anchor === "end" ? x - w + 3 : x - 3;
|
|
275
|
+
const intersects = (y) => occupiedLabels.some((box) => left < box.x + box.w && left + w > box.x && y - 12 < box.y + box.h && y - 12 + h > box.y);
|
|
276
|
+
while (intersects(y0)) y0 += 16;
|
|
277
|
+
const box = {
|
|
278
|
+
x: left,
|
|
279
|
+
y: y0 - 12,
|
|
280
|
+
w,
|
|
281
|
+
h
|
|
282
|
+
};
|
|
283
|
+
occupiedLabels.push(box);
|
|
284
|
+
labelFloor = Math.max(labelFloor, box.y + box.h);
|
|
285
|
+
if (denseForce && Math.abs(y0 - item.top) > 8) children.push(ctx.line(item.p.x, item.p.y, anchor === "end" ? x + 3 : x - 3, box.y + h / 2, {
|
|
286
|
+
stroke: ctx.color("tone3"),
|
|
287
|
+
"stroke-width": .8
|
|
288
|
+
}));
|
|
289
|
+
children.push(ctx.rect(box.x, box.y, w, h, {
|
|
290
|
+
fill: ctx.color("bg"),
|
|
291
|
+
"fill-opacity": .92,
|
|
292
|
+
rx: 2
|
|
293
|
+
}));
|
|
294
|
+
item.lines.forEach((line, i) => children.push(ctx.text(x, y0 + i * 14, line, {
|
|
295
|
+
anchor,
|
|
296
|
+
size: 12,
|
|
297
|
+
weight,
|
|
298
|
+
fill: labelFill,
|
|
299
|
+
meta: {
|
|
300
|
+
kind: "label",
|
|
301
|
+
node: String(item.n.id)
|
|
302
|
+
}
|
|
303
|
+
})));
|
|
304
|
+
previousBottom = y0 + (item.lines.length - 1) * 14;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
children.push(...arrows);
|
|
308
|
+
let outputHeight = Math.max(H, labelFloor + 20);
|
|
309
|
+
if (nodes.some((node) => node.value != null)) {
|
|
310
|
+
const legend = ctx.lang.startsWith("en") ? "Node area ∝ value" : "节点面积 ∝ 数值";
|
|
311
|
+
const circles = children.filter((child) => child.tag === "circle");
|
|
312
|
+
const labels = children.filter((child) => child.tag === "text" && child.meta?.kind === "label");
|
|
313
|
+
const nodeBottom = Math.max(0, ...circles.map((child) => child.attrs.cy + child.attrs.r));
|
|
314
|
+
const labelBottom = Math.max(0, ...labels.map((child) => child.attrs.y + 3));
|
|
315
|
+
const legendY = Math.max(nodeBottom, labelBottom) + 16;
|
|
316
|
+
children.push(ctx.text(16, legendY, legend, {
|
|
317
|
+
size: 12,
|
|
318
|
+
fill: ctx.color("muted"),
|
|
319
|
+
meta: { kind: "size-legend" }
|
|
320
|
+
}));
|
|
321
|
+
outputHeight = legendY + 12;
|
|
322
|
+
}
|
|
323
|
+
return withDescription(ctx, outputHeight, children, spec, W);
|
|
324
|
+
}
|
|
325
|
+
function networkStages(data) {
|
|
326
|
+
const indegree = new Map(data.nodes.map((n) => [n.id, 0]));
|
|
327
|
+
const next = new Map(data.nodes.map((n) => [n.id, []]));
|
|
328
|
+
for (const e of data.edges) {
|
|
329
|
+
next.get(e.source)?.push(e.target);
|
|
330
|
+
if (indegree.has(e.target)) indegree.set(e.target, indegree.get(e.target) + 1);
|
|
331
|
+
}
|
|
332
|
+
const queue = data.nodes.filter((n) => indegree.get(n.id) === 0).map((n) => n.id).sort(stable);
|
|
333
|
+
const stage = new Map(data.nodes.map((n) => [n.id, 0]));
|
|
334
|
+
while (queue.length) {
|
|
335
|
+
const from = queue.shift();
|
|
336
|
+
for (const to of next.get(from) || []) {
|
|
337
|
+
stage.set(to, Math.max(stage.get(to), stage.get(from) + 1));
|
|
338
|
+
indegree.set(to, indegree.get(to) - 1);
|
|
339
|
+
if (indegree.get(to) === 0) {
|
|
340
|
+
queue.push(to);
|
|
341
|
+
queue.sort(stable);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return stage;
|
|
346
|
+
}
|
|
347
|
+
function sankeyCrossings(stages, order, edges, stageMap) {
|
|
348
|
+
const rank = /* @__PURE__ */ new Map();
|
|
349
|
+
for (const s of stages) order.get(s).forEach((n, i) => rank.set(n.id, i));
|
|
350
|
+
let total = 0;
|
|
351
|
+
for (let k = 0; k + 1 < stages.length; k++) {
|
|
352
|
+
const list = edges.filter((e) => stageMap.get(e.source) === stages[k] && stageMap.get(e.target) === stages[k + 1]);
|
|
353
|
+
for (let i = 0; i < list.length; i++) for (let j = i + 1; j < list.length; j++) {
|
|
354
|
+
const a = list[i], b = list[j];
|
|
355
|
+
if ((rank.get(a.source) - rank.get(b.source)) * (rank.get(a.target) - rank.get(b.target)) < 0) total += a.value * b.value;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return total;
|
|
359
|
+
}
|
|
360
|
+
function orderSankeyStages(stages, grouped, edges, stageMap) {
|
|
361
|
+
const copy = (order) => new Map([...order].map(([s, list]) => [s, [...list]]));
|
|
362
|
+
const cost = (order) => sankeyCrossings(stages, order, edges, stageMap);
|
|
363
|
+
let best = copy(grouped), bestCost = cost(best);
|
|
364
|
+
if (bestCost === 0) return best;
|
|
365
|
+
const relative = (order) => {
|
|
366
|
+
const at = /* @__PURE__ */ new Map();
|
|
367
|
+
for (const s of stages) order.get(s).forEach((n, i, list) => at.set(n.id, (i + .5) / list.length));
|
|
368
|
+
return at;
|
|
369
|
+
};
|
|
370
|
+
let current = copy(grouped);
|
|
371
|
+
for (let sweep = 0; sweep < 8; sweep++) {
|
|
372
|
+
const forward = sweep % 2 === 0;
|
|
373
|
+
const sequence = forward ? stages.slice(1) : stages.slice(0, -1).reverse();
|
|
374
|
+
for (const s of sequence) {
|
|
375
|
+
const at = relative(current);
|
|
376
|
+
const keyed = current.get(s).map((n, i) => {
|
|
377
|
+
const linked = edges.filter((e) => forward ? e.target === n.id && stageMap.get(e.source) < s : e.source === n.id && stageMap.get(e.target) > s);
|
|
378
|
+
const weight = linked.reduce((sum, e) => sum + e.value, 0);
|
|
379
|
+
return {
|
|
380
|
+
n,
|
|
381
|
+
i,
|
|
382
|
+
mean: weight ? linked.reduce((sum, e) => sum + e.value * at.get(forward ? e.source : e.target), 0) / weight : at.get(n.id)
|
|
383
|
+
};
|
|
384
|
+
});
|
|
385
|
+
current.set(s, keyed.sort((a, b) => a.mean - b.mean || a.i - b.i).map((item) => item.n));
|
|
386
|
+
}
|
|
387
|
+
const c = cost(current);
|
|
388
|
+
if (c < bestCost) {
|
|
389
|
+
best = copy(current);
|
|
390
|
+
bestCost = c;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
for (let pass = 0, improved = true; pass < 12 && improved && bestCost > 0; pass++) {
|
|
394
|
+
improved = false;
|
|
395
|
+
for (const s of stages) {
|
|
396
|
+
const list = best.get(s);
|
|
397
|
+
for (let i = 0; i + 1 < list.length; i++) {
|
|
398
|
+
[list[i], list[i + 1]] = [list[i + 1], list[i]];
|
|
399
|
+
const c = cost(best);
|
|
400
|
+
if (c < bestCost) {
|
|
401
|
+
bestCost = c;
|
|
402
|
+
improved = true;
|
|
403
|
+
} else [list[i], list[i + 1]] = [list[i + 1], list[i]];
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return best;
|
|
408
|
+
}
|
|
409
|
+
var sankeyPresentation = Symbol("sankey presentation");
|
|
410
|
+
function sankey(spec, ctx) {
|
|
411
|
+
const { nodes, edges } = spec.data;
|
|
412
|
+
const budget = spec[sizeBudget] ?? {}, maxNodes = budget.nodes ?? 60, maxEdges = budget.edges ?? 120;
|
|
413
|
+
if (nodes.length > maxNodes || edges.length > maxEdges) ctx.fit(`sankey超过${maxNodes}个节点或${maxEdges}条边`, "缩小数据范围后重试");
|
|
414
|
+
const stageMap = nodes.every((n) => Number.isInteger(n.stage)) ? new Map(nodes.map((n) => [n.id, n.stage])) : networkStages(spec.data);
|
|
415
|
+
const stages = [...new Set(nodes.map((n) => stageMap.get(n.id)))].sort((a, b) => a - b);
|
|
416
|
+
const vertical = spec.layout === "down";
|
|
417
|
+
const nodeSize = 18;
|
|
418
|
+
const labelSize = 12;
|
|
419
|
+
const labelCap = vertical ? 160 : 120;
|
|
420
|
+
const showValues = spec.showValues !== false;
|
|
421
|
+
const sum = (list) => list.reduce((total, e) => total + e.value, 0);
|
|
422
|
+
const wrapped = new Map(nodes.map((n) => {
|
|
423
|
+
const name = ctx.wrap(textOf(n), labelCap, labelSize, 400);
|
|
424
|
+
const total = Math.max(sum(edges.filter((e) => e.target === n.id)), sum(edges.filter((e) => e.source === n.id)));
|
|
425
|
+
const lines = showValues && name.length ? [...name, ctx.format(total, "edge.value")] : name;
|
|
426
|
+
const width = Math.max(12, ...lines.map((line) => ctx.measure(line, labelSize, 400)));
|
|
427
|
+
return [n.id, {
|
|
428
|
+
lines,
|
|
429
|
+
width,
|
|
430
|
+
height: lines.length * 14,
|
|
431
|
+
totalIndex: name.length ? name.length : -1
|
|
432
|
+
}];
|
|
433
|
+
}));
|
|
434
|
+
const maxLabelLines = Math.max(1, ...[...wrapped.values()].map((item) => item.lines.length));
|
|
435
|
+
const margin = vertical ? Math.max(28, (maxLabelLines - 1) * 7 + 6) : 28;
|
|
436
|
+
const widestLabel = Math.max(12, ...[...wrapped.values()].map((item) => item.width));
|
|
437
|
+
const lane = widestLabel + 16;
|
|
438
|
+
const minSlot = vertical ? nodeSize + maxLabelLines * 14 + 16 : nodeSize + lane;
|
|
439
|
+
const horizontalMinimum = stages.length === 1 ? margin * 2 + nodeSize + lane : margin * 2 + stages.length * (nodeSize + lane);
|
|
440
|
+
const verticalMinimum = vertical ? Math.max(0, ...stages.map((stage) => {
|
|
441
|
+
const row = nodes.filter((node) => stageMap.get(node.id) === stage);
|
|
442
|
+
return row.reduce((sum, node) => sum + nodeSize + wrapped.get(node.id).width + 16, 0) + Math.max(0, row.length - 1) * 6 + margin * 2;
|
|
443
|
+
})) : 0;
|
|
444
|
+
const W = vertical ? Math.max(ctx.width, verticalMinimum) : Math.max(ctx.width, horizontalMinimum);
|
|
445
|
+
Math.max(1, ...stages.map((stage) => nodes.filter((node) => stageMap.get(node.id) === stage).length));
|
|
446
|
+
const labelStackHeight = Math.max(0, ...stages.map((stage) => nodes.filter((node) => stageMap.get(node.id) === stage).reduce((sum, node) => sum + wrapped.get(node.id).height + 4, 0)));
|
|
447
|
+
const H = vertical ? Math.max(460, margin * 2 + stages.length * nodeSize + Math.max(0, stages.length - 1) * (maxLabelLines * 14 + 36) + 40) : Math.max(360, margin * 2 + labelStackHeight + 40);
|
|
448
|
+
const axisStart = !vertical && stages.length === 1 ? Math.max(margin, lane + 8) : margin;
|
|
449
|
+
const axisEnd = vertical ? H - margin - nodeSize : W - margin - nodeSize;
|
|
450
|
+
const slot = stages.length > 1 ? Math.max(minSlot, (axisEnd - axisStart) / (stages.length - 1)) : 0;
|
|
451
|
+
const stageAxes = vertical ? stages.map((_, index) => axisStart + index * slot) : stages.length === 1 ? [axisStart] : (() => {
|
|
452
|
+
const gaps = stages.slice(1).map((_, index) => index === stages.length - 2 ? nodeSize + 2 * lane : minSlot);
|
|
453
|
+
const extra = Math.max(0, (axisEnd - axisStart - gaps.reduce((sum, gap) => sum + gap, 0)) / gaps.length);
|
|
454
|
+
const axes = [axisStart];
|
|
455
|
+
for (const gap of gaps) axes.push(axes.at(-1) + gap + extra);
|
|
456
|
+
return axes;
|
|
457
|
+
})();
|
|
458
|
+
const stageIndex = new Map(stages.map((s, i) => [s, i]));
|
|
459
|
+
const stageOf = new Map(stageMap), routes = /* @__PURE__ */ new Map(), segments = [], lanes = [];
|
|
460
|
+
edges.forEach((e, index) => {
|
|
461
|
+
const chain = [e.source];
|
|
462
|
+
for (let k = stageIndex.get(stageMap.get(e.source)) + 1; k < stageIndex.get(stageMap.get(e.target)); k++) {
|
|
463
|
+
const id = `\u0000lane:${index}:${k}`;
|
|
464
|
+
lanes.push({
|
|
465
|
+
id,
|
|
466
|
+
value: e.value
|
|
467
|
+
});
|
|
468
|
+
stageOf.set(id, stages[k]);
|
|
469
|
+
chain.push(id);
|
|
470
|
+
}
|
|
471
|
+
chain.push(e.target);
|
|
472
|
+
routes.set(e, chain.slice(1).map((target, i) => {
|
|
473
|
+
const segment = {
|
|
474
|
+
source: chain[i],
|
|
475
|
+
target,
|
|
476
|
+
value: e.value,
|
|
477
|
+
edge: e
|
|
478
|
+
};
|
|
479
|
+
segments.push(segment);
|
|
480
|
+
return segment;
|
|
481
|
+
}));
|
|
482
|
+
});
|
|
483
|
+
const laneIds = new Set(lanes.map((lane) => lane.id));
|
|
484
|
+
const incoming = new Map([...nodes, ...lanes].map((n) => [n.id, 0]));
|
|
485
|
+
const outgoing = new Map([...nodes, ...lanes].map((n) => [n.id, 0]));
|
|
486
|
+
for (const e of segments) {
|
|
487
|
+
incoming.set(e.target, incoming.get(e.target) + e.value);
|
|
488
|
+
outgoing.set(e.source, outgoing.get(e.source) + e.value);
|
|
489
|
+
}
|
|
490
|
+
const grouped = orderSankeyStages(stages, new Map(stages.map((s) => [s, [...nodes, ...lanes].filter((n) => stageOf.get(n.id) === s)])), segments, stageOf);
|
|
491
|
+
const maxSum = Math.max(1, ...stages.map((s) => grouped.get(s).reduce((sum, n) => sum + Math.max(incoming.get(n.id), outgoing.get(n.id)), 0)));
|
|
492
|
+
const maxCount = Math.max(1, ...stages.map((s) => grouped.get(s).length));
|
|
493
|
+
const maxLabelled = Math.max(1, ...stages.map((s) => grouped.get(s).filter((n) => !laneIds.has(n.id)).length));
|
|
494
|
+
const crossSpan = (vertical ? W : H) - margin * 2;
|
|
495
|
+
const labelReserve = vertical ? widestLabel + 16 : 0;
|
|
496
|
+
const unit = Math.max(0, crossSpan - labelReserve * maxLabelled - 8 * Math.max(0, maxCount - 1)) / maxSum;
|
|
497
|
+
const pos = /* @__PURE__ */ new Map(), ports = /* @__PURE__ */ new Map();
|
|
498
|
+
for (const s of stages) {
|
|
499
|
+
const list = grouped.get(s);
|
|
500
|
+
const sizes = list.map((n) => Math.max(incoming.get(n.id), outgoing.get(n.id))).map((amount) => amount * unit);
|
|
501
|
+
const slots = sizes.map((size, index) => vertical && !laneIds.has(list[index].id) ? size + wrapped.get(list[index].id).width + 16 : size);
|
|
502
|
+
const totalSlots = slots.reduce((sum, size) => sum + size, 0);
|
|
503
|
+
const gap = list.length > 1 ? Math.max(6, Math.min(18, (crossSpan - totalSlots) / (list.length - 1))) : 0;
|
|
504
|
+
let cursor = margin + (crossSpan - (totalSlots + gap * (list.length - 1))) / 2;
|
|
505
|
+
list.forEach((n, i) => {
|
|
506
|
+
const size = sizes[i], slotSize = slots[i];
|
|
507
|
+
pos.set(n.id, vertical ? {
|
|
508
|
+
x: cursor,
|
|
509
|
+
y: stageAxes[stages.indexOf(s)],
|
|
510
|
+
w: size,
|
|
511
|
+
h: nodeSize,
|
|
512
|
+
labelX: cursor + size / 2
|
|
513
|
+
} : {
|
|
514
|
+
x: stageAxes[stages.indexOf(s)],
|
|
515
|
+
y: cursor,
|
|
516
|
+
w: nodeSize,
|
|
517
|
+
h: size,
|
|
518
|
+
labelTop: cursor + 12
|
|
519
|
+
});
|
|
520
|
+
cursor += slotSize + gap;
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
const center = (id) => {
|
|
524
|
+
const p = pos.get(id);
|
|
525
|
+
return vertical ? p.x + p.w / 2 : p.y + p.h / 2;
|
|
526
|
+
};
|
|
527
|
+
const segmentIndex = new Map(segments.map((e, i) => [e, i]));
|
|
528
|
+
const offsets = new Map(segments.map((e) => [e, {
|
|
529
|
+
source: 0,
|
|
530
|
+
target: 0
|
|
531
|
+
}]));
|
|
532
|
+
for (const n of [...nodes, ...lanes]) ports.set(n.id, {
|
|
533
|
+
in: 0,
|
|
534
|
+
out: 0
|
|
535
|
+
});
|
|
536
|
+
for (const n of nodes) {
|
|
537
|
+
const byOther = (side) => (a, b) => center(a[side]) - center(b[side]) || segmentIndex.get(a) - segmentIndex.get(b);
|
|
538
|
+
for (const e of segments.filter((x) => x.source === n.id).sort(byOther("target"))) {
|
|
539
|
+
offsets.get(e).source = ports.get(n.id).out;
|
|
540
|
+
ports.get(n.id).out += e.value * unit;
|
|
541
|
+
}
|
|
542
|
+
for (const e of segments.filter((x) => x.target === n.id).sort(byOther("source"))) {
|
|
543
|
+
offsets.get(e).target = ports.get(n.id).in;
|
|
544
|
+
ports.get(n.id).in += e.value * unit;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
const children = [], readouts = [];
|
|
548
|
+
const cubic = (a, b, c, d, t) => (1 - t) ** 3 * a + 3 * (1 - t) ** 2 * t * b + 3 * (1 - t) * t * t * c + t ** 3 * d;
|
|
549
|
+
const at = (along, cross) => vertical ? pair(cross, along) : pair(along, cross);
|
|
550
|
+
for (const e of edges) {
|
|
551
|
+
const route = routes.get(e), thick = e.value * unit;
|
|
552
|
+
const a = pos.get(e.source), b = pos.get(e.target);
|
|
553
|
+
const stops = [vertical ? {
|
|
554
|
+
a0: a.y + a.h,
|
|
555
|
+
a1: a.y + a.h,
|
|
556
|
+
c: a.x + offsets.get(route[0]).source
|
|
557
|
+
} : {
|
|
558
|
+
a0: a.x + a.w,
|
|
559
|
+
a1: a.x + a.w,
|
|
560
|
+
c: a.y + offsets.get(route[0]).source
|
|
561
|
+
}];
|
|
562
|
+
for (const segment of route.slice(0, -1)) {
|
|
563
|
+
const lane = pos.get(segment.target);
|
|
564
|
+
stops.push(vertical ? {
|
|
565
|
+
a0: lane.y,
|
|
566
|
+
a1: lane.y + lane.h,
|
|
567
|
+
c: lane.x
|
|
568
|
+
} : {
|
|
569
|
+
a0: lane.x,
|
|
570
|
+
a1: lane.x + lane.w,
|
|
571
|
+
c: lane.y
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
stops.push(vertical ? {
|
|
575
|
+
a0: b.y,
|
|
576
|
+
a1: b.y,
|
|
577
|
+
c: b.x + offsets.get(route.at(-1)).target
|
|
578
|
+
} : {
|
|
579
|
+
a0: b.x,
|
|
580
|
+
a1: b.x,
|
|
581
|
+
c: b.y + offsets.get(route.at(-1)).target
|
|
582
|
+
});
|
|
583
|
+
let d = `M${at(stops[0].a1, stops[0].c)}`;
|
|
584
|
+
for (let i = 1; i < stops.length; i++) {
|
|
585
|
+
const prev = stops[i - 1], cur = stops[i], mid = (prev.a1 + cur.a0) / 2;
|
|
586
|
+
d += ` C${at(mid, prev.c)} ${at(mid, cur.c)} ${at(cur.a0, cur.c)}`;
|
|
587
|
+
if (cur.a1 > cur.a0) d += ` L${at(cur.a1, cur.c)}`;
|
|
588
|
+
}
|
|
589
|
+
d += ` L${at(stops.at(-1).a1, stops.at(-1).c + thick)}`;
|
|
590
|
+
for (let i = stops.length - 1; i > 0; i--) {
|
|
591
|
+
const prev = stops[i - 1], cur = stops[i], mid = (prev.a1 + cur.a0) / 2;
|
|
592
|
+
if (cur.a1 > cur.a0) d += ` L${at(cur.a0, cur.c + thick)}`;
|
|
593
|
+
d += ` C${at(mid, cur.c + thick)} ${at(mid, prev.c + thick)} ${at(prev.a1, prev.c + thick)}`;
|
|
594
|
+
}
|
|
595
|
+
d += " Z";
|
|
596
|
+
const labelAt = (t) => {
|
|
597
|
+
const i = t <= .5 ? 1 : stops.length - 1, prev = stops[i - 1], cur = stops[i], mid = (prev.a1 + cur.a0) / 2;
|
|
598
|
+
const along = cubic(prev.a1, mid, mid, cur.a0, t), cross = cubic(prev.c, prev.c, cur.c, cur.c, t) + thick / 2;
|
|
599
|
+
return vertical ? [cross, along + 4] : [along, cross + 4];
|
|
600
|
+
};
|
|
601
|
+
const ribbonColor = ctx.design[e[sankeyPresentation]] || ctx.color("tone2");
|
|
602
|
+
children.push(ctx.path(d, {
|
|
603
|
+
fill: ribbonColor,
|
|
604
|
+
"fill-opacity": ".32",
|
|
605
|
+
stroke: ribbonColor,
|
|
606
|
+
"stroke-opacity": ".18",
|
|
607
|
+
"stroke-width": ".6",
|
|
608
|
+
meta: {
|
|
609
|
+
kind: "edge",
|
|
610
|
+
edge: String(e.id ?? `${e.source}-${e.target}`),
|
|
611
|
+
source: String(e.source),
|
|
612
|
+
target: String(e.target),
|
|
613
|
+
value: e.value,
|
|
614
|
+
...spec[sankeyPresentation] ? {
|
|
615
|
+
from: String(e.source),
|
|
616
|
+
to: String(e.target)
|
|
617
|
+
} : {}
|
|
618
|
+
}
|
|
619
|
+
}));
|
|
620
|
+
if (showValues && thick >= 16) readouts.push({
|
|
621
|
+
edge: e,
|
|
622
|
+
text: ctx.format(e.value, "edge.value"),
|
|
623
|
+
labelAt
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
if (!vertical) for (const stage of stages) {
|
|
627
|
+
const labels = nodes.filter((node) => stageMap.get(node.id) === stage).map((node) => ({
|
|
628
|
+
id: node.id,
|
|
629
|
+
node,
|
|
630
|
+
place: pos.get(node.id),
|
|
631
|
+
box: wrapped.get(node.id),
|
|
632
|
+
target: pos.get(node.id).labelTop
|
|
633
|
+
}));
|
|
634
|
+
for (const item of stackLabels(labels, (item) => item.box.height, 4, margin, H - margin)) item.place.labelTop = item.top;
|
|
635
|
+
}
|
|
636
|
+
const labelBands = nodes.map((node) => {
|
|
637
|
+
const place = pos.get(node.id), box = wrapped.get(node.id);
|
|
638
|
+
if (vertical) {
|
|
639
|
+
const firstY = place.y + place.h / 2 - (box.lines.length - 1) * 7;
|
|
640
|
+
return {
|
|
641
|
+
left: place.x + place.w,
|
|
642
|
+
top: firstY - 14,
|
|
643
|
+
w: box.width + 16,
|
|
644
|
+
h: box.height + 8
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
return {
|
|
648
|
+
left: stageMap.get(node.id) === stages.at(-1) || stages.length === 1 ? place.x - 16 - box.width : place.x + place.w,
|
|
649
|
+
top: place.labelTop - 14,
|
|
650
|
+
w: box.width + 16,
|
|
651
|
+
h: box.height + 8
|
|
652
|
+
};
|
|
653
|
+
});
|
|
654
|
+
const readoutFill = ctx.textOn("tone2", {
|
|
655
|
+
opacity: .32,
|
|
656
|
+
over: "bg"
|
|
657
|
+
});
|
|
658
|
+
const readoutBoxes = [];
|
|
659
|
+
const nodeBoxes = nodes.map((node) => {
|
|
660
|
+
const place = pos.get(node.id);
|
|
661
|
+
return {
|
|
662
|
+
left: place.x - 4,
|
|
663
|
+
top: place.y - 4,
|
|
664
|
+
w: place.w + 8,
|
|
665
|
+
h: place.h + 8
|
|
666
|
+
};
|
|
667
|
+
});
|
|
668
|
+
for (const { edge, text, labelAt } of readouts) {
|
|
669
|
+
const w = ctx.measure(text, 12) + 8, h = 16;
|
|
670
|
+
const hits = (candidate) => [
|
|
671
|
+
...readoutBoxes,
|
|
672
|
+
...labelBands,
|
|
673
|
+
...nodeBoxes
|
|
674
|
+
].some((box) => candidate.left < box.left + box.w && candidate.left + w > box.left && candidate.top < box.top + box.h && candidate.top + h > box.top);
|
|
675
|
+
let best = null;
|
|
676
|
+
for (const t of [
|
|
677
|
+
.24,
|
|
678
|
+
.3,
|
|
679
|
+
.18,
|
|
680
|
+
.36,
|
|
681
|
+
.76,
|
|
682
|
+
.7,
|
|
683
|
+
.82,
|
|
684
|
+
.64,
|
|
685
|
+
.5,
|
|
686
|
+
.44,
|
|
687
|
+
.56
|
|
688
|
+
]) {
|
|
689
|
+
const [x, y] = labelAt(t);
|
|
690
|
+
const candidate = {
|
|
691
|
+
x,
|
|
692
|
+
y,
|
|
693
|
+
left: x - w / 2,
|
|
694
|
+
top: y - 12,
|
|
695
|
+
w,
|
|
696
|
+
h
|
|
697
|
+
};
|
|
698
|
+
if (!hits(candidate)) {
|
|
699
|
+
best = candidate;
|
|
700
|
+
break;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
if (!best) continue;
|
|
704
|
+
readoutBoxes.push(best);
|
|
705
|
+
children.push(ctx.text(best.x, best.y, text, {
|
|
706
|
+
anchor: "middle",
|
|
707
|
+
size: 12,
|
|
708
|
+
fill: readoutFill,
|
|
709
|
+
meta: {
|
|
710
|
+
kind: "edge-label",
|
|
711
|
+
edge: String(edge.id ?? `${edge.source}-${edge.target}`),
|
|
712
|
+
value: edge.value
|
|
713
|
+
}
|
|
714
|
+
}));
|
|
715
|
+
}
|
|
716
|
+
for (const n of nodes) {
|
|
717
|
+
const p = pos.get(n.id), box = wrapped.get(n.id), related = edges.filter((e) => e.source === n.id || e.target === n.id).map((e) => String(e.id ?? `${e.source}-${e.target}`));
|
|
718
|
+
const appearance = n[sankeyPresentation], start = children.length;
|
|
719
|
+
children.push(ctx.rect(p.x, p.y, p.w, p.h, {
|
|
720
|
+
rx: 3,
|
|
721
|
+
fill: ctx.design[appearance?.status] || ctx.color("tone1"),
|
|
722
|
+
"fill-opacity": ".85",
|
|
723
|
+
meta: {
|
|
724
|
+
kind: "node",
|
|
725
|
+
node: String(n.id),
|
|
726
|
+
stage: stageMap.get(n.id),
|
|
727
|
+
related,
|
|
728
|
+
...spec[sankeyPresentation] ? { id: String(n.id) } : {}
|
|
729
|
+
}
|
|
730
|
+
}));
|
|
731
|
+
if (vertical) {
|
|
732
|
+
const lines = box.lines;
|
|
733
|
+
const x = p.x + p.w + 8;
|
|
734
|
+
const firstY = p.y + p.h / 2 - (lines.length - 1) * 7;
|
|
735
|
+
lines.forEach((line, i) => children.push(ctx.text(x, firstY + i * 14, line, {
|
|
736
|
+
anchor: "start",
|
|
737
|
+
size: labelSize,
|
|
738
|
+
fill: ctx.color(i === box.totalIndex ? "muted" : "fg"),
|
|
739
|
+
meta: {
|
|
740
|
+
kind: i === box.totalIndex ? "node-total" : "label",
|
|
741
|
+
node: String(n.id)
|
|
742
|
+
}
|
|
743
|
+
})));
|
|
744
|
+
} else {
|
|
745
|
+
const lastStage = stageMap.get(n.id) === stages.at(-1);
|
|
746
|
+
const lines = box.lines;
|
|
747
|
+
const inward = lastStage || stages.length === 1;
|
|
748
|
+
const x = p.x + (inward ? -8 : p.w + 8);
|
|
749
|
+
const anchor = inward ? "end" : "start";
|
|
750
|
+
const firstY = p.labelTop;
|
|
751
|
+
lines.forEach((line, i) => children.push(ctx.text(x, firstY + i * 14, line, {
|
|
752
|
+
anchor,
|
|
753
|
+
size: labelSize,
|
|
754
|
+
fill: ctx.color(i === box.totalIndex ? "muted" : "fg"),
|
|
755
|
+
stroke: ctx.color("bg"),
|
|
756
|
+
"stroke-width": 4,
|
|
757
|
+
"stroke-linejoin": "round",
|
|
758
|
+
"paint-order": "stroke",
|
|
759
|
+
meta: {
|
|
760
|
+
kind: i === box.totalIndex ? "node-total" : "label",
|
|
761
|
+
node: String(n.id)
|
|
762
|
+
}
|
|
763
|
+
})));
|
|
764
|
+
}
|
|
765
|
+
const link = appearance?.link;
|
|
766
|
+
if (link) children.push(ctx.link(link.href, children.splice(start), link));
|
|
767
|
+
}
|
|
768
|
+
return withDescription(ctx, H, children, spec, W);
|
|
769
|
+
}
|
|
770
|
+
function threads(spec, ctx) {
|
|
771
|
+
const { stages, nodes, paths } = spec.data;
|
|
772
|
+
if (paths.length > 240 || nodes.length > 80 || stages.length > 8) ctx.fit("threads超过240条路径、80个节点或8个阶段", "缩小数据范围后重试");
|
|
773
|
+
const vertical = spec.layout === "down";
|
|
774
|
+
const stageLabelWidth = Math.max(0, ...stages.map((stage) => ctx.measure(String(stage.label ?? ""), 12, 400)));
|
|
775
|
+
const margin = 64, crossMargin = vertical ? Math.max(margin, stageLabelWidth + 32) : margin;
|
|
776
|
+
const stageIndex = new Map(stages.map((s, i) => [s.id, i]));
|
|
777
|
+
const nodeMap = new Map(nodes.map((n) => [n.id, n]));
|
|
778
|
+
const sortedPaths = [...paths].sort((a, b) => stable(a.id, b.id));
|
|
779
|
+
const memberships = new Map(nodes.map((n) => [n.id, []]));
|
|
780
|
+
for (const p of sortedPaths) for (const id of p.nodes) memberships.get(id)?.push(p);
|
|
781
|
+
const maxStageLoad = Math.max(1, ...stages.map((stage) => paths.filter((p) => p.nodes.some((id) => nodeMap.get(id)?.stage === stage.id)).reduce((sum, p) => sum + (p.value ?? 1), 0)));
|
|
782
|
+
const maxNodesPerStage = Math.max(1, ...stages.map((stage) => nodes.filter((node) => node.stage === stage.id).length));
|
|
783
|
+
const laneWidth = vertical ? Math.max(96, Math.min(180, (ctx.width - 96) / Math.max(1, maxNodesPerStage))) : Math.max(96, Math.min(180, (ctx.width - 96) / Math.max(1, stages.length)));
|
|
784
|
+
const labelLines = new Map(nodes.map((node) => [node.id, ctx.wrap(textOf(node), laneWidth, 12, 400)]));
|
|
785
|
+
const measuredWidth = (lines) => Math.max(12, ...lines.map((line) => ctx.measure(line, 12, 400)));
|
|
786
|
+
const maxLabelLines = Math.max(1, ...[...labelLines.values()].map((lines) => lines.length));
|
|
787
|
+
const maxLabelWidth = Math.max(12, ...[...labelLines.values()].map(measuredWidth));
|
|
788
|
+
const naturalLabelWidth = Math.max(12, ...nodes.map((node) => ctx.measure(textOf(node), 12, 400)));
|
|
789
|
+
const axisLabelWidth = Math.max(maxLabelWidth, stageLabelWidth);
|
|
790
|
+
const labelReserve = vertical ? Math.max(maxLabelWidth, naturalLabelWidth) * 2 + 24 : maxLabelLines * 16 + 8;
|
|
791
|
+
const columnLane = vertical ? 0 : axisLabelWidth * 2 + 24;
|
|
792
|
+
const flowBudget = Math.min(240, maxStageLoad * 2.5);
|
|
793
|
+
const crossMinimum = 2 * crossMargin + maxNodesPerStage * labelReserve + flowBudget;
|
|
794
|
+
const axisMinimum = 128 + axisLabelWidth + Math.max(0, stages.length - 1) * columnLane;
|
|
795
|
+
const W = vertical ? Math.max(ctx.width, crossMinimum) : Math.max(ctx.width, axisMinimum);
|
|
796
|
+
const H = vertical ? Math.max(420, stages.length * (28 + maxLabelLines * 28 + 44) + 80) : Math.max(300, crossMinimum + 32);
|
|
797
|
+
const axisSpan = Math.max(columnLane, (vertical ? H : W) - 128 - (vertical ? 0 : axisLabelWidth));
|
|
798
|
+
const crossSpan = (vertical ? W : H) - 2 * crossMargin;
|
|
799
|
+
const axisDistance = stages.length > 1 ? axisSpan / (stages.length - 1) : axisSpan;
|
|
800
|
+
const widestPath = Math.max(1, ...paths.map((path) => path.value ?? 1));
|
|
801
|
+
const unit = Math.min(2.5, Math.max(0, crossSpan - maxNodesPerStage * labelReserve - 12 * (maxNodesPerStage - 1)) / maxStageLoad, axisDistance * .05 / widestPath);
|
|
802
|
+
const positions = /* @__PURE__ */ new Map();
|
|
803
|
+
for (const stage of stages) {
|
|
804
|
+
const list = nodes.filter((node) => node.stage === stage.id).sort((a, b) => stable(a.id, b.id));
|
|
805
|
+
const flowSizes = list.map((node) => memberships.get(node.id).reduce((sum, path) => sum + (path.value ?? 1) * unit, 0));
|
|
806
|
+
const sizes = list.map((node, index) => (vertical ? Math.max(measuredWidth(labelLines.get(node.id)), ctx.measure(textOf(node), 12, 400)) + 16 : labelLines.get(node.id).length * 16 + 8) + flowSizes[index]);
|
|
807
|
+
const gap = 8;
|
|
808
|
+
let cursor = crossMargin;
|
|
809
|
+
list.forEach((n, index) => {
|
|
810
|
+
const pathsAtNode = memberships.get(n.id).sort((a, b) => stable(a.id, b.id)), total = sizes[index];
|
|
811
|
+
const labelBand = vertical ? Math.max(measuredWidth(labelLines.get(n.id)), ctx.measure(textOf(n), 12, 400)) + 16 : labelLines.get(n.id).length * 16 + 8;
|
|
812
|
+
const labelStart = cursor, flowStart = cursor + labelBand, flowTotal = flowSizes[index], cross = flowStart + flowTotal / 2;
|
|
813
|
+
const axis = margin + (vertical ? 0 : axisLabelWidth / 2) + stageIndex.get(stage.id) * axisSpan / Math.max(1, stages.length - 1);
|
|
814
|
+
positions.set(n.id, {
|
|
815
|
+
axis,
|
|
816
|
+
cross,
|
|
817
|
+
paths: pathsAtNode,
|
|
818
|
+
start: flowStart,
|
|
819
|
+
labelStart,
|
|
820
|
+
stride: Math.max(1, flowTotal / Math.max(1, pathsAtNode.length))
|
|
821
|
+
});
|
|
822
|
+
cursor += total + gap;
|
|
823
|
+
});
|
|
824
|
+
}
|
|
825
|
+
const related = new Map(nodes.map((n) => [n.id, paths.filter((p) => p.nodes.includes(n.id)).map((p) => String(p.id))]));
|
|
826
|
+
const pathRelated = new Map(paths.map((p) => [p.id, paths.filter((other) => other.id !== p.id && other.nodes.some((id) => p.nodes.includes(id))).map((other) => String(other.id)).sort(stable)]));
|
|
827
|
+
const children = [];
|
|
828
|
+
for (const p of sortedPaths) {
|
|
829
|
+
const points = p.nodes.map((id) => {
|
|
830
|
+
const node = positions.get(id);
|
|
831
|
+
const at = node.paths.findIndex((candidate) => candidate.id === p.id);
|
|
832
|
+
const precedingWidth = node.paths.slice(0, at).reduce((sum, candidate) => sum + (candidate.value ?? 1) * unit, 0);
|
|
833
|
+
const cross = node.start + precedingWidth + (p.value ?? 1) * unit / 2;
|
|
834
|
+
return vertical ? [cross, node.axis] : [node.axis, cross];
|
|
835
|
+
});
|
|
836
|
+
let d = `M${pair(...points[0])}`;
|
|
837
|
+
for (let i = 1; i < points.length; i++) {
|
|
838
|
+
const [x1, y1] = points[i - 1], [x2, y2] = points[i], m = vertical ? (y1 + y2) / 2 : (x1 + x2) / 2;
|
|
839
|
+
d += vertical ? ` C${pair(x1, m)} ${pair(x2, m)} ${pair(x2, y2)}` : ` C${pair(m, y1)} ${pair(m, y2)} ${pair(x2, y2)}`;
|
|
840
|
+
}
|
|
841
|
+
children.push(ctx.path(d, {
|
|
842
|
+
fill: "none",
|
|
843
|
+
stroke: ctx.color("tone2"),
|
|
844
|
+
"stroke-opacity": ".58",
|
|
845
|
+
"stroke-width": Math.max(.8, (p.value ?? 1) * unit),
|
|
846
|
+
"stroke-linecap": "round",
|
|
847
|
+
"stroke-linejoin": "round",
|
|
848
|
+
meta: {
|
|
849
|
+
kind: "path",
|
|
850
|
+
path: String(p.id),
|
|
851
|
+
value: p.value ?? 1,
|
|
852
|
+
related: pathRelated.get(p.id)
|
|
853
|
+
}
|
|
854
|
+
}));
|
|
855
|
+
}
|
|
856
|
+
const ordered = [...nodes].sort((a, b) => stable(nodeMap.get(a.id).stage, nodeMap.get(b.id).stage) || positions.get(a.id).labelStart - positions.get(b.id).labelStart);
|
|
857
|
+
if (!vertical) for (const stage of stages) {
|
|
858
|
+
const items = nodes.filter((node) => node.stage === stage.id).map((node) => ({
|
|
859
|
+
id: node.id,
|
|
860
|
+
place: positions.get(node.id),
|
|
861
|
+
lines: labelLines.get(node.id),
|
|
862
|
+
target: positions.get(node.id).labelStart + 12
|
|
863
|
+
}));
|
|
864
|
+
for (const item of stackLabels(items, (item) => item.lines.length * 14, 4, margin, H - margin)) item.place.labelStart = item.top - 12;
|
|
865
|
+
}
|
|
866
|
+
for (const n of ordered) {
|
|
867
|
+
const p = positions.get(n.id);
|
|
868
|
+
const x = vertical ? p.cross : p.axis, y = vertical ? p.axis : p.cross;
|
|
869
|
+
children.push(ctx.circle(x, y, 3.5, {
|
|
870
|
+
fill: ctx.color("tone1"),
|
|
871
|
+
meta: {
|
|
872
|
+
kind: "node",
|
|
873
|
+
node: String(n.id),
|
|
874
|
+
related: related.get(n.id)
|
|
875
|
+
}
|
|
876
|
+
}));
|
|
877
|
+
const lines = labelLines.get(n.id);
|
|
878
|
+
const textWidth = measuredWidth(lines);
|
|
879
|
+
if (vertical) {
|
|
880
|
+
const firstY = y - (lines.length - 1) * 7;
|
|
881
|
+
children.push(ctx.rect(p.labelStart + 4, firstY - 12, textWidth + 8, lines.length * 14 + 4, {
|
|
882
|
+
fill: ctx.color("bg"),
|
|
883
|
+
stroke: "none",
|
|
884
|
+
meta: {
|
|
885
|
+
kind: "label-plate",
|
|
886
|
+
node: String(n.id)
|
|
887
|
+
}
|
|
888
|
+
}));
|
|
889
|
+
lines.forEach((line, i) => children.push(ctx.text(p.labelStart + 8, y - (lines.length - 1) * 7 + i * 14, line, {
|
|
890
|
+
anchor: "start",
|
|
891
|
+
size: 12,
|
|
892
|
+
fill: ctx.color("fg"),
|
|
893
|
+
stroke: ctx.color("bg"),
|
|
894
|
+
"stroke-width": 4,
|
|
895
|
+
"stroke-linejoin": "round",
|
|
896
|
+
"paint-order": "stroke",
|
|
897
|
+
meta: {
|
|
898
|
+
kind: "label",
|
|
899
|
+
node: String(n.id),
|
|
900
|
+
related: related.get(n.id)
|
|
901
|
+
}
|
|
902
|
+
})));
|
|
903
|
+
} else {
|
|
904
|
+
children.push(ctx.rect(x - textWidth / 2 - 4, p.labelStart, textWidth + 8, lines.length * 14 + 4, {
|
|
905
|
+
fill: ctx.color("bg"),
|
|
906
|
+
stroke: "none",
|
|
907
|
+
meta: {
|
|
908
|
+
kind: "label-plate",
|
|
909
|
+
node: String(n.id)
|
|
910
|
+
}
|
|
911
|
+
}));
|
|
912
|
+
lines.forEach((line, i) => children.push(ctx.text(x, p.labelStart + 12 + i * 14, line, {
|
|
913
|
+
anchor: "middle",
|
|
914
|
+
size: 12,
|
|
915
|
+
fill: ctx.color("fg"),
|
|
916
|
+
stroke: ctx.color("bg"),
|
|
917
|
+
"stroke-width": 4,
|
|
918
|
+
"stroke-linejoin": "round",
|
|
919
|
+
"paint-order": "stroke",
|
|
920
|
+
meta: {
|
|
921
|
+
kind: "label",
|
|
922
|
+
node: String(n.id),
|
|
923
|
+
related: related.get(n.id),
|
|
924
|
+
width: textWidth
|
|
925
|
+
}
|
|
926
|
+
})));
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
const graphEnd = Math.max(margin, ...[...positions.values()].map((p) => p.start + p.paths.reduce((sum, path) => sum + (path.value ?? 1) * unit, 0)));
|
|
930
|
+
const labelBottom = vertical ? Math.max(margin, ...[...positions.values()].map((place) => place.axis + 12)) : Math.max(margin, ...nodes.map((node) => positions.get(node.id).labelStart + labelLines.get(node.id).length * 14));
|
|
931
|
+
const stageLabelY = Math.max(graphEnd, labelBottom) + 24;
|
|
932
|
+
stages.forEach((s, i) => {
|
|
933
|
+
const axis = margin + (vertical ? 0 : axisLabelWidth / 2) + i * axisSpan / Math.max(1, stages.length - 1);
|
|
934
|
+
children.push(ctx.text(vertical ? 18 : axis, vertical ? axis - 18 : stageLabelY, s.label, {
|
|
935
|
+
anchor: vertical ? "start" : "middle",
|
|
936
|
+
size: 12,
|
|
937
|
+
fill: ctx.color("muted")
|
|
938
|
+
}));
|
|
939
|
+
});
|
|
940
|
+
const placed = [...positions.values()];
|
|
941
|
+
return withDescription(ctx, Math.max(vertical ? H : 0, vertical ? 0 : stageLabelY + 16, labelBottom, ...placed.map((place) => (vertical ? place.axis : place.cross) + 24)), children, spec, W);
|
|
942
|
+
}
|
|
943
|
+
function install({ register }) {
|
|
944
|
+
register("network", network);
|
|
945
|
+
register("sankey", sankey);
|
|
946
|
+
register("threads", threads);
|
|
947
|
+
}
|
|
948
|
+
install({ register });
|
|
949
|
+
export { install, relations_exports, sankeyPresentation };
|