@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,422 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { error, warning } from "./support.js";
|
|
3
|
+
import { numericLabel, renderWith, validateWith } from "./numeric-model.js";
|
|
4
|
+
import { diagramLine } from "../chart/basic.js";
|
|
5
|
+
import { diagramCombo } from "../chart/multivariate.js";
|
|
6
|
+
// src/diagram/numeric-xy.js
|
|
7
|
+
var numeric_xy_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
|
|
8
|
+
var NUMBER = "[+-]?(?:\\d+(?:\\.\\d+)?|\\.\\d+)";
|
|
9
|
+
new RegExp(`^${NUMBER}$`);
|
|
10
|
+
var RANGE = new RegExp(`^(.*?)\\s*(${NUMBER})\\s*-->\\s*(${NUMBER})\\s*$`);
|
|
11
|
+
var BAND = /^(.*?)\s*\[([^\]]*)\]\s*$/;
|
|
12
|
+
var PLOT = /^(bar|line)\b([\s\S]*)$/i;
|
|
13
|
+
var POINT = new RegExp(`^(${NUMBER})\\s*(?:"((?:[^"\\\\]|\\\\.)*)"|'((?:[^'\\\\]|\\\\.)*)')?$`);
|
|
14
|
+
var NAME = /^\s*(?:"((?:[^"\\]|\\.)*)"|'((?:[^'\\]|\\.)*)'|([^\s[\]"]+))?\s*\[([\s\S]*)\]\s*$/;
|
|
15
|
+
var PANEL_PLOTS = 6;
|
|
16
|
+
var MAX_PLOTS = 24;
|
|
17
|
+
var MAX_POINTS = 120;
|
|
18
|
+
var issue = (info, line, zh, en, fixZh = "修正 Mermaid xychart 语法", fixEn = "Correct the Mermaid xychart syntax") => error(info.lang, line, zh, en, fixZh, fixEn);
|
|
19
|
+
var hint = (errors, info, line, zh, en, fixZh = "检查图表声明", fixEn = "Check the chart declaration") => errors.push(warning("DIAGRAM_HINT", info.lang, line, zh, en, fixZh, fixEn));
|
|
20
|
+
var isEnglish = (lang) => /^en(?:-|$)/i.test(String(lang || ""));
|
|
21
|
+
var unescape = (text) => String(text).replace(/\\([\\"'])/g, "$1");
|
|
22
|
+
var unquote = (value) => {
|
|
23
|
+
const text = String(value).trim();
|
|
24
|
+
return text.startsWith("\"") && text.endsWith("\"") || text.startsWith("'") && text.endsWith("'") ? unescape(text.slice(1, -1)) : text;
|
|
25
|
+
};
|
|
26
|
+
var quoteOf = (double, single, bare) => [
|
|
27
|
+
double,
|
|
28
|
+
single,
|
|
29
|
+
bare
|
|
30
|
+
].find((value) => value !== void 0);
|
|
31
|
+
function splitTop(text, separator = ",") {
|
|
32
|
+
const parts = [];
|
|
33
|
+
let current = "", quote = "", depth = 0;
|
|
34
|
+
for (const char of String(text)) {
|
|
35
|
+
if (quote) {
|
|
36
|
+
current += char;
|
|
37
|
+
if (char === quote) quote = "";
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (char === "\"" || char === "'") {
|
|
41
|
+
quote = char;
|
|
42
|
+
current += char;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (char === "[" || char === "{") depth += 1;
|
|
46
|
+
if (char === "]" || char === "}") depth -= 1;
|
|
47
|
+
if (char === separator && depth === 0) {
|
|
48
|
+
parts.push(current.trim());
|
|
49
|
+
current = "";
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
current += char;
|
|
53
|
+
}
|
|
54
|
+
parts.push(current.trim());
|
|
55
|
+
return parts;
|
|
56
|
+
}
|
|
57
|
+
function statements(source, firstLine) {
|
|
58
|
+
const out = [];
|
|
59
|
+
String(source).split("\n").forEach((raw, index) => {
|
|
60
|
+
const text = raw.trim();
|
|
61
|
+
if (!text || text.startsWith("%%")) return;
|
|
62
|
+
for (const part of splitTop(text, ";")) if (part) out.push({
|
|
63
|
+
text: part,
|
|
64
|
+
line: firstLine + index
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
function readHeader(info, errors) {
|
|
70
|
+
let orientation = null;
|
|
71
|
+
for (const token of String(info.params || "").split(/\s+/).filter(Boolean)) {
|
|
72
|
+
if (!/^(?:horizontal|vertical)$/i.test(token)) {
|
|
73
|
+
errors.push(issue(info, info.headerLine || info.firstLine, `xychart 不支持头行选项「${token}」`, `xychart does not accept header options: ${token}`, "头行只接受 horizontal 或 vertical", "Only horizontal or vertical is allowed after the header"));
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
orientation = token.toLowerCase();
|
|
77
|
+
}
|
|
78
|
+
return orientation;
|
|
79
|
+
}
|
|
80
|
+
function readPoints(text, info, line, errors) {
|
|
81
|
+
const points = [];
|
|
82
|
+
for (const item of splitTop(text)) {
|
|
83
|
+
const match = POINT.exec(item);
|
|
84
|
+
if (!match) {
|
|
85
|
+
errors.push(issue(info, line, `数据点「${item}」须为数值,可带一个引号标签`, `Data point "${item}" must be a number, optionally with a quoted label`, "写成 `12` 或 `12 \"标签\"`", "Write `12` or `12 \"Label\"`"));
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
const label = quoteOf(match[2], match[3], void 0);
|
|
89
|
+
points.push({
|
|
90
|
+
value: Number(match[1]),
|
|
91
|
+
label: label === void 0 ? null : unescape(label) || null
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return points;
|
|
95
|
+
}
|
|
96
|
+
function readAxis(info, rest, line, errors) {
|
|
97
|
+
const band = BAND.exec(rest);
|
|
98
|
+
if (band) {
|
|
99
|
+
const title = band[1].trim() ? unquote(band[1]) : null;
|
|
100
|
+
const categories = splitTop(band[2]);
|
|
101
|
+
if (!categories.length || categories.some((item) => !item)) {
|
|
102
|
+
errors.push(issue(info, line, "x-axis 需要非空类别列表", "x-axis needs a non-empty category list", "去掉空类别或多余的逗号", "Remove empty categories or extra commas"));
|
|
103
|
+
return {
|
|
104
|
+
title,
|
|
105
|
+
data: null
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
title,
|
|
110
|
+
data: {
|
|
111
|
+
kind: "band",
|
|
112
|
+
categories: categories.map(unquote)
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const range = RANGE.exec(rest);
|
|
117
|
+
if (range && !/-$/.test(range[1])) {
|
|
118
|
+
const min = Number(range[2]), max = Number(range[3]);
|
|
119
|
+
return {
|
|
120
|
+
title: range[1].trim() ? unquote(range[1]) : null,
|
|
121
|
+
data: {
|
|
122
|
+
kind: "linear",
|
|
123
|
+
min,
|
|
124
|
+
max
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
title: rest ? unquote(rest) : null,
|
|
130
|
+
data: null
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function uniqueCategories(errors, info, line, categories) {
|
|
134
|
+
const seen = /* @__PURE__ */ new Map(), out = [];
|
|
135
|
+
let duplicated = false;
|
|
136
|
+
for (const category of categories) {
|
|
137
|
+
const count = (seen.get(category) || 0) + 1;
|
|
138
|
+
seen.set(category, count);
|
|
139
|
+
if (count === 1) {
|
|
140
|
+
out.push(category);
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
duplicated = true;
|
|
144
|
+
out.push(`${category} (${count})`);
|
|
145
|
+
}
|
|
146
|
+
if (duplicated) hint(errors, info, line, "x-axis 类别名重复,已按出现顺序加序号区分", "Duplicate x-axis categories were numbered in source order", "让每个类别名唯一", "Give every category a unique name");
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
function linearLabels(min, max, count) {
|
|
150
|
+
const round = (value) => String(Number(value.toPrecision(12)));
|
|
151
|
+
if (count <= 0) return [];
|
|
152
|
+
if (count === 1) return [round(min)];
|
|
153
|
+
const step = (max - min) / (count - 1);
|
|
154
|
+
return Array.from({ length: count }, (_, index) => round(min + index * step));
|
|
155
|
+
}
|
|
156
|
+
function readPlots(info, text, line, errors) {
|
|
157
|
+
const head = PLOT.exec(text), type = head[1].toLowerCase(), body = NAME.exec(head[2]);
|
|
158
|
+
if (!body) {
|
|
159
|
+
errors.push(issue(info, line, `无法识别 ${type} 数据「${text}」`, `Unrecognized ${type} data "${text}"`, `写成 \`${type} [值, 值]\` 或 \`${type} "名称" [值, 值]\``, `Write \`${type} [value, value]\` or \`${type} "Name" [value, value]\``));
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
const points = readPoints(body[4], info, line, errors);
|
|
163
|
+
if (!points) return null;
|
|
164
|
+
const name = quoteOf(body[1], body[2], body[3]);
|
|
165
|
+
return {
|
|
166
|
+
type,
|
|
167
|
+
name: name === void 0 ? null : unescape(name),
|
|
168
|
+
points,
|
|
169
|
+
line
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function parseXychart(source, info) {
|
|
173
|
+
const errors = [];
|
|
174
|
+
let title = null, titleLine = null, xTitle = null, yTitle = null, xData = null, xAxisLine = null, yAxisLine = null, yDomain = null;
|
|
175
|
+
const plots = [];
|
|
176
|
+
const orientation = readHeader(info, errors);
|
|
177
|
+
for (const item of statements(source, info.firstLine)) {
|
|
178
|
+
if (/^title\b/i.test(item.text)) {
|
|
179
|
+
const text = item.text.replace(/^title\b/i, "").trim();
|
|
180
|
+
if (!text) {
|
|
181
|
+
errors.push(issue(info, item.line, "title 需要标题文字", "title needs some text"));
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
if (title !== null) {
|
|
185
|
+
errors.push(issue(info, item.line, "xychart 只能有一个 title", "xychart accepts one title"));
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
title = numericLabel(unquote(text), info, item.line, errors);
|
|
189
|
+
titleLine = item.line;
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (/^x-axis\b/i.test(item.text)) {
|
|
193
|
+
const axis = readAxis(info, item.text.replace(/^x-axis\b/i, "").trim(), item.line, errors);
|
|
194
|
+
if (axis.title !== null) {
|
|
195
|
+
if (xTitle !== null) hint(errors, info, item.line, "xychart 重复的 x-axis,只保留最后一条", "Duplicate x-axis; the last one is used");
|
|
196
|
+
xTitle = numericLabel(axis.title, info, item.line, errors);
|
|
197
|
+
}
|
|
198
|
+
if (axis.data) {
|
|
199
|
+
if (xData) hint(errors, info, item.line, "xychart 重复的 x-axis 数据,只保留最后一条", "Duplicate x-axis data; the last one is used");
|
|
200
|
+
xData = axis.data;
|
|
201
|
+
xAxisLine = item.line;
|
|
202
|
+
}
|
|
203
|
+
xAxisLine ??= item.line;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (/^y-axis\b/i.test(item.text)) {
|
|
207
|
+
const axis = readAxis(info, item.text.replace(/^y-axis\b/i, "").trim(), item.line, errors);
|
|
208
|
+
yAxisLine ??= item.line;
|
|
209
|
+
if (axis.title !== null) {
|
|
210
|
+
if (yTitle !== null) hint(errors, info, item.line, "xychart 重复的 y-axis,只保留最后一条", "Duplicate y-axis; the last one is used");
|
|
211
|
+
yTitle = numericLabel(axis.title, info, item.line, errors);
|
|
212
|
+
}
|
|
213
|
+
if (axis.data && axis.data.kind === "linear") {
|
|
214
|
+
if (yDomain) hint(errors, info, item.line, "xychart 重复的 y-axis 范围,只保留最后一条", "Duplicate y-axis range; the last one is used");
|
|
215
|
+
yDomain = [axis.data.min, axis.data.max];
|
|
216
|
+
} else if (axis.data?.kind === "band") errors.push(issue(info, item.line, "y-axis 只接受数值范围 `min --> max`", "y-axis accepts only a numeric range `min --> max`", "改成 `y-axis \"标题\" 0 --> 100`", "Use `y-axis \"Title\" 0 --> 100`"));
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (PLOT.test(item.text)) {
|
|
220
|
+
const plot = readPlots(info, item.text, item.line, errors);
|
|
221
|
+
if (plot) plots.push(plot);
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
errors.push(issue(info, item.line, `无法识别 xychart 语句「${item.text}」`, `Unrecognized xychart statement "${item.text}"`, "支持 title、x-axis、y-axis、bar、line", "Supported: title, x-axis, y-axis, bar, line"));
|
|
225
|
+
}
|
|
226
|
+
if (!plots.length) errors.push(issue(info, info.firstLine, "xychart 至少需要一条 bar 或 line 数据", "xychart needs at least one bar or line plot"));
|
|
227
|
+
if (plots.length > MAX_PLOTS) errors.push(issue(info, plots[MAX_PLOTS].line, `xychart 安全上限为 ${MAX_PLOTS} 条数据序列,当前 ${plots.length} 条`, `xychart safety cap is ${MAX_PLOTS} plots; got ${plots.length}`, `将数据序列合并到 ${MAX_PLOTS} 条以内,或拆成多张图`, `Merge series to stay within ${MAX_PLOTS} plots, or split the chart`));
|
|
228
|
+
const hasNamedOnly = plots.length === 1 && plots[0].name;
|
|
229
|
+
const hasPointLabels = plots.some((plot) => plot.type === "line" && plot.points.some((point) => point.label));
|
|
230
|
+
const chart = plots.length > 1 ? "combo" : hasNamedOnly && !(plots[0].type === "line" && plots[0].points.some((point) => point.label)) ? "combo" : plots[0]?.type === "bar" ? "column" : "line";
|
|
231
|
+
const categoryLine = xAxisLine || plots[0]?.line || info.firstLine;
|
|
232
|
+
const lengths = plots.map((plot) => plot.points.length);
|
|
233
|
+
const categoryCount = xData?.kind === "band" ? xData.categories.length : Math.max(0, ...lengths);
|
|
234
|
+
if (categoryCount > MAX_POINTS && chart !== "column") errors.push(issue(info, categoryLine, `xychart 最多支持 ${MAX_POINTS} 个横轴类别`, `xychart supports at most ${MAX_POINTS} x-axis categories`, "按横轴范围拆分图表", "Split the chart by x-axis range"));
|
|
235
|
+
let categories;
|
|
236
|
+
if (xData?.kind === "band") categories = uniqueCategories(errors, info, categoryLine, xData.categories.map((value) => numericLabel(value, info, categoryLine, errors)));
|
|
237
|
+
else if (xData?.kind === "linear") categories = linearLabels(xData.min, xData.max, categoryCount);
|
|
238
|
+
else categories = Array.from({ length: categoryCount }, (_, index) => String(index + 1));
|
|
239
|
+
const datasets = plots.map((plot) => {
|
|
240
|
+
const truncated = plot.points.length > categories.length;
|
|
241
|
+
const pad = chart === "combo" || plot.type === "line" ? null : 0;
|
|
242
|
+
if (truncated) hint(errors, info, plot.line, `数据点数 ${plot.points.length} 多于类别数 ${categories.length},多余的点已略去`, `${plot.points.length} points exceed ${categories.length} categories; extra points were dropped`, "补齐 x-axis 类别或删去多余的点", "Add x-axis categories or remove the extra points");
|
|
243
|
+
else if (plot.points.length < categories.length && pad === 0) hint(errors, info, plot.line, `bar 数据点数少于类别数,缺少的点按 0 绘制`, `The bar plot has fewer points than categories; missing points are drawn as 0`, "补齐数据点", "Provide a value for every category");
|
|
244
|
+
return {
|
|
245
|
+
type: plot.type,
|
|
246
|
+
name: plot.name && numericLabel(plot.name, info, plot.line, errors),
|
|
247
|
+
line: plot.line,
|
|
248
|
+
values: categories.map((_, index) => plot.points[index]?.value ?? pad),
|
|
249
|
+
pointLabels: categories.map((_, index) => {
|
|
250
|
+
const label = plot.points[index]?.label;
|
|
251
|
+
return label ? numericLabel(label, info, plot.line, errors) : null;
|
|
252
|
+
})
|
|
253
|
+
};
|
|
254
|
+
});
|
|
255
|
+
if (chart === "combo" && hasPointLabels) hint(errors, info, plots[0].line, "组合图中的数据点标签未绘制", "Point labels are not drawn on a combined chart", "把该序列拆成单独的 line 图,或删去标签", "Split that plot into a line-only chart or drop the labels");
|
|
256
|
+
if (chart !== "combo" && datasets[0]?.name && hasPointLabels) hint(errors, info, plots[0].line, `单序列折线的名称「${datasets[0].name}」未作为图例绘制`, `The name "${datasets[0].name}" of this single line is not drawn as a legend`, "需要图例时不要同时使用数据点标签", "Do not combine point labels with a series name if the legend matters");
|
|
257
|
+
const labelledBars = plots.find((plot) => plot.type === "bar" && plot.points.some((point) => point.label));
|
|
258
|
+
if (labelledBars) hint(errors, info, labelledBars.line, "bar 的数据点标签被忽略(Mermaid 也只对 line 绘制点标签)", "Point labels on a bar plot are ignored (Mermaid draws them on line plots only)", "改用 line 绘制带标签的序列", "Use line for a labeled series");
|
|
259
|
+
const values = plots.flatMap((plot) => plot.points.map((point) => point.value));
|
|
260
|
+
const dataMin = Math.min(...values), dataMax = Math.max(...values);
|
|
261
|
+
let domain = null;
|
|
262
|
+
if (yDomain) {
|
|
263
|
+
if (!(yDomain[1] > yDomain[0])) hint(errors, info, yAxisLine, `y-axis 范围 ${yDomain[0]} --> ${yDomain[1]} 不是递增区间,已改为按数据取值`, `y-axis range ${yDomain[0]} --> ${yDomain[1]} is not increasing; derived from data instead`, "把范围写成 `小值 --> 大值`", "Write the range as `low --> high`");
|
|
264
|
+
else domain = [yDomain[0], yDomain[1]];
|
|
265
|
+
}
|
|
266
|
+
if (domain) {
|
|
267
|
+
if (domain[0] > dataMin) {
|
|
268
|
+
hint(errors, info, yAxisLine, `y-axis 最小值 ${domain[0]} 高于数据最小值 ${dataMin},已扩展到 ${dataMin}`, `y-axis minimum ${domain[0]} is above the data minimum ${dataMin}; extended to ${dataMin}`, `把最小值改为不大于 ${dataMin}`, `Set the minimum to at most ${dataMin}`);
|
|
269
|
+
domain[0] = dataMin;
|
|
270
|
+
}
|
|
271
|
+
if (domain[1] < dataMax) {
|
|
272
|
+
hint(errors, info, yAxisLine, `y-axis 最大值 ${domain[1]} 低于数据最大值 ${dataMax},已扩展到 ${dataMax}`, `y-axis maximum ${domain[1]} is below the data maximum ${dataMax}; extended to ${dataMax}`, `把最大值改为不小于 ${dataMax}`, `Set the maximum to at least ${dataMax}`);
|
|
273
|
+
domain[1] = dataMax;
|
|
274
|
+
}
|
|
275
|
+
if (plots.some((plot) => plot.type === "bar") && (domain[0] > 0 || domain[1] < 0)) {
|
|
276
|
+
hint(errors, info, yAxisLine, `柱形 y 轴已自动包含 0 基线(${domain[0]} --> ${domain[1]} → ${Math.min(0, domain[0])} --> ${Math.max(0, domain[1])})`, `The bar y-axis now includes the zero baseline (${domain[0]} --> ${domain[1]} → ${Math.min(0, domain[0])} --> ${Math.max(0, domain[1])})`, "把 y-axis 范围写成包含 0 的区间", "Declare a y-axis range that includes 0");
|
|
277
|
+
domain = [Math.min(0, domain[0]), Math.max(0, domain[1])];
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (orientation === "horizontal") hint(errors, info, info.headerLine || info.firstLine, "xychart horizontal 暂不支持,按纵向绘制", "Horizontal xychart is not supported yet; drawing vertically", "改成 `xychart` 或 `xychart vertical`", "Use `xychart` or `xychart vertical`");
|
|
281
|
+
if (xData?.kind === "linear" && new Set(categories).size !== categories.length) hint(errors, info, categoryLine, `数值 x 轴 ${xData.min} --> ${xData.max} 的刻度有重复,类别名已加序号`, `The numeric x-axis ${xData.min} --> ${xData.max} produces repeated ticks; categories were numbered`, "扩大范围或减少数据点", "Widen the range or use fewer points");
|
|
282
|
+
return {
|
|
283
|
+
model: {
|
|
284
|
+
chart,
|
|
285
|
+
title,
|
|
286
|
+
titleLine,
|
|
287
|
+
xLabel: xTitle,
|
|
288
|
+
yLabel: yTitle,
|
|
289
|
+
xLine: xAxisLine,
|
|
290
|
+
yLine: yAxisLine,
|
|
291
|
+
yDomain: domain,
|
|
292
|
+
categories,
|
|
293
|
+
datasets,
|
|
294
|
+
horizontal: orientation === "horizontal"
|
|
295
|
+
},
|
|
296
|
+
errors
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
function specOf(model, info, panelIndex = 0) {
|
|
300
|
+
const en = isEnglish(info?.lang);
|
|
301
|
+
const subtitle = [model.xLabel, model.yLabel].filter(Boolean).join(" · ") || void 0;
|
|
302
|
+
const domain = model.yDomain ? { domain: model.yDomain } : {};
|
|
303
|
+
if (model.chart === "combo") {
|
|
304
|
+
const data = model.categories.map((category, index) => Object.fromEntries([["category", category], ...model.datasets.map((dataset, series) => [`v${series}`, dataset.values[index]])]));
|
|
305
|
+
return {
|
|
306
|
+
chart: "combo",
|
|
307
|
+
[diagramCombo]: { index: panelIndex },
|
|
308
|
+
...model.title ? { title: model.title } : {},
|
|
309
|
+
...subtitle ? { subtitle } : {},
|
|
310
|
+
data,
|
|
311
|
+
encode: { category: "category" },
|
|
312
|
+
layers: model.datasets.map((dataset, series) => ({
|
|
313
|
+
mark: dataset.type === "bar" ? "column" : "line",
|
|
314
|
+
field: `v${series}`,
|
|
315
|
+
axis: "left"
|
|
316
|
+
})),
|
|
317
|
+
fields: Object.fromEntries(model.datasets.map((dataset, series) => [`v${series}`, {
|
|
318
|
+
label: dataset.name || (en ? `Series ${series + 1}` : `系列 ${series + 1}`),
|
|
319
|
+
...domain
|
|
320
|
+
}]))
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
const dataset = model.datasets[0];
|
|
324
|
+
const labelled = dataset.pointLabels.some(Boolean);
|
|
325
|
+
const data = model.categories.map((category, index) => ({
|
|
326
|
+
category,
|
|
327
|
+
value: dataset.values[index],
|
|
328
|
+
...labelled ? { id: String(index) } : {}
|
|
329
|
+
}));
|
|
330
|
+
return {
|
|
331
|
+
chart: model.chart,
|
|
332
|
+
...model.chart === "line" ? { [diagramLine]: true } : {},
|
|
333
|
+
...model.title ? { title: model.title } : {},
|
|
334
|
+
...subtitle ? { subtitle } : {},
|
|
335
|
+
data,
|
|
336
|
+
encode: {
|
|
337
|
+
category: "category",
|
|
338
|
+
value: "value",
|
|
339
|
+
...labelled ? { id: "id" } : {}
|
|
340
|
+
},
|
|
341
|
+
fields: { value: {
|
|
342
|
+
...domain,
|
|
343
|
+
...dataset.name ? { label: dataset.name } : {}
|
|
344
|
+
} },
|
|
345
|
+
...labelled ? { annotations: dataset.pointLabels.flatMap((text, index) => text ? [{
|
|
346
|
+
kind: "note",
|
|
347
|
+
field: "value",
|
|
348
|
+
record: String(index),
|
|
349
|
+
text
|
|
350
|
+
}] : []) } : {}
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
function lineFor(model, detail) {
|
|
354
|
+
const path = String(detail?.path || "").split("/").slice(1).map((part) => part.replace(/~1/g, "/").replace(/~0/g, "~"));
|
|
355
|
+
const series = (value) => {
|
|
356
|
+
const match = /^v(\d+)$/.exec(String(value || ""));
|
|
357
|
+
return match ? Number(match[1]) : null;
|
|
358
|
+
};
|
|
359
|
+
if (path[0] === "title") return model.titleLine;
|
|
360
|
+
if (path[0] === "subtitle") return model.xLine || model.yLine;
|
|
361
|
+
if (path[0] === "layers") return model.datasets[Number(path[1])]?.line || model.datasets[0]?.line;
|
|
362
|
+
if (path[0] === "fields") {
|
|
363
|
+
const index = series(path[1]);
|
|
364
|
+
return (index === null ? null : model.datasets[index]?.line) || model.yLine || model.datasets[0]?.line;
|
|
365
|
+
}
|
|
366
|
+
if (path[0] === "encode") return model.xLine || model.datasets[0]?.line;
|
|
367
|
+
if (path[0] === "annotations") return model.datasets[0]?.line;
|
|
368
|
+
if (path[0] === "data") {
|
|
369
|
+
const index = series(path[2]);
|
|
370
|
+
return (index === null ? null : model.datasets[index]?.line) || model.datasets[0]?.line;
|
|
371
|
+
}
|
|
372
|
+
return model.datasets[0]?.line;
|
|
373
|
+
}
|
|
374
|
+
function panelsOf(model, info) {
|
|
375
|
+
if (model.datasets.length <= PANEL_PLOTS) return [model];
|
|
376
|
+
const values = model.datasets.flatMap((dataset) => dataset.values.filter(Number.isFinite));
|
|
377
|
+
const zero = model.datasets.some((dataset) => dataset.type === "bar") ? [0] : [];
|
|
378
|
+
const min = Math.min(...values, ...zero), max = Math.max(...values, ...zero);
|
|
379
|
+
const yDomain = model.yDomain ?? [min, max > min ? max : min + 1];
|
|
380
|
+
const panels = [];
|
|
381
|
+
for (let offset = 0; offset < model.datasets.length; offset += PANEL_PLOTS) panels.push({
|
|
382
|
+
...model,
|
|
383
|
+
yDomain,
|
|
384
|
+
title: offset ? null : model.title,
|
|
385
|
+
datasets: model.datasets.slice(offset, offset + PANEL_PLOTS).map((dataset, i) => ({
|
|
386
|
+
...dataset,
|
|
387
|
+
name: dataset.name || `${isEnglish(info.lang) ? "Series " : "系列 "}${offset + i + 1}`
|
|
388
|
+
}))
|
|
389
|
+
});
|
|
390
|
+
return panels;
|
|
391
|
+
}
|
|
392
|
+
var validate = (model, info) => panelsOf(model, info).flatMap((panel) => validateWith(specOf(panel, info), info, (detail) => lineFor(panel, detail)));
|
|
393
|
+
var layout = (model, options, info) => {
|
|
394
|
+
const results = panelsOf(model, info).map((panel, index) => renderWith(specOf(panel, info, index), options, info, (detail) => lineFor(panel, detail)));
|
|
395
|
+
if (results.some((result) => !result.scene)) return results.find((result) => !result.scene);
|
|
396
|
+
const result = results[0];
|
|
397
|
+
if (results.length > 1) {
|
|
398
|
+
let width = 0;
|
|
399
|
+
const children = results.map(({ scene }) => {
|
|
400
|
+
const group = options.ctx.el("g", { transform: `translate(${width} 0)` }, scene.children);
|
|
401
|
+
width += scene.width + 24;
|
|
402
|
+
return group;
|
|
403
|
+
});
|
|
404
|
+
result.width = width - 24;
|
|
405
|
+
result.height = Math.max(...results.map((panel) => panel.height));
|
|
406
|
+
result.scene = {
|
|
407
|
+
...result.scene,
|
|
408
|
+
width: result.width,
|
|
409
|
+
height: result.height,
|
|
410
|
+
children
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
if (model.categories.length > 60 || result.width > options.width * 2 || results.length > 1) result.errors.push(warning("LARGE_DIAGRAM", info.lang, model.xLine || model.datasets[0]?.line || info.firstLine, results.length > 1 ? "XY 图按数据序列分栏,可横向滚动" : "XY 图较大,可滚动查看", results.length > 1 ? "XY series are arranged in panels; scroll horizontally" : "Large XY chart; scroll to view it", "按数据序列或横轴范围拆分图表", "Split the chart by series or x-axis range"));
|
|
414
|
+
return result;
|
|
415
|
+
};
|
|
416
|
+
var implementations = { xychart: {
|
|
417
|
+
parse: parseXychart,
|
|
418
|
+
validate,
|
|
419
|
+
layout,
|
|
420
|
+
optionMode: "raw"
|
|
421
|
+
} };
|
|
422
|
+
export { implementations, numeric_xy_exports };
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { normalizeLabel } from "./label.js";
|
|
3
|
+
import { presentation } from "./presentation.js";
|
|
4
|
+
import { FONT, appearance, error, textBlock, warning } from "./support.js";
|
|
5
|
+
// src/diagram/packet.js
|
|
6
|
+
var packet_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
|
|
7
|
+
var honoredConfig = [
|
|
8
|
+
"bitsPerRow",
|
|
9
|
+
"bitOrder",
|
|
10
|
+
"showBits",
|
|
11
|
+
"bitWidth",
|
|
12
|
+
"rowHeight",
|
|
13
|
+
"paddingX",
|
|
14
|
+
"paddingY"
|
|
15
|
+
].map((key) => `packet.${key}`);
|
|
16
|
+
function parse(body, { lang, firstLine = 2, title = "", config, options = {} }) {
|
|
17
|
+
const fields = [], errors = [], assets = /* @__PURE__ */ new Set(), look = presentation({ lang });
|
|
18
|
+
const clean = (value, line) => {
|
|
19
|
+
const { text, icon, diagnostics } = normalizeLabel(value, {
|
|
20
|
+
lang,
|
|
21
|
+
line
|
|
22
|
+
});
|
|
23
|
+
if (icon) diagnostics.push(warning("IGNORED_ASSET", lang, line, "位域仅显示文本", "Packet fields display text only"));
|
|
24
|
+
for (const d of diagnostics) if (!assets.has(d.message)) {
|
|
25
|
+
assets.add(d.message);
|
|
26
|
+
errors.push(d);
|
|
27
|
+
}
|
|
28
|
+
return text;
|
|
29
|
+
};
|
|
30
|
+
const fail = (line, zh, en) => errors.push(error(lang, line, zh, en, "修正连续位域,或拆分图表", "Fix the contiguous bit fields, or split the diagram"));
|
|
31
|
+
if (Object.keys(options).length) fail(firstLine - 1, "packet 不接受标题行选项", "Unexpected packet header option");
|
|
32
|
+
let next = 0;
|
|
33
|
+
for (const [i, raw] of body.split("\n").entries()) {
|
|
34
|
+
const line = firstLine + i, s = raw.trim();
|
|
35
|
+
if (!s || s.startsWith("%%") || look.accept(s, line)) continue;
|
|
36
|
+
const heading = /^title(?:\s*:\s*|\s+)(.+)/i.exec(s);
|
|
37
|
+
if (heading) {
|
|
38
|
+
title = clean(heading[1], line);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const m = /^(?:(\d+)(?:\s*-\s*(\d+))?|\+(\d+))\s*:\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*')\s*(?:::\:([\w-]+))?\s*(?:%%.*)?$/.exec(s);
|
|
42
|
+
if (!m) {
|
|
43
|
+
fail(line, "需要位范围及带引号的名称", "Expected a bit range and a quoted field name");
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const start = m[3] === void 0 ? Number(m[1]) : next;
|
|
47
|
+
const end = m[3] === void 0 ? Number(m[2] ?? m[1]) : next + Number(m[3]) - 1;
|
|
48
|
+
if (start !== next) {
|
|
49
|
+
fail(line, `位域应从第 ${next} 位开始`, `Field must start at bit ${next}`);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (end < start) {
|
|
53
|
+
fail(line, "位域长度必须为正", "Field must have positive width");
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (end >= 4096 || fields.length >= 2e3) {
|
|
57
|
+
fail(line, "位域超过 4096 位或 2000 个字段", "Packet exceeds 4096 bits or 2000 fields");
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
const id = m[4].slice(1, -1).replace(/\\(.)/g, "$1");
|
|
61
|
+
if (m[5]) look.addClasses(id, [m[5]], line);
|
|
62
|
+
fields.push({
|
|
63
|
+
start,
|
|
64
|
+
end,
|
|
65
|
+
id,
|
|
66
|
+
label: clean(id, line)
|
|
67
|
+
});
|
|
68
|
+
next = end + 1;
|
|
69
|
+
}
|
|
70
|
+
if (!fields.length) fail(firstLine, "缺少位域", "Expected at least one field");
|
|
71
|
+
for (const f of fields) {
|
|
72
|
+
f.status = look.statusOf(f.id);
|
|
73
|
+
f.link = look.linkOf(f.id);
|
|
74
|
+
}
|
|
75
|
+
errors.push(...look.diagnostics());
|
|
76
|
+
return {
|
|
77
|
+
model: {
|
|
78
|
+
type: "packet",
|
|
79
|
+
title,
|
|
80
|
+
fields,
|
|
81
|
+
config: config?.packet || {}
|
|
82
|
+
},
|
|
83
|
+
errors
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function layout(model, { width, ctx, renderOptions }, info = {}) {
|
|
87
|
+
const { title, fields, config } = model, t = appearance(ctx);
|
|
88
|
+
const perRow = Number.isInteger(config.bitsPerRow) && config.bitsPerRow >= 1 && config.bitsPerRow <= 128 ? config.bitsPerRow : 32;
|
|
89
|
+
const showBits = config.showBits !== false, descending = config.bitOrder === "descending";
|
|
90
|
+
const rows = [];
|
|
91
|
+
fields.forEach((field, id) => {
|
|
92
|
+
for (let start = field.start; start <= field.end;) {
|
|
93
|
+
const row = Math.floor(start / perRow), end = Math.min(field.end, (row + 1) * perRow - 1);
|
|
94
|
+
(rows[row] ??= []).push({
|
|
95
|
+
...field,
|
|
96
|
+
start,
|
|
97
|
+
end,
|
|
98
|
+
id,
|
|
99
|
+
bits: end - start + 1
|
|
100
|
+
});
|
|
101
|
+
start = end + 1;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
const dimension = (key, fallback, min, max) => Number.isFinite(config[key]) ? Math.min(max, Math.max(min, config[key])) : fallback;
|
|
105
|
+
const gap = dimension("paddingX", 0, 0, 24), rowGap = dimension("paddingY", 12, 6, 60);
|
|
106
|
+
const minimumRow = dimension("rowHeight", 34, 26, 240);
|
|
107
|
+
const pad = 18, range = (f) => f.start === f.end ? String(f.start) : `${f.start}–${f.end}`;
|
|
108
|
+
const needed = Math.ceil(rows.flat().reduce((min, f) => Math.max(min, ...f.label.split(/\s+/).map((word) => (ctx.measure(word, FONT.body.size) + 10 + gap) / f.bits), showBits ? (ctx.measure(range(f), FONT.label.size) + 6) / f.bits : 0), 12));
|
|
109
|
+
const pitch = Math.max(needed, Math.floor((width - 36) / perRow), dimension("bitWidth", 12, 12, 96));
|
|
110
|
+
const w = Math.max(width, perRow * pitch + 36), left = (w - perRow * pitch) / 2;
|
|
111
|
+
const children = [];
|
|
112
|
+
let y = pad;
|
|
113
|
+
if (title && !renderOptions?.frame) {
|
|
114
|
+
children.push(ctx.text(w / 2, y + 8, title, {
|
|
115
|
+
...FONT.title,
|
|
116
|
+
anchor: "middle",
|
|
117
|
+
fill: t.fg1,
|
|
118
|
+
"dominant-baseline": "central"
|
|
119
|
+
}));
|
|
120
|
+
y += 30;
|
|
121
|
+
}
|
|
122
|
+
for (const row of rows) {
|
|
123
|
+
const cells = row.map((f) => ({
|
|
124
|
+
...f,
|
|
125
|
+
lines: ctx.wrap(f.label, f.bits * pitch - gap - 8, FONT.body.size)
|
|
126
|
+
}));
|
|
127
|
+
const h = Math.max(minimumRow, ...cells.map((f) => f.lines.length * 17 + 14));
|
|
128
|
+
for (const f of cells) {
|
|
129
|
+
const x = left + (descending ? perRow - f.start % perRow - f.bits : f.start % perRow) * pitch;
|
|
130
|
+
const cellWidth = f.bits * pitch - gap, cx = x + cellWidth / 2;
|
|
131
|
+
const stroke = t[f.status] || t.line;
|
|
132
|
+
const cell = [ctx.rect(x, y, cellWidth, h, {
|
|
133
|
+
fill: f.status ? ctx.tint(t.bg, stroke, .1) : f.id % 2 ? t.surface : t.bg,
|
|
134
|
+
stroke,
|
|
135
|
+
meta: {
|
|
136
|
+
kind: "node",
|
|
137
|
+
id: `${f.id}:${f.start}`
|
|
138
|
+
}
|
|
139
|
+
}), textBlock(ctx, {
|
|
140
|
+
x: cx,
|
|
141
|
+
y: y + h / 2,
|
|
142
|
+
lines: f.lines,
|
|
143
|
+
lineHeight: 17
|
|
144
|
+
})];
|
|
145
|
+
children.push(...f.link ? [ctx.link(f.link.href, cell, f.link)] : cell);
|
|
146
|
+
if (showBits) children.push(ctx.text(cx, y + h + 12, range(f), {
|
|
147
|
+
...FONT.label,
|
|
148
|
+
anchor: "middle",
|
|
149
|
+
"dominant-baseline": "central"
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
y += h + rowGap + (showBits ? 16 : 0);
|
|
153
|
+
}
|
|
154
|
+
const height = y + 6;
|
|
155
|
+
const errors = w > width * 2 || fields.length > 60 ? [warning("LARGE_DIAGRAM", ctx.lang, info.headerLine || 1, "封包图较大", "Large packet diagram", "考虑拆分图表", "Consider splitting the diagram")] : [];
|
|
156
|
+
return {
|
|
157
|
+
width: w,
|
|
158
|
+
height,
|
|
159
|
+
scene: {
|
|
160
|
+
width: w,
|
|
161
|
+
height,
|
|
162
|
+
children,
|
|
163
|
+
title,
|
|
164
|
+
heading: title
|
|
165
|
+
},
|
|
166
|
+
errors
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
var implementations = { packet: {
|
|
170
|
+
parse,
|
|
171
|
+
layout,
|
|
172
|
+
honoredConfig,
|
|
173
|
+
acceptsPresentation: true
|
|
174
|
+
} };
|
|
175
|
+
export { implementations, packet_exports };
|