@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.
Files changed (201) hide show
  1. package/CHANGELOG.md +17 -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 +1229 -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 +1415 -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 +948 -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 +1038 -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 +712 -0
  201. package/package.json +30 -4
@@ -0,0 +1,1313 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { register } from "./registry.js";
3
+ // src/chart/basic.js
4
+ var basic_exports = /* @__PURE__ */ __exportAll({
5
+ diagramLine: () => diagramLine,
6
+ install: () => install
7
+ });
8
+ var segmentsOf = (values) => {
9
+ const segments = [];
10
+ let current = null;
11
+ values.forEach((value, index) => {
12
+ if (value == null) {
13
+ current = null;
14
+ return;
15
+ }
16
+ if (!current) {
17
+ current = [];
18
+ segments.push(current);
19
+ }
20
+ current.push({
21
+ index,
22
+ value
23
+ });
24
+ });
25
+ return segments;
26
+ };
27
+ var path = (points, close = false) => {
28
+ if (!points.length) return "";
29
+ return `${points.map(([x, y], i) => `${i ? "L" : "M"}${Number(x.toFixed(2))},${Number(y.toFixed(2))}`).join(" ")}${close ? " Z" : ""}`;
30
+ };
31
+ var tone = (ctx, index) => ctx.color(`tone${index % 6 + 1}`);
32
+ /**
33
+ * 按统一步长抽稀类别轴,避免零散的标签间隔被误读成数据缺失。
34
+ * 优先保留首尾;末标签与前一个整步标签相撞时去掉后者。在不超过最小可用步长 1.5 倍的范围里
35
+ * 取末段间隔最接近整步的一种。crowded(a, b) 由图族按自己的对齐方式判断两个标签是否太近。
36
+ */
37
+ var evenLabels = (count, crowded, start = 1) => {
38
+ const last = count - 1;
39
+ const pick = (step) => {
40
+ const shown = [];
41
+ for (let index = 0; index < count; index += step) shown.push(index);
42
+ if (shown.at(-1) !== last) {
43
+ if (shown.length > 1 && crowded(shown.at(-1), last)) shown.pop();
44
+ shown.push(last);
45
+ }
46
+ return shown;
47
+ };
48
+ const fits = (step) => !pick(step).some((index, i, shown) => i > 0 && crowded(shown[i - 1], index));
49
+ const unevenness = (step) => {
50
+ const shown = pick(step);
51
+ return shown.length < 3 ? 0 : Math.abs(shown.at(-1) - shown.at(-2) - step) / step;
52
+ };
53
+ let step = Math.max(1, start);
54
+ while (step < last && !fits(step)) step += 1;
55
+ for (let next = step + 1, limit = Math.floor(step * 1.5); next <= limit && next < last; next += 1) if (fits(next) && unevenness(next) < unevenness(step) - 1e-9) step = next;
56
+ return {
57
+ step,
58
+ shown: new Set(pick(step))
59
+ };
60
+ };
61
+ var emphasisStyle = (ctx, spec, series, field, record, index, recolor = false) => {
62
+ const emphasis = spec.emphasis || {};
63
+ const selected = (emphasis.series || []).includes(series) || (emphasis.fields || []).includes(field) || record != null && (emphasis.rows || []).includes(record);
64
+ const active = [
65
+ "series",
66
+ "fields",
67
+ "rows"
68
+ ].some((key) => Array.isArray(emphasis[key]) && emphasis[key].length > 0);
69
+ return {
70
+ fill: selected && recolor ? ctx.color("accent") : tone(ctx, index),
71
+ opacity: active && !selected ? .28 : 1,
72
+ selected
73
+ };
74
+ };
75
+ var isEnglish = (lang) => /^en(?:-|$)/i.test(String(lang || ""));
76
+ var aria = (type, data, lang = "zh") => {
77
+ return `${(isEnglish(lang) ? {
78
+ line: "Line chart",
79
+ area: "Area chart",
80
+ column: "Column chart",
81
+ bar: "Bar chart",
82
+ share: "Share chart",
83
+ funnel: "Funnel chart"
84
+ } : {
85
+ line: "折线图",
86
+ area: "面积图",
87
+ column: "柱状图",
88
+ bar: "条形图",
89
+ share: "占比图",
90
+ funnel: "漏斗图"
91
+ })[type]}: ${data.categories.join(isEnglish(lang) ? ", " : "、")}`;
92
+ };
93
+ var labelWidth = (ctx, value, size = 12, weight = 400) => ctx.measure(String(value), size, weight);
94
+ var LINE_BOX = 16;
95
+ var dodgeReadouts = (items, minY, maxY) => {
96
+ const groups = [];
97
+ items.forEach((item) => {
98
+ const group = groups.find((candidate) => candidate.label === item.label && Math.abs(candidate.y - item.y) < .5);
99
+ if (group) group.items.push(item);
100
+ else groups.push({
101
+ label: item.label,
102
+ y: item.y,
103
+ items: [item]
104
+ });
105
+ });
106
+ groups.sort((a, b) => a.y - b.y);
107
+ groups.forEach((group, index) => {
108
+ group.labelY = Math.max(minY, group.y, index ? groups[index - 1].labelY + LINE_BOX : minY);
109
+ });
110
+ const overflow = groups.length ? groups.at(-1).labelY - maxY : 0;
111
+ if (overflow > 0) {
112
+ const span = groups.at(-1).labelY - groups[0].labelY;
113
+ const room = Math.max(0, maxY - minY);
114
+ if (span <= room) groups.forEach((group) => {
115
+ group.labelY -= overflow;
116
+ });
117
+ else groups.forEach((group, index) => {
118
+ group.labelY = groups.length === 1 ? minY : minY + index / (groups.length - 1) * room;
119
+ });
120
+ }
121
+ return groups;
122
+ };
123
+ var placeAnnotationLabels = (labels, minY, maxY) => {
124
+ const room = Math.max(LINE_BOX, maxY - minY);
125
+ const pack = (group) => {
126
+ group.sort((a, b) => a.y - b.y);
127
+ group.forEach((item, index) => {
128
+ item.placedY = Math.max(minY, item.y, index ? group[index - 1].placedY + LINE_BOX : minY);
129
+ });
130
+ const overflow = group.length ? group.at(-1).placedY - maxY : 0;
131
+ if (overflow > 0 && group.length > 1) group.forEach((item, index) => {
132
+ item.placedY = minY + index / (group.length - 1) * (room - LINE_BOX);
133
+ });
134
+ else if (overflow > 0) group[0].placedY = maxY;
135
+ };
136
+ const start = labels.filter((item) => item.side !== "end");
137
+ const end = labels.filter((item) => item.side === "end");
138
+ pack(start);
139
+ pack(end);
140
+ const crowded = (group) => group.length > 1 && group.at(-1).placedY - group[0].placedY + LINE_BOX > room + .5;
141
+ if (crowded(end) && start.length * 2 <= end.length) {
142
+ end.splice(0, Math.ceil(end.length / 2)).forEach((item) => {
143
+ item.side = "start";
144
+ start.push(item);
145
+ });
146
+ pack(start);
147
+ pack(end);
148
+ }
149
+ };
150
+ var legendChildren = (ctx, data, type, width) => {
151
+ if (type === "funnel" || type !== "share" && data.series.length < 2 && !(data.stack && type !== "line")) return {
152
+ children: [],
153
+ height: 0
154
+ };
155
+ const items = type === "share" ? data.categories.map((label, i) => ({
156
+ label: String(label),
157
+ color: tone(ctx, i),
158
+ meta: {
159
+ kind: "category",
160
+ label
161
+ }
162
+ })) : data.series.map((s, i) => ({
163
+ label: s.label,
164
+ color: tone(ctx, i),
165
+ meta: {
166
+ kind: "series",
167
+ series: s.id,
168
+ label: s.label
169
+ }
170
+ }));
171
+ return ctx.legend(items, width);
172
+ };
173
+ var base = (ctx, spec, data, type, top, bottom, left, right, legend) => {
174
+ const children = legend.children.slice();
175
+ const legendGap = legend.height ? 8 : 0;
176
+ const plotTop = top + legend.height + legendGap;
177
+ return {
178
+ children,
179
+ legendGap,
180
+ plotTop,
181
+ scene: (h) => ({
182
+ width: ctx.width,
183
+ height: h,
184
+ children,
185
+ title: spec.title || aria(type, data, ctx.lang),
186
+ description: spec.subtitle || void 0
187
+ }),
188
+ plot: (plotHeight) => ({
189
+ left,
190
+ right: Math.max(left + 32, ctx.width - right),
191
+ top: plotTop,
192
+ bottom: plotTop + plotHeight
193
+ }),
194
+ totalHeight: (plotHeight) => plotTop + plotHeight + bottom
195
+ };
196
+ };
197
+ var pointsFor = (values, x, y) => segmentsOf(values).map((segment) => segment.map(({ index, value }) => [x(index), y(value)]));
198
+ var shareSegmentPath = (x, y, width, height, roundLeft, roundRight) => {
199
+ const radius = Math.min(height / 2, width / 2);
200
+ const left = roundLeft ? radius : 0, right = roundRight ? radius : 0;
201
+ return `M${x + left},${y} H${x + width - right}${roundRight ? ` Q${x + width},${y} ${x + width},${y + right}` : ""} V${y + height - right}${roundRight ? ` Q${x + width},${y + height} ${x + width - right},${y + height}` : ""} H${x + left}${roundLeft ? ` Q${x},${y + height} ${x},${y + height - left}` : ""} V${y + left}${roundLeft ? ` Q${x},${y} ${x + left},${y}` : ""} Z`;
202
+ };
203
+ var diagramLine = Symbol("diagram line");
204
+ function placePointLabels(ctx, children, plot, segments, labels) {
205
+ const edges = segments.flatMap((points) => points.slice(1).map((point, i) => [points[i], point]));
206
+ const points = segments.flat(), occupied = [], leaders = [];
207
+ const clamp = (n, min, max) => Math.max(min, Math.min(max, n));
208
+ const intersects = (box, [a, b]) => {
209
+ const [[ax, ay], [bx, by]] = a[0] <= b[0] ? [a, b] : [b, a];
210
+ if (bx < box.left || ax > box.right) return false;
211
+ if (ax === bx) return Math.max(ay, by) >= box.top && Math.min(ay, by) <= box.bottom;
212
+ const at = (x) => ay + (by - ay) * (x - ax) / (bx - ax);
213
+ const lo = at(Math.max(ax, box.left)), hi = at(Math.min(bx, box.right));
214
+ return Math.max(lo, hi) >= box.top && Math.min(lo, hi) <= box.bottom;
215
+ };
216
+ for (const { x, y, text, weight, fill, meta } of labels) {
217
+ const w = ctx.measure(text, 12, weight) + 6, h = 18;
218
+ const candidates = [];
219
+ for (const gap of [
220
+ 6,
221
+ 12,
222
+ 22,
223
+ 34,
224
+ 50,
225
+ 72
226
+ ]) for (const dx of [
227
+ -1,
228
+ 0,
229
+ 1
230
+ ]) for (const dy of [
231
+ -1,
232
+ 0,
233
+ 1
234
+ ]) {
235
+ if (!dx && !dy) continue;
236
+ const left = clamp(x - w / 2 + dx * (w / 2 + gap), plot.left + 1, plot.right - w - 1);
237
+ const top = clamp(y - h / 2 + dy * (h / 2 + gap), plot.top + 1, plot.bottom - h - 1);
238
+ const right = left + w, bottom = top + h;
239
+ const ex = clamp(x, left, right), ey = clamp(y, top, bottom), distance = Math.hypot(x - ex, y - ey);
240
+ const leader = distance > 12 ? [[x + (ex - x) * 5 / distance, y + (ey - y) * 5 / distance], [ex, ey]] : null;
241
+ candidates.push({
242
+ left,
243
+ top,
244
+ right,
245
+ bottom,
246
+ distance,
247
+ leader
248
+ });
249
+ }
250
+ candidates.sort((a, b) => a.distance - b.distance);
251
+ const box = candidates.find((b) => b.right <= plot.right && b.bottom <= plot.bottom && !edges.some((edge) => intersects(b, edge)) && !points.some(([px, py]) => px + 5 > b.left && px - 5 < b.right && py + 5 > b.top && py - 5 < b.bottom) && !leaders.some((leader) => intersects(b, leader)) && !occupied.some((o) => b.left < o.right && b.right > o.left && b.top < o.bottom && b.bottom > o.top || b.leader && intersects(o, b.leader)));
252
+ if (!box) ctx.fit("折线标签无法在绘图区内放置", "缩短标注或增加图幅", "Cannot fit line labels inside the plot", "Shorten labels or increase chart width");
253
+ occupied.push(box);
254
+ if (box.leader) {
255
+ leaders.push(box.leader);
256
+ children.push(ctx.line(...box.leader.flat(), {
257
+ stroke: fill,
258
+ "stroke-width": 1
259
+ }));
260
+ }
261
+ children.push(ctx.text(box.left + 3, box.top + 13, text, {
262
+ size: 12,
263
+ weight,
264
+ fill,
265
+ meta
266
+ }));
267
+ }
268
+ }
269
+ var lineArea = (spec, ctx) => {
270
+ const data = ctx.table(spec), isArea = spec.chart === "area", stacked = Boolean(spec.stack);
271
+ if (stacked && data.series.some((series) => series.values.some((value) => value == null))) ctx.fit("堆叠折线/面积不支持缺失值", "补齐数值后再使用stack,或去掉stack");
272
+ const stackBase = (seriesIndex, categoryIndex) => {
273
+ if (spec.stack === "percent") return data.series.slice(0, seriesIndex).reduce((sum, series) => sum + (series.values[categoryIndex] ?? 0), 0) / data.series.reduce((sum, series) => sum + (series.values[categoryIndex] ?? 0), 0);
274
+ const sign = (data.series[seriesIndex].values[categoryIndex] ?? 0) < 0 ? -1 : 1;
275
+ return data.series.slice(0, seriesIndex).reduce((sum, series) => {
276
+ const prior = series.values[categoryIndex] ?? 0;
277
+ return sum + (prior === 0 || Math.sign(prior) === sign ? prior : 0);
278
+ }, 0);
279
+ };
280
+ const stackAt = (seriesIndex, categoryIndex) => {
281
+ const values = data.series.map((series) => series.values[categoryIndex] ?? 0);
282
+ if (spec.stack === "percent") {
283
+ const total = values.reduce((sum, value) => sum + value, 0);
284
+ if (!(total > 0)) ctx.fit("百分比堆叠的每类合计必须大于0", "补充正数或改用普通堆叠");
285
+ return values.slice(0, seriesIndex + 1).reduce((sum, value) => sum + value, 0) / total;
286
+ }
287
+ const sign = values[seriesIndex] < 0 ? -1 : 1;
288
+ return values.slice(0, seriesIndex + 1).reduce((sum, value) => sum + (Math.sign(value) === sign || value === 0 ? value : 0), 0);
289
+ };
290
+ const all = data.series.flatMap((s) => s.values.filter((v) => v != null));
291
+ const lowerValues = spec.encode.lower ? data.categories.map((category) => data.rows.find((row) => row[data.categoryField] === category)?.[spec.encode.lower] ?? null) : null;
292
+ const upperValues = spec.encode.upper ? data.categories.map((category) => data.rows.find((row) => row[data.categoryField] === category)?.[spec.encode.upper] ?? null) : null;
293
+ if (lowerValues) all.push(...lowerValues.filter((value) => value != null), ...upperValues.filter((value) => value != null));
294
+ if (stacked) {
295
+ if (spec.stack === "percent") all.splice(0, all.length, 0, 1);
296
+ else data.categories.forEach((_, i) => data.series.forEach((_, si) => all.push(stackAt(si, i))));
297
+ }
298
+ const field = data.valueFields[0];
299
+ const fieldMeta = ctx.field(field);
300
+ const annotationRows = (spec.annotations || []).filter((annotation) => annotation.label || annotation.text).length;
301
+ const layout = base(ctx, spec, data, spec.chart, 18 + (isArea ? annotationRows * 16 : 0), 28, 52, 64, legendChildren(ctx, data, spec.chart, ctx.width));
302
+ const plot = layout.plot(Math.max(60, 206));
303
+ let ticks, y;
304
+ if (fieldMeta.scale === "log") {
305
+ const vals = [
306
+ ...all,
307
+ ...fieldMeta.domain || [],
308
+ ...(spec.annotations || []).flatMap((a) => a.field === field && a.kind === "rule" ? [a.value] : a.field === field && a.kind === "band" ? [a.from, a.to] : [])
309
+ ].filter((v) => v != null);
310
+ const min = Math.min(...vals), max = Math.max(...vals);
311
+ if (!(min > 0 && max >= min)) ctx.fit("log坐标要求所有值大于0", "移除非正值或改用linear");
312
+ const lo = Math.floor(Math.log10(min)), hi = Math.ceil(Math.log10(max));
313
+ ticks = {
314
+ min: Math.min(lo === hi ? lo - 1 : lo, Math.log10(min)),
315
+ max: Math.max(lo === hi ? hi + 1 : hi, Math.log10(max))
316
+ };
317
+ ticks.ticks = Array.from({ length: Math.floor(ticks.max) - Math.ceil(ticks.min) + 1 }, (_, i) => 10 ** (Math.ceil(ticks.min) + i));
318
+ if (!ticks.ticks.length) ticks.ticks = [10 ** ((ticks.min + ticks.max) / 2)];
319
+ ticks.labels = ticks.ticks.map((v) => ctx.format(v, field));
320
+ y = (value) => ctx.linear(ticks.min, ticks.max, plot.bottom, plot.top)(Math.log10(value));
321
+ } else {
322
+ ticks = ctx.ticks(all, field, {
323
+ zero: isArea,
324
+ count: 4
325
+ });
326
+ y = ctx.linear(ticks.min, ticks.max, plot.bottom, plot.top);
327
+ }
328
+ if (spec.stack === "percent" && stacked) ticks.labels = ticks.ticks.map((tick) => ctx.percent(tick));
329
+ const tickWidth = Math.max(0, ...ticks.labels.map((label) => labelWidth(ctx, label))) + 14;
330
+ const lastLabels = data.series.map((series) => {
331
+ for (let i = series.values.length - 1; i >= 0; i -= 1) if (series.values[i] != null) return spec.stack === "percent" && stacked ? ctx.percent(stackAt(data.series.indexOf(series), i)) : ctx.format(stacked ? stackAt(data.series.indexOf(series), i) : series.values[i], field);
332
+ return "";
333
+ });
334
+ plot.left = Math.max(36, tickWidth);
335
+ const rightLabelWidth = Math.max(20, ...lastLabels.map((label) => labelWidth(ctx, label, 12, 700)));
336
+ const dateCategory = (label) => /^\d{4}[-/]\d{1,2}(?:[-/]\d{1,2})?$/.test(String(label));
337
+ const categoryCount = Math.max(1, data.categories.length);
338
+ const axisLength = (right) => Math.max(1, right - plot.left);
339
+ const visibleStep = (right) => {
340
+ const spacing = axisLength(right) / Math.max(1, categoryCount - 1);
341
+ if (data.categories.every(dateCategory)) {
342
+ const dateLabelWidth = Math.max(0, ...data.categories.map((label) => labelWidth(ctx, label, 12)));
343
+ return Math.max(1, Math.ceil((dateLabelWidth + 8) / spacing));
344
+ }
345
+ const widest = Math.max(36, ...data.categories.map((label) => labelWidth(ctx, label, 12)));
346
+ return Math.max(1, Math.ceil(Math.min(96, Math.max(36, widest)) / spacing));
347
+ };
348
+ const neededRight = () => {
349
+ let right = Math.max(plot.left + 40, ctx.width - rightLabelWidth - 14);
350
+ for (let pass = 0; pass < 3; pass += 1) {
351
+ const step = visibleStep(right);
352
+ const shown = data.categories.filter((_, index) => index % step === 0 || index === categoryCount - 1);
353
+ const widest = Math.max(36, ...shown.map((label) => Math.min(labelWidth(ctx, label, 12), 120)));
354
+ const gaps = Math.max(1, Math.ceil(categoryCount / step) - 1);
355
+ right = Math.max(right, plot.left + gaps * Math.min(96, widest + 8));
356
+ }
357
+ return right;
358
+ };
359
+ const diagram = spec[diagramLine];
360
+ const noteWidth = diagram ? Math.max(0, ...(spec.annotations || []).map((a) => ctx.measure(a.text || "", 12))) : 0;
361
+ const labelRoom = noteWidth ? (noteWidth + 24) * Math.max(1, categoryCount - 1) + rightLabelWidth : 0;
362
+ plot.right = diagram ? Math.max(neededRight(), plot.left + labelRoom) : neededRight();
363
+ const x = (i) => data.categories.length === 1 ? (plot.left + plot.right) / 2 : plot.left + i / (data.categories.length - 1) * (plot.right - plot.left);
364
+ const seenTickLabels = /* @__PURE__ */ new Set();
365
+ ticks.ticks.forEach((tick, i) => {
366
+ if (seenTickLabels.has(ticks.labels[i])) return;
367
+ seenTickLabels.add(ticks.labels[i]);
368
+ const py = y(tick);
369
+ if (tick !== 0) layout.children.push(ctx.line(plot.left, py, plot.right, py, {
370
+ stroke: ctx.color("grid"),
371
+ "stroke-width": 1
372
+ }));
373
+ layout.children.push(ctx.text(plot.left - 10, py + 4, ticks.labels[i], {
374
+ anchor: "end",
375
+ size: 12,
376
+ fill: ctx.color("muted")
377
+ }));
378
+ });
379
+ const baseline = fieldMeta.scale !== "log" && ticks.min <= 0 && ticks.max >= 0 ? y(0) : plot.bottom;
380
+ layout.children.push(ctx.line(plot.left, baseline, plot.right, baseline, {
381
+ stroke: ctx.color("axis"),
382
+ "stroke-width": 1
383
+ }));
384
+ const crowded = (a, b) => {
385
+ const gap = (b - a) * axisLength(plot.right) / Math.max(1, categoryCount - 1);
386
+ const leftWidth = labelWidth(ctx, data.categories[a], 12), rightWidth = labelWidth(ctx, data.categories[b], 12);
387
+ return gap < (a === 0 ? leftWidth : leftWidth / 2) + (b === categoryCount - 1 ? rightWidth : rightWidth / 2) + 8;
388
+ };
389
+ const { step: categoryStep, shown: visibleCategoryIndices } = evenLabels(categoryCount, crowded, visibleStep(plot.right));
390
+ const categoryGap = () => {
391
+ let guard = data.categories.length;
392
+ while (guard > 0) {
393
+ guard -= 1;
394
+ const shown = [...visibleCategoryIndices].sort((a, b) => a - b);
395
+ let removed = false;
396
+ for (let i = 1; i < shown.length; i += 1) if (crowded(shown[i - 1], shown[i])) {
397
+ visibleCategoryIndices.delete(shown[i - 1] === 0 ? shown[i] : shown[i - 1]);
398
+ removed = true;
399
+ break;
400
+ }
401
+ if (!removed) break;
402
+ }
403
+ };
404
+ categoryGap();
405
+ const categoryLines = data.categories.map((label, index) => {
406
+ if (!visibleCategoryIndices.has(index)) return [];
407
+ if (dateCategory(label)) return [String(label)];
408
+ const spacing = axisLength(plot.right) / Math.max(1, categoryCount - 1) * categoryStep;
409
+ return ctx.wrap(String(label), Math.max(36, Math.min(120, spacing - 8)), 12);
410
+ });
411
+ data.categories.forEach((label, i) => {
412
+ if (!visibleCategoryIndices.has(i)) return;
413
+ const lines = categoryLines[i];
414
+ const anchor = i === 0 ? "start" : i === categoryCount - 1 ? "end" : "middle";
415
+ const anchorX = i === 0 ? x(i) + 2 : i === categoryCount - 1 ? x(i) - 2 : x(i);
416
+ layout.children.push(ctx.textLines(anchorX, plot.bottom + 18, lines, {
417
+ anchor,
418
+ size: 12,
419
+ fill: ctx.color("muted"),
420
+ meta: {
421
+ kind: "category",
422
+ label,
423
+ index: i
424
+ }
425
+ }));
426
+ });
427
+ if (lowerValues && upperValues) {
428
+ const complete = lowerValues.map((lo, index) => lo != null && upperValues[index] != null);
429
+ let start = 0;
430
+ while (start < complete.length) {
431
+ while (start < complete.length && !complete[start]) start += 1;
432
+ let end = start;
433
+ while (end < complete.length && complete[end]) end += 1;
434
+ if (end > start) {
435
+ const upper = Array.from({ length: end - start }, (_, j) => [x(start + j), y(upperValues[start + j])]);
436
+ const lower = Array.from({ length: end - start }, (_, j) => [x(start + j), y(lowerValues[start + j])]).reverse();
437
+ layout.children.push(ctx.path(path([...upper, ...lower], true), {
438
+ fill: tone(ctx, 0),
439
+ "fill-opacity": .13,
440
+ stroke: "none",
441
+ meta: {
442
+ kind: "series",
443
+ series: data.series[0].id
444
+ }
445
+ }));
446
+ }
447
+ start = Math.max(end, start + 1);
448
+ }
449
+ }
450
+ const lastReadouts = [];
451
+ data.series.forEach((series, si) => {
452
+ const emphasis = spec.emphasis || {};
453
+ const emphasizedSeries = (emphasis.series || []).includes(series.id) || (emphasis.fields || []).includes(series.field);
454
+ const role = emphasizedSeries ? "accent" : `tone${si % 6 + 1}`, color = ctx.color(role);
455
+ const seriesOpacity = ((emphasis.series?.length || 0) > 0 || (emphasis.fields?.length || 0) > 0) && !emphasizedSeries ? .28 : 1;
456
+ if (stacked) {
457
+ const topPoints = data.categories.map((_, i) => [x(i), y(stackAt(si, i))]);
458
+ const bottomPoints = data.categories.map((_, i) => [x(i), y(stackBase(si, i))]);
459
+ if (isArea) layout.children.push(ctx.path(path([...topPoints, ...bottomPoints.slice().reverse()], true), {
460
+ fill: color,
461
+ "fill-opacity": .12,
462
+ opacity: seriesOpacity,
463
+ stroke: "none",
464
+ meta: {
465
+ kind: "series",
466
+ series: series.id,
467
+ label: series.label
468
+ }
469
+ }));
470
+ layout.children.push(ctx.path(path(topPoints), {
471
+ fill: "none",
472
+ stroke: color,
473
+ "stroke-width": emphasizedSeries ? 3 : 2,
474
+ "stroke-opacity": seriesOpacity,
475
+ "stroke-linejoin": "round",
476
+ meta: {
477
+ kind: "series",
478
+ series: series.id,
479
+ label: series.label
480
+ }
481
+ }));
482
+ } else for (const segment of pointsFor(series.values, x, y)) {
483
+ if (isArea && segment.length > 1) layout.children.push(ctx.path(path([
484
+ [segment[0][0], y(0)],
485
+ ...segment,
486
+ [segment.at(-1)[0], y(0)]
487
+ ], true), {
488
+ fill: color,
489
+ "fill-opacity": .12,
490
+ opacity: seriesOpacity,
491
+ stroke: "none",
492
+ meta: {
493
+ kind: "series",
494
+ series: series.id,
495
+ label: series.label
496
+ }
497
+ }));
498
+ layout.children.push(ctx.path(path(segment), {
499
+ fill: "none",
500
+ stroke: color,
501
+ "stroke-width": emphasizedSeries ? 3 : 2,
502
+ "stroke-opacity": seriesOpacity,
503
+ "stroke-linecap": "round",
504
+ "stroke-linejoin": "round",
505
+ meta: {
506
+ kind: "series",
507
+ series: series.id,
508
+ label: series.label
509
+ }
510
+ }));
511
+ }
512
+ const valid = series.values.map((value, index) => ({
513
+ value,
514
+ index
515
+ })).filter((p) => p.value != null);
516
+ valid.forEach(({ value, index }) => {
517
+ const pyValue = stacked ? stackAt(si, index) : value;
518
+ const recordId = data.rows.find((r) => r[data.categoryField] === data.categories[index] && (!spec.encode.series || String(r[spec.encode.series]) === series.id))?.[spec.encode.id] ?? `${index}`;
519
+ const recordSelected = (spec.emphasis?.rows || []).includes(recordId);
520
+ layout.children.push(ctx.circle(x(index), y(pyValue), recordSelected ? 5.5 : index === valid.at(-1).index ? 4 : 2.7, {
521
+ fill: recordSelected ? ctx.color("accent") : color,
522
+ stroke: ctx.color("bg"),
523
+ "stroke-width": recordSelected ? 2 : 1.5,
524
+ "fill-opacity": spec.emphasis?.rows?.length && !recordSelected ? .28 : 1,
525
+ meta: {
526
+ kind: "record",
527
+ record: recordId,
528
+ series: series.id,
529
+ value,
530
+ label: String(data.categories[index])
531
+ }
532
+ }));
533
+ });
534
+ if (valid.length) {
535
+ const last = valid.at(-1), value = stacked ? stackAt(si, last.index) : last.value;
536
+ const label = spec.stack === "percent" && stacked ? ctx.percent(value) : ctx.format(value, field);
537
+ lastReadouts.push({
538
+ si,
539
+ series,
540
+ last,
541
+ value,
542
+ label,
543
+ color,
544
+ labelColor: ctx.textColor(role),
545
+ y: y(value)
546
+ });
547
+ }
548
+ });
549
+ const pointLabels = [];
550
+ (!diagram && lastReadouts.length && !(spec.variant === "counter" && spec.unit) ? dodgeReadouts(lastReadouts, plot.top + 8, plot.bottom - 8) : []).forEach((group) => {
551
+ const sample = group.items[0];
552
+ group.items.forEach(({ series, last, value, label, color }) => {
553
+ const record = data.rows.find((r) => r[data.categoryField] === data.categories[last.index] && (!spec.encode.series || String(r[spec.encode.series]) === series.id))?.[spec.encode.id] ?? `${last.index}`;
554
+ layout.children.push(ctx.line(x(last.index), y(value), plot.right + 4, group.labelY, {
555
+ stroke: color,
556
+ "stroke-width": .75,
557
+ "stroke-opacity": .7,
558
+ meta: {
559
+ kind: "connector",
560
+ record,
561
+ series: series.id,
562
+ value: last.value,
563
+ label
564
+ }
565
+ }));
566
+ });
567
+ const record = data.rows.find((r) => r[data.categoryField] === data.categories[sample.last.index] && (!spec.encode.series || String(r[spec.encode.series]) === sample.series.id))?.[spec.encode.id] ?? `${sample.last.index}`;
568
+ layout.children.push(ctx.text(plot.right + 8, group.labelY + 4, group.label, {
569
+ size: 12,
570
+ weight: 700,
571
+ fill: sample.labelColor,
572
+ meta: {
573
+ kind: "value",
574
+ record,
575
+ series: sample.series.id,
576
+ value: sample.last.value,
577
+ label: group.label,
578
+ category: data.categories[sample.last.index]
579
+ }
580
+ }));
581
+ });
582
+ const annotationLabels = [];
583
+ if (spec.annotations) spec.annotations.forEach((a) => {
584
+ if (a.field !== field) return;
585
+ if (a.kind === "band") {
586
+ const topY = Math.min(y(a.from), y(a.to)), height = Math.abs(y(a.from) - y(a.to));
587
+ const bandFill = ctx.color("accent");
588
+ layout.children.push(ctx.rect(plot.left, topY, plot.right - plot.left, height, {
589
+ fill: bandFill,
590
+ "fill-opacity": .1,
591
+ meta: {
592
+ kind: "annotation",
593
+ label: a.label || ""
594
+ }
595
+ }));
596
+ if (a.label) annotationLabels.push({
597
+ text: a.label,
598
+ y: topY + 12,
599
+ side: "end",
600
+ fill: ctx.textColor("fg"),
601
+ meta: {
602
+ kind: "annotation",
603
+ label: a.label
604
+ }
605
+ });
606
+ }
607
+ if (a.kind === "note") {
608
+ const row = data.rows.find((r) => r[spec.encode.id] === a.record);
609
+ const index = row ? data.categories.indexOf(row[data.categoryField]) : -1;
610
+ const value = row?.[a.field];
611
+ if (index >= 0 && value != null) {
612
+ const fill = ctx.textColor(isArea ? "fg" : "accent"), meta = {
613
+ kind: "annotation",
614
+ record: a.record,
615
+ label: a.text
616
+ };
617
+ if (diagram) pointLabels.push({
618
+ text: a.text,
619
+ x: x(index),
620
+ y: y(value),
621
+ weight: 400,
622
+ fill,
623
+ meta
624
+ });
625
+ else annotationLabels.push({
626
+ text: a.text,
627
+ y: y(value) - 4,
628
+ side: index > data.categories.length / 2 ? "end" : "start",
629
+ x: x(index),
630
+ fill,
631
+ meta
632
+ });
633
+ }
634
+ }
635
+ if (a.kind === "rule") {
636
+ const py = y(a.value);
637
+ layout.children.push(ctx.line(plot.left, py, plot.right, py, {
638
+ stroke: ctx.color("accent"),
639
+ "stroke-dasharray": "4 3"
640
+ }));
641
+ if (a.label) annotationLabels.push({
642
+ text: a.label,
643
+ y: Math.max(plot.top + 12, py - 16),
644
+ side: "start",
645
+ fill: ctx.textColor("fg"),
646
+ meta: {
647
+ kind: "annotation",
648
+ label: a.label
649
+ }
650
+ });
651
+ }
652
+ });
653
+ if (diagram) {
654
+ for (const { last, label, labelColor, value, series } of lastReadouts) pointLabels.push({
655
+ text: label,
656
+ x: x(last.index),
657
+ y: y(value),
658
+ weight: 700,
659
+ fill: labelColor,
660
+ meta: {
661
+ kind: "value",
662
+ series: series.id,
663
+ value,
664
+ label
665
+ }
666
+ });
667
+ placePointLabels(ctx, layout.children, plot, data.series.flatMap((series) => pointsFor(series.values, x, y)), pointLabels);
668
+ }
669
+ placeAnnotationLabels(annotationLabels, plot.top + 4, plot.bottom - 4);
670
+ annotationLabels.forEach((item, index) => {
671
+ const anchor = item.side === "end" ? "end" : "start";
672
+ const px = isArea ? anchor === "end" ? plot.right - 4 : plot.left + 4 : item.x != null ? item.x + (anchor === "end" ? -6 : 6) : anchor === "end" ? plot.right - 4 : plot.left + 4;
673
+ const py = isArea ? plot.top - 8 - index * 16 : item.placedY;
674
+ layout.children.push(ctx.text(px, py, item.text, {
675
+ anchor,
676
+ size: 12,
677
+ fill: item.fill,
678
+ meta: item.meta
679
+ }));
680
+ });
681
+ const scene = layout.scene(layout.totalHeight(206 + Math.max(0, ...categoryLines.map((lines) => lines.length - 1)) * 16));
682
+ scene.width = Math.max(ctx.width, plot.right + rightLabelWidth + 14);
683
+ return scene;
684
+ };
685
+ var rounded = (ctx, x, y, w, h, color, meta, opacity = 1) => w <= 0 || h <= 0 ? null : ctx.rect(x, y, w, h, {
686
+ rx: Math.min(10, Math.max(0, Math.min(w, h) / 2)),
687
+ fill: color,
688
+ opacity,
689
+ meta
690
+ });
691
+ var bars = (spec, ctx) => {
692
+ const data = ctx.table(spec), horizontal = spec.chart === "bar", stacked = Boolean(spec.stack);
693
+ const legend = legendChildren(ctx, data, spec.chart, ctx.width);
694
+ const annotationCount = (spec.annotations || []).filter((item) => item.label || item.text).length;
695
+ const layout = base(ctx, spec, data, spec.chart, (horizontal ? 14 : 38) + Math.min(2, annotationCount) * 16, 30, 46, 14, legend);
696
+ const categoryN = data.categories.length, seriesN = data.series.length;
697
+ if (stacked && data.series.some((series) => series.values.some((value) => value == null))) ctx.fit("堆叠图不支持缺失值", "补齐数值后再使用stack,或去掉stack");
698
+ const stackTotal = (categoryIndex) => data.series.reduce((sum, series) => sum + series.values[categoryIndex], 0);
699
+ if (spec.stack === "percent" && data.categories.some((_, index) => !(stackTotal(index) > 0))) ctx.fit("百分比堆叠的每类合计必须大于0", "补充正数或改用普通堆叠");
700
+ const stackStart = (seriesIndex, categoryIndex) => {
701
+ if (spec.stack === "percent") return data.series.slice(0, seriesIndex).reduce((sum, series) => sum + series.values[categoryIndex], 0) / stackTotal(categoryIndex);
702
+ const sign = (data.series[seriesIndex].values[categoryIndex] ?? 0) < 0 ? -1 : 1;
703
+ return data.series.slice(0, seriesIndex).reduce((sum, series) => {
704
+ const value = series.values[categoryIndex] ?? 0;
705
+ return sum + (value === 0 || Math.sign(value) === sign ? value : 0);
706
+ }, 0);
707
+ };
708
+ const stackValue = (seriesIndex, categoryIndex) => stackStart(seriesIndex, categoryIndex) + (data.series[seriesIndex].values[categoryIndex] ?? 0) / (spec.stack === "percent" ? stackTotal(categoryIndex) : 1);
709
+ const values = stacked ? data.categories.flatMap((_, i) => spec.stack === "percent" ? [0, 1] : data.series.map((_, si) => stackValue(si, i))) : data.series.flatMap((s) => s.values.filter((v) => v != null));
710
+ values.push(0);
711
+ const field = data.valueFields[0];
712
+ const sourceAt = (ci, series) => data.rows.find((row) => row[data.categoryField] === data.categories[ci] && (!spec.encode.series || String(row[spec.encode.series]) === series.id));
713
+ let ticks = ctx.ticks(values, field, {
714
+ zero: true,
715
+ count: horizontal ? Math.max(2, Math.min(5, Math.floor(ctx.width / 74))) : 4
716
+ });
717
+ if (spec.stack === "percent") ticks.labels = ticks.ticks.map((tick) => ctx.percent(tick));
718
+ if (horizontal) {
719
+ const labelCap = Math.max(24, ...data.categories.map((c) => labelWidth(ctx, c, 12, 500))) + 14;
720
+ const readoutTexts = data.categories.flatMap((_, ci) => stacked ? [spec.stack === "percent" ? ctx.percent(1) : ctx.format(stackTotal(ci), field)] : data.series.map((series) => series.values[ci] == null ? "" : ctx.format(series.values[ci], field))).filter(Boolean);
721
+ const valueW = Math.max(20, ...readoutTexts.map((label) => labelWidth(ctx, label)));
722
+ const negativeW = Math.max(0, ...readoutTexts.filter((label) => label.startsWith("-")).map((label) => labelWidth(ctx, label)));
723
+ const tickGutter = Math.max(0, ...ticks.labels.map((label) => labelWidth(ctx, label))) + 8;
724
+ const available = Math.max(1, ctx.width - labelCap - valueW - 28 - (ticks.min < 0 ? negativeW + 14 : 0) - tickGutter);
725
+ ticks = ctx.ticks(values, field, {
726
+ zero: true,
727
+ count: horizontal ? Math.max(2, Math.min(5, Math.floor(ctx.width / 74))) : 4,
728
+ length: available
729
+ });
730
+ if (spec.stack === "percent") ticks.labels = ticks.ticks.map((tick) => ctx.percent(tick));
731
+ const tickHalf = Math.max(0, ...ticks.labels.map((label) => labelWidth(ctx, label))) / 2;
732
+ const tickWidth = Math.max(0, ...ticks.labels.map((label) => labelWidth(ctx, label)));
733
+ const minTickSpan = Math.max(0, ticks.ticks.length - 1) * (tickWidth + 8);
734
+ const plot = {
735
+ left: labelCap + (ticks.min < 0 ? negativeW + 14 : 0) + tickHalf,
736
+ right: Math.max(labelCap + negativeW + 54, ctx.width - valueW - 14 - tickHalf),
737
+ top: layout.plotTop,
738
+ bottom: layout.plotTop + categoryN * Math.max(34, seriesN * 16 + 14)
739
+ };
740
+ plot.right = Math.max(plot.right, plot.left + minTickSpan);
741
+ const x = ctx.linear(ticks.min, ticks.max, plot.left, plot.right);
742
+ ticks.ticks.forEach((tick, i) => {
743
+ const px = x(tick);
744
+ if (tick !== 0) layout.children.push(ctx.line(px, plot.top, px, plot.bottom, { stroke: ctx.color("grid") }));
745
+ layout.children.push(ctx.text(px, plot.bottom + 18, ticks.labels[i], {
746
+ anchor: "middle",
747
+ fill: ctx.color("muted"),
748
+ size: 12
749
+ }));
750
+ });
751
+ layout.children.push(ctx.line(x(0), plot.top, x(0), plot.bottom, { stroke: ctx.color("axis") }));
752
+ const groupH = (plot.bottom - plot.top) / categoryN;
753
+ data.categories.forEach((category, ci) => {
754
+ const center = plot.top + groupH * (ci + .5);
755
+ const lines = ctx.wrap(String(category), Math.max(16, labelCap - 8), 12, 500);
756
+ layout.children.push(ctx.textLines(labelCap - 12, center - (lines.length - 1) * 15 / 2 + 4, lines, {
757
+ anchor: "end",
758
+ fill: ctx.color("fg"),
759
+ meta: {
760
+ kind: "category",
761
+ label: category,
762
+ index: ci
763
+ }
764
+ }));
765
+ if (stacked) {
766
+ data.series.forEach((series, si) => {
767
+ const value = series.values[ci] ?? 0, start = stackStart(si, ci), end = stackValue(si, ci);
768
+ const record = sourceAt(ci, series)?.[spec.encode.id] ?? `${ci}`, style = emphasisStyle(ctx, spec, series.id, series.field || series.id, record, si, seriesN === 1);
769
+ if (value !== 0) layout.children.push(rounded(ctx, Math.min(x(start), x(end)), center - 10, Math.abs(x(end) - x(start)), 20, style.fill, {
770
+ kind: "record",
771
+ record,
772
+ series: series.id,
773
+ category,
774
+ value,
775
+ label: String(category)
776
+ }, style.opacity));
777
+ });
778
+ const total = spec.stack === "percent" ? 1 : stackTotal(ci);
779
+ if (total !== 0) layout.children.push(ctx.text(x(total) + (total >= 0 ? 6 : -6), center + 4, spec.stack === "percent" ? ctx.percent(total) : ctx.format(total, field), {
780
+ anchor: total >= 0 ? "start" : "end",
781
+ fill: ctx.color("fg"),
782
+ meta: {
783
+ kind: "value",
784
+ category,
785
+ value: total
786
+ }
787
+ }));
788
+ } else data.series.forEach((series, si) => {
789
+ const value = series.values[ci];
790
+ if (value == null) return;
791
+ const record = sourceAt(ci, series)?.[spec.encode.id] ?? `${ci}`, style = emphasisStyle(ctx, spec, series.id, series.field || series.id, record, si, seriesN === 1);
792
+ const cy = center + (si - (seriesN - 1) / 2) * Math.max(12, (groupH - 12) / seriesN);
793
+ const end = x(value);
794
+ if (value !== 0) layout.children.push(rounded(ctx, Math.min(x(0), end), cy - Math.min(9, groupH / seriesN / 2), Math.abs(end - x(0)), Math.min(18, groupH / seriesN - 3), style.fill, {
795
+ kind: "record",
796
+ record,
797
+ series: series.id,
798
+ category,
799
+ value,
800
+ label: String(category)
801
+ }, style.opacity));
802
+ layout.children.push(ctx.text(end + (value >= 0 ? 6 : -6), cy + 4, ctx.format(value, field), {
803
+ anchor: value >= 0 ? "start" : "end",
804
+ fill: ctx.color("fg"),
805
+ meta: {
806
+ kind: "value",
807
+ record,
808
+ category,
809
+ series: series.id,
810
+ value,
811
+ label: ctx.format(value, field)
812
+ }
813
+ }));
814
+ });
815
+ });
816
+ const barAnnotations = [];
817
+ (spec.annotations || []).forEach((a) => {
818
+ if (a.field !== field) return;
819
+ if (a.kind === "rule") {
820
+ const px = x(a.value);
821
+ layout.children.push(ctx.line(px, plot.top, px, plot.bottom, {
822
+ stroke: ctx.color("accent"),
823
+ "stroke-dasharray": "4 3",
824
+ meta: {
825
+ kind: "annotation",
826
+ label: a.label || ""
827
+ }
828
+ }));
829
+ if (a.label) barAnnotations.push({
830
+ text: a.label,
831
+ x: px,
832
+ fill: ctx.textColor("fg"),
833
+ meta: {
834
+ kind: "annotation",
835
+ label: a.label
836
+ }
837
+ });
838
+ } else if (a.kind === "band") {
839
+ const left = Math.min(x(a.from), x(a.to)), width = Math.abs(x(a.from) - x(a.to));
840
+ layout.children.push(ctx.rect(left, plot.top, width, plot.bottom - plot.top, {
841
+ fill: ctx.color("accent"),
842
+ "fill-opacity": .1,
843
+ meta: {
844
+ kind: "annotation",
845
+ label: a.label || ""
846
+ }
847
+ }));
848
+ if (a.label) barAnnotations.push({
849
+ text: a.label,
850
+ x: left + width / 2,
851
+ fill: ctx.textColor("fg"),
852
+ meta: {
853
+ kind: "annotation",
854
+ label: a.label
855
+ }
856
+ });
857
+ } else if (a.kind === "note") {
858
+ const row = data.rows.find((item) => item[spec.encode.id] === a.record);
859
+ if ((row ? data.categories.indexOf(row[data.categoryField]) : -1) >= 0 && row[a.field] != null) barAnnotations.push({
860
+ text: a.text,
861
+ x: x(row[a.field]),
862
+ fill: ctx.textColor("fg"),
863
+ meta: {
864
+ kind: "annotation",
865
+ record: a.record,
866
+ label: a.text
867
+ }
868
+ });
869
+ }
870
+ });
871
+ const lanes = [[], []];
872
+ barAnnotations.sort((a, b) => a.x - b.x).forEach((item) => {
873
+ const width = labelWidth(ctx, item.text);
874
+ const lane = lanes.find((items) => !items.length || items.at(-1).right + 8 <= item.x - width / 2) || lanes[0];
875
+ const previous = lane.at(-1);
876
+ item.placedX = Math.max(item.x, previous ? previous.right + 8 + width / 2 : item.x);
877
+ item.lane = lanes.indexOf(lane);
878
+ lane.push({ right: item.placedX + width / 2 });
879
+ });
880
+ barAnnotations.forEach((item) => {
881
+ layout.children.push(ctx.text(item.placedX, plot.top - 4 - item.lane * 16, item.text, {
882
+ anchor: "middle",
883
+ size: 12,
884
+ fill: item.fill,
885
+ meta: item.meta
886
+ }));
887
+ });
888
+ const scene = layout.scene(plot.bottom + 32);
889
+ scene.width = Math.max(ctx.width, plot.right + valueW + 14);
890
+ return scene;
891
+ }
892
+ const tickLabelWidth = Math.max(0, ...ticks.labels.map((label) => labelWidth(ctx, label)));
893
+ const axisUnit = spec.stack !== "percent" && ctx.field(field).unit ? ctx.field(field).unit : "";
894
+ const leftLabelWidth = Math.max(tickLabelWidth, axisUnit ? labelWidth(ctx, axisUnit) : 0);
895
+ const plot = {
896
+ left: Math.max(42, leftLabelWidth + 16),
897
+ right: ctx.width - 14,
898
+ top: layout.plotTop + 16,
899
+ bottom: layout.plotTop + Math.max(60, 256 - layout.plotTop - 30)
900
+ };
901
+ const minGroup = Math.max(18, seriesN * (stacked ? 10 : 8));
902
+ plot.right = Math.max(plot.right, plot.left + categoryN * minGroup);
903
+ const y = ctx.linear(ticks.min, ticks.max, plot.bottom, plot.top);
904
+ if (axisUnit) layout.children.push(ctx.text(plot.left - 10, Math.max(16, plot.top - 36), axisUnit, {
905
+ anchor: "end",
906
+ size: 12,
907
+ fill: ctx.color("muted"),
908
+ meta: {
909
+ kind: "axis-unit",
910
+ label: axisUnit
911
+ }
912
+ }));
913
+ const yShown = [];
914
+ ticks.ticks.forEach((tick, i) => {
915
+ const py = y(tick);
916
+ const height = 16;
917
+ if (yShown.some((placed) => Math.abs(placed - py) < height)) return;
918
+ yShown.push(py);
919
+ if (tick !== 0) layout.children.push(ctx.line(plot.left, py, plot.right, py, { stroke: ctx.color("grid") }));
920
+ layout.children.push(ctx.text(plot.left - 10, py + 4, ticks.labels[i], {
921
+ anchor: "end",
922
+ fill: ctx.color("muted"),
923
+ size: 12
924
+ }));
925
+ });
926
+ layout.children.push(ctx.line(plot.left, y(0), plot.right, y(0), { stroke: ctx.color("axis") }));
927
+ const groupW = (plot.right - plot.left) / categoryN;
928
+ const slot = stacked ? groupW * .62 : groupW * .76 / seriesN;
929
+ const barW = stacked ? Math.max(2, Math.min(44, slot)) : Math.max(1.5, Math.min(40, slot - (seriesN > 1 ? 3 : 0)));
930
+ const labelValues = stacked && spec.stack === "percent" ? [1] : values.filter((v) => v !== 0);
931
+ const withUnit = (value) => spec.stack === "percent" ? ctx.percent(value) : ctx.format(value, field, true);
932
+ const bare = (value) => spec.stack === "percent" ? ctx.percent(value) : ctx.format(value, field, false);
933
+ const formatLabel = labelValues.every((v) => labelWidth(ctx, withUnit(v)) <= Math.max(0, slot - 2)) ? withUnit : bare;
934
+ const mode = labelValues.every((v) => labelWidth(ctx, formatLabel(v)) <= Math.max(0, slot - 2)) ? "value" : "none";
935
+ const placedColumnReadouts = /* @__PURE__ */ new Map();
936
+ data.categories.forEach((category, ci) => {
937
+ const center = plot.left + groupW * (ci + .5);
938
+ if (stacked) {
939
+ data.series.forEach((series, si) => {
940
+ const value = series.values[ci] ?? 0, start = stackStart(si, ci), end = stackValue(si, ci);
941
+ const record = sourceAt(ci, series)?.[spec.encode.id] ?? `${ci}`, style = emphasisStyle(ctx, spec, series.id, series.field || series.id, record, si, seriesN === 1);
942
+ if (value !== 0) layout.children.push(rounded(ctx, center - barW / 2, Math.min(y(start), y(end)), barW, Math.abs(y(end) - y(start)), style.fill, {
943
+ kind: "record",
944
+ record,
945
+ series: series.id,
946
+ category,
947
+ value,
948
+ label: String(category)
949
+ }, style.opacity));
950
+ });
951
+ const total = spec.stack === "percent" ? 1 : stackTotal(ci);
952
+ if (total && mode !== "none") layout.children.push(ctx.text(center, total >= 0 ? y(total) - 8 : y(0) + 16, formatLabel(total), {
953
+ anchor: "middle",
954
+ fill: ctx.color("fg"),
955
+ meta: {
956
+ kind: "value",
957
+ category,
958
+ value: total
959
+ }
960
+ }));
961
+ } else data.series.forEach((series, si) => {
962
+ const value = series.values[ci];
963
+ if (value == null) return;
964
+ const record = sourceAt(ci, series)?.[spec.encode.id] ?? `${ci}`, style = emphasisStyle(ctx, spec, series.id, series.field || series.id, record, si, seriesN === 1);
965
+ const bx = center + (si - (seriesN - 1) / 2) * slot, end = y(value), zero = y(0);
966
+ if (value !== 0) layout.children.push(rounded(ctx, bx - barW / 2, Math.min(zero, end), barW, Math.abs(end - zero), style.fill, {
967
+ kind: "record",
968
+ record,
969
+ series: series.id,
970
+ category,
971
+ value,
972
+ label: String(category)
973
+ }, style.opacity));
974
+ if (mode !== "none" && !(annotationCount && value === 0)) {
975
+ const label = formatLabel(value);
976
+ const readoutY = value >= 0 ? end - 8 : zero + 16;
977
+ const readoutWidth = labelWidth(ctx, label, 12, 400);
978
+ const key = `${label}@${Math.round(readoutY)}`;
979
+ const previous = placedColumnReadouts.get(key);
980
+ if (previous != null && Math.abs(bx - previous) < readoutWidth + 8) return;
981
+ placedColumnReadouts.set(key, bx);
982
+ layout.children.push(ctx.text(bx, readoutY, label, {
983
+ anchor: "middle",
984
+ fill: ctx.color("fg"),
985
+ meta: {
986
+ kind: "value",
987
+ record,
988
+ category,
989
+ series: series.id,
990
+ value,
991
+ label
992
+ }
993
+ }));
994
+ }
995
+ });
996
+ });
997
+ const columnAnnotations = [];
998
+ (spec.annotations || []).forEach((a) => {
999
+ if (a.field !== field) return;
1000
+ if (a.kind === "rule") {
1001
+ const py = y(a.value);
1002
+ layout.children.push(ctx.line(plot.left, py, plot.right, py, {
1003
+ stroke: ctx.color("accent"),
1004
+ "stroke-dasharray": "4 3",
1005
+ meta: {
1006
+ kind: "annotation",
1007
+ label: a.label || ""
1008
+ }
1009
+ }));
1010
+ if (a.label) columnAnnotations.push({
1011
+ text: a.label,
1012
+ x: plot.left + 8,
1013
+ fill: ctx.textColor("fg"),
1014
+ meta: {
1015
+ kind: "annotation",
1016
+ label: a.label
1017
+ }
1018
+ });
1019
+ } else if (a.kind === "band") {
1020
+ const topY = Math.min(y(a.from), y(a.to)), height = Math.abs(y(a.from) - y(a.to));
1021
+ layout.children.push(ctx.rect(plot.left, topY, plot.right - plot.left, height, {
1022
+ fill: ctx.color("accent"),
1023
+ "fill-opacity": .1,
1024
+ meta: {
1025
+ kind: "annotation",
1026
+ label: a.label || ""
1027
+ }
1028
+ }));
1029
+ if (a.label) columnAnnotations.push({
1030
+ text: a.label,
1031
+ x: plot.right - 8,
1032
+ fill: ctx.textColor("fg"),
1033
+ meta: {
1034
+ kind: "annotation",
1035
+ label: a.label
1036
+ }
1037
+ });
1038
+ } else if (a.kind === "note") {
1039
+ const row = data.rows.find((item) => item[spec.encode.id] === a.record);
1040
+ if ((row ? data.categories.indexOf(row[data.categoryField]) : -1) >= 0 && row[a.field] != null) columnAnnotations.push({
1041
+ text: a.text,
1042
+ x: plot.right - 8,
1043
+ fill: ctx.textColor("fg"),
1044
+ meta: {
1045
+ kind: "annotation",
1046
+ record: a.record,
1047
+ label: a.text
1048
+ }
1049
+ });
1050
+ }
1051
+ });
1052
+ const columnLanes = [[], []];
1053
+ columnAnnotations.sort((a, b) => a.x - b.x).forEach((item) => {
1054
+ const width = labelWidth(ctx, item.text);
1055
+ const lane = columnLanes.find((items) => !items.length || items.at(-1).right + 8 <= item.x - width / 2) || columnLanes[0];
1056
+ const previous = lane.at(-1);
1057
+ item.placedX = Math.max(item.x, previous ? previous.right + 8 + width / 2 : item.x);
1058
+ item.lane = columnLanes.indexOf(lane);
1059
+ lane.push({ right: item.placedX + width / 2 });
1060
+ });
1061
+ columnAnnotations.forEach((item) => {
1062
+ layout.children.push(ctx.text(item.placedX, plot.top - 20 - item.lane * 16, item.text, {
1063
+ anchor: "middle",
1064
+ size: 12,
1065
+ fill: item.fill,
1066
+ meta: item.meta
1067
+ }));
1068
+ });
1069
+ const labelSlot = Math.max(48, Math.min(120, groupW - 4));
1070
+ const sceneWidth = Math.max(ctx.width, plot.right + 14);
1071
+ const centerOf = (i, width) => Math.min(sceneWidth - width / 2 - 2, Math.max(width / 2 + 2, plot.left + groupW * (i + .5)));
1072
+ const slotWidths = data.categories.map((label) => Math.max(...ctx.wrap(String(label), labelSlot, 12).map((line) => labelWidth(ctx, line, 12))));
1073
+ const crowded = (a, b, widths) => centerOf(b, widths[b]) - widths[b] / 2 - (centerOf(a, widths[a]) + widths[a] / 2) < 8;
1074
+ const { step, shown } = evenLabels(categoryN, (a, b) => crowded(a, b, slotWidths));
1075
+ const categoryLines = data.categories.map((category, index) => shown.has(index) ? ctx.wrap(String(category), Math.max(36, groupW * step - 8), 12) : []);
1076
+ const drawnWidths = categoryLines.map((lines) => Math.max(0, ...lines.map((line) => labelWidth(ctx, line, 12))));
1077
+ for (let guard = categoryN; guard > 0; guard -= 1) {
1078
+ const list = data.categories.map((_, index) => index).filter((index) => categoryLines[index].length);
1079
+ const position = list.findIndex((index, i) => i > 0 && crowded(list[i - 1], index, drawnWidths));
1080
+ if (position < 0) break;
1081
+ categoryLines[list[position - 1] === 0 ? list[position] : list[position - 1]] = [];
1082
+ }
1083
+ data.categories.forEach((category, i) => {
1084
+ if (!categoryLines[i].length) return;
1085
+ const cx = centerOf(i, drawnWidths[i]);
1086
+ layout.children.push(ctx.textLines(cx, plot.bottom + 18, categoryLines[i], {
1087
+ anchor: "middle",
1088
+ size: 12,
1089
+ fill: ctx.color("muted"),
1090
+ meta: {
1091
+ kind: "category",
1092
+ label: category,
1093
+ index: i
1094
+ }
1095
+ }));
1096
+ });
1097
+ const scene = layout.scene(plot.bottom + 28 + Math.max(0, ...categoryLines.map((lines) => Math.max(0, lines.length - 1))) * 16);
1098
+ scene.width = sceneWidth;
1099
+ return scene;
1100
+ };
1101
+ var share = (spec, ctx) => {
1102
+ const data = ctx.table(spec), legend = legendChildren(ctx, data, "share", ctx.width);
1103
+ const top = 8 + legend.height + (legend.height ? 8 : 0), barH = 22;
1104
+ const labelColumn = Math.max(36, ...data.series.map((series) => {
1105
+ const full = labelWidth(ctx, series.label, 12, 500);
1106
+ if (full <= ctx.width * .42) return full;
1107
+ const lines = ctx.wrap(series.label, Math.max(36, ctx.width * .42), 12, 500);
1108
+ return Math.max(...lines.map((line) => labelWidth(ctx, line, 12, 500)));
1109
+ }));
1110
+ const left = labelColumn + 24, right = 10;
1111
+ const longestReadout = Math.max(40, ...data.series.flatMap((series) => {
1112
+ const total = series.values.reduce((sum, value) => sum + (value ?? 0), 0) || 1;
1113
+ const items = series.values.map((value, ci) => `${data.categories[ci]} ${ctx.percent((value ?? 0) / total)}`);
1114
+ items.push(`${isEnglish(ctx.lang) ? "total" : "合计"} ${ctx.format(series.values.reduce((sum, value) => sum + (value ?? 0), 0), series.field)}`);
1115
+ return items.map((item) => labelWidth(ctx, item));
1116
+ }));
1117
+ const chartWidth = Math.max(ctx.width, left + Math.min(longestReadout, 220) + right);
1118
+ const barW = Math.max(30, chartWidth - left - right);
1119
+ const rows = data.series.map((series) => {
1120
+ const total = series.values.reduce((sum, value) => sum + (value ?? 0), 0);
1121
+ if (!(total > 0)) ctx.fit("share系列合计必须大于0", "补充正数或改用其他图型");
1122
+ const items = series.values.map((value, ci) => `${data.categories[ci]} ${ctx.percent(value / total)}`);
1123
+ items.push(`${isEnglish(ctx.lang) ? "total" : "合计"} ${ctx.format(total, series.field)}`);
1124
+ const placeBelow = series.values.some((value) => value > 0 && value / total * barW < labelWidth(ctx, ctx.percent(value / total)) + 4);
1125
+ const flow = placeBelow ? ctx.flow(items, barW, 12) : [];
1126
+ const label = ctx.wrap(series.label, Math.max(18, labelColumn), 12, 500);
1127
+ const labelHeight = label.length * 16;
1128
+ return {
1129
+ series,
1130
+ total,
1131
+ items,
1132
+ placeBelow,
1133
+ flow,
1134
+ label,
1135
+ height: Math.max(50, 28 + labelHeight) + flow.length * 16
1136
+ };
1137
+ });
1138
+ const children = legend.children.slice();
1139
+ let rowTop = top;
1140
+ rows.forEach(({ series, total, placeBelow, flow, label, height }) => {
1141
+ const y = rowTop + 20;
1142
+ const labelY = y + barH / 2 + 4 - (label.length - 1) * 8;
1143
+ children.push(ctx.textLines(left - 12, labelY, label, {
1144
+ anchor: "end",
1145
+ fill: ctx.color("fg"),
1146
+ meta: {
1147
+ kind: "series",
1148
+ series: series.id
1149
+ }
1150
+ }));
1151
+ children.push(ctx.rect(left, y, barW, barH, {
1152
+ rx: barH / 2,
1153
+ fill: ctx.color("grid")
1154
+ }));
1155
+ let cursor = left;
1156
+ series.values.forEach((value, ci) => {
1157
+ const width = total > 0 ? value / total * barW : 0;
1158
+ if (width <= 0) {
1159
+ cursor += width;
1160
+ return;
1161
+ }
1162
+ const category = data.categories[ci];
1163
+ const record = data.rows.find((item) => item[data.categoryField] === category)?.[spec.encode.id] ?? `${ci}`;
1164
+ const style = emphasisStyle(ctx, spec, series.id, series.field || series.id, record, ci);
1165
+ children.push(ctx.path(shareSegmentPath(cursor, y, width, barH, ci === 0, ci === data.categories.length - 1), {
1166
+ fill: style.fill,
1167
+ opacity: style.opacity,
1168
+ meta: {
1169
+ kind: "record",
1170
+ record,
1171
+ category,
1172
+ series: series.id,
1173
+ value,
1174
+ label: String(category)
1175
+ }
1176
+ }));
1177
+ const percent = ctx.percent(value / total);
1178
+ if (!placeBelow && value > 0) children.push(ctx.text(cursor + width / 2, y - 6, percent, {
1179
+ anchor: "middle",
1180
+ size: 12,
1181
+ fill: ctx.color("fg"),
1182
+ meta: {
1183
+ kind: "value",
1184
+ record,
1185
+ series: series.id,
1186
+ category,
1187
+ value: value / total,
1188
+ label: percent
1189
+ }
1190
+ }));
1191
+ cursor += width;
1192
+ });
1193
+ if (placeBelow) flow.forEach((items, i) => children.push(ctx.text(left, y + barH + 16 + i * 16, items.join(" · "), {
1194
+ size: 12,
1195
+ fill: ctx.color("muted"),
1196
+ meta: {
1197
+ kind: "value",
1198
+ series: series.id,
1199
+ label: items.join(" · ")
1200
+ }
1201
+ })));
1202
+ rowTop += height;
1203
+ });
1204
+ return {
1205
+ width: chartWidth,
1206
+ height: rowTop + 4,
1207
+ children,
1208
+ title: spec.title || aria("share", data, ctx.lang),
1209
+ description: spec.subtitle || void 0
1210
+ };
1211
+ };
1212
+ var funnel = (spec, ctx) => {
1213
+ const data = ctx.table(spec), series = data.series[0], field = data.valueFields[0], top = 8;
1214
+ const barH = 20, maxValue = Math.max(0, ...series.values), first = series.values[0];
1215
+ const labelCap = Math.max(48, ...data.categories.map((category) => {
1216
+ const full = labelWidth(ctx, category, 12, 500);
1217
+ const limit = Math.max(48, ctx.width * .42);
1218
+ if (full <= limit) return full;
1219
+ const lines = ctx.wrap(String(category), limit, 12, 500);
1220
+ return Math.max(...lines.map((line) => labelWidth(ctx, line, 12, 500)));
1221
+ }));
1222
+ const left = labelCap + 14, valueTexts = series.values.map((value) => ctx.format(value, field));
1223
+ const labels = data.categories.map((category) => ctx.wrap(String(category), labelCap, 12, 500));
1224
+ const noFirst = isEnglish(ctx.lang) ? "first step unavailable" : "首步为0,比例不可计算";
1225
+ const firstShares = series.values.map((value) => first > 0 ? `${isEnglish(ctx.lang) ? "Of first" : "占首步"} ${ctx.percent(value / first)}` : noFirst);
1226
+ const previousShares = series.values.map((value, i) => i === 0 ? "" : series.values[i - 1] > 0 ? `${isEnglish(ctx.lang) ? "Of previous" : "占上一步"} ${ctx.percent(value / series.values[i - 1])}` : noFirst);
1227
+ const readoutWidth = Math.max(32, ...valueTexts.map((text) => labelWidth(ctx, text, 14, 700)), ...firstShares.map((text) => labelWidth(ctx, text)), ...previousShares.map((text) => labelWidth(ctx, text)));
1228
+ const chartWidth = Math.max(ctx.width, left + 24 + readoutWidth + 18);
1229
+ const barW = Math.max(24, chartWidth - left - readoutWidth - 18);
1230
+ const rowH = Math.max(64, ...labels.map((lines) => lines.length * 15 + barH + 14));
1231
+ const children = [];
1232
+ data.categories.forEach((category, i) => {
1233
+ const centerY = top + i * rowH + rowH / 2 - 2;
1234
+ const label = labels[i];
1235
+ children.push(ctx.textLines(left - 12, centerY + 4 - (label.length - 1) * 8, label, {
1236
+ anchor: "end",
1237
+ fill: ctx.color("fg"),
1238
+ meta: {
1239
+ kind: "category",
1240
+ label: category,
1241
+ index: i
1242
+ }
1243
+ }));
1244
+ children.push(ctx.rect(left, centerY - barH / 2, barW, barH, {
1245
+ rx: barH / 2,
1246
+ fill: ctx.color("grid")
1247
+ }));
1248
+ const length = maxValue > 0 ? series.values[i] / maxValue * barW : 0;
1249
+ const record = data.rows[i]?.[spec.encode.id] ?? `${i}`;
1250
+ const style = emphasisStyle(ctx, spec, series.id, series.field || series.id, record, i, true);
1251
+ if (length > 0) children.push(ctx.rect(left, centerY - barH / 2, length, barH, {
1252
+ rx: barH / 2,
1253
+ fill: style.fill,
1254
+ opacity: style.opacity,
1255
+ meta: {
1256
+ kind: "record",
1257
+ record,
1258
+ series: series.id,
1259
+ category,
1260
+ value: series.values[i],
1261
+ label: String(category)
1262
+ }
1263
+ }));
1264
+ const readoutX = left + barW + 12;
1265
+ const meta = {
1266
+ kind: "value",
1267
+ record,
1268
+ series: series.id,
1269
+ category,
1270
+ value: series.values[i]
1271
+ };
1272
+ children.push(ctx.text(readoutX, centerY - 19, valueTexts[i], {
1273
+ size: 13,
1274
+ weight: 700,
1275
+ fill: ctx.color("fg"),
1276
+ meta: {
1277
+ ...meta,
1278
+ label: valueTexts[i]
1279
+ }
1280
+ }));
1281
+ children.push(ctx.text(readoutX, centerY - 3, firstShares[i], {
1282
+ size: 12,
1283
+ fill: ctx.color("muted"),
1284
+ meta: {
1285
+ ...meta,
1286
+ label: firstShares[i]
1287
+ }
1288
+ }));
1289
+ if (previousShares[i]) children.push(ctx.text(readoutX, centerY + 13, previousShares[i], {
1290
+ size: 12,
1291
+ fill: ctx.color("muted"),
1292
+ meta: {
1293
+ ...meta,
1294
+ label: previousShares[i]
1295
+ }
1296
+ }));
1297
+ });
1298
+ return {
1299
+ width: chartWidth,
1300
+ height: top + data.categories.length * rowH + 4,
1301
+ children,
1302
+ title: spec.title || aria("funnel", data, ctx.lang),
1303
+ description: spec.subtitle || void 0
1304
+ };
1305
+ };
1306
+ function install({ register }) {
1307
+ register(["line", "area"], lineArea);
1308
+ register(["column", "bar"], bars);
1309
+ register("share", share);
1310
+ register("funnel", funnel);
1311
+ }
1312
+ install({ register });
1313
+ export { basic_exports, diagramLine, install };