@fengwk/kchart 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE.md +11 -0
  4. package/README.md +68 -2
  5. package/README.zh-CN.md +69 -0
  6. package/dist/LICENSE +21 -0
  7. package/dist/NOTICE.md +11 -0
  8. package/dist/assets/ICONS-LICENSE.txt +53 -0
  9. package/dist/assets/maps/LICENSE.md +75 -0
  10. package/dist/assets/maps/METADATA.md +9 -0
  11. package/dist/assets/maps/REGIONS.md +28 -0
  12. package/dist/assets/maps/us-states.geojson +1 -0
  13. package/dist/assets/maps/world.geojson +1 -0
  14. package/dist/esm/_virtual/_kchart-families.js +48 -0
  15. package/dist/esm/_virtual/_rolldown/runtime.js +12 -0
  16. package/dist/esm/assets/icons.js +11 -0
  17. package/dist/esm/assets/maps/us-states.js +3 -0
  18. package/dist/esm/assets/maps/world.js +3 -0
  19. package/dist/esm/assets/themes/claude.js +69 -0
  20. package/dist/esm/assets/themes/cloudmusic.js +62 -0
  21. package/dist/esm/assets/themes/kanagawa.js +67 -0
  22. package/dist/esm/assets/themes/obsidian.js +67 -0
  23. package/dist/esm/assets/themes/paper.js +61 -0
  24. package/dist/esm/assets/themes/terminal.js +60 -0
  25. package/dist/esm/assets/themes/tokyo.js +62 -0
  26. package/dist/esm/chart/basic.js +1313 -0
  27. package/dist/esm/chart/catalog.js +476 -0
  28. package/dist/esm/chart/composition.js +844 -0
  29. package/dist/esm/chart/context.js +189 -0
  30. package/dist/esm/chart/distribution.js +442 -0
  31. package/dist/esm/chart/fields.js +67 -0
  32. package/dist/esm/chart/maps.js +563 -0
  33. package/dist/esm/chart/mindmap.js +629 -0
  34. package/dist/esm/chart/multivariate.js +1214 -0
  35. package/dist/esm/chart/registry.js +108 -0
  36. package/dist/esm/chart/relations.js +949 -0
  37. package/dist/esm/chart/spec.js +1411 -0
  38. package/dist/esm/chart/stat-compare.js +302 -0
  39. package/dist/esm/chart/stat-trend.js +181 -0
  40. package/dist/esm/chart/statistics.js +1007 -0
  41. package/dist/esm/chart/unit-variants.js +645 -0
  42. package/dist/esm/chart/variants.js +552 -0
  43. package/dist/esm/core/color.js +197 -0
  44. package/dist/esm/core/context.js +256 -0
  45. package/dist/esm/core/diagnostics.js +23 -0
  46. package/dist/esm/core/format.js +167 -0
  47. package/dist/esm/core/geometry.js +130 -0
  48. package/dist/esm/core/icon-data.js +5 -0
  49. package/dist/esm/core/icons.js +173 -0
  50. package/dist/esm/core/scale.js +112 -0
  51. package/dist/esm/core/scene.js +234 -0
  52. package/dist/esm/core/svg.js +89 -0
  53. package/dist/esm/core/text.js +184 -0
  54. package/dist/esm/core/theme.js +318 -0
  55. package/dist/esm/diagram/agentflow.js +596 -0
  56. package/dist/esm/diagram/architecture.js +729 -0
  57. package/dist/esm/diagram/block.js +988 -0
  58. package/dist/esm/diagram/blocks.js +687 -0
  59. package/dist/esm/diagram/c4.js +1002 -0
  60. package/dist/esm/diagram/class.js +1158 -0
  61. package/dist/esm/diagram/cynefin.js +749 -0
  62. package/dist/esm/diagram/emphasis.js +7 -0
  63. package/dist/esm/diagram/er.js +657 -0
  64. package/dist/esm/diagram/eventmodeling.js +939 -0
  65. package/dist/esm/diagram/gantt-date.js +323 -0
  66. package/dist/esm/diagram/gantt.js +661 -0
  67. package/dist/esm/diagram/gitgraph.js +472 -0
  68. package/dist/esm/diagram/graph-layout.js +4046 -0
  69. package/dist/esm/diagram/graph-parse.js +1366 -0
  70. package/dist/esm/diagram/graph-route.js +490 -0
  71. package/dist/esm/diagram/graph.js +553 -0
  72. package/dist/esm/diagram/ishikawa.js +496 -0
  73. package/dist/esm/diagram/journey.js +512 -0
  74. package/dist/esm/diagram/kanban.js +280 -0
  75. package/dist/esm/diagram/label.js +56 -0
  76. package/dist/esm/diagram/layers.js +385 -0
  77. package/dist/esm/diagram/map-label.js +44 -0
  78. package/dist/esm/diagram/mindmap.js +277 -0
  79. package/dist/esm/diagram/numeric-composition.js +78 -0
  80. package/dist/esm/diagram/numeric-model.js +86 -0
  81. package/dist/esm/diagram/numeric-parse.js +205 -0
  82. package/dist/esm/diagram/numeric-radar.js +355 -0
  83. package/dist/esm/diagram/numeric-sankey.js +201 -0
  84. package/dist/esm/diagram/numeric-xy.js +422 -0
  85. package/dist/esm/diagram/packet.js +175 -0
  86. package/dist/esm/diagram/presentation.js +252 -0
  87. package/dist/esm/diagram/quadrant.js +297 -0
  88. package/dist/esm/diagram/railroad-parse.js +465 -0
  89. package/dist/esm/diagram/railroad.js +294 -0
  90. package/dist/esm/diagram/registry.js +296 -0
  91. package/dist/esm/diagram/relations.js +966 -0
  92. package/dist/esm/diagram/requirement.js +341 -0
  93. package/dist/esm/diagram/sequence.js +1228 -0
  94. package/dist/esm/diagram/shapes.js +366 -0
  95. package/dist/esm/diagram/source.js +538 -0
  96. package/dist/esm/diagram/support.js +153 -0
  97. package/dist/esm/diagram/swimlane-mermaid.js +140 -0
  98. package/dist/esm/diagram/swimlane.js +1186 -0
  99. package/dist/esm/diagram/time.js +748 -0
  100. package/dist/esm/diagram/timeline.js +376 -0
  101. package/dist/esm/diagram/tree-family-icon.js +22 -0
  102. package/dist/esm/diagram/treeview.js +293 -0
  103. package/dist/esm/diagram/usecase.js +852 -0
  104. package/dist/esm/diagram/venn.js +1090 -0
  105. package/dist/esm/diagram/wardley.js +838 -0
  106. package/dist/esm/index.d.ts +133 -0
  107. package/dist/esm/index.js +92 -0
  108. package/dist/esm/interaction.js +561 -0
  109. package/dist/esm/mermaid.d.ts +28 -0
  110. package/dist/esm/mermaid.js +70 -0
  111. package/dist/esm/node.d.ts +68 -0
  112. package/dist/esm/node.js +373 -0
  113. package/dist/esm/package.json +1 -0
  114. package/dist/esm/page-api.js +65 -0
  115. package/dist/esm/runtime.js +352 -0
  116. package/dist/esm/src/families.js +209 -0
  117. package/dist/page-api.js +1 -0
  118. package/dist/units/chart/basic.js +1 -0
  119. package/dist/units/chart/catalog.js +1 -0
  120. package/dist/units/chart/composition.js +1 -0
  121. package/dist/units/chart/context.js +1 -0
  122. package/dist/units/chart/distribution.js +1 -0
  123. package/dist/units/chart/fields.js +1 -0
  124. package/dist/units/chart/maps.js +1 -0
  125. package/dist/units/chart/mindmap.js +1 -0
  126. package/dist/units/chart/multivariate.js +1 -0
  127. package/dist/units/chart/registry.js +1 -0
  128. package/dist/units/chart/relations.js +1 -0
  129. package/dist/units/chart/spec.js +3 -0
  130. package/dist/units/chart/stat-compare.js +1 -0
  131. package/dist/units/chart/stat-trend.js +1 -0
  132. package/dist/units/chart/statistics.js +1 -0
  133. package/dist/units/chart/unit-variants.js +1 -0
  134. package/dist/units/chart/variants.js +1 -0
  135. package/dist/units/core/color.js +1 -0
  136. package/dist/units/core/context.js +1 -0
  137. package/dist/units/core/diagnostics.js +1 -0
  138. package/dist/units/core/format.js +1 -0
  139. package/dist/units/core/geometry.js +1 -0
  140. package/dist/units/core/icon-data.js +1 -0
  141. package/dist/units/core/icons.js +1 -0
  142. package/dist/units/core/scale.js +1 -0
  143. package/dist/units/core/scene.js +1 -0
  144. package/dist/units/core/svg.js +1 -0
  145. package/dist/units/core/text.js +2 -0
  146. package/dist/units/core/theme.js +1 -0
  147. package/dist/units/diagram/agentflow.js +12 -0
  148. package/dist/units/diagram/architecture.js +4 -0
  149. package/dist/units/diagram/block.js +2 -0
  150. package/dist/units/diagram/blocks.js +2 -0
  151. package/dist/units/diagram/c4.js +6 -0
  152. package/dist/units/diagram/class.js +5 -0
  153. package/dist/units/diagram/cynefin.js +4 -0
  154. package/dist/units/diagram/emphasis.js +1 -0
  155. package/dist/units/diagram/er.js +9 -0
  156. package/dist/units/diagram/eventmodeling.js +5 -0
  157. package/dist/units/diagram/gantt-date.js +1 -0
  158. package/dist/units/diagram/gantt.js +3 -0
  159. package/dist/units/diagram/gitgraph.js +2 -0
  160. package/dist/units/diagram/graph-layout.js +4 -0
  161. package/dist/units/diagram/graph-parse.js +9 -0
  162. package/dist/units/diagram/graph-route.js +1 -0
  163. package/dist/units/diagram/graph.js +1 -0
  164. package/dist/units/diagram/ishikawa.js +2 -0
  165. package/dist/units/diagram/journey.js +5 -0
  166. package/dist/units/diagram/kanban.js +2 -0
  167. package/dist/units/diagram/label.js +4 -0
  168. package/dist/units/diagram/layers.js +1 -0
  169. package/dist/units/diagram/map-label.js +1 -0
  170. package/dist/units/diagram/mindmap.js +2 -0
  171. package/dist/units/diagram/numeric-composition.js +1 -0
  172. package/dist/units/diagram/numeric-model.js +1 -0
  173. package/dist/units/diagram/numeric-parse.js +2 -0
  174. package/dist/units/diagram/numeric-radar.js +3 -0
  175. package/dist/units/diagram/numeric-sankey.js +2 -0
  176. package/dist/units/diagram/numeric-xy.js +2 -0
  177. package/dist/units/diagram/packet.js +2 -0
  178. package/dist/units/diagram/presentation.js +2 -0
  179. package/dist/units/diagram/quadrant.js +2 -0
  180. package/dist/units/diagram/railroad-parse.js +4 -0
  181. package/dist/units/diagram/railroad.js +1 -0
  182. package/dist/units/diagram/registry.js +2 -0
  183. package/dist/units/diagram/relations.js +3 -0
  184. package/dist/units/diagram/requirement.js +3 -0
  185. package/dist/units/diagram/sequence.js +2 -0
  186. package/dist/units/diagram/shapes.js +60 -0
  187. package/dist/units/diagram/source.js +20 -0
  188. package/dist/units/diagram/support.js +3 -0
  189. package/dist/units/diagram/swimlane-mermaid.js +3 -0
  190. package/dist/units/diagram/swimlane.js +2 -0
  191. package/dist/units/diagram/time.js +3 -0
  192. package/dist/units/diagram/timeline.js +2 -0
  193. package/dist/units/diagram/tree-family-icon.js +1 -0
  194. package/dist/units/diagram/treeview.js +2 -0
  195. package/dist/units/diagram/usecase.js +3 -0
  196. package/dist/units/diagram/venn.js +2 -0
  197. package/dist/units/diagram/wardley.js +4 -0
  198. package/dist/units/interaction.js +1 -0
  199. package/dist/units/runtime.js +1 -0
  200. package/dist/units.json +710 -0
  201. package/package.json +30 -4
@@ -0,0 +1,1411 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { catalog } from "./catalog.js";
3
+ // src/chart/spec.js
4
+ var spec_exports = /* @__PURE__ */ __exportAll({
5
+ parse: () => parse,
6
+ schema: () => schema,
7
+ sizeBudget: () => sizeBudget,
8
+ validate: () => validate
9
+ });
10
+ var TOP = /* @__PURE__ */ new Set([
11
+ "chart",
12
+ "title",
13
+ "subtitle",
14
+ "source",
15
+ "note",
16
+ "data",
17
+ "encode",
18
+ "fields",
19
+ "unit",
20
+ "variant",
21
+ "layout",
22
+ "sort",
23
+ "emphasis",
24
+ "annotations",
25
+ "interaction",
26
+ "stack",
27
+ "unitMark",
28
+ "range",
29
+ "start",
30
+ "layers",
31
+ "period",
32
+ "directed",
33
+ ...Object.values(catalog).flatMap((c) => Object.keys(c.special))
34
+ ]);
35
+ var NODE_SHAPES = [
36
+ "rect",
37
+ "rounded",
38
+ "circle",
39
+ "bang",
40
+ "cloud",
41
+ "hexagon",
42
+ "none"
43
+ ];
44
+ var ENCODE = /* @__PURE__ */ new Set([
45
+ "category",
46
+ "value",
47
+ "series",
48
+ "id",
49
+ "lower",
50
+ "upper",
51
+ "intensity",
52
+ "panel",
53
+ "actual",
54
+ "target",
55
+ "before",
56
+ "after",
57
+ "open",
58
+ "close",
59
+ "low",
60
+ "high",
61
+ "delta",
62
+ "count",
63
+ "q1",
64
+ "median",
65
+ "q3",
66
+ "outliers",
67
+ "density",
68
+ "x",
69
+ "y",
70
+ "size",
71
+ "label",
72
+ "row",
73
+ "column",
74
+ "date",
75
+ "rank",
76
+ "dimensions",
77
+ "start",
78
+ "region",
79
+ "longitude",
80
+ "latitude"
81
+ ]);
82
+ var FIELD_KEYS = /* @__PURE__ */ new Set([
83
+ "label",
84
+ "type",
85
+ "unit",
86
+ "format",
87
+ "decimals",
88
+ "domain",
89
+ "scale"
90
+ ]);
91
+ var ALL_EMPHASIS = [
92
+ "rows",
93
+ "fields",
94
+ "series"
95
+ ];
96
+ var ROW_EMPHASIS = ["rows"];
97
+ var SERIES_EMPHASIS = ["fields", "series"];
98
+ var EMPHASIS = {
99
+ line: {
100
+ "": ALL_EMPHASIS,
101
+ counter: ALL_EMPHASIS,
102
+ barcode: ROW_EMPHASIS
103
+ },
104
+ area: { "": ALL_EMPHASIS },
105
+ bar: {
106
+ "": ALL_EMPHASIS,
107
+ solid: ALL_EMPHASIS,
108
+ lollipop: ROW_EMPHASIS,
109
+ rungs: ROW_EMPHASIS,
110
+ pictorial: ROW_EMPHASIS
111
+ },
112
+ column: {
113
+ "": ALL_EMPHASIS,
114
+ solid: ALL_EMPHASIS,
115
+ lollipop: ROW_EMPHASIS,
116
+ rungs: ROW_EMPHASIS,
117
+ pictorial: ROW_EMPHASIS
118
+ },
119
+ share: {
120
+ "": SERIES_EMPHASIS,
121
+ solid: SERIES_EMPHASIS
122
+ },
123
+ funnel: {
124
+ "": ROW_EMPHASIS,
125
+ bars: ROW_EMPHASIS
126
+ }
127
+ };
128
+ var err = (errors, path, message, fix = "请按图型契约修正字段或数据。", code = "BAD_KCHART", loc) => errors.push({
129
+ code,
130
+ path,
131
+ message,
132
+ fix,
133
+ ...loc ? { location: loc } : {}
134
+ });
135
+ var obj = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
136
+ var finite = (v) => typeof v === "number" && Number.isFinite(v);
137
+ var ptr = (p) => p.map((x) => `/${String(x).replace(/~/g, "~0").replace(/\//g, "~1")}`).join("");
138
+ var keys = (value, allowed, path, errors) => {
139
+ if (!obj(value)) {
140
+ err(errors, path, "必须是对象");
141
+ return;
142
+ }
143
+ for (const key of Object.keys(value)) if (!allowed.has(key)) err(errors, `${path}/${key}`, "未知字段");
144
+ };
145
+ var safeDate = (v, type) => {
146
+ if (typeof v !== "string") return false;
147
+ if (type === "month") return /^\d{4}-(0[1-9]|1[0-2])$/.test(v);
148
+ if (type === "date") {
149
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(v)) return false;
150
+ const d = /* @__PURE__ */ new Date(`${v}T00:00:00Z`);
151
+ return Number.isFinite(d.getTime()) && d.toISOString().slice(0, 10) === v;
152
+ }
153
+ if (type === "datetime") return /^\d{4}-\d\d-\d\dT.*(?:Z|[+-]\d\d:\d\d)$/.test(v) && safeDate(v.slice(0, 10), "date") && Number.isFinite(Date.parse(v));
154
+ return true;
155
+ };
156
+ var sizeBudget = Symbol("size budget");
157
+ function validate(spec) {
158
+ const errors = [];
159
+ if (!obj(spec)) {
160
+ err(errors, "", "声明必须是对象");
161
+ return {
162
+ errors,
163
+ data: null
164
+ };
165
+ }
166
+ keys(spec, TOP, "", errors);
167
+ const c = typeof spec.chart === "string" ? catalog[spec.chart] : void 0;
168
+ if (!c) err(errors, "/chart", "未知图型", "chart 必须是注册图型名。");
169
+ for (const k of [
170
+ "title",
171
+ "subtitle",
172
+ "source",
173
+ "note",
174
+ "unit"
175
+ ]) if (spec[k] !== void 0 && typeof spec[k] !== "string") err(errors, `/${k}`, "必须是字符串");
176
+ if (spec.unit === "%") err(errors, "/unit", "根 unit 不可写 %", "比例数据请用 fields.<字段>.format=percent。");
177
+ if (spec.data === void 0) err(errors, "/data", "必填字段缺失");
178
+ if (c) {
179
+ const shapeOk = c.shape === "table" ? Array.isArray(spec.data) && spec.data.length > 0 && spec.data.every(obj) : c.shape === "tree" ? obj(spec.data) : c.shape === "network" ? obj(spec.data) && Array.isArray(spec.data.nodes) && Array.isArray(spec.data.edges) : c.shape === "paths" ? obj(spec.data) && Array.isArray(spec.data.stages) && Array.isArray(spec.data.nodes) && Array.isArray(spec.data.paths) : obj(spec.data) && obj(spec.data.map) && Array.isArray(spec.data.rows);
180
+ if (!shapeOk) err(errors, "/data", `数据必须匹配 ${c.shape} 形状`);
181
+ const errorsBeforeEncode = errors.length;
182
+ if (c.shape === "table") {
183
+ if (!obj(spec.encode)) err(errors, "/encode", "必须提供角色映射");
184
+ else {
185
+ keys(spec.encode, ENCODE, "/encode", errors);
186
+ for (const role of c.required) if (spec.encode[role] === void 0) err(errors, `/encode/${role}`, "必填角色缺失");
187
+ for (const role of Object.keys(spec.encode)) if (![...c.required, ...c.optional].includes(role)) err(errors, `/encode/${role}`, "该图型不支持此角色");
188
+ for (const role of [...c.required, ...c.optional]) if (spec.encode[role] !== void 0) {
189
+ const value = spec.encode[role];
190
+ const allowsArray = c.arrayRoles.includes(role);
191
+ if (role === "dimensions") {
192
+ if (!Array.isArray(value) || !value.length || !value.every((x) => typeof x === "string" && x)) err(errors, `/encode/${role}`, "dimensions须为非空字段名数组");
193
+ } else if (allowsArray) {
194
+ if (!(typeof value === "string" && value || Array.isArray(value) && value.length && value.every((x) => typeof x === "string" && x))) err(errors, `/encode/${role}`, "须为非空字段名或字段名数组");
195
+ } else if (typeof value !== "string" || !value) err(errors, `/encode/${role}`, "必须是单个非空字段名");
196
+ }
197
+ }
198
+ } else if (c.shape === "geo") {
199
+ if (!obj(spec.encode)) err(errors, "/encode", "地图必须提供角色映射");
200
+ else {
201
+ keys(spec.encode, ENCODE, "/encode", errors);
202
+ for (const role of c.required) if (spec.encode[role] === void 0) err(errors, `/encode/${role}`, "必填角色缺失");
203
+ for (const [role, value] of Object.entries(spec.encode)) if (![...c.required, ...c.optional].includes(role)) err(errors, `/encode/${role}`, "该地图不支持此角色");
204
+ else if (typeof value !== "string" || !value) err(errors, `/encode/${role}`, "必须是单个非空字段名");
205
+ }
206
+ } else if (spec.encode !== void 0) err(errors, "/encode", "该图型不使用 encode");
207
+ const encodeOk = errors.length === errorsBeforeEncode;
208
+ const layersOk = c.name !== "combo" || Array.isArray(spec.layers) && spec.layers.every((layer) => obj(layer) && typeof layer.field === "string");
209
+ const commonFields = /* @__PURE__ */ new Set([
210
+ "chart",
211
+ "title",
212
+ "subtitle",
213
+ "source",
214
+ "note",
215
+ "data",
216
+ "fields",
217
+ "unit",
218
+ "sort",
219
+ "emphasis",
220
+ "annotations",
221
+ "interaction",
222
+ "encode"
223
+ ]);
224
+ for (const field of Object.keys(spec)) if (!commonFields.has(field) && c.special[field] === void 0 && !(field === "unitMark" && [
225
+ "bar",
226
+ "column",
227
+ "dumbbell",
228
+ "waterfall",
229
+ "histogram"
230
+ ].includes(c.name))) err(errors, `/${field}`, `图型 ${c.name} 不支持此参数`);
231
+ for (const field of c.requiredSpecial) if (spec[field] === void 0) err(errors, `/${field}`, "必填专用字段缺失");
232
+ for (const [field, allowed] of Object.entries(c.special)) {
233
+ const v = spec[field];
234
+ if (v === void 0) {
235
+ if (allowed === "range") err(errors, `/${field}`, "必填专用字段缺失");
236
+ continue;
237
+ }
238
+ if (Array.isArray(allowed) && !allowed.includes(v)) err(errors, `/${field}`, `必须为 ${allowed.join("、")} 中的一项`);
239
+ else if (allowed === "boolean" && typeof v !== "boolean") err(errors, `/${field}`, "必须是布尔值");
240
+ else if (allowed === "number" && !finite(v)) err(errors, `/${field}`, "必须是有限数");
241
+ else if (field === "rings" && (!Number.isInteger(v) || v < 2 || v > 8)) err(errors, `/${field}`, "必须是 2 至 8 的整数");
242
+ else if (allowed === "year" && (typeof v !== "string" || !/^\d{4}$/.test(v))) err(errors, `/${field}`, "必须是YYYY年份");
243
+ else if (allowed === "range" && !(Array.isArray(v) && v.length === 2 && v.every(finite) && v[0] < v[1])) err(errors, `/${field}`, "必须是递增的有限数范围");
244
+ else if (allowed === "layers" && (!Array.isArray(v) || !v.length || v.some((x) => !obj(x) || ![
245
+ "line",
246
+ "area",
247
+ "column"
248
+ ].includes(x.mark) || typeof x.field !== "string" || !["left", "right"].includes(x.axis)))) err(errors, `/${field}`, "必须是有效 layers 数组");
249
+ }
250
+ if (spec.unitMark !== void 0) {
251
+ keys(spec.unitMark, /* @__PURE__ */ new Set([
252
+ "value",
253
+ "label",
254
+ "icon"
255
+ ]), "/unitMark", errors);
256
+ if (!obj(spec.unitMark) || !finite(spec.unitMark.value) || spec.unitMark.value <= 0 || typeof spec.unitMark.label !== "string" || !spec.unitMark.label) err(errors, "/unitMark", "unitMark须含正有限value及非空label");
257
+ if (spec.variant === "rungs" && spec.unitMark?.icon !== void 0) err(errors, "/unitMark/icon", "rungs不支持icon");
258
+ if (spec.variant === "pictorial" && (typeof spec.unitMark?.icon !== "string" || !spec.unitMark.icon)) err(errors, "/unitMark/icon", "pictorial必须指定受控icon ID");
259
+ }
260
+ if (["rungs", "pictorial"].includes(spec.variant) && spec.unitMark === void 0) err(errors, "/unitMark", "该变体必须声明unitMark");
261
+ if (spec.unitMark !== void 0 && ![
262
+ "bar",
263
+ "column",
264
+ "dumbbell",
265
+ "waterfall",
266
+ "histogram"
267
+ ].includes(c.name)) err(errors, "/unitMark", "该图型不支持unitMark");
268
+ if (shapeOk && c.shape === "tree") checkTree(spec.data, c.name, errors, spec[sizeBudget]);
269
+ if (shapeOk && c.shape === "network") checkNetwork(spec.data, spec.chart, errors, spec.tolerant === true);
270
+ if (shapeOk && c.shape === "paths") checkPaths(spec.data, errors);
271
+ if (c.shape === "geo" && spec.data?.map) {
272
+ const m = spec.data.map;
273
+ const builtin = obj(m) && Object.keys(m).length === 1 && ["world", "us-states"].includes(m.asset);
274
+ const external = obj(m) && obj(m.geojson) && m.geojson.type === "FeatureCollection" && Array.isArray(m.geojson.features) && typeof m.idProperty === "string" && Object.keys(m).every((k) => ["geojson", "idProperty"].includes(k));
275
+ if (!builtin && !external) err(errors, "/data/map", "map须是world、us-states或GeoJSON FeatureCollection");
276
+ if (external) {
277
+ const ids = /* @__PURE__ */ new Set();
278
+ for (const [i, f] of m.geojson.features.entries()) {
279
+ if (!obj(f) || f.type !== "Feature" || !obj(f.properties) || !obj(f.geometry) || !["Polygon", "MultiPolygon"].includes(f.geometry.type)) {
280
+ err(errors, `/data/map/geojson/features/${i}`, "地图仅支持带properties的Polygon/MultiPolygon Feature");
281
+ continue;
282
+ }
283
+ const id = f.properties[m.idProperty];
284
+ if (typeof id !== "string" || !id || ids.has(id)) err(errors, `/data/map/geojson/features/${i}/properties/${m.idProperty}`, "地图地区键须唯一非空字符串");
285
+ ids.add(id);
286
+ }
287
+ }
288
+ }
289
+ if (c.shape === "table" && shapeOk && encodeOk && layersOk) checkTable(spec, c, errors);
290
+ if (c.shape === "geo" && shapeOk && encodeOk) checkGeoRows(spec, c, errors);
291
+ }
292
+ if (spec.fields !== void 0) {
293
+ if (!obj(spec.fields)) err(errors, "/fields", "必须是对象");
294
+ else for (const [name, f] of Object.entries(spec.fields)) {
295
+ keys(f, FIELD_KEYS, `/fields/${name}`, errors);
296
+ if (!obj(f)) continue;
297
+ if (f.label !== void 0 && typeof f.label !== "string") err(errors, `/fields/${name}/label`, "必须是字符串");
298
+ if (f.type !== void 0 && ![
299
+ "number",
300
+ "category",
301
+ "date",
302
+ "month",
303
+ "datetime"
304
+ ].includes(f.type)) err(errors, `/fields/${name}/type`, "无效字段类型");
305
+ if (f.format !== void 0 && !["number", "percent"].includes(f.format)) err(errors, `/fields/${name}/format`, "无效数值格式");
306
+ if (f.unit !== void 0 && typeof f.unit !== "string") err(errors, `/fields/${name}/unit`, "必须是字符串");
307
+ if (f.unit !== void 0 && f.format === "percent") err(errors, `/fields/${name}`, "percent已隐含%单位,不可再声明unit");
308
+ if (f.decimals !== void 0 && (!Number.isInteger(f.decimals) || f.decimals < 0 || f.decimals > 6)) err(errors, `/fields/${name}/decimals`, "必须是0至6的整数");
309
+ if (f.scale !== void 0 && !["linear", "log"].includes(f.scale)) err(errors, `/fields/${name}/scale`, "无效scale");
310
+ if (f.domain !== void 0) {
311
+ const d = f.domain;
312
+ if (!(Array.isArray(d) && (d.length === 2 && d.every(finite) && d[0] < d[1] || d.every((x) => typeof x === "string") && new Set(d).size === d.length))) err(errors, `/fields/${name}/domain`, "domain须为递增数值范围或无重复分类序列");
313
+ }
314
+ }
315
+ }
316
+ if (spec.unit !== void 0 && spec.fields && Object.values(spec.fields).some((f) => obj(f) && (f.unit !== void 0 || f.format === "percent"))) err(errors, "/unit", "根 unit 与字段级 unit/percent 冲突");
317
+ if (spec.unit !== void 0 && ["radial-share", "split-compare"].includes(spec.chart)) err(errors, "/unit", "该图型不支持根 unit,须分别声明字段单位或格式");
318
+ if (spec.sort !== void 0) {
319
+ keys(spec.sort, /* @__PURE__ */ new Set(["field", "direction"]), "/sort", errors);
320
+ if (!obj(spec.sort) || typeof spec.sort.field !== "string" || !["asc", "desc"].includes(spec.sort.direction)) err(errors, "/sort", "须为 {field,direction:asc|desc}");
321
+ if (![
322
+ "bar",
323
+ "column",
324
+ "pie",
325
+ "donut",
326
+ "radial-share",
327
+ "rose",
328
+ "tally"
329
+ ].includes(spec.chart)) err(errors, "/sort", "该图型不支持排序");
330
+ if (obj(spec.encode) && spec.sort?.field) {
331
+ const vals = spec.encode.value;
332
+ const category = spec.encode.category;
333
+ if (Array.isArray(vals) && vals.length !== 1) err(errors, "/sort", "多系列图不能排序");
334
+ if (![category, ...Array.isArray(vals) ? vals : [vals]].includes(spec.sort.field)) err(errors, "/sort/field", "sort.field必须是实际category或value字段");
335
+ }
336
+ }
337
+ if (spec.interaction !== void 0) {
338
+ if (!Array.isArray(spec.interaction) || spec.interaction.some((action) => ![
339
+ "inspect",
340
+ "legend",
341
+ "focus",
342
+ "pan-zoom",
343
+ "drag",
344
+ "collapse"
345
+ ].includes(action))) err(errors, "/interaction", "包含无效交互");
346
+ else for (const action of spec.interaction) if (!(action === "inspect" || action === "legend" && [
347
+ "line",
348
+ "area",
349
+ "bar",
350
+ "column",
351
+ "scatter"
352
+ ].includes(spec.chart) || action === "focus" && [
353
+ "network",
354
+ "sankey",
355
+ "threads",
356
+ "tree",
357
+ "mindmap",
358
+ "parallel"
359
+ ].includes(spec.chart) || action === "pan-zoom" && [
360
+ "network",
361
+ "tree",
362
+ "mindmap",
363
+ "sankey",
364
+ "threads",
365
+ "choropleth",
366
+ "geo-scatter"
367
+ ].includes(spec.chart) || action === "drag" && spec.chart === "network" && spec.layout === "force" || action === "collapse" && ["tree", "mindmap"].includes(spec.chart))) err(errors, "/interaction", `该图型不支持 ${action}`);
368
+ if (Array.isArray(spec.interaction) && spec.interaction.includes("drag") && !spec.interaction.includes("pan-zoom")) err(errors, "/interaction", "drag必须搭配pan-zoom");
369
+ }
370
+ if (spec.annotations !== void 0) {
371
+ const allowedCharts = [
372
+ "line",
373
+ "area",
374
+ "bar",
375
+ "column",
376
+ "scatter",
377
+ "combo",
378
+ "interval",
379
+ "histogram",
380
+ "candlestick"
381
+ ];
382
+ if (!Array.isArray(spec.annotations)) err(errors, "/annotations", "必须是数组");
383
+ else for (const [i, annotation] of spec.annotations.entries()) {
384
+ const path = `/annotations/${i}`;
385
+ if (!obj(annotation) || ![
386
+ "rule",
387
+ "band",
388
+ "note"
389
+ ].includes(annotation.kind)) {
390
+ err(errors, path, "无效注释");
391
+ continue;
392
+ }
393
+ const allowedKeys = annotation.kind === "rule" ? [
394
+ "kind",
395
+ "field",
396
+ "value",
397
+ "label"
398
+ ] : annotation.kind === "band" ? [
399
+ "kind",
400
+ "field",
401
+ "from",
402
+ "to",
403
+ "label"
404
+ ] : [
405
+ "kind",
406
+ "record",
407
+ "field",
408
+ "text"
409
+ ];
410
+ keys(annotation, new Set(allowedKeys), path, errors);
411
+ if (!allowedCharts.includes(spec.chart)) err(errors, path, `${spec.chart}不支持annotations`);
412
+ if (annotation.kind === "rule" && (!finite(annotation.value) || typeof annotation.field !== "string")) err(errors, path, "rule须指定数值field和value");
413
+ if (annotation.kind === "band" && (!finite(annotation.from) || !finite(annotation.to) || annotation.from >= annotation.to || typeof annotation.field !== "string")) err(errors, path, "band须指定field及递增数值边界");
414
+ if (annotation.kind === "note" && (typeof annotation.record !== "string" || typeof annotation.field !== "string" || typeof annotation.text !== "string" || !obj(spec.encode) || !spec.encode.id)) err(errors, path, "note须通过encode.id引用记录并提供field/text");
415
+ if (annotation.label !== void 0 && typeof annotation.label !== "string" || annotation.text !== void 0 && typeof annotation.text !== "string") err(errors, path, "注释label/text须为纯文本");
416
+ const enc = spec.encode || {};
417
+ const scalarFields = Object.entries(enc).filter(([role, value]) => typeof value === "string" && ![
418
+ "category",
419
+ "series",
420
+ "id",
421
+ "panel",
422
+ "label",
423
+ "row",
424
+ "column",
425
+ "date"
426
+ ].includes(role)).map(([, value]) => value);
427
+ if (Array.isArray(enc.value)) scalarFields.push(...enc.value);
428
+ if (Array.isArray(spec.layers)) scalarFields.push(...spec.layers.filter(obj).map((layer) => layer.field));
429
+ if (typeof annotation.field === "string" && !scalarFields.includes(annotation.field)) err(errors, `${path}/field`, "注释字段必须是本图已引用的标量数值角色");
430
+ if (spec.chart === "histogram" && typeof annotation.field === "string" && !["lower", "upper"].some((role) => enc[role] === annotation.field)) err(errors, `${path}/field`, "histogram注释只允许lower或upper");
431
+ if (annotation.kind === "note" && Array.isArray(spec.data)) {
432
+ const idField = enc.id;
433
+ if (!spec.data.some((row) => row?.[idField] === annotation.record)) err(errors, `${path}/record`, "note引用的record id不存在");
434
+ }
435
+ }
436
+ }
437
+ if (spec.emphasis !== void 0) {
438
+ const kind = Object.keys(obj(spec.emphasis) ? spec.emphasis : {});
439
+ const values = kind.length === 1 ? spec.emphasis[kind[0]] : null;
440
+ const supported = EMPHASIS[spec.chart]?.[spec.variant ?? ""] || [];
441
+ const target = `${spec.chart}${spec.variant ? ` 的 ${spec.variant} 变体` : ""}`;
442
+ if (kind.length !== 1 || !ALL_EMPHASIS.includes(kind[0]) || !Array.isArray(values)) err(errors, "/emphasis", "强调须在 rows、fields、series 中选一种身份数组");
443
+ else if (!supported.includes(kind[0])) err(errors, "/emphasis", supported.length ? `${target}只绘制 ${supported.join("、")} 强调` : `${target}不绘制强调`, supported.length ? `改用 ${supported.join(" 或 ")}` : "删去 emphasis;改用 line、area、bar、column、share 或 funnel,或用标题、注释指出重点");
444
+ else {
445
+ const enc = spec.encode || {};
446
+ let actual = [];
447
+ if (kind[0] === "rows" && Array.isArray(spec.data) && enc.id) actual = spec.data.map((row) => row?.[enc.id]);
448
+ if (kind[0] === "fields" && Array.isArray(enc.value)) actual = enc.value;
449
+ if (kind[0] === "series" && Array.isArray(spec.data) && enc.series) actual = spec.data.map((row) => row?.[enc.series]);
450
+ if (kind[0] === "series" && Array.isArray(enc.value)) actual = enc.value;
451
+ if (!actual.length || values.some((value) => !actual.includes(value)) || new Set(values).size !== values.length) err(errors, "/emphasis", `强调引用不存在或重复的${kind[0]}`);
452
+ if (kind[0] === "rows" && !enc.id || kind[0] === "fields" && !Array.isArray(enc.value)) err(errors, "/emphasis", `该图型不支持${kind[0]}强调`);
453
+ }
454
+ }
455
+ return {
456
+ errors,
457
+ data: errors.length ? null : spec
458
+ };
459
+ }
460
+ function checkGeoRows(spec, chart, errors) {
461
+ const { encode, data: { rows } } = spec;
462
+ for (const [i, row] of rows.entries()) {
463
+ const path = `/data/rows/${i}`;
464
+ if (!obj(row)) {
465
+ err(errors, path, "地图记录必须是对象");
466
+ continue;
467
+ }
468
+ for (const role of [...chart.required, ...chart.optional]) {
469
+ const field = encode[role];
470
+ if (field === void 0) continue;
471
+ if (!Object.hasOwn(row, field)) err(errors, `/encode/${role}`, `引用字段不存在:${field}`);
472
+ else if ([
473
+ "longitude",
474
+ "latitude",
475
+ "value"
476
+ ].includes(role)) {
477
+ const value = row[field];
478
+ const fieldPath = ptr([
479
+ "data",
480
+ "rows",
481
+ i,
482
+ field
483
+ ]);
484
+ if (!finite(value) && !(role === "value" && value === null)) err(errors, fieldPath, "数值角色必须是有限数");
485
+ else if (role === "longitude" && Math.abs(value) > 180 || role === "latitude" && Math.abs(value) > 90) err(errors, fieldPath, "经纬度超出合法范围");
486
+ }
487
+ }
488
+ }
489
+ }
490
+ function checkTable(s, c, e) {
491
+ const enc = s.encode, names = [];
492
+ for (const [role, val] of Object.entries(enc)) names.push(...Array.isArray(val) ? val : typeof val === "string" ? [val] : []);
493
+ if (s.chart === "combo" && Array.isArray(s.layers)) for (const l of s.layers) names.push(l.field);
494
+ const fields = Object.assign(Object.create(null), Object.fromEntries(Object.entries(obj(s.fields) ? s.fields : {}).filter(([, field]) => obj(field))));
495
+ for (const name of names) if (!s.data.every((row) => Object.hasOwn(row, name))) err(e, "/encode", `引用字段不存在:${name}`, "请修正 encode 字段名。");
496
+ for (const [i, row] of s.data.entries()) for (const [k, value] of Object.entries(row)) if (!(value === null || typeof value === "string" || typeof value === "boolean" || finite(value) || Array.isArray(value) && ["density", "outliers"].some((role) => s.encode?.[role] === k))) err(e, `/data/${i}/${k}`, "记录值必须是标量或图型明确允许的数组");
497
+ for (const [name, meta] of Object.entries(fields)) if (!s.data.some((row) => Object.hasOwn(row, name))) err(e, `/fields/${name}`, "元数据字段不在数据中");
498
+ validateFieldMeta(s, fields, enc, e);
499
+ for (const [role, name] of Object.entries(enc)) if (typeof name === "string" && (role === "date" || fields[name] && [
500
+ "date",
501
+ "month",
502
+ "datetime"
503
+ ].includes(fields[name].type))) {
504
+ for (const [i, row] of s.data.entries()) if (!safeDate(row[name], role === "date" ? "date" : fields[name].type)) err(e, `/data/${i}/${name}`, "非法日期或时间值");
505
+ }
506
+ const nonNumericRoles = /* @__PURE__ */ new Set([
507
+ "category",
508
+ "series",
509
+ "id",
510
+ "panel",
511
+ "label",
512
+ "row",
513
+ "column",
514
+ "date"
515
+ ]);
516
+ if (s.chart === "launch-fan" && [
517
+ "date",
518
+ "month",
519
+ "datetime"
520
+ ].includes(fields[enc.start]?.type)) nonNumericRoles.add("start");
521
+ if (s.chart === "launch-fan" && ![
522
+ "number",
523
+ "date",
524
+ "month",
525
+ "datetime"
526
+ ].includes(fields[enc.start]?.type)) err(e, `/fields/${enc.start}/type`, "launch-fan.start必须显式声明number或时间类型");
527
+ const values = [.../* @__PURE__ */ new Set([...Object.entries(enc).filter(([role]) => !nonNumericRoles.has(role)).flatMap(([, name]) => Array.isArray(name) ? name : typeof name === "string" ? [name] : []), ...s.chart === "combo" && Array.isArray(s.layers) ? s.layers.map((layer) => layer.field) : []])];
528
+ for (const [i, row] of s.data.entries()) for (const name of values) if (Object.hasOwn(row, name)) {
529
+ const v = row[name];
530
+ if (v === null && ![
531
+ "line",
532
+ "area",
533
+ "sparkline",
534
+ "small-multiples",
535
+ "bump",
536
+ "combo"
537
+ ].includes(s.chart)) err(e, `/data/${i}/${name}`, "该图型不允许数值缺失");
538
+ if (v !== null && !finite(v) && !(Array.isArray(v) && ["density", "outliers"].some((r) => enc[r] === name))) err(e, `/data/${i}/${name}`, "数值角色必须是有限JSON number或允许的数组");
539
+ const type = fields[name]?.type;
540
+ if (type && [
541
+ "date",
542
+ "month",
543
+ "datetime"
544
+ ].includes(type) && !safeDate(v, type)) err(e, `/data/${i}/${name}`, "非法日期或时间值");
545
+ if (fields[name]?.scale === "log" && finite(v) && v <= 0) err(e, `/data/${i}/${name}`, "log数值必须大于0");
546
+ }
547
+ for (const role of ["id"]) if (typeof enc[role] === "string") unique(s.data.map((r) => r[enc[role]]), `/encode/${role}`, e, true);
548
+ if (c.encodeForms.length && !c.encodeForms.some((form) => form.length === Object.keys(enc).filter((k) => k === "value" || k === "panel").length && form.every((k) => Object.hasOwn(enc, k)))) err(e, "/encode", "small-multiples须使用value[]宽表或value+panel长表");
549
+ if (s.chart === "small-multiples" && (enc.panel ? Array.isArray(enc.value) : !Array.isArray(enc.value))) err(e, "/encode", "宽表value须为字段数组;长表value须为单个字段并配panel");
550
+ if (Array.isArray(enc.value) && enc.value.length > 6) err(e, "/encode/value", "最多支持6个度量系列");
551
+ if (["radar", "parallel"].includes(s.chart) && (enc.dimensions?.length < 3 || enc.dimensions?.length > 6)) err(e, "/encode/dimensions", "radar至少3维,parallel为3至6维");
552
+ if (s.chart === "radar") {
553
+ for (const name of enc.dimensions || []) if (!fields[name]?.domain) err(e, `/fields/${name}/domain`, `${s.chart}的每个维度都必须显式声明domain`);
554
+ }
555
+ if (enc.category && !["strip", "scatter"].includes(s.chart)) {
556
+ const seen = /* @__PURE__ */ new Set();
557
+ for (const row of s.data) {
558
+ const key = [row[enc.category], enc.series ? row[enc.series] : s.chart === "small-multiples" ? row[enc.panel] : null];
559
+ const k = JSON.stringify(key);
560
+ if (seen.has(k)) err(e, "/data", "重复的category/series组合");
561
+ seen.add(k);
562
+ }
563
+ }
564
+ if ([
565
+ "interval",
566
+ "bullet",
567
+ "dumbbell",
568
+ "slope",
569
+ "candlestick"
570
+ ].includes(s.chart)) for (const [i, r] of s.data.entries()) {
571
+ const value = (role) => finite(r[enc[role]]) ? r[enc[role]] : NaN;
572
+ if (s.chart === "interval" && !(value("low") <= value("high") && (enc.value === void 0 || value("value") >= value("low") && value("value") <= value("high")))) err(e, `/data/${i}`, "interval须满足low≤value≤high");
573
+ if (s.chart === "candlestick" && !(value("low") <= Math.min(value("open"), value("close")) && Math.max(value("open"), value("close")) <= value("high"))) err(e, `/data/${i}`, "candlestick须满足low≤open/close≤high");
574
+ if (s.chart === "bullet" && [enc.actual, enc.target].some((field) => finite(r[field]) && r[field] < 0)) err(e, `/data/${i}`, "bullet数值须非负");
575
+ }
576
+ if (s.chart === "histogram") {
577
+ let previous = null;
578
+ for (const [i, row] of s.data.entries()) {
579
+ const lo = row[enc.lower], hi = row[enc.upper], count = row[enc.count];
580
+ if (!(finite(lo) && finite(hi) && lo < hi && (previous === null || lo === previous) && finite(count) && count >= 0)) err(e, `/data/${i}`, "histogram箱须连续精确接壤、递增且count非负");
581
+ previous = hi;
582
+ }
583
+ if (s.variant === "rungs" && s.data.length > 1 && s.data.every((row) => finite(row[enc.lower]) && finite(row[enc.upper]))) {
584
+ const width = s.data[0][enc.upper] - s.data[0][enc.lower];
585
+ if (s.data.some((row) => Math.abs(row[enc.upper] - row[enc.lower] - width) > Math.max(1, Math.abs(width)) * 1e-9)) err(e, "/data", "histogram rungs要求等宽分箱;不等宽时请使用默认实心直方图");
586
+ }
587
+ }
588
+ if (s.chart === "calendar") {
589
+ const dates = /* @__PURE__ */ new Set();
590
+ for (const [i, row] of s.data.entries()) {
591
+ const date = row[enc.date];
592
+ if (typeof date === "string" && date.slice(0, 4) !== s.period) err(e, `/data/${i}/${enc.date}`, "calendar日期须属于period指定年份");
593
+ if (dates.has(date)) err(e, `/data/${i}/${enc.date}`, "calendar日期重复");
594
+ dates.add(date);
595
+ }
596
+ }
597
+ if (["line", "area"].includes(s.chart) && (enc.lower || enc.upper)) {
598
+ if (!enc.lower || !enc.upper || Array.isArray(enc.value) || enc.series || s.stack) err(e, "/encode", "区间lower/upper仅支持非堆叠单度量");
599
+ if (enc.lower && enc.upper) for (const [i, row] of s.data.entries()) {
600
+ const lo = row[enc.lower], hi = row[enc.upper], value = row[enc.value];
601
+ if (!(lo === null && hi === null && value === null || finite(lo) && finite(hi) && finite(value) && lo <= value && value <= hi)) err(e, `/data/${i}`, "lower/value/upper须同时缺失或满足lower≤value≤upper");
602
+ }
603
+ }
604
+ if (s.chart === "heatmap" || s.chart === "bubble-matrix") {
605
+ const cells = /* @__PURE__ */ new Set();
606
+ for (const [i, row] of s.data.entries()) {
607
+ const cell = JSON.stringify([row[enc.row], row[enc.column]]);
608
+ if (cells.has(cell)) err(e, `/data/${i}`, "矩阵单元格重复");
609
+ cells.add(cell);
610
+ }
611
+ }
612
+ if (["boxplot", "violin"].includes(s.chart)) checkStats(s, e);
613
+ checkTableLimits(s, e);
614
+ const valueFields = Array.isArray(enc.value) ? enc.value : typeof enc.value === "string" ? [enc.value] : [];
615
+ if (s.stack === "percent" && valueFields.some((f) => s.data.some((r) => finite(r[f]) && r[f] < 0))) err(e, "/data", "percent stack不允许负值");
616
+ if ([
617
+ "share",
618
+ "pie",
619
+ "donut",
620
+ "radial-share",
621
+ "rose",
622
+ "funnel",
623
+ "waffle",
624
+ "tally",
625
+ "bullet",
626
+ "stream"
627
+ ].includes(s.chart) || [
628
+ "bar",
629
+ "column",
630
+ "dumbbell"
631
+ ].includes(s.chart) && ["rungs", "pictorial"].includes(s.variant)) {
632
+ for (const f of valueFields) if (s.data.some((r) => finite(r[f]) && r[f] < 0)) err(e, "/data", "该图型或变体数值不可为负");
633
+ }
634
+ if (s.chart === "bump") {
635
+ for (const [i, row] of s.data.entries()) if (row[enc.rank] !== null && (!Number.isInteger(row[enc.rank]) || row[enc.rank] < 1)) err(e, `/data/${i}/${enc.rank}`, "rank必须是正整数或null");
636
+ }
637
+ if (s.chart === "launch-fan" && s.data.some((row) => finite(row[enc.value]) && row[enc.value] < 0)) err(e, "/data", "launch-fan当前量必须非负");
638
+ if (s.chart === "bubble-matrix" && s.data.some((row) => finite(row[enc.value]) && row[enc.value] < 0)) err(e, "/data", "气泡矩阵面积值必须非负");
639
+ if (s.chart === "radial-share") for (const role of ["value", "intensity"]) {
640
+ const name = enc[role], meta = fields[name];
641
+ if (!obj(meta) || meta.unit === void 0 && meta.format !== "percent") err(e, `/fields/${name}`, "份额和强度必须分别声明单位或比例格式");
642
+ if (s.data.some((row) => finite(row[name]) && row[name] < 0) || !s.data.some((row) => finite(row[name]) && row[name] > 0)) err(e, `/encode/${role}`, "份额和强度须非负且各至少一个正值");
643
+ if (meta?.domain && meta.domain[0] !== 0) err(e, `/fields/${name}/domain`, "强度/份额范围必须从0开始");
644
+ }
645
+ if (s.chart === "heatmap" && s.variant === "arc") {
646
+ const meta = fields[enc.value];
647
+ if (meta?.format !== "percent" && !(Array.isArray(meta?.domain) && meta.domain[0] === 0 && meta.domain[1] > 0)) err(e, `/fields/${enc.value}`, "arc须使用比例格式或从0开始的数值范围");
648
+ if (s.data.some((row) => finite(row[enc.value]) && (row[enc.value] < 0 || meta?.format === "percent" && row[enc.value] > 1))) err(e, "/data", "arc数值必须位于0至整弧范围");
649
+ }
650
+ if (s.chart === "combo" && Array.isArray(s.layers)) {
651
+ if (s.layers.length > 6 || !s.layers.some((layer) => layer.axis === "left")) err(e, "/layers", "combo最多6层且必须有left轴");
652
+ unique(s.layers.map((layer) => layer.field), "/layers/field", e, true);
653
+ for (const [i, layer] of s.layers.entries()) keys(layer, /* @__PURE__ */ new Set([
654
+ "mark",
655
+ "field",
656
+ "axis"
657
+ ]), `/layers/${i}`, e);
658
+ }
659
+ if (s.chart === "dumbbell" && s.variant === "rungs") {
660
+ for (const role of ["before", "after"]) if (s.data.some((r) => finite(r[enc[role]]) && r[enc[role]] < 0)) err(e, "/data", `rungs的${role}须非负`);
661
+ }
662
+ if (s.chart === "tally") {
663
+ if (fields[enc.value]?.format !== "percent") err(e, `/fields/${enc.value}/format`, "tally数值字段必须format=percent");
664
+ if (s.data.some((r) => !finite(r[enc.value]) || r[enc.value] < 0 || r[enc.value] > 1)) err(e, "/data", "tally每项须在0至1之间");
665
+ }
666
+ if (s.chart === "funnel") {
667
+ let previous = Infinity;
668
+ for (const [i, row] of s.data.entries()) {
669
+ const value = row[enc.value];
670
+ if (!finite(value) || value < 0 || value > previous || i === 0 && value <= 0) err(e, `/data/${i}/${enc.value}`, "funnel须非负递减且首步大于0");
671
+ if (finite(value)) previous = value;
672
+ }
673
+ }
674
+ if (s.chart === "gauge") {
675
+ if (s.data.length !== 1) err(e, "/data", "gauge必须恰有一条记录");
676
+ else if (Array.isArray(s.range) && s.range.length === 2 && s.range.every(finite)) for (const role of ["value", "target"]) {
677
+ const value = s.data[0][enc[role]];
678
+ if (enc[role] && finite(value) && (value < s.range[0] || value > s.range[1])) err(e, `/data/0/${enc[role]}`, "gauge值与target必须位于range内");
679
+ }
680
+ }
681
+ if (["scatter", "strip"].includes(s.chart) && enc.size && s.data.some((r) => finite(r[enc.size]) && r[enc.size] < 0)) err(e, "/data", "size必须非负");
682
+ if (s.chart === "radar" && valueFields.length && s.data.some((r) => finite(r[enc.value]) && r[enc.value] < 0)) err(e, "/data", "radar维度值须非负");
683
+ }
684
+ function validateFieldMeta(s, fields, enc, e) {
685
+ const numeric = /* @__PURE__ */ new Set([...Object.entries(enc).flatMap(([role, value]) => role === "dimensions" && Array.isArray(value) ? value : typeof value === "string" && ![
686
+ "category",
687
+ "series",
688
+ "id",
689
+ "panel",
690
+ "label",
691
+ "row",
692
+ "column",
693
+ "date"
694
+ ].includes(role) ? [value] : Array.isArray(value) ? value : []), ...s.chart === "combo" && Array.isArray(s.layers) ? s.layers.map((layer) => layer.field) : []]);
695
+ const categorical = new Set([
696
+ "category",
697
+ "series",
698
+ "panel",
699
+ "label",
700
+ "row",
701
+ "column",
702
+ "date"
703
+ ].map((role) => enc[role]).filter((x) => typeof x === "string"));
704
+ const domainCharts = /* @__PURE__ */ new Set([
705
+ "line",
706
+ "area",
707
+ "bar",
708
+ "column",
709
+ "sparkline",
710
+ "small-multiples",
711
+ "combo",
712
+ "split-compare",
713
+ "scatter",
714
+ "strip",
715
+ "interval",
716
+ "bullet",
717
+ "dumbbell",
718
+ "slope",
719
+ "histogram",
720
+ "boxplot",
721
+ "violin",
722
+ "candlestick",
723
+ "parallel",
724
+ "radar",
725
+ "heatmap",
726
+ "bubble-matrix",
727
+ "radial-share",
728
+ "bump"
729
+ ]);
730
+ for (const [name, f] of Object.entries(fields)) {
731
+ if (f.domain !== void 0) {
732
+ if (!domainCharts.has(s.chart)) err(e, `/fields/${name}/domain`, "该图型不支持domain");
733
+ if (s.sort && name === enc.category) err(e, "/sort", "分类domain已确定顺序,不能同时sort");
734
+ if (Array.isArray(f.domain) && f.domain.every(finite) && [
735
+ "bar",
736
+ "column",
737
+ "area",
738
+ "bullet",
739
+ "split-compare"
740
+ ].includes(s.chart) && f.domain[0] > 0) err(e, `/fields/${name}/domain`, "面积和柱条度量范围必须包含0");
741
+ if (Array.isArray(f.domain) && f.domain.every(finite) && Array.isArray(s.annotations)) for (const a of s.annotations) {
742
+ if (!obj(a) || a.field !== name || !["rule", "band"].includes(a.kind)) continue;
743
+ if ((a.kind === "rule" ? [a.value] : [a.from, a.to]).some((v) => finite(v) && (v < f.domain[0] || v > f.domain[1]))) err(e, `/fields/${name}/domain`, "domain不得裁剪参考线或参考区间");
744
+ }
745
+ if (Array.isArray(f.domain) && f.domain.length === 2 && f.domain.every(finite)) {
746
+ if (!numeric.has(name)) err(e, `/fields/${name}/domain`, "数值domain只能用于数值角色");
747
+ for (const row of s.data) if (finite(row[name]) && (row[name] < f.domain[0] || row[name] > f.domain[1])) err(e, `/fields/${name}/domain`, "domain不得裁剪数据");
748
+ } else {
749
+ if (!categorical.has(name)) err(e, `/fields/${name}/domain`, "分类domain只能用于分类角色");
750
+ if (Array.isArray(f.domain) && s.data.some((row) => !f.domain.includes(row[name]))) err(e, `/fields/${name}/domain`, "domain必须包含所有实际分类");
751
+ }
752
+ }
753
+ if (f.scale !== void 0) {
754
+ if (!numeric.has(name) || !domainCharts.has(s.chart)) err(e, `/fields/${name}/scale`, "scale仅允许适用图型的数值角色");
755
+ if (f.scale === "log" && ![
756
+ "line",
757
+ "scatter",
758
+ "parallel"
759
+ ].includes(s.chart)) err(e, `/fields/${name}/scale`, "log仅支持line/scatter/parallel");
760
+ if (f.scale === "log" && (s.data.some((row) => finite(row[name]) && row[name] <= 0) || Array.isArray(f.domain) && f.domain.some((v) => finite(v) && v <= 0))) err(e, `/fields/${name}/scale`, "log数据与domain必须大于0");
761
+ if (f.scale === "log" && s.stack) err(e, `/fields/${name}/scale`, "log坐标不支持堆叠");
762
+ }
763
+ if (f.type === "number" && !numeric.has(name) || f.type === "category" && !categorical.has(name)) err(e, `/fields/${name}/type`, "字段type与编码角色不相符");
764
+ }
765
+ const grouped = [...Array.isArray(enc.value) ? enc.value : typeof enc.value === "string" ? [enc.value] : []];
766
+ if (s.chart === "bullet") grouped.push(enc.actual, enc.target);
767
+ if (["dumbbell", "slope"].includes(s.chart)) grouped.push(enc.before, enc.after);
768
+ if (s.chart === "interval") grouped.push(enc.low, enc.high, enc.value);
769
+ if (s.chart === "candlestick") grouped.push(enc.open, enc.close, enc.low, enc.high);
770
+ const rootUnit = typeof s.unit === "string" ? s.unit : "";
771
+ const sig = (name) => {
772
+ const { unit, format } = fields[name] || {};
773
+ return JSON.stringify([typeof unit === "string" ? unit : rootUnit, format === "percent" ? "percent" : "number"]);
774
+ };
775
+ if (grouped.filter(Boolean).length > 1 && new Set(grouped.filter(Boolean).map(sig)).size > 1) err(e, "/fields", "同一坐标轴的可比较度量必须单位和format一致");
776
+ if (s.chart === "combo" && Array.isArray(s.layers)) for (const axis of ["left", "right"]) {
777
+ const names = s.layers.filter((l) => l.axis === axis).map((l) => l.field);
778
+ if (new Set(names.map(sig)).size > 1) err(e, "/layers", `${axis}轴的字段必须单位和format一致`);
779
+ }
780
+ }
781
+ function checkTableLimits(s, e) {
782
+ const enc = s.encode, rows = s.data, uniq = (field) => new Set(rows.map((r) => r[field])).size;
783
+ const categories = enc.category ? uniq(enc.category) : 0;
784
+ const limits = {
785
+ funnel: 6,
786
+ share: 6,
787
+ pie: 6,
788
+ donut: 6,
789
+ "radial-share": 6,
790
+ rose: 6,
791
+ waffle: 6,
792
+ tally: 6,
793
+ bullet: 12,
794
+ dumbbell: 12,
795
+ interval: 12,
796
+ boxplot: 12,
797
+ violin: 12,
798
+ slope: 8,
799
+ waterfall: 16,
800
+ histogram: 40,
801
+ scatter: 1e3,
802
+ strip: 1e3,
803
+ calendar: 366,
804
+ "candlestick": 120,
805
+ "launch-fan": 24
806
+ };
807
+ if (limits[s.chart] !== void 0 && rows.length > limits[s.chart]) err(e, "/data", `${s.chart}最多${limits[s.chart]}条记录`);
808
+ if (["bar", "column"].includes(s.chart)) {
809
+ if (categories > 60 || s.chart === "bar" && categories > 24) err(e, "/data", `${s.chart}类别数超限`);
810
+ if (categories > 24 && (s.chart !== "column" || Array.isArray(enc.value) && enc.value.length !== 1 || enc.series || !["solid", void 0].includes(s.variant))) err(e, "/data", "25至60类仅支持单系列solid通栏column");
811
+ }
812
+ if ([
813
+ "line",
814
+ "area",
815
+ "sparkline",
816
+ "small-multiples",
817
+ "stream",
818
+ "split-compare",
819
+ "combo"
820
+ ].includes(s.chart) && categories > 120) err(e, "/data", "时点最多120");
821
+ if (s.chart === "small-multiples") {
822
+ if ((enc.panel ? uniq(enc.panel) : Array.isArray(enc.value) ? enc.value.length : 0) > 6) err(e, "/data", "small-multiples最多6面板");
823
+ if (enc.panel) {
824
+ const counts = /* @__PURE__ */ new Map();
825
+ for (const r of rows) counts.set(r[enc.panel], (counts.get(r[enc.panel]) || 0) + 1);
826
+ for (const n of counts.values()) if (n > 120) err(e, "/data", "每个small-multiples面板最多120时点");
827
+ }
828
+ }
829
+ if (["heatmap", "bubble-matrix"].includes(s.chart) && (uniq(enc.row) > 24 || uniq(enc.column) > 24)) err(e, "/data", "矩阵每轴最多24类");
830
+ if (s.chart === "radar" || s.chart === "parallel") {
831
+ if (rows.length > 24) err(e, "/data", "多维图最多24实体");
832
+ }
833
+ if (s.chart === "bump" && (uniq(enc.series) > 12 || uniq(enc.category) > 24)) err(e, "/data", "bump最多12实体、24时点");
834
+ }
835
+ function unique(arr, path, e, nonempty = false) {
836
+ const set = /* @__PURE__ */ new Set();
837
+ for (const x of arr) {
838
+ if (nonempty && typeof x !== "string" || x === "" || x === null || x === void 0) err(e, path, "身份必须是非空字符串");
839
+ else if (set.has(x)) err(e, path, `重复身份:${x}`);
840
+ set.add(x);
841
+ }
842
+ }
843
+ function checkStats(s, e) {
844
+ const en = s.encode;
845
+ for (const [i, row] of s.data.entries()) if (s.chart === "boxplot") {
846
+ const values = [
847
+ "low",
848
+ "q1",
849
+ "median",
850
+ "q3",
851
+ "high"
852
+ ].map((key) => row[en[key]]);
853
+ if (values.every(finite) && !(values[0] <= values[1] && values[1] <= values[2] && values[2] <= values[3] && values[3] <= values[4])) err(e, `/data/${i}`, "箱线统计值必须low≤q1≤median≤q3≤high");
854
+ if (en.outliers && Array.isArray(row[en.outliers])) {
855
+ if (row[en.outliers].length > 200) err(e, `/data/${i}/${en.outliers}`, "每组最多200个异常点");
856
+ if (values.every(finite) && row[en.outliers].some((value) => !finite(value) || value >= values[0] && value <= values[4])) err(e, `/data/${i}/${en.outliers}`, "异常值必须严格位于须线之外");
857
+ }
858
+ } else {
859
+ const density = row[en.density], median = row[en.median];
860
+ if (!Array.isArray(density) || density.length < 2 || density.length > 120 || density.some((point) => !Array.isArray(point) || point.length !== 2 || !point.every(finite) || point[1] < 0) || density.some((point, index) => index && point[0] <= density[index - 1][0]) || !finite(median) || density.length && !(median >= density[0][0] && median <= density.at(-1)[0])) {
861
+ err(e, `/data/${i}`, "violin须提供2至120个递增的有限密度采样与范围内中位数");
862
+ continue;
863
+ }
864
+ let integral = 0;
865
+ for (let j = 1; j < density.length; j++) integral += (density[j][0] - density[j - 1][0]) * (density[j][1] + density[j - 1][1]) / 2;
866
+ if (!(integral > 0)) err(e, `/data/${i}/${en.density}`, "violin密度积分必须大于0");
867
+ }
868
+ }
869
+ function checkTree(rootNode, chart, e, budget = {}) {
870
+ const maxDepth = budget.depth ?? 5, maxNodes = budget.nodes ?? 80;
871
+ const ids = /* @__PURE__ */ new Set(), seen = /* @__PURE__ */ new Set();
872
+ let count = 0;
873
+ const walk = (node, path, depth) => {
874
+ if (!obj(node)) {
875
+ err(e, path, "树节点必须是对象");
876
+ return 0;
877
+ }
878
+ if (seen.has(node)) {
879
+ err(e, path, "树不允许共享节点或环");
880
+ return finite(node.value) ? node.value : 0;
881
+ }
882
+ seen.add(node);
883
+ if (depth > maxDepth) {
884
+ err(e, path, "超过树深度上限");
885
+ return finite(node.value) ? node.value : 0;
886
+ }
887
+ count++;
888
+ if (typeof node.label !== "string" || !node.label) err(e, `${path}/label`, "节点label必填");
889
+ for (const key of Object.keys(node)) if (![
890
+ "label",
891
+ "children",
892
+ "value",
893
+ "id",
894
+ "icon",
895
+ "note",
896
+ ...["mindmap", "tree"].includes(chart) ? ["status", "shape"] : []
897
+ ].includes(key)) err(e, `${path}/${key}`, "未知节点字段");
898
+ if (node.shape !== void 0 && ["mindmap", "tree"].includes(chart) && !NODE_SHAPES.includes(node.shape)) err(e, `${path}/shape`, "未知节点形状");
899
+ if (node.status !== void 0 && ["mindmap", "tree"].includes(chart) && ![
900
+ "new",
901
+ "info",
902
+ "ok",
903
+ "warn",
904
+ "danger",
905
+ "ext"
906
+ ].includes(node.status)) err(e, `${path}/status`, "无效节点状态");
907
+ if (node.id !== void 0) {
908
+ if (typeof node.id !== "string" || !node.id || ids.has(node.id)) err(e, `${path}/id`, "节点ID须唯一非空");
909
+ ids.add(node.id);
910
+ }
911
+ if (node.value !== void 0 && (!finite(node.value) || node.value < 0)) err(e, `${path}/value`, "节点权重须为非负有限数");
912
+ if (node.children !== void 0 && !Array.isArray(node.children)) err(e, `${path}/children`, "children须为数组");
913
+ const children = Array.isArray(node.children) ? node.children : [];
914
+ const sum = children.reduce((total, child, i) => total + walk(child, `${path}/children/${i}`, depth + 1), 0);
915
+ if (children.length && node.value !== void 0 && finite(node.value) && Math.abs(node.value - sum) > 1e-9 * Math.max(1, node.value, sum)) err(e, `${path}/value`, "父节点value必须等于子树叶子合计");
916
+ if (["treemap", "sunburst"].includes(chart) && !children.length && (!finite(node.value) || node.value < 0)) err(e, path, "层级面积图叶子必须有非负value");
917
+ return finite(node.value) ? node.value : sum;
918
+ };
919
+ walk(rootNode, "/data", 1);
920
+ if (count > maxNodes) err(e, "/data", `超过${maxNodes}节点上限`);
921
+ }
922
+ function checkNetwork(d, chart, e, tolerant = false) {
923
+ const rawNodes = d.nodes || [], rawEdges = d.edges || [];
924
+ rawNodes.forEach((n, i) => {
925
+ if (!obj(n)) err(e, `/data/nodes/${i}`, "节点必须是对象");
926
+ });
927
+ rawEdges.forEach((x, i) => {
928
+ if (!obj(x)) err(e, `/data/edges/${i}`, "边必须是对象");
929
+ });
930
+ const nodes = rawNodes.filter(obj), edges = rawEdges.filter(obj);
931
+ const ids = new Set(nodes.map((n) => n.id));
932
+ unique(nodes.map((n) => n?.id), "/data/nodes/id", e, true);
933
+ for (const [i, n] of rawNodes.entries()) {
934
+ if (!obj(n)) continue;
935
+ for (const k of Object.keys(n)) if (![
936
+ "id",
937
+ "label",
938
+ "group",
939
+ "value",
940
+ "stage",
941
+ "icon",
942
+ "note"
943
+ ].includes(k)) err(e, `/data/nodes/${i}/${k}`, "未知节点字段");
944
+ if (n.label !== void 0 && typeof n.label !== "string") err(e, `/data/nodes/${i}/label`, "label须为字符串");
945
+ if (n.value !== void 0 && !finite(n.value)) err(e, `/data/nodes/${i}/value`, "value须为有限数");
946
+ if (chart === "sankey" && n.value !== void 0) err(e, `/data/nodes/${i}/value`, "sankey禁用node.value");
947
+ if (n.stage !== void 0 && (!Number.isInteger(n.stage) || n.stage < 0)) err(e, `/data/nodes/${i}/stage`, "stage须为非负整数");
948
+ }
949
+ const edgeIds = [];
950
+ for (const [i, x] of rawEdges.entries()) {
951
+ if (!obj(x)) continue;
952
+ for (const k of Object.keys(x)) if (![
953
+ "source",
954
+ "target",
955
+ "id",
956
+ "label",
957
+ "value"
958
+ ].includes(k)) err(e, `/data/edges/${i}/${k}`, "未知边字段");
959
+ if (!ids.has(x.source) || !ids.has(x.target)) err(e, `/data/edges/${i}`, "边必须引用已声明节点");
960
+ if (x.label !== void 0 && typeof x.label !== "string") err(e, `/data/edges/${i}/label`, "label须为字符串");
961
+ if (x.id !== void 0) edgeIds.push(x.id);
962
+ if (chart === "sankey" && (!finite(x.value) || x.value <= 0 || x.source === x.target)) err(e, `/data/edges/${i}/value`, "Sankey边值须为正且不可自环");
963
+ else if (chart === "network" && x.value !== void 0 && !finite(x.value)) err(e, `/data/edges/${i}/value`, "边value须为有限数");
964
+ }
965
+ unique(edgeIds, "/data/edges/id", e, true);
966
+ const pairCounts = /* @__PURE__ */ new Map();
967
+ for (const x of edges) {
968
+ const key = JSON.stringify([x.source, x.target]);
969
+ pairCounts.set(key, (pairCounts.get(key) || 0) + 1);
970
+ }
971
+ for (const [i, x] of rawEdges.entries()) if (obj(x) && pairCounts.get(JSON.stringify([x.source, x.target])) > 1 && x.id === void 0) err(e, `/data/edges/${i}/id`, "平行边必须提供唯一id");
972
+ if (chart === "sankey") {
973
+ const stage = new Map(nodes.map((n) => [n.id, n.stage]));
974
+ const specified = nodes.filter((n) => n.stage !== void 0).length;
975
+ if (specified && specified !== nodes.length) err(e, "/data/nodes", "stage不可部分指定");
976
+ const ins = /* @__PURE__ */ new Map(), outs = /* @__PURE__ */ new Map(), indegree = new Map(nodes.map((n) => [n.id, 0])), next = new Map(nodes.map((n) => [n.id, []]));
977
+ for (const x of edges) {
978
+ if (specified && !(Number.isInteger(stage.get(x.source)) && Number.isInteger(stage.get(x.target)) && stage.get(x.target) > stage.get(x.source))) err(e, "/data/edges", "Sankey边必须指向更高stage");
979
+ if (finite(x.value)) {
980
+ ins.set(x.target, (ins.get(x.target) || 0) + x.value);
981
+ outs.set(x.source, (outs.get(x.source) || 0) + x.value);
982
+ }
983
+ if (ids.has(x.source) && ids.has(x.target)) {
984
+ next.get(x.source).push(x.target);
985
+ indegree.set(x.target, (indegree.get(x.target) || 0) + 1);
986
+ }
987
+ }
988
+ const ready = [...indegree].filter(([, n]) => n === 0).map(([id]) => id);
989
+ let visited = 0;
990
+ while (ready.length) {
991
+ const id = ready.pop();
992
+ visited++;
993
+ for (const to of next.get(id)) {
994
+ indegree.set(to, indegree.get(to) - 1);
995
+ if (indegree.get(to) === 0) ready.push(to);
996
+ }
997
+ }
998
+ if (visited !== nodes.length) err(e, "/data/edges", "Sankey必须是DAG,不允许环");
999
+ for (const n of nodes) {
1000
+ const a = ins.get(n.id) || 0, b = outs.get(n.id) || 0;
1001
+ if (!tolerant && a && b && Math.abs(a - b) > 1e-9 * Math.max(1, a, b)) err(e, "/data/nodes", "Sankey中间节点流量不守恒");
1002
+ }
1003
+ }
1004
+ }
1005
+ function checkPaths(d, e) {
1006
+ const stages = d.stages.filter(obj), nodes = d.nodes.filter(obj), paths = d.paths.filter(obj);
1007
+ for (const [i, x] of d.stages.entries()) if (!obj(x)) err(e, `/data/stages/${i}`, "stage必须是对象");
1008
+ for (const [i, x] of d.nodes.entries()) if (!obj(x)) err(e, `/data/nodes/${i}`, "path node必须是对象");
1009
+ for (const [i, x] of d.paths.entries()) if (!obj(x)) err(e, `/data/paths/${i}`, "path必须是对象");
1010
+ unique(stages.map((x) => x.id), "/data/stages/id", e, true);
1011
+ unique(nodes.map((x) => x.id), "/data/nodes/id", e, true);
1012
+ unique(paths.map((x) => x.id), "/data/paths/id", e, true);
1013
+ stages.forEach((x, i) => {
1014
+ for (const k of Object.keys(x)) if (!["id", "label"].includes(k)) err(e, `/data/stages/${i}/${k}`, "未知stage字段");
1015
+ if (typeof x.label !== "string") err(e, `/data/stages/${i}/label`, "label必填");
1016
+ });
1017
+ const stage = new Map(stages.map((x, i) => [x.id, i])), nodeMap = new Map(nodes.map((x) => [x.id, x]));
1018
+ nodes.forEach((n, i) => {
1019
+ for (const k of Object.keys(n)) if (![
1020
+ "id",
1021
+ "label",
1022
+ "stage"
1023
+ ].includes(k)) err(e, `/data/nodes/${i}/${k}`, "未知路径节点字段");
1024
+ if (!stage.has(n.stage)) err(e, `/data/nodes/${i}/stage`, "节点stage不存在");
1025
+ if (typeof n.label !== "string") err(e, `/data/nodes/${i}/label`, "label必填");
1026
+ });
1027
+ for (const [i, p] of d.paths.entries()) if (obj(p)) {
1028
+ for (const k of Object.keys(p)) if (![
1029
+ "id",
1030
+ "nodes",
1031
+ "value",
1032
+ "note"
1033
+ ].includes(k)) err(e, `/data/paths/${i}/${k}`, "未知path字段");
1034
+ if (p.note !== void 0 && typeof p.note !== "string") err(e, `/data/paths/${i}/note`, "note须为字符串");
1035
+ if (!Array.isArray(p.nodes) || p.nodes.length < 2) {
1036
+ err(e, `/data/paths/${i}/nodes`, "路径至少包含两个节点");
1037
+ continue;
1038
+ }
1039
+ let prev = -1;
1040
+ for (const id of p.nodes) {
1041
+ const n = nodeMap.get(id), at = stage.get(n?.stage);
1042
+ if (!n || at === void 0 || at <= prev) {
1043
+ err(e, `/data/paths/${i}/nodes`, "路径节点须按阶段严格前进");
1044
+ continue;
1045
+ }
1046
+ prev = at;
1047
+ }
1048
+ if (p.value !== void 0 && (!finite(p.value) || p.value <= 0)) err(e, `/data/paths/${i}/value`, "路径权重须为正有限数");
1049
+ }
1050
+ }
1051
+ function location(text, offset) {
1052
+ const before = text.slice(0, offset);
1053
+ return {
1054
+ offset,
1055
+ line: before.split("\n").length,
1056
+ column: offset - before.lastIndexOf("\n")
1057
+ };
1058
+ }
1059
+ function checkDuplicateKeys(text, errors) {
1060
+ const stack = [];
1061
+ for (const match of text.matchAll(/"(?:[^"\\]|\\.)*"|[{}\[\]:,]|[^\s{}\[\]:,"]+/g)) {
1062
+ const [token] = match;
1063
+ const frame = stack.at(-1);
1064
+ if (token === "}" || token === "]") stack.pop();
1065
+ else if (token === ",") {
1066
+ if (frame.keys) frame.key = void 0;
1067
+ } else if (token !== ":") {
1068
+ if (frame?.keys && frame.key === void 0) {
1069
+ const key = JSON.parse(token);
1070
+ if (frame.keys.has(key)) err(errors, ptr([...stack.slice(1).map((entry) => entry.at), key]), `重复 JSON 属性:${key}`, "删除重复属性并保留唯一值。", "BAD_KCHART", location(text, match.index));
1071
+ frame.keys.add(key);
1072
+ frame.key = key;
1073
+ } else {
1074
+ const at = frame?.keys ? frame.key : frame ? frame.next++ : void 0;
1075
+ if (token === "{") stack.push({
1076
+ at,
1077
+ keys: /* @__PURE__ */ new Set()
1078
+ });
1079
+ if (token === "[") stack.push({
1080
+ at,
1081
+ next: 0
1082
+ });
1083
+ }
1084
+ }
1085
+ }
1086
+ }
1087
+ function parse(text) {
1088
+ const errors = [];
1089
+ if (typeof text !== "string") {
1090
+ err(errors, "", "输入必须是 JSON 文本", "提供合法的 JSON 字符串。");
1091
+ return {
1092
+ errors,
1093
+ data: null
1094
+ };
1095
+ }
1096
+ let spec;
1097
+ try {
1098
+ spec = JSON.parse(text);
1099
+ } catch (cause) {
1100
+ if (!(cause instanceof SyntaxError)) throw cause;
1101
+ const offset = Number(/position (\d+)/.exec(cause.message)?.[1] ?? 0);
1102
+ err(errors, "", `JSON 语法错误:${cause.message}`, "修正 JSON 语法。", "BAD_KCHART", location(text, offset));
1103
+ return {
1104
+ errors,
1105
+ data: null
1106
+ };
1107
+ }
1108
+ checkDuplicateKeys(text, errors);
1109
+ return errors.length ? {
1110
+ errors,
1111
+ data: null
1112
+ } : validate(spec);
1113
+ }
1114
+ function schema(chart) {
1115
+ if (chart !== void 0 && (typeof chart !== "string" || !catalog[chart])) throw new TypeError("未知图型,请从图型目录中选择");
1116
+ const properties = {
1117
+ chart: {
1118
+ type: "string",
1119
+ enum: chart === void 0 ? Object.keys(catalog) : [chart]
1120
+ },
1121
+ title: { type: "string" },
1122
+ subtitle: { type: "string" },
1123
+ source: { type: "string" },
1124
+ note: { type: "string" },
1125
+ unit: {
1126
+ type: "string",
1127
+ not: { const: "%" }
1128
+ },
1129
+ variant: { type: "string" },
1130
+ layout: { type: "string" },
1131
+ data: {},
1132
+ encode: {
1133
+ type: "object",
1134
+ additionalProperties: { type: ["string", "array"] }
1135
+ },
1136
+ fields: {
1137
+ type: "object",
1138
+ additionalProperties: {
1139
+ type: "object",
1140
+ properties: {
1141
+ label: { type: "string" },
1142
+ type: { enum: [
1143
+ "number",
1144
+ "category",
1145
+ "date",
1146
+ "month",
1147
+ "datetime"
1148
+ ] },
1149
+ unit: { type: "string" },
1150
+ format: { enum: ["number", "percent"] },
1151
+ decimals: {
1152
+ type: "integer",
1153
+ minimum: 0,
1154
+ maximum: 6
1155
+ },
1156
+ domain: { type: "array" },
1157
+ scale: { enum: ["linear", "log"] }
1158
+ },
1159
+ additionalProperties: false
1160
+ }
1161
+ },
1162
+ sort: {
1163
+ type: "object",
1164
+ properties: {
1165
+ field: { type: "string" },
1166
+ direction: { enum: ["asc", "desc"] }
1167
+ },
1168
+ required: ["field", "direction"],
1169
+ additionalProperties: false
1170
+ },
1171
+ emphasis: { type: "object" },
1172
+ annotations: { type: "array" },
1173
+ interaction: {
1174
+ type: "array",
1175
+ items: { enum: [
1176
+ "inspect",
1177
+ "legend",
1178
+ "focus",
1179
+ "pan-zoom",
1180
+ "drag",
1181
+ "collapse"
1182
+ ] }
1183
+ },
1184
+ stack: { enum: ["absolute", "percent"] },
1185
+ unitMark: { type: "object" },
1186
+ range: {
1187
+ type: "array",
1188
+ minItems: 2,
1189
+ maxItems: 2,
1190
+ items: { type: "number" }
1191
+ },
1192
+ start: { type: "number" },
1193
+ layers: { type: "array" },
1194
+ period: {
1195
+ type: "string",
1196
+ pattern: "^\\d{4}$"
1197
+ },
1198
+ directed: { type: "boolean" }
1199
+ };
1200
+ const entries = Object.values(catalog).filter((c) => chart === void 0 || c.name === chart);
1201
+ const anyOf = entries.map((c) => {
1202
+ const base = {
1203
+ required: ["chart", "data"],
1204
+ properties: { chart: { const: c.name } }
1205
+ };
1206
+ if (c.shape === "table" || c.shape === "geo") {
1207
+ base.required.push("encode");
1208
+ base.properties.data = c.shape === "table" ? {
1209
+ type: "array",
1210
+ minItems: 1,
1211
+ items: { type: "object" }
1212
+ } : {
1213
+ type: "object",
1214
+ required: ["map", "rows"],
1215
+ properties: {
1216
+ map: { type: "object" },
1217
+ rows: { type: "array" }
1218
+ },
1219
+ additionalProperties: false
1220
+ };
1221
+ const roles = [...c.required, ...c.optional];
1222
+ const roleSchema = (role) => role === "dimensions" ? {
1223
+ type: "array",
1224
+ minItems: 3,
1225
+ items: { type: "string" }
1226
+ } : c.arrayRoles.includes(role) ? { oneOf: [{ type: "string" }, {
1227
+ type: "array",
1228
+ items: { type: "string" },
1229
+ minItems: 1
1230
+ }] } : { type: "string" };
1231
+ base.properties.encode = {
1232
+ type: "object",
1233
+ required: c.required,
1234
+ properties: Object.fromEntries(roles.map((role) => [role, roleSchema(role)])),
1235
+ additionalProperties: false
1236
+ };
1237
+ if (c.encodeForms.length) base.properties.encode.oneOf = c.encodeForms.map((form) => ({
1238
+ required: form,
1239
+ properties: { value: form.includes("panel") ? { type: "string" } : {
1240
+ type: "array",
1241
+ items: { type: "string" },
1242
+ minItems: 1
1243
+ } }
1244
+ }));
1245
+ } else {
1246
+ base.properties.encode = false;
1247
+ base.properties.data = c.shape === "tree" ? {
1248
+ type: "object",
1249
+ required: ["label"]
1250
+ } : c.shape === "network" ? {
1251
+ type: "object",
1252
+ required: ["nodes", "edges"],
1253
+ properties: {
1254
+ nodes: { type: "array" },
1255
+ edges: { type: "array" }
1256
+ },
1257
+ additionalProperties: false
1258
+ } : {
1259
+ type: "object",
1260
+ required: [
1261
+ "stages",
1262
+ "nodes",
1263
+ "paths"
1264
+ ],
1265
+ properties: {
1266
+ stages: { type: "array" },
1267
+ nodes: { type: "array" },
1268
+ paths: { type: "array" }
1269
+ },
1270
+ additionalProperties: false
1271
+ };
1272
+ }
1273
+ return base;
1274
+ });
1275
+ const schemaFor = (value) => Array.isArray(value) ? { enum: value } : value === "boolean" ? { type: "boolean" } : value === "number" ? { type: "number" } : value === "year" ? {
1276
+ type: "string",
1277
+ pattern: "^[0-9]{4}$"
1278
+ } : value === "range" ? {
1279
+ type: "array",
1280
+ minItems: 2,
1281
+ maxItems: 2,
1282
+ items: { type: "number" }
1283
+ } : value === "layers" ? {
1284
+ type: "array",
1285
+ items: {
1286
+ type: "object",
1287
+ required: [
1288
+ "mark",
1289
+ "field",
1290
+ "axis"
1291
+ ],
1292
+ properties: {
1293
+ mark: { enum: [
1294
+ "line",
1295
+ "area",
1296
+ "column"
1297
+ ] },
1298
+ field: { type: "string" },
1299
+ axis: { enum: ["left", "right"] }
1300
+ },
1301
+ additionalProperties: false
1302
+ }
1303
+ } : { type: "string" };
1304
+ const schema = {
1305
+ type: "object",
1306
+ properties,
1307
+ required: ["chart", "data"],
1308
+ additionalProperties: false,
1309
+ anyOf,
1310
+ allOf: entries.map((c) => {
1311
+ const special = Object.fromEntries(Object.entries(c.special).map(([k, v]) => [k, schemaFor(v)]));
1312
+ if (special.rings) special.rings = {
1313
+ type: "integer",
1314
+ minimum: 2,
1315
+ maximum: 8
1316
+ };
1317
+ for (const key of [
1318
+ "tolerant",
1319
+ "showValues",
1320
+ "rings",
1321
+ "graticule",
1322
+ "legend",
1323
+ "sliceLabels"
1324
+ ]) if (special[key]) properties[key] ??= special[key];
1325
+ else special[key] = false;
1326
+ if ([
1327
+ "bar",
1328
+ "column",
1329
+ "dumbbell",
1330
+ "waterfall",
1331
+ "histogram"
1332
+ ].includes(c.name)) special.unitMark = {
1333
+ type: "object",
1334
+ required: ["value", "label"],
1335
+ properties: {
1336
+ value: {
1337
+ type: "number",
1338
+ exclusiveMinimum: 0
1339
+ },
1340
+ label: {
1341
+ type: "string",
1342
+ minLength: 1
1343
+ },
1344
+ icon: { type: "string" }
1345
+ },
1346
+ additionalProperties: false
1347
+ };
1348
+ for (const field of [
1349
+ "variant",
1350
+ "stack",
1351
+ "layout",
1352
+ "directed",
1353
+ "range",
1354
+ "start",
1355
+ "layers",
1356
+ "period",
1357
+ "unitMark"
1358
+ ]) if (special[field] === void 0) special[field] = false;
1359
+ return {
1360
+ if: {
1361
+ properties: { chart: { const: c.name } },
1362
+ required: ["chart"]
1363
+ },
1364
+ then: {
1365
+ properties: special,
1366
+ required: [...c.requiredSpecial, ...Object.entries(c.special).filter(([, v]) => v === "range").map(([k]) => k)]
1367
+ }
1368
+ };
1369
+ })
1370
+ };
1371
+ if (chart === void 0 || chart === "mindmap" || chart === "tree") {
1372
+ const nodeSchema = (chartType) => ({
1373
+ type: "object",
1374
+ required: ["label"],
1375
+ additionalProperties: false,
1376
+ properties: {
1377
+ label: { type: "string" },
1378
+ id: { type: "string" },
1379
+ note: { type: "string" },
1380
+ value: {
1381
+ type: "number",
1382
+ minimum: 0
1383
+ },
1384
+ icon: { type: "string" },
1385
+ status: { enum: [
1386
+ "new",
1387
+ "info",
1388
+ "ok",
1389
+ "warn",
1390
+ "danger",
1391
+ "ext"
1392
+ ] },
1393
+ shape: { enum: NODE_SHAPES },
1394
+ children: {
1395
+ type: "array",
1396
+ items: { $ref: `#/$defs/${chartType}Node` }
1397
+ }
1398
+ }
1399
+ });
1400
+ schema.$defs = {};
1401
+ if (chart === void 0 || chart === "mindmap") schema.$defs.mindmapNode = nodeSchema("mindmap");
1402
+ if (chart === void 0 || chart === "tree") schema.$defs.treeNode = nodeSchema("tree");
1403
+ for (const chartType of ["mindmap", "tree"]) {
1404
+ if (chart !== void 0 && chart !== chartType) continue;
1405
+ const branch = schema.anyOf.find((item) => item.properties.chart.const === chartType);
1406
+ if (branch) branch.properties.data = { $ref: `#/$defs/${chartType}Node` };
1407
+ }
1408
+ }
1409
+ return schema;
1410
+ }
1411
+ export { parse, schema, sizeBudget, spec_exports, validate };