@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,197 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ // src/core/color.js
3
+ var color_exports = /* @__PURE__ */ __exportAll({
4
+ contrast: () => contrast,
5
+ deltaE2000: () => deltaE2000,
6
+ isHex: () => isHex,
7
+ luminance: () => luminance,
8
+ mix: () => mix,
9
+ pushContrast: () => pushContrast,
10
+ simulateCvd: () => simulateCvd,
11
+ tint: () => tint
12
+ });
13
+ var isHex = (value) => typeof value === "string" && /^#[\da-f]{6}$/i.test(value);
14
+ var clamp = (value, min, max) => Math.max(min, Math.min(max, value));
15
+ function mix(colorA, colorB, ratio) {
16
+ if (!isHex(colorA) || !isHex(colorB)) throw new TypeError("mix 需要 #RRGGBB 颜色");
17
+ const amount = Number(ratio);
18
+ if (!Number.isFinite(amount)) throw new TypeError("mix 比例必须是有限数");
19
+ const t = clamp(amount, 0, 1);
20
+ const channel = (hex, shift) => parseInt(hex.slice(1 + shift, 3 + shift), 16);
21
+ return `#${[
22
+ 0,
23
+ 2,
24
+ 4
25
+ ].map((shift) => Math.round(channel(colorA, shift) * (1 - t) + channel(colorB, shift) * t).toString(16).padStart(2, "0")).join("")}`;
26
+ }
27
+ function luminance(hex) {
28
+ if (!isHex(hex)) throw new TypeError("颜色必须是 #RRGGBB");
29
+ const [r, g, b] = linearRgb(hex);
30
+ return .2126 * r + .7152 * g + .0722 * b;
31
+ }
32
+ var contrast = (colorA, colorB) => {
33
+ const [high, low] = [luminance(colorA), luminance(colorB)].sort((a, b) => b - a);
34
+ return (high + .05) / (low + .05);
35
+ };
36
+ function pushContrast(color, toward, against, min) {
37
+ if (contrast(color, against) >= min) return color;
38
+ let low = 0;
39
+ let high = 1;
40
+ for (let i = 0; i < 12; i += 1) {
41
+ const middle = (low + high) / 2;
42
+ if (contrast(mix(color, toward, middle), against) >= min) high = middle;
43
+ else low = middle;
44
+ }
45
+ return mix(color, toward, high);
46
+ }
47
+ var toLin = (c) => c <= .04045 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4;
48
+ var fromLin = (c) => c <= .0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - .055;
49
+ var hexToRgb = (hex) => [
50
+ 1,
51
+ 3,
52
+ 5
53
+ ].map((i) => parseInt(hex.slice(i, i + 2), 16));
54
+ var linearRgb = (hex) => hexToRgb(hex).map((v) => toLin(v / 255));
55
+ var rgbToHex = (rgb) => `#${rgb.map((v) => Math.round(clamp(v, 0, 255)).toString(16).padStart(2, "0")).join("")}`;
56
+ var labOf = (hex) => {
57
+ const [r, g, b] = linearRgb(hex);
58
+ const x = (.4124564 * r + .3575761 * g + .1804375 * b) / .95047;
59
+ const y = .2126729 * r + .7151522 * g + .072175 * b;
60
+ const z = (.0193339 * r + .119192 * g + .9503041 * b) / 1.08883;
61
+ const f = (t) => t > 216 / 24389 ? Math.cbrt(t) : (24389 / 27 * t + 16) / 116;
62
+ return [
63
+ 116 * f(y) - 16,
64
+ 500 * (f(x) - f(y)),
65
+ 200 * (f(y) - f(z))
66
+ ];
67
+ };
68
+ function deltaE2000(x, y) {
69
+ const [L1, a1, b1] = labOf(x);
70
+ const [L2, a2, b2] = labOf(y);
71
+ const rad = Math.PI / 180;
72
+ const Cbar = (Math.hypot(a1, b1) + Math.hypot(a2, b2)) / 2;
73
+ const G = .5 * (1 - Math.sqrt(Cbar ** 7 / (Cbar ** 7 + 25 ** 7)));
74
+ const a1p = (1 + G) * a1;
75
+ const a2p = (1 + G) * a2;
76
+ const C1p = Math.hypot(a1p, b1);
77
+ const C2p = Math.hypot(a2p, b2);
78
+ let h1p = Math.atan2(b1, a1p) / rad;
79
+ let h2p = Math.atan2(b2, a2p) / rad;
80
+ if (h1p < 0) h1p += 360;
81
+ if (h2p < 0) h2p += 360;
82
+ const dLp = L2 - L1;
83
+ const dCp = C2p - C1p;
84
+ let dhp = 0;
85
+ if (C1p * C2p !== 0) {
86
+ dhp = h2p - h1p;
87
+ if (dhp > 180) dhp -= 360;
88
+ else if (dhp < -180) dhp += 360;
89
+ }
90
+ const dHp = 2 * Math.sqrt(C1p * C2p) * Math.sin(dhp * rad / 2);
91
+ const Lbar = (L1 + L2) / 2;
92
+ const CbarP = (C1p + C2p) / 2;
93
+ let hbar = h1p + h2p;
94
+ if (C1p * C2p !== 0) hbar = Math.abs(h1p - h2p) > 180 ? (h1p + h2p + 360) / 2 : (h1p + h2p) / 2;
95
+ const T = 1 - .17 * Math.cos((hbar - 30) * rad) + .24 * Math.cos(2 * hbar * rad) + .32 * Math.cos((3 * hbar + 6) * rad) - .2 * Math.cos((4 * hbar - 63) * rad);
96
+ const dTheta = 30 * Math.exp(-(((hbar - 275) / 25) ** 2));
97
+ const RC = 2 * Math.sqrt(CbarP ** 7 / (CbarP ** 7 + 25 ** 7));
98
+ const SL = 1 + .015 * (Lbar - 50) ** 2 / Math.sqrt(20 + (Lbar - 50) ** 2);
99
+ const SC = 1 + .045 * CbarP;
100
+ const SH = 1 + .015 * CbarP * T;
101
+ const RT = -Math.sin(2 * dTheta * rad) * RC;
102
+ return Math.sqrt((dLp / SL) ** 2 + (dCp / SC) ** 2 + (dHp / SH) ** 2 + RT * (dCp / SC) * (dHp / SH));
103
+ }
104
+ var CVD = {
105
+ protan: [
106
+ [
107
+ .152286,
108
+ 1.052583,
109
+ -.204868
110
+ ],
111
+ [
112
+ .114503,
113
+ .786281,
114
+ .099216
115
+ ],
116
+ [
117
+ -.003882,
118
+ -.048116,
119
+ 1.051998
120
+ ]
121
+ ],
122
+ deutan: [
123
+ [
124
+ .367322,
125
+ .860646,
126
+ -.227968
127
+ ],
128
+ [
129
+ .280085,
130
+ .672501,
131
+ .047413
132
+ ],
133
+ [
134
+ -.01182,
135
+ .04294,
136
+ .968881
137
+ ]
138
+ ]
139
+ };
140
+ function simulateCvd(hex, type) {
141
+ if (!isHex(hex)) throw new TypeError("颜色必须是 #RRGGBB");
142
+ const matrix = CVD[type];
143
+ if (!matrix) return hex;
144
+ const linear = linearRgb(hex);
145
+ return rgbToHex(matrix.map((row) => fromLin(clamp(row[0] * linear[0] + row[1] * linear[1] + row[2] * linear[2], 0, 1)) * 255));
146
+ }
147
+ var toOklab = (hex) => {
148
+ const [r, g, b] = linearRgb(hex);
149
+ const l = Math.cbrt(.4122214708 * r + .5363325363 * g + .0514459929 * b);
150
+ const m = Math.cbrt(.2119034982 * r + .6806995451 * g + .1073969566 * b);
151
+ const s = Math.cbrt(.0883024619 * r + .2817188376 * g + .6299787005 * b);
152
+ return [
153
+ .2104542553 * l + .793617785 * m - .0040720468 * s,
154
+ 1.9779984951 * l - 2.428592205 * m + .4505937099 * s,
155
+ .0259040371 * l + .7827717662 * m - .808675766 * s
156
+ ];
157
+ };
158
+ var fromOklab = ([L, a, b]) => {
159
+ const l = (L + .3963377774 * a + .2158037573 * b) ** 3;
160
+ const m = (L - .1055613458 * a - .0638541728 * b) ** 3;
161
+ const s = (L - .0894841775 * a - 1.291485548 * b) ** 3;
162
+ return rgbToHex([
163
+ 4.0767416621 * l - 3.3077115913 * m + .2309699292 * s,
164
+ -1.2684380046 * l + 2.6097574011 * m - .3413193965 * s,
165
+ -.0041960863 * l - .7034186147 * m + 1.707614701 * s
166
+ ].map((v) => fromLin(clamp(v, 0, 1)) * 255));
167
+ };
168
+ var oklch = (hex) => {
169
+ const [L, a, b] = toOklab(hex);
170
+ return [
171
+ L,
172
+ Math.hypot(a, b),
173
+ Math.atan2(b, a) * 180 / Math.PI
174
+ ];
175
+ };
176
+ function tint(paper, color, ratio, style) {
177
+ if (!style) return mix(paper, color, ratio);
178
+ if (!isHex(paper) || !isHex(color)) throw new TypeError("tint 需要 #RRGGBB 颜色");
179
+ const amount = Number(ratio);
180
+ if (!Number.isFinite(amount)) throw new TypeError("tint 比例必须是有限数");
181
+ const t = clamp(amount, 0, 1);
182
+ if (t === 0) return paper;
183
+ if (t === 1) return color;
184
+ const [L2, C2, h2] = oklch(color);
185
+ if (C2 < .04) return mix(paper, color, t);
186
+ const [L1, C1] = oklch(paper);
187
+ let turn = h2 - style.hue;
188
+ if (turn > 180) turn -= 360;
189
+ else if (turn < -180) turn += 360;
190
+ const L = L1 + (L2 - L1) * t, C = (C1 + (C2 - C1) * t) * style.chroma, h = (style.hue + turn * t) * Math.PI / 180;
191
+ return fromOklab([
192
+ L,
193
+ C * Math.cos(h),
194
+ C * Math.sin(h)
195
+ ]);
196
+ }
197
+ export { color_exports, contrast, deltaE2000, isHex, luminance, mix, pushContrast, simulateCvd, tint };
@@ -0,0 +1,256 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { el, hash, link, primitive } from "./scene.js";
3
+ import { defaultStyle, paletteOf, theme } from "./theme.js";
4
+ import { contrast, isHex, luminance, mix, pushContrast, tint } from "./color.js";
5
+ import { fallbackMeasure, wrapText } from "./text.js";
6
+ import { extent, linear } from "./scale.js";
7
+ import { apportion, barPath, columnHeaders, labelLanes } from "./geometry.js";
8
+ import { fit } from "./diagnostics.js";
9
+ // src/core/context.js
10
+ var context_exports = /* @__PURE__ */ __exportAll({
11
+ createContext: () => createContext,
12
+ structureRoles: () => structureRoles
13
+ });
14
+ var roles = [
15
+ "ink",
16
+ "accent",
17
+ "tone1",
18
+ "tone2",
19
+ "tone3",
20
+ "tone4",
21
+ "tone5",
22
+ "tone6",
23
+ "fg",
24
+ "muted",
25
+ "bg",
26
+ "grid",
27
+ "axis"
28
+ ];
29
+ var TEXT_CONTRAST = 4.5;
30
+ function createContext(options = {}) {
31
+ const width = Math.max(1, Number(options.width) || 800), lang = options.lang || "zh";
32
+ const resolved = theme(options.theme || "claude", { media: options.media || "screen" });
33
+ const theme$1 = resolved.id || resolved.name || (typeof options.theme === "string" ? options.theme : "claude");
34
+ const palette = paletteOf(resolved);
35
+ const fontFamily = resolved.fontFamily || "system-ui, -apple-system, BlinkMacSystemFont, \"Segoe UI\", \"Noto Sans SC\", \"Microsoft YaHei\", sans-serif";
36
+ if (typeof fontFamily !== "string" || !fontFamily.trim()) throw new TypeError("Invalid theme font family");
37
+ const measure = options.measure || fallbackMeasure;
38
+ const color = (role) => {
39
+ if (!roles.includes(role)) throw new TypeError(`Unknown color role: ${role}`);
40
+ const value = palette[role];
41
+ if (typeof value !== "string") throw new TypeError(`Unknown color role: ${role}`);
42
+ return value;
43
+ };
44
+ const frameTone = (value, prominent = false) => resolved.frameInk ? prominent ? resolved.fg1 : resolved.frameInk : value;
45
+ const wrap = (value, maxWidth, size = 12, weight = 400, options) => wrapText(String(value ?? ""), maxWidth, {
46
+ size,
47
+ weight
48
+ }, (text, font) => measure(text, font.size, font.weight, fontFamily), options);
49
+ const text = (x, y, value, opts = {}) => {
50
+ const { anchor = "start", size = 12, weight = 400, fill = color("muted"), meta, ...attrs } = opts;
51
+ if (![
52
+ "start",
53
+ "middle",
54
+ "end"
55
+ ].includes(anchor)) throw new TypeError("Invalid text anchor");
56
+ return {
57
+ ...primitive("text", {
58
+ x,
59
+ y,
60
+ ...attrs,
61
+ "text-anchor": anchor,
62
+ "font-size": size,
63
+ "font-weight": weight,
64
+ fill
65
+ }, meta),
66
+ text: String(value ?? "")
67
+ };
68
+ };
69
+ const textLines = (x, y, lines, opts = {}) => {
70
+ const { leading = 16, ...textOpts } = opts;
71
+ return el("g", {}, lines.map((line, i) => text(x, y + i * leading, line, textOpts)));
72
+ };
73
+ const flow = (items, maxWidth, size = 12, weight = 400) => {
74
+ const rows = [];
75
+ let row = [], rowWidth = 0;
76
+ const gap = measure(" · ", size, weight, fontFamily);
77
+ for (const item of items || []) {
78
+ const value = String(item), itemWidth = measure(value, size, weight, fontFamily);
79
+ if (itemWidth > maxWidth) {
80
+ if (row.length) rows.push(row);
81
+ row = [];
82
+ rowWidth = 0;
83
+ rows.push(...wrap(value, maxWidth, size, weight).map((line) => [line]));
84
+ continue;
85
+ }
86
+ if (row.length && rowWidth + gap + itemWidth > maxWidth) {
87
+ rows.push(row);
88
+ row = [];
89
+ rowWidth = 0;
90
+ }
91
+ row.push(value);
92
+ rowWidth += (row.length > 1 ? gap : 0) + itemWidth;
93
+ }
94
+ if (row.length) rows.push(row);
95
+ return rows;
96
+ };
97
+ const titled = (node, tip) => tip == null || tip === "" ? node : el("g", {}, [node, {
98
+ ...el("title"),
99
+ text: String(tip)
100
+ }]);
101
+ const hexOf = (value) => {
102
+ if (isHex(value)) return value;
103
+ if (roles.includes(value)) return color(value);
104
+ throw new TypeError(`Unknown color: ${value}`);
105
+ };
106
+ const textColor = (value) => {
107
+ const hex = hexOf(value);
108
+ if (contrast(hex, color("bg")) >= TEXT_CONTRAST) return roles.includes(value) ? color(value) : hex;
109
+ return pushContrast(hex, color("fg"), color("bg"), TEXT_CONTRAST);
110
+ };
111
+ const textOn = (fill, { opacity = 1, over = "bg" } = {}) => {
112
+ const under = opacity >= 1 ? hexOf(fill) : mix(hexOf(over), hexOf(fill), opacity);
113
+ const useFg = contrast(color("fg"), under) >= contrast(color("bg"), under);
114
+ const base = color(useFg ? "fg" : "bg");
115
+ if (contrast(base, under) >= TEXT_CONTRAST) return base;
116
+ const extreme = luminance(base) > luminance(under) ? "#ffffff" : "#000000";
117
+ return pushContrast(base, extreme, under, TEXT_CONTRAST);
118
+ };
119
+ const legend = (items, maxWidth, context) => {
120
+ const gap = 10, swatch = 10, textGap = 6, lineHeight = 16;
121
+ let x = 0, y = 0, rowHeight = 0;
122
+ const children = [], textWidth = Math.max(1, maxWidth - swatch - textGap);
123
+ for (const item of items || []) {
124
+ const room = x ? textWidth - x : textWidth;
125
+ let lines = context.wrap(item.label, Math.max(1, room), 12, 400);
126
+ let itemWidth = 16 + Math.max(...lines.map((line) => context.measure(line, 12, 400)));
127
+ if (x && (lines.length > 1 || x + itemWidth > maxWidth)) {
128
+ x = 0;
129
+ y += rowHeight + gap;
130
+ rowHeight = 0;
131
+ lines = context.wrap(item.label, textWidth, 12, 400);
132
+ itemWidth = 16 + Math.max(...lines.map((line) => context.measure(line, 12, 400)));
133
+ }
134
+ const group = el("g", {}, [context.rect(x, y + 3, swatch, swatch, { fill: item.color }), context.textLines(x + swatch + textGap, y + 12, lines, { fill: context.color("fg") })]);
135
+ group.meta = {
136
+ ...item.meta,
137
+ kind: "legend"
138
+ };
139
+ children.push(group);
140
+ x += itemWidth + gap;
141
+ rowHeight = Math.max(rowHeight, lines.length * lineHeight);
142
+ }
143
+ return {
144
+ children,
145
+ height: items?.length ? y + rowHeight : 0
146
+ };
147
+ };
148
+ const ctx = {
149
+ width,
150
+ lang,
151
+ theme: theme$1,
152
+ fontFamily,
153
+ scheme: resolved.scheme,
154
+ design: resolved,
155
+ assets: options.assets || null,
156
+ measure: (value, size, weight) => measure(value, size, weight, fontFamily),
157
+ el,
158
+ shape: primitive,
159
+ text,
160
+ titled,
161
+ link,
162
+ textColor,
163
+ textOn,
164
+ rect: (x, y, w, h, opts = {}) => primitive("rect", {
165
+ x,
166
+ y,
167
+ width: w,
168
+ height: h,
169
+ ...opts
170
+ }, opts.meta),
171
+ line: (x1, y1, x2, y2, opts = {}) => primitive("line", {
172
+ x1,
173
+ y1,
174
+ x2,
175
+ y2,
176
+ ...opts
177
+ }, opts.meta),
178
+ circle: (x, y, r, opts = {}) => primitive("circle", {
179
+ cx: x,
180
+ cy: y,
181
+ r,
182
+ ...opts
183
+ }, opts.meta),
184
+ path: (d, opts = {}) => primitive("path", {
185
+ d,
186
+ ...opts
187
+ }, opts.meta),
188
+ color,
189
+ palette,
190
+ colors: palette,
191
+ frameTone,
192
+ mix,
193
+ tint: (paper, value, ratio) => tint(paper, value, ratio, resolved.tint),
194
+ extent,
195
+ linear,
196
+ hash,
197
+ columnHeaders,
198
+ labelLanes,
199
+ apportion,
200
+ barPath,
201
+ wrap,
202
+ textLines,
203
+ flow,
204
+ legend: (items, maxWidth) => legend(items, maxWidth, ctx),
205
+ scene: (height, children, title) => ({
206
+ width,
207
+ height,
208
+ children,
209
+ ...title ? { title } : {}
210
+ }),
211
+ fit
212
+ };
213
+ return ctx;
214
+ }
215
+ function structureRoles({ theme, media = "screen", tones } = {}) {
216
+ const ctx = createContext({
217
+ theme,
218
+ media,
219
+ width: 800
220
+ });
221
+ const d = ctx.design, look = {
222
+ ...defaultStyle,
223
+ ...d.style,
224
+ ...d.diagram
225
+ };
226
+ if (tones !== void 0 && (!Array.isArray(tones) || tones.length !== 6 || ![...tones].every(isHex))) throw new TypeError("Structure tones must contain six hex colors");
227
+ const palette = tones && media === "screen" ? paletteOf({
228
+ ...d,
229
+ tones
230
+ }) : ctx.palette;
231
+ const colors = {};
232
+ for (let k = 1; k <= 6; k += 1) {
233
+ const tone = ctx.frameTone(palette[`tone${k}`]);
234
+ colors[`frame-${k}`] = ctx.tint(d.bg, tone, .45);
235
+ colors[`frame-${k}-fill`] = ctx.tint(d.bg, tone, .07);
236
+ }
237
+ const em = ctx.frameTone(palette.accent, true);
238
+ return Object.freeze({
239
+ colors: Object.freeze({
240
+ ...colors,
241
+ node: d[look.nodeBackground],
242
+ "node-line": ctx.tint(d.bg, ctx.frameTone(palette.tone1), .4),
243
+ edge: d.fg4,
244
+ "edge-strong": ctx.color("muted"),
245
+ em,
246
+ "on-em": ctx.textOn(em),
247
+ "em-fill": ctx.tint(d.bg, ctx.frameTone(palette.accent), .12),
248
+ "em-line": ctx.tint(d.bg, ctx.frameTone(palette.accent), .45),
249
+ chip: d.bg
250
+ }),
251
+ radius: look.radius,
252
+ smallRadius: look.smallRadius,
253
+ fontFamily: ctx.fontFamily
254
+ });
255
+ }
256
+ export { context_exports, createContext, structureRoles };
@@ -0,0 +1,23 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ // src/core/diagnostics.js
3
+ var diagnostics_exports = /* @__PURE__ */ __exportAll({
4
+ ExpectedDiagnostic: () => ExpectedDiagnostic,
5
+ fit: () => fit
6
+ });
7
+ var ExpectedDiagnostic = class extends Error {
8
+ constructor(message, errors) {
9
+ super(message);
10
+ this.errors = errors;
11
+ this.code = errors[0]?.code;
12
+ if (errors.length === 1 && errors[0].fix) this.fix = errors[0].fix;
13
+ }
14
+ };
15
+ function fit(reason, fix) {
16
+ const message = `图型不适用:${reason}。${fix}`;
17
+ throw new ExpectedDiagnostic(message, [{
18
+ code: "CHART_FIT",
19
+ message,
20
+ fix
21
+ }]);
22
+ }
23
+ export { ExpectedDiagnostic, diagnostics_exports, fit };
@@ -0,0 +1,167 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { decimalsFor } from "./scale.js";
3
+ // src/core/format.js
4
+ var format_exports = /* @__PURE__ */ __exportAll({
5
+ compactTemporal: () => compactTemporal,
6
+ decimalRound: () => decimalRound,
7
+ formatPercent: () => formatPercent,
8
+ formatValue: () => formatValue,
9
+ isEnglish: () => isEnglish,
10
+ makeFormatters: () => makeFormatters,
11
+ quantityParts: () => quantityParts,
12
+ tickTexts: () => tickTexts
13
+ });
14
+ var isEnglish = (lang) => !/^zh\b/i.test(String(lang || "zh"));
15
+ var localeOf = (lang) => {
16
+ const tag = String(lang || "").trim();
17
+ return !tag || /^zh\b/i.test(tag) ? "zh-CN" : tag;
18
+ };
19
+ function compactTemporal(labels, lang = "zh") {
20
+ let previousYear = null;
21
+ const locale = localeOf(lang);
22
+ const english = isEnglish(lang);
23
+ return Array.from(labels || [], (raw) => {
24
+ const label = String(raw ?? "");
25
+ const match = /^(\d{4})-(\d{2})$/.exec(label);
26
+ if (!match || Number(match[2]) < 1 || Number(match[2]) > 12) return {
27
+ label,
28
+ year: ""
29
+ };
30
+ const year = Number(match[1]), month = Number(match[2]);
31
+ const monthLabel = new Intl.DateTimeFormat(locale, {
32
+ month: "short",
33
+ timeZone: "UTC"
34
+ }).format(new Date(Date.UTC(year, month - 1, 1)));
35
+ const yearLabel = previousYear === year ? "" : english ? String(year) : `${year}年`;
36
+ previousYear = year;
37
+ return {
38
+ label: monthLabel,
39
+ year: yearLabel
40
+ };
41
+ });
42
+ }
43
+ var trim = (value) => String(value ?? "").trim();
44
+ var HAN_START = /^\p{Script=Han}/u;
45
+ function decimalRound(value, digits, shift = 0) {
46
+ const [mantissa, exponent = "0"] = Math.abs(value).toString().split("e");
47
+ const shifted = Number(`${mantissa}e${Number(exponent) + digits + shift}`);
48
+ if (!Number.isFinite(shifted)) return value * 10 ** shift;
49
+ const [whole, scale = "0"] = Math.round(shifted).toString().split("e");
50
+ const rounded = Number(`${whole}e${Number(scale) - digits}`);
51
+ return rounded === 0 ? 0 : Math.sign(value) * rounded;
52
+ }
53
+ function makeFormatters(lang) {
54
+ const locale = localeOf(lang), cache = /* @__PURE__ */ new Map();
55
+ const numberFormat = (min, max) => {
56
+ const key = `${min}|${max}`;
57
+ if (!cache.has(key)) cache.set(key, new Intl.NumberFormat(locale, {
58
+ minimumFractionDigits: min,
59
+ maximumFractionDigits: max
60
+ }));
61
+ return cache.get(key);
62
+ };
63
+ const clamp = (digits) => Math.max(0, Math.min(6, Number(digits) || 0));
64
+ return {
65
+ lang: String(lang || "zh"),
66
+ decimals: 0,
67
+ percentDecimals: 1,
68
+ fixed(value, digits) {
69
+ const d = clamp(digits);
70
+ return numberFormat(d, d).format(value);
71
+ },
72
+ atMost(value, digits) {
73
+ return numberFormat(0, clamp(digits)).format(value);
74
+ }
75
+ };
76
+ }
77
+ var englishScales = [
78
+ {
79
+ threshold: 1e9,
80
+ scale: 1e9,
81
+ suffix: "B"
82
+ },
83
+ {
84
+ threshold: 1e6,
85
+ scale: 1e6,
86
+ suffix: "M"
87
+ },
88
+ {
89
+ threshold: 1e4,
90
+ scale: 1e3,
91
+ suffix: "K"
92
+ }
93
+ ];
94
+ var chineseScales = [{
95
+ threshold: 1e8,
96
+ scale: 1e8,
97
+ suffix: "亿"
98
+ }, {
99
+ threshold: 1e4,
100
+ scale: 1e4,
101
+ suffix: "万"
102
+ }];
103
+ function unitScaleOf(magnitude, lang) {
104
+ return (isEnglish(lang) ? englishScales : chineseScales).find(({ threshold }) => magnitude >= threshold) ?? {
105
+ scale: 1,
106
+ suffix: ""
107
+ };
108
+ }
109
+ function quantityParts(value, lang, digitsOf) {
110
+ const abs = Math.abs(value);
111
+ let unit = unitScaleOf(abs, lang);
112
+ while (true) {
113
+ const digits = digitsOf(unit.scale);
114
+ const scaled = decimalRound(abs, Math.min(6, digits), -Math.log10(unit.scale));
115
+ const next = unitScaleOf(scaled * unit.scale, lang);
116
+ if (next.scale <= unit.scale) return {
117
+ scaled,
118
+ digits,
119
+ scale: unit.scale,
120
+ suffix: unit.suffix,
121
+ sign: value < 0 && scaled !== 0 ? "-" : ""
122
+ };
123
+ unit = next;
124
+ }
125
+ }
126
+ function formatQuantity(value, f, declared) {
127
+ function digitsOf(scale) {
128
+ if (declared != null) return declared;
129
+ return scale === 1 ? Math.max(0, Math.min(4, Number(f.decimals) || 0)) : 1;
130
+ }
131
+ const { scaled, suffix, digits, sign } = quantityParts(value, f.lang, digitsOf);
132
+ return `${sign}${f.fixed(scaled, digits)}${suffix}`;
133
+ }
134
+ function formatValue(value, unit, f, decimals = null) {
135
+ if (value == null || !Number.isFinite(value)) return "—";
136
+ const suffix = trim(unit);
137
+ const declared = decimals == null ? null : Math.max(0, Math.min(6, Number(decimals) || 0));
138
+ if (suffix === "%") {
139
+ const digits = declared == null ? Math.max(0, Math.min(6, Number(f.decimals) || 0)) : declared;
140
+ const rounded = decimalRound(Math.abs(value), digits);
141
+ return `${value < 0 && rounded !== 0 ? "-" : ""}${f.fixed(rounded, digits)}%`;
142
+ }
143
+ const text = formatQuantity(value, f, declared);
144
+ return suffix ? `${text}${suffix === "%" ? "" : `${HAN_START.test(suffix) ? "" : " "}${suffix}`}` : text;
145
+ }
146
+ function formatPercent(value, f, decimals = null) {
147
+ if (!Number.isFinite(value)) return "—";
148
+ const digits = Math.max(0, Math.min(2, Number(decimals == null ? f.percentDecimals == null ? 1 : f.percentDecimals : decimals) || 0));
149
+ const threshold = 10 ** -digits;
150
+ if (value > 0 && value < threshold) return `<${f.fixed(threshold, digits)}%`;
151
+ const rounded = decimalRound(Math.abs(value), digits);
152
+ return `${value < 0 && rounded !== 0 ? "-" : ""}${f.fixed(rounded, digits)}%`;
153
+ }
154
+ function tickTexts(ticks, step, f, unit = null, { percentSymbol = true } = {}) {
155
+ if (trim(unit) === "%") {
156
+ const digits = decimalsFor(Math.abs(step));
157
+ const suffix = percentSymbol ? "%" : "";
158
+ return ticks.map((tick) => `${f.atMost(Number(tick.toFixed(Math.min(6, digits))) || 0, digits)}${suffix}`);
159
+ }
160
+ const scale = unitScaleOf(ticks.reduce((max, tick) => Math.max(max, Math.abs(tick)), 0), f.lang);
161
+ const digits = decimalsFor(Math.abs(step) / scale.scale);
162
+ return ticks.map((tick) => {
163
+ const rounded = Number((tick / scale.scale).toFixed(Math.min(6, digits)));
164
+ return rounded === 0 ? "0" : `${f.atMost(rounded, digits)}${scale.suffix}`;
165
+ });
166
+ }
167
+ export { compactTemporal, decimalRound, formatPercent, formatValue, format_exports, isEnglish, makeFormatters, quantityParts, tickTexts };