@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,844 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { register } from "./registry.js";
3
+ // src/chart/composition.js
4
+ var composition_exports = /* @__PURE__ */ __exportAll({
5
+ compositionPresentation: () => compositionPresentation,
6
+ install: () => install
7
+ });
8
+ var TAU = Math.PI * 2;
9
+ var polar = (cx, cy, r, a) => [cx + Math.cos(a) * r, cy + Math.sin(a) * r];
10
+ var words = (ctx, zh, en) => /^en(?:-|$)/i.test(String(ctx.lang || "")) ? en : zh;
11
+ function outerArc(cx, cy, r, a0, a1) {
12
+ const span = a1 - a0, start = polar(cx, cy, r, a0);
13
+ if (span <= 1e-12) return {
14
+ start,
15
+ curve: ""
16
+ };
17
+ if (span >= TAU - 1e-9) return {
18
+ start,
19
+ curve: ` A${r},${r} 0 1 1 ${polar(cx, cy, r, a0 + span / 2)} A${r},${r} 0 1 1 ${start}`,
20
+ full: true
21
+ };
22
+ const large = span > Math.PI ? 1 : 0;
23
+ return {
24
+ start,
25
+ curve: ` A${r},${r} 0 ${large} 1 ${polar(cx, cy, r, a1)}`,
26
+ large
27
+ };
28
+ }
29
+ var solidPath = (cx, cy, { start, curve, full }) => !curve ? `M${start}` : full ? `M${start}${curve} Z` : `M${cx},${cy} L${start}${curve} Z`;
30
+ function arcPath(cx, cy, r0, r1, a0, a1) {
31
+ const arc = outerArc(cx, cy, r1, a0, a1), outer = `M${arc.start}${arc.curve}`;
32
+ if (!arc.curve) return outer;
33
+ if (r0 <= 0) return solidPath(cx, cy, arc);
34
+ const innerStart = polar(cx, cy, r0, a0);
35
+ if (arc.full) return `${outer} Z M${innerStart} A${r0},${r0} 0 1 0 ${polar(cx, cy, r0, a0 + (a1 - a0) / 2)} A${r0},${r0} 0 1 0 ${innerStart} Z`;
36
+ return `${outer} L${polar(cx, cy, r0, a1)} A${r0},${r0} 0 ${arc.large} 0 ${innerStart} Z`;
37
+ }
38
+ function solidSectors(ctx, cx, cy, sectors) {
39
+ const fills = [], border = [];
40
+ const arcs = sectors.map(({ r, a0, a1 }) => r > 0 ? outerArc(cx, cy, r, a0, a1) : null);
41
+ for (const [i, { r, a0, fill, meta }] of sectors.entries()) {
42
+ const arc = arcs[i];
43
+ if (arc) fills.push(ctx.path(solidPath(cx, cy, arc), {
44
+ fill,
45
+ meta
46
+ }));
47
+ if (arc?.curve) border.push(`M${arc.start}${arc.curve}${arc.full ? " Z" : ""}`);
48
+ if (sectors.length < 2) continue;
49
+ const previous = (i + sectors.length - 1) % sectors.length;
50
+ const reach = Math.max(arc?.curve ? r : 0, arcs[previous]?.curve ? sectors[previous].r : 0);
51
+ if (reach > 0) border.push(`M${cx},${cy} L${polar(cx, cy, reach, a0)}`);
52
+ }
53
+ if (border.length) fills.push(ctx.path(border.join(" "), {
54
+ fill: "none",
55
+ stroke: ctx.color("bg"),
56
+ "stroke-width": 2,
57
+ "stroke-linecap": "round",
58
+ "pointer-events": "none"
59
+ }));
60
+ return fills;
61
+ }
62
+ var pointMeta = (row, field, value, name) => ({
63
+ id: row.id == null ? void 0 : String(row.id),
64
+ kind: name,
65
+ record: row,
66
+ value,
67
+ label: field
68
+ });
69
+ var stackedLegendWidth = (ctx, items, maxWidth) => {
70
+ if (items.length < 2) return maxWidth;
71
+ const widths = items.map((item) => 16 + ctx.measure(item.label, 12, 400));
72
+ const longest = Math.max(...widths);
73
+ if (longest <= maxWidth) return longest;
74
+ return maxWidth;
75
+ };
76
+ var pieSlices = (spec, ctx) => {
77
+ const table = ctx.table(spec), field = table.valueFields[0];
78
+ const rows = table.categories.map((category) => table.rows.find((r) => r[table.categoryField] === category)).filter(Boolean);
79
+ const values = rows.map((row) => row[field]);
80
+ const total = values.reduce((sum, value) => sum + value, 0);
81
+ if (!(total > 0)) ctx.fit("构成图总值必须大于0", "为至少一个类别提供正值");
82
+ return {
83
+ table,
84
+ field,
85
+ rows,
86
+ values,
87
+ total
88
+ };
89
+ };
90
+ var compositionPresentation = Symbol("composition presentation");
91
+ function pieLayout(spec, ctx, isDonut = false) {
92
+ const { table, field, rows, values, total } = pieSlices(spec, ctx);
93
+ const width = ctx.width;
94
+ const items = rows.map((row, i) => ({
95
+ label: `${row[table.categoryField]} ${ctx.format(values[i], field)} (${ctx.percent(values[i] / total)})`,
96
+ color: ctx.color(`tone${i % 6 + 1}`),
97
+ meta: pointMeta(row, String(row[table.categoryField]), values[i], spec.chart)
98
+ }));
99
+ const sideLegend = width > 560;
100
+ const legendWidth = sideLegend ? Math.min(320, Math.max(180, width * .34)) : Math.min(width - 32, 270);
101
+ const chartSpan = sideLegend ? Math.min(width - legendWidth - 36, 460) : width;
102
+ const groupWidth = sideLegend ? chartSpan + 28 + legendWidth : width;
103
+ const groupX = sideLegend ? (width - groupWidth) / 2 : 0;
104
+ const cx = sideLegend ? groupX + chartSpan / 2 : width / 2;
105
+ const r = width <= 360 ? Math.min(112, width * .275) : Math.min(sideLegend ? 168 : 112, Math.max(60, chartSpan * .32));
106
+ const cy = r + 36, inner = isDonut ? r * .58 : 0;
107
+ const labelRadius = r * (spec[compositionPresentation]?.labelRadius ?? .72);
108
+ const children = [], sectors = [], sliceLabels = [], ticksVariant = isDonut && spec.variant === "ticks";
109
+ let a = -Math.PI / 2;
110
+ rows.forEach((row, i) => {
111
+ const next = a + TAU * values[i] / total;
112
+ if (!ticksVariant && values[i] > 0) {
113
+ const fill = ctx.color(`tone${i % 6 + 1}`);
114
+ const meta = pointMeta(row, String(row[table.categoryField]), values[i], spec.chart);
115
+ if (isDonut) children.push(ctx.path(arcPath(cx, cy, inner, r, a, next), {
116
+ fill,
117
+ stroke: ctx.color("bg"),
118
+ "stroke-width": 2,
119
+ meta
120
+ }));
121
+ else sectors.push({
122
+ r,
123
+ a0: a,
124
+ a1: next,
125
+ fill,
126
+ meta
127
+ });
128
+ if (spec.sliceLabels && next - a >= 14 * Math.PI / 180 && 2 * labelRadius * Math.sin(Math.min(Math.PI, next - a) / 2) >= ctx.measure("100%", 12, 600) + 8) {
129
+ const [x, y] = polar(cx, cy, labelRadius, (a + next) / 2);
130
+ sliceLabels.push(ctx.text(x, y + 4, `${Math.round(100 * values[i] / total)}%`, {
131
+ anchor: "middle",
132
+ size: 12,
133
+ weight: 600,
134
+ fill: ctx.textOn(fill)
135
+ }));
136
+ }
137
+ }
138
+ a = next;
139
+ });
140
+ if (!isDonut) children.push(...solidSectors(ctx, cx, cy, sectors), ...sliceLabels);
141
+ if (ticksVariant) {
142
+ const count = Math.min(240, Math.max(96, Math.floor(TAU * (r - 8.5) / 2.5)));
143
+ for (let tick = 0; tick < count; tick += 1) {
144
+ const fraction = (tick + .5) / count;
145
+ let cumulative = 0, index = values.length - 1;
146
+ for (let i = 0; i < values.length; i += 1) {
147
+ cumulative += values[i] / total;
148
+ if (fraction <= cumulative) {
149
+ index = i;
150
+ break;
151
+ }
152
+ }
153
+ const angle = -Math.PI / 2 + TAU * fraction, p1 = polar(cx, cy, r - 17, angle), p2 = polar(cx, cy, r, angle);
154
+ children.push(ctx.line(...p1, ...p2, {
155
+ stroke: ctx.color(`tone${index % 6 + 1}`),
156
+ "stroke-width": 2,
157
+ "stroke-linecap": "round",
158
+ meta: {
159
+ kind: "donut-tick",
160
+ category: String(rows[index][table.categoryField]),
161
+ fraction
162
+ }
163
+ }));
164
+ }
165
+ }
166
+ if (isDonut) {
167
+ const caption = spec.fields?.[field]?.label || words(ctx, "合计", "Total");
168
+ const hole = ticksVariant ? r - 17 : inner;
169
+ const room = (dy) => 2 * Math.sqrt(Math.max(0, hole * hole - dy * dy)) - 16;
170
+ const whole = ctx.format(total, field), unit = String(ctx.field(field).unit || "").trim();
171
+ if (ctx.measure(whole, 20, 650) <= room(20) || !unit) children.push(ctx.text(cx, cy - 12, caption, {
172
+ anchor: "middle",
173
+ size: 12,
174
+ fill: ctx.color("muted")
175
+ }), ctx.text(cx, cy + 14, whole, {
176
+ anchor: "middle",
177
+ size: 20,
178
+ weight: 650,
179
+ fill: ctx.color("fg")
180
+ }));
181
+ else children.push(ctx.text(cx, cy - 20, caption, {
182
+ anchor: "middle",
183
+ size: 12,
184
+ fill: ctx.color("muted")
185
+ }), ctx.text(cx, cy + 6, ctx.format(total, field, false), {
186
+ anchor: "middle",
187
+ size: 20,
188
+ weight: 650,
189
+ fill: ctx.color("fg")
190
+ }), ctx.text(cx, cy + 24, unit, {
191
+ anchor: "middle",
192
+ size: 12,
193
+ fill: ctx.color("muted")
194
+ }));
195
+ }
196
+ const actualLegendWidth = sideLegend || !(width <= 360) ? legendWidth : stackedLegendWidth(ctx, items, legendWidth);
197
+ const legend = ctx.legend(items, actualLegendWidth);
198
+ if (sideLegend) {
199
+ const legendX = groupX + chartSpan + 28;
200
+ const legendY = Math.max(16, cy - legend.height / 2);
201
+ children.push(ctx.el("g", { transform: `translate(${legendX}, ${legendY})` }, legend.children));
202
+ return ctx.scene(Math.max(cy + r + 24, legendY + legend.height + 8), children, spec.title);
203
+ }
204
+ children.push(ctx.el("g", { transform: `translate(${(width - actualLegendWidth) / 2}, ${cy + r + 24})` }, legend.children));
205
+ return ctx.scene(cy + r + 30 + legend.height, children, spec.title);
206
+ }
207
+ function radialShare(spec, ctx) {
208
+ const t = ctx.table(spec), valueField = t.valueFields[0], intensityField = spec.encode.intensity;
209
+ const rows = t.categories.map((category) => t.rows.find((row) => row[t.categoryField] === category)).filter(Boolean);
210
+ const sum = rows.reduce((n, row) => n + row[valueField], 0), [, hi] = ctx.field(intensityField).domain || ctx.extent(rows.map((r) => r[intensityField]));
211
+ const sideLegend = ctx.width > 560;
212
+ const legendWidth = sideLegend ? Math.min(360, Math.max(200, ctx.width * .38)) : Math.min(360, ctx.width - 32);
213
+ const chartSpan = sideLegend ? Math.min(ctx.width - legendWidth - 36, 460) : ctx.width;
214
+ const groupX = sideLegend ? (ctx.width - chartSpan - 28 - legendWidth) / 2 : 0;
215
+ const cx = sideLegend ? groupX + chartSpan / 2 : ctx.width / 2;
216
+ const maxR = ctx.width <= 360 ? Math.min(112, ctx.width * .275) : Math.min(sideLegend ? 168 : 112, chartSpan * .32);
217
+ const cy = maxR + 42, minR = 44, children = [];
218
+ let a = -Math.PI / 2;
219
+ rows.forEach((row, i) => {
220
+ const share = row[valueField] / sum, next = a + TAU * share, intensity = row[intensityField];
221
+ const strength = hi > 0 ? Math.max(0, Math.min(1, intensity / hi)) : 0;
222
+ const r = Math.sqrt(1936 + (maxR * maxR - 1936) * strength);
223
+ if (share > 0) children.push(ctx.path(arcPath(cx, cy, minR, r, a, next), {
224
+ fill: ctx.color(`tone${i % 6 + 1}`),
225
+ stroke: ctx.color("bg"),
226
+ "stroke-width": 2,
227
+ meta: {
228
+ ...pointMeta(row, String(row[t.categoryField]), row[valueField], "radial-share"),
229
+ intensity
230
+ }
231
+ }));
232
+ children.push(ctx.path(arcPath(cx, cy, maxR + 6, maxR + 9, a, next), {
233
+ fill: ctx.color(`tone${i % 6 + 1}`),
234
+ meta: {
235
+ kind: "share-label-ring",
236
+ label: String(row[t.categoryField]),
237
+ value: row[valueField]
238
+ }
239
+ }));
240
+ a = next;
241
+ });
242
+ const key = [words(ctx, "角度=份额", "Angle = share"), words(ctx, "半径=强度", "Radius = intensity")];
243
+ const room = (dy) => 2 * Math.sqrt(Math.max(0, 1936 - dy * dy)) - 12;
244
+ const inside = ctx.measure(key[0], 12) <= room(16) && ctx.measure(key[1], 12) <= room(17);
245
+ if (inside) children.push(ctx.text(cx, cy - 4, key[0], {
246
+ anchor: "middle",
247
+ size: 12,
248
+ fill: ctx.color("fg")
249
+ }), ctx.text(cx, cy + 14, key[1], {
250
+ anchor: "middle",
251
+ size: 12,
252
+ fill: ctx.color("muted")
253
+ }));
254
+ const note = (x, top, w) => {
255
+ if (inside) return 0;
256
+ const lines = ctx.wrap(words(ctx, "角度表示份额,半径表示强度。", "Angle shows share; radius shows intensity."), w, 12);
257
+ children.push(ctx.textLines(x, top + 12, lines, {
258
+ fill: ctx.color("muted"),
259
+ meta: { kind: "radial-share-key" }
260
+ }));
261
+ return lines.length * 16 + 8;
262
+ };
263
+ const items = rows.map((row, i) => ({
264
+ label: `${row[t.categoryField]} · ${ctx.format(row[valueField], valueField)} (${ctx.percent(row[valueField] / sum)}) · ${words(ctx, "强度", "intensity")} ${ctx.format(row[intensityField], intensityField)}`,
265
+ color: ctx.color(`tone${i % 6 + 1}`),
266
+ meta: {
267
+ kind: "share-label",
268
+ label: String(row[t.categoryField]),
269
+ value: row[valueField],
270
+ intensity: row[intensityField]
271
+ }
272
+ }));
273
+ const actualLegendWidth = sideLegend || ctx.width > 360 ? legendWidth : stackedLegendWidth(ctx, items, legendWidth);
274
+ const legend = ctx.legend(items, actualLegendWidth);
275
+ if (sideLegend) {
276
+ const legendX = groupX + chartSpan + 28;
277
+ const legendY = Math.max(16, cy - legend.height / 2);
278
+ children.push(ctx.el("g", { transform: `translate(${legendX}, ${legendY})` }, legend.children));
279
+ const noted = note(legendX, legendY + legend.height + 10, legendWidth);
280
+ return ctx.scene(Math.max(cy + maxR + 24, legendY + legend.height + 8 + noted), children, spec.title);
281
+ }
282
+ const legendY = cy + maxR + 22, legendX = (ctx.width - actualLegendWidth) / 2;
283
+ children.push(ctx.el("g", { transform: `translate(${legendX}, ${legendY})` }, legend.children));
284
+ const noted = note(legendX, legendY + legend.height + 10, actualLegendWidth);
285
+ return ctx.scene(legendY + legend.height + 8 + noted, children, spec.title);
286
+ }
287
+ function rose(spec, ctx) {
288
+ const { table, field, rows, values, total } = pieSlices(spec, ctx);
289
+ const sideLegend = ctx.width > 560;
290
+ const legendWidth = sideLegend ? Math.min(320, Math.max(180, ctx.width * .34)) : Math.min(360, ctx.width - 32);
291
+ const chartSpan = sideLegend ? Math.min(ctx.width - legendWidth - 36, 460) : ctx.width;
292
+ const groupX = sideLegend ? (ctx.width - chartSpan - 28 - legendWidth) / 2 : 0;
293
+ const cx = sideLegend ? groupX + chartSpan / 2 : ctx.width / 2;
294
+ const maxR = ctx.width <= 360 ? Math.min(116, ctx.width * .275) : Math.min(sideLegend ? 168 : 116, chartSpan * .32);
295
+ const cy = maxR + 32, maxValue = Math.max(...values);
296
+ let a = -Math.PI / 2;
297
+ const children = solidSectors(ctx, cx, cy, rows.map((row, i) => {
298
+ const next = a + TAU / rows.length;
299
+ const sector = {
300
+ a0: a,
301
+ a1: next,
302
+ r: maxR * Math.sqrt(values[i] / maxValue),
303
+ fill: ctx.color(`tone${i % 6 + 1}`),
304
+ meta: pointMeta(row, String(row[table.categoryField]), values[i], "rose")
305
+ };
306
+ a = next;
307
+ return sector;
308
+ }));
309
+ const items = rows.map((row, i) => ({
310
+ label: `${row[table.categoryField]} · ${ctx.format(values[i], field)}`,
311
+ color: ctx.color(`tone${i % 6 + 1}`),
312
+ meta: {
313
+ kind: "rose-label",
314
+ label: String(row[table.categoryField]),
315
+ value: values[i]
316
+ }
317
+ }));
318
+ const actualLegendWidth = sideLegend || ctx.width > 360 ? legendWidth : stackedLegendWidth(ctx, items, legendWidth);
319
+ const legend = ctx.legend(items, actualLegendWidth);
320
+ const caption = spec.fields?.[field]?.label || words(ctx, "合计", "Total");
321
+ if (sideLegend) {
322
+ const legendX = groupX + chartSpan + 28;
323
+ const legendY = Math.max(16, cy - legend.height / 2);
324
+ children.push(ctx.text(cx, cy + maxR + 22, `${caption} ${ctx.format(total, field)}`, {
325
+ anchor: "middle",
326
+ size: 12,
327
+ fill: ctx.color("muted")
328
+ }));
329
+ children.push(ctx.el("g", { transform: `translate(${legendX}, ${legendY})` }, legend.children));
330
+ return ctx.scene(Math.max(cy + maxR + 40, legendY + legend.height + 8), children, spec.title);
331
+ }
332
+ const readingY = cy + maxR + 18, legendY = readingY + 20;
333
+ children.push(ctx.text(cx, readingY, `${caption} ${ctx.format(total, field)}`, {
334
+ anchor: "middle",
335
+ size: 12,
336
+ fill: ctx.color("muted")
337
+ }));
338
+ children.push(ctx.el("g", { transform: `translate(${(ctx.width - actualLegendWidth) / 2}, ${legendY})` }, legend.children));
339
+ return ctx.scene(legendY + legend.height + 8, children, spec.title);
340
+ }
341
+ function gauge(spec, ctx) {
342
+ const row = spec.data[0], field = spec.encode.value, value = row[field], target = spec.encode.target ? row[spec.encode.target] : null;
343
+ const [min, max] = spec.range, ratio = (value - min) / (max - min), x = 64, width = Math.min(ctx.width - 128, 560), children = [];
344
+ const unitValue = (v) => ctx.format(v, field), targetLabel = words(ctx, "目标", "Target");
345
+ const arcRadius = Math.min(200, ctx.width * .38);
346
+ const ends = ctx.ticks([min, max], field, { count: 2 });
347
+ const endText = (v) => {
348
+ const index = ends.ticks.findIndex((tick) => Math.abs(tick - v) < 1e-9);
349
+ return index >= 0 ? ends.labels[index] : unitValue(v);
350
+ };
351
+ const reading = `${unitValue(value)}${target == null ? "" : ` · ${targetLabel} ${unitValue(target)}`}`;
352
+ const readingWidth = ctx.measure(reading, 16, 600);
353
+ const readingX = readingWidth <= width ? x : x + width / 2;
354
+ const readingAnchor = readingWidth <= width ? "start" : "middle";
355
+ const barReadingY = 34;
356
+ if ((spec.variant || "bar") === "bar") {
357
+ const y = 56;
358
+ children.push(ctx.rect(x, y, width, 18, {
359
+ rx: 9,
360
+ fill: ctx.color("grid")
361
+ }), ctx.rect(x, y, width * ratio, 18, {
362
+ rx: 9,
363
+ fill: ctx.color("accent"),
364
+ meta: {
365
+ kind: "gauge-value",
366
+ value
367
+ }
368
+ }));
369
+ if (target != null) {
370
+ const tx = x + width * (target - min) / (max - min);
371
+ children.push(ctx.line(tx, 47, tx, 83, {
372
+ stroke: ctx.color("ink"),
373
+ "stroke-width": 2,
374
+ meta: {
375
+ kind: "gauge-target",
376
+ value: target
377
+ }
378
+ }));
379
+ }
380
+ children.push(ctx.text(x, 100, endText(min), { size: 12 }), ctx.text(x + width, 100, endText(max), {
381
+ anchor: "end",
382
+ size: 12
383
+ }));
384
+ } else {
385
+ const cx = ctx.width / 2, r = arcRadius, cy = r + 26, start = Math.PI, end = Math.PI + Math.PI * ratio;
386
+ const ticks = 20;
387
+ for (let i = 0; i <= ticks; i++) {
388
+ const a = Math.PI + Math.PI * i / ticks, p1 = polar(cx, cy, r + 4, a), p2 = polar(cx, cy, r + (i % 5 === 0 ? 15 : 10), a);
389
+ children.push(ctx.line(...p1, ...p2, {
390
+ stroke: ctx.color(i / ticks <= ratio ? "accent" : "axis"),
391
+ "stroke-width": i % 5 === 0 ? 2 : 1,
392
+ meta: {
393
+ kind: "gauge-tick",
394
+ major: i % 5 === 0,
395
+ fraction: i / ticks
396
+ }
397
+ }));
398
+ }
399
+ if ((spec.variant || "bar") === "arc") {
400
+ children.push(ctx.path(arcPath(cx, cy, r - 7, r, start, 2 * Math.PI), {
401
+ fill: ctx.color("grid"),
402
+ meta: { kind: "gauge-track" }
403
+ }));
404
+ children.push(ctx.path(arcPath(cx, cy, r - 7, r, start, end), {
405
+ fill: ctx.color("accent"),
406
+ meta: {
407
+ kind: "gauge-arc",
408
+ value
409
+ }
410
+ }));
411
+ } else {
412
+ const [x1, y1] = polar(cx, cy, 8, start);
413
+ const [x2, y2] = polar(cx, cy, r, end);
414
+ children.push(ctx.line(x1, y1, x2, y2, {
415
+ stroke: ctx.color("accent"),
416
+ "stroke-width": 3,
417
+ "stroke-linecap": "round",
418
+ meta: {
419
+ kind: "gauge-needle",
420
+ value
421
+ }
422
+ }), ctx.circle(cx, cy, 6, { fill: ctx.color("ink") }));
423
+ }
424
+ const minLabel = endText(min), maxLabel = endText(max);
425
+ const minLabelWidth = ctx.measure(minLabel, 12, 400), maxLabelWidth = ctx.measure(maxLabel, 12, 400);
426
+ const minLabelX = Math.max(minLabelWidth / 2, cx - r);
427
+ const maxLabelX = Math.min(ctx.width - maxLabelWidth / 2, cx + r);
428
+ children.push(ctx.text(cx, cy + 54, unitValue(value), {
429
+ anchor: "middle",
430
+ size: 20,
431
+ weight: 600,
432
+ fill: ctx.color("fg"),
433
+ meta: {
434
+ kind: "gauge-reading",
435
+ value
436
+ }
437
+ }), ctx.text(minLabelX, cy + 24, minLabel, {
438
+ anchor: "middle",
439
+ size: 12,
440
+ meta: {
441
+ kind: "gauge-min",
442
+ value: min
443
+ }
444
+ }), ctx.text(maxLabelX, cy + 24, maxLabel, {
445
+ anchor: "middle",
446
+ size: 12,
447
+ meta: {
448
+ kind: "gauge-max",
449
+ value: max
450
+ }
451
+ }));
452
+ if (target != null) {
453
+ const fraction = (target - min) / (max - min);
454
+ const angle = Math.PI + Math.PI * fraction;
455
+ const label = `${targetLabel} ${unitValue(target)}`;
456
+ const labelWidth = ctx.measure(label, 12, 400);
457
+ const anchor = fraction <= .2 ? "start" : fraction >= .8 ? "end" : "middle";
458
+ const inward = anchor === "end" ? -labelWidth / 2 : anchor === "start" ? labelWidth / 2 : 0;
459
+ const [tx, targetY] = polar(cx, cy, r + 20, angle);
460
+ const ty = Math.max(12, targetY);
461
+ children.push(ctx.text(tx + inward, ty, label, {
462
+ anchor,
463
+ size: 12,
464
+ fill: ctx.color("ink"),
465
+ meta: {
466
+ kind: "gauge-target",
467
+ value: target
468
+ }
469
+ }));
470
+ }
471
+ }
472
+ if ((spec.variant || "bar") === "bar") children.push(ctx.text(readingX, barReadingY, reading, {
473
+ anchor: readingAnchor,
474
+ size: 16,
475
+ weight: 600,
476
+ fill: ctx.color("fg"),
477
+ meta: {
478
+ kind: "gauge-reading",
479
+ value,
480
+ target
481
+ }
482
+ }));
483
+ const scene = ctx.scene((spec.variant || "bar") === "bar" ? 122 : arcRadius + 26 + 92, children, spec.title);
484
+ if ((spec.variant || "bar") === "bar") {
485
+ const readingRight = readingAnchor === "middle" ? readingX + readingWidth / 2 : readingX + readingWidth;
486
+ scene.width = Math.max(ctx.width, readingRight + 16, x + width + 16);
487
+ }
488
+ return scene;
489
+ }
490
+ var treeNodes = (root) => {
491
+ const walk = (node, parent, depth, path) => {
492
+ const item = {
493
+ node,
494
+ parent,
495
+ depth,
496
+ path,
497
+ children: [],
498
+ value: 0
499
+ };
500
+ const nodes = Array.isArray(node.children) ? node.children.filter(Boolean) : [];
501
+ item.children = nodes.map((child, i) => walk(child, item, depth + 1, `${path}.${i + 1}`));
502
+ item.value = nodes.length ? item.children.reduce((sum, child) => sum + child.value, 0) : Number(node.value || 0);
503
+ return item;
504
+ };
505
+ const entry = walk(root, null, 0, "1"), out = [];
506
+ const visit = (item) => {
507
+ out.push(item);
508
+ item.children.forEach(visit);
509
+ };
510
+ visit(entry);
511
+ return out;
512
+ };
513
+ function fallbackLegend(children, entries, x, y, width, ctx, kind, format = (value) => ctx.format(value, "value")) {
514
+ for (const item of entries) {
515
+ const lines = ctx.wrap(`${item.index == null ? "" : `${item.index}. `}${item.label} · ${format(item.value)}`, Math.max(1, width - 28), 12);
516
+ children.push(ctx.rect(x, y - 10, 12, 12, { fill: item.color }), ctx.textLines(x + 18, y, lines, {
517
+ size: 12,
518
+ fill: ctx.color("fg")
519
+ }));
520
+ const text = children[children.length - 1];
521
+ if (text.children) text.children[0].meta = {
522
+ kind,
523
+ path: item.path,
524
+ label: item.label,
525
+ value: item.value,
526
+ index: item.index
527
+ };
528
+ y += lines.length * 16 + 2;
529
+ }
530
+ return y;
531
+ }
532
+ function treemap(spec, ctx) {
533
+ const root = spec.data, tree = treeNodes(root), leaves = tree.filter((x) => !x.children.length);
534
+ const diagram = spec[compositionPresentation];
535
+ let width = Math.min(ctx.width - 48, 752), mapHeight = 242, needWidth = false, needHeight = false;
536
+ const tones = new Map(leaves.map((leaf, i) => [leaf, `tone${i % 6 + 1}`])), children = [], fallback = [];
537
+ const statusOf = (entry) => ctx.design[entry.node[compositionPresentation]?.status];
538
+ const colorOf = (leaf) => statusOf(leaf) || ctx.color(tones.get(leaf));
539
+ const showValues = diagram?.showValues !== false;
540
+ const format = (value) => `${diagram?.prefix || ""}${ctx.format(value, "value")}`;
541
+ const wrapLink = (entry, start) => {
542
+ const link = entry.node[compositionPresentation]?.link;
543
+ if (link) children.push(ctx.link(link.href, children.splice(start), link));
544
+ };
545
+ const place = (entry, x, y, w, h, depth, hidden = []) => {
546
+ const start = children.length;
547
+ if (depth > 0 && w > 0 && h > 0) children.push(ctx.rect(x, y, w, h, {
548
+ fill: entry.children.length && statusOf(entry) ? ctx.tint(ctx.palette.bg, statusOf(entry), .14) : depth === 1 ? ctx.color("bg") : "none",
549
+ stroke: statusOf(entry) || ctx.color("axis"),
550
+ "stroke-width": 1,
551
+ meta: {
552
+ kind: diagram ? "frame" : "treemap-node",
553
+ ...diagram ? { id: entry.path } : {},
554
+ label: String(entry.node.label),
555
+ value: entry.value,
556
+ path: entry.path
557
+ }
558
+ }));
559
+ if (!entry.children.length) {
560
+ if (diagram && entry.value > 0) {
561
+ const index = leaves.indexOf(entry) + 1, label = String(entry.node.label), value = format(entry.value);
562
+ const tileW = w - 4, tileH = h - 4;
563
+ const fits = (text) => ctx.measure(text, 12, 600) + 8 <= tileW;
564
+ if (tileH < 16 || !fits(String(index))) {
565
+ needHeight ||= tileH < 16;
566
+ needWidth ||= !fits(String(index));
567
+ return;
568
+ }
569
+ const qualified = [...hidden, label].join(" / ");
570
+ const name = fits(qualified) ? qualified : label, full = `${name} · ${value}`;
571
+ const lines = showValues && fits(full) ? [full] : fits(name) ? showValues && tileH >= 34 && fits(value) ? [name, value] : [name] : [String(index)];
572
+ const indexed = lines[0] === String(index) && !fits(name);
573
+ const fill = colorOf(entry), ink = ctx.textOn(statusOf(entry) || tones.get(entry));
574
+ children.push(ctx.rect(x + 2, y + 2, tileW, tileH, {
575
+ fill,
576
+ meta: {
577
+ kind: "node",
578
+ id: entry.path,
579
+ path: entry.path,
580
+ label,
581
+ value
582
+ }
583
+ }));
584
+ lines.forEach((text, i) => children.push(ctx.text(x + 6, y + 2 + (tileH < 22 ? tileH / 2 + 4 : 15) + i * 15, text, {
585
+ size: 12,
586
+ weight: 600,
587
+ fill: ink,
588
+ meta: {
589
+ kind: indexed ? "treemap-tile-index" : "treemap-tile-label",
590
+ path: entry.path,
591
+ label,
592
+ ...indexed ? { index } : {}
593
+ }
594
+ })));
595
+ if (indexed || name !== qualified) fallback.push({
596
+ index: indexed ? index : null,
597
+ label: qualified,
598
+ value: entry.value,
599
+ path: entry.path,
600
+ color: fill
601
+ });
602
+ } else if (!diagram && entry.value > 0 && w > 0 && h > 0) {
603
+ const i = leaves.indexOf(entry), inset = 2, label = String(entry.node.label), value = format(entry.value);
604
+ const listed = [...hidden, label].join(" / ");
605
+ const tileW = Math.max(0, w - 4), tileH = Math.max(0, h - 4);
606
+ const labelColor = ctx.textOn(statusOf(entry) || tones.get(entry));
607
+ children.push(ctx.rect(x + inset, y + inset, tileW, tileH, {
608
+ fill: colorOf(entry),
609
+ meta: {
610
+ kind: diagram ? "node" : "treemap-leaf",
611
+ ...diagram ? { id: entry.path } : {},
612
+ label,
613
+ value: entry.value,
614
+ path: entry.path
615
+ }
616
+ }));
617
+ const fullLabel = showValues ? `${label} · ${value}` : label;
618
+ const fullWidth = ctx.measure(fullLabel, 12, 600);
619
+ const index = i + 1;
620
+ if (!hidden.length && tileW >= fullWidth + 12 && tileH >= 34) children.push(ctx.text(x + 7, y + 18, fullLabel, {
621
+ size: 12,
622
+ weight: 600,
623
+ fill: labelColor,
624
+ meta: {
625
+ kind: "treemap-tile-label",
626
+ label,
627
+ value: entry.value,
628
+ path: entry.path,
629
+ mode: "name-value"
630
+ }
631
+ }));
632
+ else {
633
+ fallback.push({
634
+ index,
635
+ label: listed,
636
+ value: entry.value,
637
+ path: entry.path,
638
+ color: colorOf(entry)
639
+ });
640
+ if (tileW >= ctx.measure(String(index), 12, 600) + 12 && tileH >= 22) children.push(ctx.text(x + 7, y + 16, String(index), {
641
+ size: 12,
642
+ weight: 600,
643
+ fill: labelColor,
644
+ meta: {
645
+ kind: "treemap-tile-index",
646
+ label,
647
+ value: entry.value,
648
+ path: entry.path,
649
+ index
650
+ }
651
+ }));
652
+ }
653
+ } else if (!entry.children.length) {
654
+ const i = leaves.indexOf(entry);
655
+ fallback.push({
656
+ index: diagram ? null : i + 1,
657
+ label: [...hidden, String(entry.node.label)].join(" / "),
658
+ value: entry.value,
659
+ path: entry.path,
660
+ color: colorOf(entry)
661
+ });
662
+ }
663
+ wrapLink(entry, start);
664
+ return;
665
+ }
666
+ const header = depth > 0 && h >= 32 && ctx.measure(String(entry.node.label), 12, 600) + 14 <= w ? 20 : 0;
667
+ if (depth > 0 && header) children.push(ctx.text(x + 7, y + 15, String(entry.node.label), {
668
+ size: 12,
669
+ weight: 600,
670
+ fill: ctx.color("fg"),
671
+ meta: {
672
+ kind: "treemap-group-label",
673
+ label: String(entry.node.label),
674
+ path: entry.path
675
+ }
676
+ }));
677
+ wrapLink(entry, start);
678
+ const below = depth > 0 && !header ? [...hidden, String(entry.node.label)] : hidden;
679
+ const contentY = y + header, contentH = Math.max(0, h - header);
680
+ const vertical = depth % 2 === 0;
681
+ const span = vertical ? w : contentH;
682
+ let offset = 0;
683
+ entry.children.forEach((child) => {
684
+ const part = entry.value > 0 && child.value > 0 ? span * child.value / entry.value : 0;
685
+ if (vertical) place(child, x + offset, contentY, part, contentH, depth + 1, below);
686
+ else place(child, x, contentY + offset, w, part, depth + 1, below);
687
+ offset += part;
688
+ });
689
+ };
690
+ const rootTotal = () => spec.fields?.value?.unit === "$" && spec.fields.value.decimals === 0 ? `$${new Intl.NumberFormat("en-US", { maximumFractionDigits: 0 }).format(tree[0].value)}` : format(tree[0].value);
691
+ const heading = diagram?.namedRoot ? `${root.label} · ${rootTotal()}` : "";
692
+ let top;
693
+ for (let attempt = 0;; attempt += 1) {
694
+ const rootLines = heading ? ctx.wrap(heading, width, 13, 650) : [];
695
+ top = 44 + rootLines.length * 18;
696
+ if (rootLines.length) children.push(ctx.textLines(24, 39, rootLines, {
697
+ size: 13,
698
+ weight: 650,
699
+ leading: 18,
700
+ fill: ctx.color("fg")
701
+ }));
702
+ place(tree[0], 24, top, width, mapHeight, 0);
703
+ if (!needWidth && !needHeight) break;
704
+ if (attempt === 6) ctx.fit("矩形树图的最小分块无法容纳编号", "拆分图表或缩小权重差距");
705
+ if (needWidth) width *= 1.5;
706
+ if (needHeight) mapHeight *= 1.5;
707
+ needWidth = needHeight = false;
708
+ children.length = fallback.length = 0;
709
+ }
710
+ const listEnd = fallbackLegend(children, fallback, 24, top + mapHeight + 24, Math.min(width, 360), ctx, "treemap-name", format);
711
+ const height = fallback.length ? listEnd + 8 : top + mapHeight + 8;
712
+ const scene = ctx.scene(height, children, spec.title);
713
+ if (diagram) scene.width = Math.max(scene.width, width + 48);
714
+ return scene;
715
+ }
716
+ function sunburst(spec, ctx) {
717
+ const tree = treeNodes(spec.data), root = tree[0], nodes = tree.filter((entry) => entry.depth > 0), fallback = [];
718
+ const indexes = new Map(nodes.map((entry, i) => [entry, i + 1]));
719
+ const maxDepth = Math.max(1, ...tree.map((entry) => entry.depth)), outerR = Math.max(8, Math.min(ctx.width / 2 - 20, 220));
720
+ const rootLabel = String(root.node.label);
721
+ const rootWidth = ctx.measure(rootLabel, 12, 600);
722
+ const hole = Math.min(Math.max(36, rootWidth + 16), Math.max(36, outerR * .42));
723
+ const r0 = Math.min(hole, Math.max(36, outerR / (maxDepth + 1)));
724
+ const thickness = (outerR - r0) / maxDepth, cx = ctx.width / 2, cy = outerR + 24, children = [];
725
+ const rootBranch = (entry) => {
726
+ let branch = entry;
727
+ while (branch.parent && branch.parent !== root) branch = branch.parent;
728
+ return branch;
729
+ };
730
+ const branchColor = new Map(root.children.map((branch, i) => [branch, ctx.palette[`tone${i % 6 + 1}`]]));
731
+ const colorFor = (entry) => branchColor.get(rootBranch(entry)) || ctx.palette.tone1;
732
+ const shadeFor = (entry) => ctx.mix(colorFor(entry), ctx.palette.bg, Math.min(.72, .35 + Math.max(0, entry.depth - 1) * .12));
733
+ const walk = (entry, a0, a1) => {
734
+ const depth = entry.depth;
735
+ if (depth > 0 && entry.value > 0 && a1 > a0) {
736
+ const nodeColor = shadeFor(entry);
737
+ const innerR = r0 + (depth - 1) * thickness, outer = r0 + depth * thickness, index = indexes.get(entry);
738
+ children.push(ctx.path(arcPath(cx, cy, innerR, outer, a0, a1), {
739
+ fill: nodeColor,
740
+ stroke: ctx.color("bg"),
741
+ "stroke-width": 2,
742
+ meta: {
743
+ kind: "sunburst-node",
744
+ label: String(entry.node.label),
745
+ value: entry.value,
746
+ path: entry.path,
747
+ depth
748
+ }
749
+ }));
750
+ const midR = (innerR + outer) / 2, midA = (a0 + a1) / 2, available = 2 * midR * Math.sin(Math.min(Math.PI, a1 - a0) / 2), radialWidth = 2 * Math.max(0, midR - innerR - 5), name = String(entry.node.label);
751
+ const fullLabel = `${name} · ${ctx.format(entry.value, "value")}`, fullLabelWidth = ctx.measure(fullLabel, 12, 600);
752
+ const labelOn = ctx.textOn(nodeColor);
753
+ if (thickness >= 16 && available >= fullLabelWidth + 8 && radialWidth >= fullLabelWidth + 8) {
754
+ const [tx, ty] = polar(cx, cy, midR, midA);
755
+ children.push(ctx.text(tx, ty + 4, fullLabel, {
756
+ anchor: "middle",
757
+ size: 12,
758
+ weight: 600,
759
+ fill: labelOn,
760
+ meta: {
761
+ kind: "sunburst-ring-label",
762
+ label: name,
763
+ value: entry.value,
764
+ path: entry.path,
765
+ depth
766
+ }
767
+ }));
768
+ } else {
769
+ fallback.push({
770
+ index,
771
+ label: name,
772
+ value: entry.value,
773
+ path: entry.path,
774
+ color: nodeColor
775
+ });
776
+ const indexWidth = ctx.measure(String(index), 12, 600);
777
+ if (thickness >= 16 && available >= indexWidth + 8 && radialWidth >= indexWidth + 8) {
778
+ const [tx, ty] = polar(cx, cy, midR, midA);
779
+ children.push(ctx.text(tx, ty + 4, String(index), {
780
+ anchor: "middle",
781
+ size: 12,
782
+ weight: 600,
783
+ fill: labelOn,
784
+ meta: {
785
+ kind: "sunburst-ring-index",
786
+ label: name,
787
+ value: entry.value,
788
+ path: entry.path,
789
+ depth,
790
+ index
791
+ }
792
+ }));
793
+ }
794
+ }
795
+ } else if (depth > 0 && entry.value === 0) fallback.push({
796
+ index: indexes.get(entry),
797
+ label: String(entry.node.label),
798
+ value: 0,
799
+ path: entry.path,
800
+ color: shadeFor(entry)
801
+ });
802
+ let angle = a0;
803
+ entry.children.forEach((child) => {
804
+ const next = entry.value > 0 ? angle + (a1 - a0) * child.value / entry.value : angle;
805
+ walk(child, angle, next);
806
+ angle = next;
807
+ });
808
+ };
809
+ walk(root, -Math.PI / 2, Math.PI * 1.5);
810
+ children.push(ctx.circle(cx, cy, Math.max(1, r0 - 2), { fill: ctx.color("bg") }));
811
+ const holeWidth = Math.max(1, 2 * (r0 - 8));
812
+ const holeLines = Math.max(1, Math.floor(2 * (r0 - 6) / 16));
813
+ let rootLines = ctx.wrap(rootLabel, holeWidth, 12, 600);
814
+ let centreOutside = false;
815
+ if (rootLines.length > holeLines || rootLines.some((line) => ctx.measure(line, 12, 600) > holeWidth)) {
816
+ centreOutside = true;
817
+ rootLines = ctx.wrap(rootLabel, Math.max(80, ctx.width - 36), 12, 600);
818
+ }
819
+ const rootY = centreOutside ? cy + outerR + 22 : cy - (rootLines.length - 1) * 8 + 4;
820
+ children.push(ctx.textLines(cx, rootY, rootLines, {
821
+ anchor: "middle",
822
+ size: 12,
823
+ weight: 600,
824
+ fill: ctx.color("fg"),
825
+ meta: {
826
+ kind: "sunburst-centre",
827
+ label: rootLabel
828
+ }
829
+ }));
830
+ const listEnd = fallbackLegend(children, fallback, 18, centreOutside ? rootY + rootLines.length * 16 + 12 : cy + outerR + 28, ctx.width - 36, ctx, "sunburst-name");
831
+ const centreEnd = centreOutside ? rootY + rootLines.length * 16 + 8 : cy + outerR + 20;
832
+ return ctx.scene(fallback.length ? listEnd + 8 : centreEnd, children, spec.title);
833
+ }
834
+ function install({ register }) {
835
+ register("pie", (spec, ctx) => pieLayout(spec, ctx, false));
836
+ register("donut", (spec, ctx) => pieLayout(spec, ctx, true));
837
+ register("radial-share", radialShare);
838
+ register("rose", rose);
839
+ register("gauge", gauge);
840
+ register("treemap", treemap);
841
+ register("sunburst", sunburst);
842
+ }
843
+ install({ register });
844
+ export { compositionPresentation, composition_exports, install };