@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,496 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { labelIcon } from "./tree-family-icon.js";
|
|
3
|
+
import { resolveIconRef } from "../core/icons.js";
|
|
4
|
+
import { normalizeLabel } from "./label.js";
|
|
5
|
+
import { presentation } from "./presentation.js";
|
|
6
|
+
import { FONT, appearance, error, fallbackMeasure, warning, wrapText } from "./support.js";
|
|
7
|
+
// src/diagram/ishikawa.js
|
|
8
|
+
var ishikawa_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
|
|
9
|
+
var LEAN = Math.cos(82 * Math.PI / 180) / Math.sin(82 * Math.PI / 180);
|
|
10
|
+
var BODY = FONT.label.size;
|
|
11
|
+
var LINE = 15;
|
|
12
|
+
var HEAD_LINE = 18;
|
|
13
|
+
var HEAD_PAD_X = 16;
|
|
14
|
+
var HEAD_MIN_W = 96;
|
|
15
|
+
var HEAD_MIN_H = 44;
|
|
16
|
+
var CHIP_PAD_X = 10;
|
|
17
|
+
var CHIP_MIN_W = 72;
|
|
18
|
+
var CHIP_MIN_H = 30;
|
|
19
|
+
var LABEL_GAP = 12;
|
|
20
|
+
var LEAF_RUN = 34;
|
|
21
|
+
var BRANCH_RUN = 44;
|
|
22
|
+
var STEP_RUN = 18;
|
|
23
|
+
var SPINE_CLEAR = 26;
|
|
24
|
+
var SIB_GAP = 18;
|
|
25
|
+
var CHILD_GAP = 18;
|
|
26
|
+
var CHIP_GAP = 14;
|
|
27
|
+
var MIN_BONE = 122;
|
|
28
|
+
var CAT_PAD = 30;
|
|
29
|
+
var HEAD_GAP = 24;
|
|
30
|
+
var MARGIN = 24;
|
|
31
|
+
var NODE_CAP = 2e3;
|
|
32
|
+
var NODE_HINT = 60;
|
|
33
|
+
var CAPS = [
|
|
34
|
+
220,
|
|
35
|
+
190,
|
|
36
|
+
160,
|
|
37
|
+
140,
|
|
38
|
+
120
|
|
39
|
+
];
|
|
40
|
+
function parse(source, info = {}) {
|
|
41
|
+
const lang = info.lang || "zh";
|
|
42
|
+
const firstLine = Number.isInteger(info.firstLine) ? info.firstLine : 2;
|
|
43
|
+
const headerLine = Number.isInteger(info.headerLine) ? info.headerLine : Math.max(1, firstLine - 1);
|
|
44
|
+
const errors = [];
|
|
45
|
+
const rows = [];
|
|
46
|
+
const look = presentation({ lang }), assets = /* @__PURE__ */ new Set();
|
|
47
|
+
const asset = (issue) => {
|
|
48
|
+
if (!assets.has(issue.message)) {
|
|
49
|
+
assets.add(issue.message);
|
|
50
|
+
errors.push(issue);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const normalize = (raw, line) => {
|
|
54
|
+
const refs = [];
|
|
55
|
+
raw = raw.replace(/(?<![\w:/.-])(?:logos|mdi|tabler|simple-icons):[a-z\d-]+/gi, (ref) => {
|
|
56
|
+
refs.push(ref);
|
|
57
|
+
return "";
|
|
58
|
+
});
|
|
59
|
+
const value = normalizeLabel(raw, {
|
|
60
|
+
lang,
|
|
61
|
+
line
|
|
62
|
+
});
|
|
63
|
+
value.diagnostics.forEach(asset);
|
|
64
|
+
let icon = value.icon;
|
|
65
|
+
for (const ref of refs) {
|
|
66
|
+
const resolved = resolveIconRef(ref);
|
|
67
|
+
icon ??= resolved;
|
|
68
|
+
if (!resolved) asset(warning("IGNORED_ASSET", lang, line, `未知图标 ${ref}`, `Unknown icon ${ref}`, "使用内置图标", "Use a supported icon"));
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
label: value.text,
|
|
72
|
+
icon
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
if (typeof info.params === "string" && info.params.trim() && !look.accept(info.params.trim(), headerLine)) rows.push({
|
|
76
|
+
indent: 0,
|
|
77
|
+
text: info.params.trim(),
|
|
78
|
+
line: headerLine
|
|
79
|
+
});
|
|
80
|
+
for (const [index, raw] of String(source ?? "").split("\n").entries()) {
|
|
81
|
+
if (!raw.trim() || /^\s*%%/.test(raw)) continue;
|
|
82
|
+
const line = firstLine + index;
|
|
83
|
+
if (look.accept(raw.trim(), line)) continue;
|
|
84
|
+
const indent = /^[^\S\n]*/.exec(raw)[0].length;
|
|
85
|
+
rows.push({
|
|
86
|
+
indent,
|
|
87
|
+
text: raw.slice(indent).trim(),
|
|
88
|
+
line
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
const problem = rows[0];
|
|
92
|
+
if (!problem) {
|
|
93
|
+
errors.push(error(lang, headerLine, "鱼骨图缺少问题(第一行)", "Ishikawa diagram has no problem line", "先写问题,再逐层缩进写原因", "Write the problem first, then indent its causes"));
|
|
94
|
+
return {
|
|
95
|
+
model: {
|
|
96
|
+
kind: "ishikawa",
|
|
97
|
+
title: "",
|
|
98
|
+
root: {
|
|
99
|
+
id: "root",
|
|
100
|
+
label: "",
|
|
101
|
+
line: headerLine,
|
|
102
|
+
children: []
|
|
103
|
+
},
|
|
104
|
+
count: 0
|
|
105
|
+
},
|
|
106
|
+
errors
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const causes = rows.slice(1);
|
|
110
|
+
const root = {
|
|
111
|
+
id: "root",
|
|
112
|
+
...normalize(problem.text, problem.line),
|
|
113
|
+
line: problem.line,
|
|
114
|
+
children: []
|
|
115
|
+
};
|
|
116
|
+
const base = causes.length ? causes[0].indent : 0;
|
|
117
|
+
const stack = [{
|
|
118
|
+
level: 0,
|
|
119
|
+
node: root
|
|
120
|
+
}];
|
|
121
|
+
let count = 1;
|
|
122
|
+
for (const row of causes) {
|
|
123
|
+
if (count >= NODE_CAP) {
|
|
124
|
+
errors.push(error(lang, row.line, `鱼骨图最多 ${NODE_CAP} 个原因`, `An Ishikawa diagram can contain at most ${NODE_CAP} causes`, "拆分鱼骨图,不要删除原因", "Split the diagram without dropping causes"));
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
const level = Math.max(1, row.indent - base + 1);
|
|
128
|
+
while (stack.length > 1 && stack[stack.length - 1].level >= level) stack.pop();
|
|
129
|
+
const node = {
|
|
130
|
+
id: `n${count}`,
|
|
131
|
+
...normalize(row.text, row.line),
|
|
132
|
+
line: row.line,
|
|
133
|
+
children: []
|
|
134
|
+
};
|
|
135
|
+
stack[stack.length - 1].node.children.push(node);
|
|
136
|
+
stack.push({
|
|
137
|
+
level,
|
|
138
|
+
node
|
|
139
|
+
});
|
|
140
|
+
count += 1;
|
|
141
|
+
}
|
|
142
|
+
const visit = (node) => {
|
|
143
|
+
node.status = look.statusOf(node.id);
|
|
144
|
+
node.link = look.linkOf(node.id);
|
|
145
|
+
node.children.forEach(visit);
|
|
146
|
+
};
|
|
147
|
+
visit(root);
|
|
148
|
+
errors.push(...look.diagnostics());
|
|
149
|
+
return {
|
|
150
|
+
model: {
|
|
151
|
+
kind: "ishikawa",
|
|
152
|
+
title: root.label,
|
|
153
|
+
root,
|
|
154
|
+
count
|
|
155
|
+
},
|
|
156
|
+
errors
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function assignBands(list, start) {
|
|
160
|
+
let cursor = start;
|
|
161
|
+
for (const [index, node] of list.entries()) {
|
|
162
|
+
node.s = cursor + node.h / 2;
|
|
163
|
+
cursor += node.h;
|
|
164
|
+
if (node.children.length) cursor = assignBands(node.children, cursor + CHILD_GAP);
|
|
165
|
+
if (index < list.length - 1) cursor += SIB_GAP;
|
|
166
|
+
}
|
|
167
|
+
return cursor;
|
|
168
|
+
}
|
|
169
|
+
function build(model, { cap, lang, measure, width }) {
|
|
170
|
+
const errors = [];
|
|
171
|
+
const ink = (value, font) => Math.max(0, measure(value, font));
|
|
172
|
+
const wrap = (value, font) => wrapText(value, cap, font, (piece, spec) => measure(piece, spec));
|
|
173
|
+
const headFont = {
|
|
174
|
+
size: FONT.title.size,
|
|
175
|
+
weight: FONT.title.weight
|
|
176
|
+
};
|
|
177
|
+
const bodyFont = {
|
|
178
|
+
size: BODY,
|
|
179
|
+
weight: 400
|
|
180
|
+
};
|
|
181
|
+
const chipFont = {
|
|
182
|
+
size: BODY,
|
|
183
|
+
weight: 600
|
|
184
|
+
};
|
|
185
|
+
const headLines = wrap(model.root.label, headFont);
|
|
186
|
+
const iconSpace = (icon) => icon ? 20 : 0;
|
|
187
|
+
const headW = Math.max(HEAD_MIN_W, Math.ceil(Math.max(0, ...headLines.map((line) => ink(line, headFont))) + 32 + iconSpace(model.root.icon)));
|
|
188
|
+
const headH = Math.max(HEAD_MIN_H, headLines.length * HEAD_LINE + 28);
|
|
189
|
+
if (headLines.length > 4) errors.push(warning("DIAGRAM_HINT", lang, model.root.line, `问题换成 ${headLines.length} 行,鱼头会很高`, `The problem label wraps to ${headLines.length} lines and makes a tall head`, "缩短问题描述", "Shorten the problem statement"));
|
|
190
|
+
const nodeStatus = { root: model.root.status };
|
|
191
|
+
const subtree = (from, depth) => {
|
|
192
|
+
const lines = wrap(from.label, bodyFont);
|
|
193
|
+
nodeStatus[from.id] = from.status;
|
|
194
|
+
const node = {
|
|
195
|
+
id: from.id,
|
|
196
|
+
line: from.line,
|
|
197
|
+
depth,
|
|
198
|
+
icon: from.icon,
|
|
199
|
+
status: from.status,
|
|
200
|
+
link: from.link,
|
|
201
|
+
lines,
|
|
202
|
+
children: []
|
|
203
|
+
};
|
|
204
|
+
node.h = lines.length * LINE;
|
|
205
|
+
node.w = Math.max(0, ...lines.map((line) => ink(line, bodyFont))) + iconSpace(from.icon);
|
|
206
|
+
node.children = (from.children || []).map((child) => subtree(child, depth + 1));
|
|
207
|
+
return node;
|
|
208
|
+
};
|
|
209
|
+
const categories = model.root.children.map((from, index) => {
|
|
210
|
+
nodeStatus[from.id] = from.status;
|
|
211
|
+
const lines = wrap(from.label, chipFont);
|
|
212
|
+
return {
|
|
213
|
+
id: from.id,
|
|
214
|
+
line: from.line,
|
|
215
|
+
icon: from.icon,
|
|
216
|
+
status: from.status,
|
|
217
|
+
link: from.link,
|
|
218
|
+
index,
|
|
219
|
+
dir: index % 2 === 0 ? -1 : 1,
|
|
220
|
+
lines,
|
|
221
|
+
chipW: Math.max(CHIP_MIN_W, Math.ceil(Math.max(0, ...lines.map((line) => ink(line, chipFont))) + 20 + iconSpace(from.icon))),
|
|
222
|
+
chipH: Math.max(CHIP_MIN_H, lines.length * LINE + 12),
|
|
223
|
+
children: (from.children || []).map((child) => subtree(child, 2))
|
|
224
|
+
};
|
|
225
|
+
});
|
|
226
|
+
for (const category of categories) {
|
|
227
|
+
const end = assignBands(category.dir < 0 ? [...category.children].reverse() : category.children, SPINE_CLEAR);
|
|
228
|
+
category.reach = Math.max(MIN_BONE, end + CHIP_GAP + category.chipH);
|
|
229
|
+
}
|
|
230
|
+
const sides = [categories.filter((category) => category.dir < 0), categories.filter((category) => category.dir > 0)];
|
|
231
|
+
for (const side of sides) {
|
|
232
|
+
if (!side.length) continue;
|
|
233
|
+
const reach = Math.max(...side.map((category) => category.reach));
|
|
234
|
+
for (const category of side) category.reach = reach;
|
|
235
|
+
}
|
|
236
|
+
for (const category of categories) {
|
|
237
|
+
const place = (node, parent) => {
|
|
238
|
+
if (node.depth % 2 === 0) {
|
|
239
|
+
node.y0 = node.s;
|
|
240
|
+
node.x0 = parent === category ? -LEAN * node.s : parent.x1 - LEAN * (node.s - parent.y1);
|
|
241
|
+
node.run = node.children.length ? Math.max(BRANCH_RUN, STEP_RUN * node.children.length) : LEAF_RUN;
|
|
242
|
+
node.x1 = node.x0 - node.run;
|
|
243
|
+
node.y1 = node.s;
|
|
244
|
+
} else {
|
|
245
|
+
const count = parent.children.length, k = parent.children.indexOf(node);
|
|
246
|
+
node.y0 = parent.y1;
|
|
247
|
+
node.x0 = parent.x1 + (parent.x0 - parent.x1) * (k + 1) / (count + 1);
|
|
248
|
+
const end = node.children.length ? Math.max(node.s, ...node.children.map(deepest)) : node.s;
|
|
249
|
+
node.y1 = end;
|
|
250
|
+
node.x1 = node.x0 - LEAN * (end - node.y0);
|
|
251
|
+
}
|
|
252
|
+
node.children.forEach((child) => place(child, node));
|
|
253
|
+
};
|
|
254
|
+
category.children.forEach((node) => place(node, category));
|
|
255
|
+
const leftOf = (node) => Math.min(node.x1 - LABEL_GAP - node.w, node.x0, node.x1, ...node.children.map(leftOf));
|
|
256
|
+
const leaves = category.children.map(leftOf);
|
|
257
|
+
const chipX = -LEAN * category.reach;
|
|
258
|
+
category.leftRel = Math.min(chipX - category.chipW / 2, ...leaves);
|
|
259
|
+
category.rightRel = Math.max(0, chipX + category.chipW / 2);
|
|
260
|
+
}
|
|
261
|
+
for (const side of sides) {
|
|
262
|
+
const step = Math.max(0, ...side.slice(1).map((category, index) => category.rightRel - side[index].leftRel + CAT_PAD));
|
|
263
|
+
side.forEach((category, index) => {
|
|
264
|
+
category.ax = index ? side[index - 1].ax - step : -(category.rightRel + HEAD_GAP);
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
const spineLeft = categories.length ? Math.min(...categories.map((category) => category.ax + Math.min(category.leftRel, -26))) : -26;
|
|
268
|
+
const minX = Math.min(spineLeft, 0, ...categories.map((category) => category.ax + category.leftRel));
|
|
269
|
+
const maxX = Math.max(headW, ...categories.map((category) => category.ax + category.rightRel));
|
|
270
|
+
const above = Math.max(0, ...sides[0].map((category) => category.reach));
|
|
271
|
+
const below = Math.max(0, ...sides[1].map((category) => category.reach));
|
|
272
|
+
const minY = Math.min(-headH / 2, -above);
|
|
273
|
+
const maxY = Math.max(headH / 2, below);
|
|
274
|
+
const dx = MARGIN - minX;
|
|
275
|
+
const dy = MARGIN - minY;
|
|
276
|
+
const point = (relX, magnitude, dir) => [dx + relX, dy + dir * magnitude];
|
|
277
|
+
const bones = [], chips = [], labels = [];
|
|
278
|
+
for (const category of categories) {
|
|
279
|
+
bones.push({
|
|
280
|
+
id: `${category.id}-bone`,
|
|
281
|
+
from: category.id,
|
|
282
|
+
to: "root",
|
|
283
|
+
main: true,
|
|
284
|
+
points: [point(category.ax - LEAN * (category.reach - category.chipH), category.reach - category.chipH, category.dir), point(category.ax, 0, 1)]
|
|
285
|
+
});
|
|
286
|
+
chips.push({
|
|
287
|
+
id: category.id,
|
|
288
|
+
line: category.line,
|
|
289
|
+
tone: category.index % 6,
|
|
290
|
+
lines: category.lines,
|
|
291
|
+
icon: category.icon,
|
|
292
|
+
status: category.status,
|
|
293
|
+
link: category.link,
|
|
294
|
+
x: dx + category.ax - LEAN * category.reach - category.chipW / 2,
|
|
295
|
+
y: dy + (category.dir < 0 ? -category.reach : category.reach - category.chipH),
|
|
296
|
+
w: category.chipW,
|
|
297
|
+
h: category.chipH
|
|
298
|
+
});
|
|
299
|
+
const walk = (node) => {
|
|
300
|
+
bones.push({
|
|
301
|
+
id: `${node.id}-bone`,
|
|
302
|
+
from: node.parent || category.id,
|
|
303
|
+
to: node.id,
|
|
304
|
+
main: false,
|
|
305
|
+
points: [point(category.ax + node.x0, node.y0, category.dir), point(category.ax + node.x1, node.y1, category.dir)]
|
|
306
|
+
});
|
|
307
|
+
labels.push({
|
|
308
|
+
id: node.id,
|
|
309
|
+
line: node.line,
|
|
310
|
+
lines: node.lines,
|
|
311
|
+
icon: node.icon,
|
|
312
|
+
side: category.dir,
|
|
313
|
+
status: node.status,
|
|
314
|
+
link: node.link,
|
|
315
|
+
x: dx + category.ax + node.x1 - LABEL_GAP,
|
|
316
|
+
y: dy + category.dir * node.s
|
|
317
|
+
});
|
|
318
|
+
node.children.forEach(walk);
|
|
319
|
+
};
|
|
320
|
+
for (const node of category.children) {
|
|
321
|
+
node.parent = category.id;
|
|
322
|
+
const annotate = (child, parent) => {
|
|
323
|
+
child.parent = parent.id;
|
|
324
|
+
child.children.forEach((grand) => annotate(grand, child));
|
|
325
|
+
};
|
|
326
|
+
annotate(node, category);
|
|
327
|
+
walk(node);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (model.count > NODE_HINT) errors.push(warning("LARGE_DIAGRAM", lang, model.root.line, `鱼骨图有 ${model.count} 个原因,建议拆分`, `The Ishikawa diagram has ${model.count} causes; consider splitting it`, "按类别拆成多张鱼骨图", "Split the diagram by category"));
|
|
331
|
+
const natural = Math.ceil(maxX - minX + 48);
|
|
332
|
+
if (model.count <= NODE_HINT && natural > width * 2) errors.push(warning("LARGE_DIAGRAM", lang, model.root.line, `鱼骨图需要 ${natural}px 宽,超过图框两倍`, `The Ishikawa diagram needs ${natural}px, more than twice the frame`, "加宽图框或拆分鱼骨图", "Widen the frame or split the diagram"));
|
|
333
|
+
return {
|
|
334
|
+
width: natural,
|
|
335
|
+
height: Math.ceil(maxY - minY + 48),
|
|
336
|
+
cap,
|
|
337
|
+
nodeStatus,
|
|
338
|
+
errors,
|
|
339
|
+
head: {
|
|
340
|
+
x: dx,
|
|
341
|
+
y: dy - headH / 2,
|
|
342
|
+
w: headW,
|
|
343
|
+
h: headH,
|
|
344
|
+
lines: headLines,
|
|
345
|
+
icon: model.root.icon,
|
|
346
|
+
link: model.root.link
|
|
347
|
+
},
|
|
348
|
+
spine: {
|
|
349
|
+
x1: dx + spineLeft,
|
|
350
|
+
y1: dy,
|
|
351
|
+
x2: dx,
|
|
352
|
+
y2: dy
|
|
353
|
+
},
|
|
354
|
+
bones,
|
|
355
|
+
chips,
|
|
356
|
+
labels
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
var deepest = (node) => Math.max(node.s, ...node.children.map(deepest));
|
|
360
|
+
function layout(model, options = {}) {
|
|
361
|
+
const measure = options.measure || fallbackMeasure;
|
|
362
|
+
const width = Math.max(1, Number(options.width) || 800);
|
|
363
|
+
const lang = options.lang || "zh";
|
|
364
|
+
let plan = null;
|
|
365
|
+
for (const cap of CAPS) {
|
|
366
|
+
plan = build(model, {
|
|
367
|
+
cap,
|
|
368
|
+
lang,
|
|
369
|
+
measure,
|
|
370
|
+
width
|
|
371
|
+
});
|
|
372
|
+
if (plan.width <= width) break;
|
|
373
|
+
}
|
|
374
|
+
return plan;
|
|
375
|
+
}
|
|
376
|
+
function scene(plan, ctx) {
|
|
377
|
+
const look = appearance(ctx);
|
|
378
|
+
const ink = ctx.frameTone(ctx.color("fg"));
|
|
379
|
+
const strong = ctx.frameTone(ctx.color("accent"), true);
|
|
380
|
+
const tones = Array.from({ length: 6 }, (_, index) => ctx.frameTone(ctx.color(`tone${index + 1}`)));
|
|
381
|
+
const arrowId = "ishikawa-arrow";
|
|
382
|
+
const arrowEnd = `url(#${arrowId})`;
|
|
383
|
+
const children = [ctx.el("defs", {}, [ctx.el("marker", {
|
|
384
|
+
id: arrowId,
|
|
385
|
+
viewBox: "0 0 8 8",
|
|
386
|
+
refX: 8,
|
|
387
|
+
refY: 4,
|
|
388
|
+
markerWidth: 8,
|
|
389
|
+
markerHeight: 8,
|
|
390
|
+
markerUnits: "userSpaceOnUse",
|
|
391
|
+
orient: "auto"
|
|
392
|
+
}, [ctx.path("M0 0 L8 4 L0 8 Z", { fill: ink })])])];
|
|
393
|
+
children.push(...plan.bones.map((bone) => ctx.path(`M${bone.points[0][0]} ${bone.points[0][1]} L${bone.points[1][0]} ${bone.points[1][1]}`, {
|
|
394
|
+
fill: "none",
|
|
395
|
+
stroke: bone.main ? ink : plan.nodeStatus?.[bone.to] ? ctx.frameTone(look[plan.nodeStatus[bone.to]]) : look.fg4,
|
|
396
|
+
"stroke-width": bone.main ? 2 : 1.4,
|
|
397
|
+
"stroke-linecap": "round",
|
|
398
|
+
...bone.main ? { "marker-end": arrowEnd } : {},
|
|
399
|
+
meta: {
|
|
400
|
+
kind: "edge",
|
|
401
|
+
from: bone.from,
|
|
402
|
+
to: bone.to
|
|
403
|
+
}
|
|
404
|
+
})));
|
|
405
|
+
children.push(ctx.line(plan.spine.x1, plan.spine.y1, plan.spine.x2, plan.spine.y2, {
|
|
406
|
+
stroke: ink,
|
|
407
|
+
"stroke-width": 2,
|
|
408
|
+
"stroke-linecap": "round",
|
|
409
|
+
"marker-end": arrowEnd,
|
|
410
|
+
meta: {
|
|
411
|
+
kind: "edge",
|
|
412
|
+
from: "root",
|
|
413
|
+
to: "root"
|
|
414
|
+
}
|
|
415
|
+
}));
|
|
416
|
+
for (const chip of plan.chips) {
|
|
417
|
+
const tone = tones[chip.tone];
|
|
418
|
+
const chipFill = chip.status ? ctx.tint(look.bg, look[chip.status], .12) : ctx.tint(look.bg, tone, .07);
|
|
419
|
+
const drawing = ctx.el("g", {}, [
|
|
420
|
+
ctx.rect(chip.x, chip.y, chip.w, chip.h, {
|
|
421
|
+
rx: look.radius,
|
|
422
|
+
fill: chipFill,
|
|
423
|
+
stroke: chip.status ? look[chip.status] : ctx.tint(look.bg, tone, .45),
|
|
424
|
+
"stroke-width": 1,
|
|
425
|
+
meta: {
|
|
426
|
+
kind: "node",
|
|
427
|
+
id: chip.id,
|
|
428
|
+
label: chip.id
|
|
429
|
+
}
|
|
430
|
+
}),
|
|
431
|
+
...chip.icon ? [labelIcon(ctx, chip.icon, chip.x + CHIP_PAD_X + 7, chip.y + chip.h / 2, 14, look.fg3)] : [],
|
|
432
|
+
...chip.lines.map((line, index) => ctx.text(chip.x + chip.w / 2 + (chip.icon ? 10 : 0), chip.y + chip.h / 2 + (index - (chip.lines.length - 1) / 2) * LINE, line, {
|
|
433
|
+
size: BODY,
|
|
434
|
+
weight: 600,
|
|
435
|
+
fill: look.fg1,
|
|
436
|
+
anchor: "middle",
|
|
437
|
+
"dominant-baseline": "central"
|
|
438
|
+
}))
|
|
439
|
+
]);
|
|
440
|
+
children.push(chip.link ? ctx.link(chip.link.href, [drawing], chip.link) : drawing);
|
|
441
|
+
}
|
|
442
|
+
for (const group of plan.labels) {
|
|
443
|
+
const widest = group.icon ? Math.max(0, ...group.lines.map((line) => ctx.measure(line, BODY, 400))) : 0;
|
|
444
|
+
const drawing = ctx.el("g", {}, [...group.icon ? [labelIcon(ctx, group.icon, group.x - widest - 12, group.y, 14, look.fg3)] : [], ...group.lines.map((line, index) => ctx.text(group.x, group.y + (index - (group.lines.length - 1) / 2) * LINE, line, {
|
|
445
|
+
size: BODY,
|
|
446
|
+
weight: 400,
|
|
447
|
+
fill: group.status ? ctx.textColor(look[group.status]) : look.fg2,
|
|
448
|
+
anchor: "end",
|
|
449
|
+
"dominant-baseline": "central"
|
|
450
|
+
}))]);
|
|
451
|
+
children.push(group.link ? ctx.link(group.link.href, [drawing], group.link) : drawing);
|
|
452
|
+
}
|
|
453
|
+
const headTone = plan.nodeStatus?.root;
|
|
454
|
+
const headFill = headTone ? ctx.tint(look.bg, look[headTone], .12) : ctx.tint(look.bg, strong, .12);
|
|
455
|
+
const headDrawing = ctx.el("g", {}, [
|
|
456
|
+
ctx.rect(plan.head.x, plan.head.y, plan.head.w, plan.head.h, {
|
|
457
|
+
rx: Math.min(plan.head.h / 2, look.radius + 10),
|
|
458
|
+
fill: headFill,
|
|
459
|
+
stroke: headTone ? look[headTone] : ctx.tint(look.bg, strong, .5),
|
|
460
|
+
"stroke-width": 1.5,
|
|
461
|
+
meta: {
|
|
462
|
+
kind: "node",
|
|
463
|
+
id: "root",
|
|
464
|
+
label: "root"
|
|
465
|
+
}
|
|
466
|
+
}),
|
|
467
|
+
...plan.head.icon ? [labelIcon(ctx, plan.head.icon, plan.head.x + HEAD_PAD_X + 7, plan.head.y + plan.head.h / 2, 14, look.fg3)] : [],
|
|
468
|
+
...plan.head.lines.map((line, index) => ctx.text(plan.head.x + plan.head.w / 2 + (plan.head.icon ? 10 : 0), plan.head.y + plan.head.h / 2 + (index - (plan.head.lines.length - 1) / 2) * HEAD_LINE, line, {
|
|
469
|
+
size: FONT.title.size,
|
|
470
|
+
weight: FONT.title.weight,
|
|
471
|
+
fill: ctx.textOn(headFill),
|
|
472
|
+
anchor: "middle",
|
|
473
|
+
"dominant-baseline": "central"
|
|
474
|
+
}))
|
|
475
|
+
]);
|
|
476
|
+
children.push(plan.head.link ? ctx.link(plan.head.link.href, [headDrawing], plan.head.link) : headDrawing);
|
|
477
|
+
return {
|
|
478
|
+
width: plan.width,
|
|
479
|
+
height: plan.height,
|
|
480
|
+
title: plan.head.lines.join(" "),
|
|
481
|
+
children,
|
|
482
|
+
anchor: {
|
|
483
|
+
x: plan.head.x + plan.head.w / 2,
|
|
484
|
+
y: plan.head.y + plan.head.h / 2
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
var implementations = { ishikawa: {
|
|
489
|
+
optionMode: "raw",
|
|
490
|
+
parse,
|
|
491
|
+
layout,
|
|
492
|
+
scene,
|
|
493
|
+
acceptsPresentation: true,
|
|
494
|
+
honoredConfig: []
|
|
495
|
+
} };
|
|
496
|
+
export { implementations, ishikawa_exports };
|