@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,563 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { register } from "./registry.js";
3
+ // src/chart/maps.js
4
+ var maps_exports = /* @__PURE__ */ __exportAll({
5
+ install: () => install,
6
+ maps: () => maps
7
+ });
8
+ var RAD = Math.PI / 180;
9
+ var project = (longitude, latitude, projection = "equirectangular") => {
10
+ const lon = Number(longitude), lat = Number(latitude);
11
+ if (!Number.isFinite(lon) || !Number.isFinite(lat) || lon < -180 || lon > 180 || lat < -90 || lat > 90) return null;
12
+ if (projection === "mercator") {
13
+ const clipped = Math.max(-85.05112878, Math.min(85.05112878, lat));
14
+ return [lon * RAD, Math.log(Math.tan(Math.PI / 4 + clipped * RAD / 2))];
15
+ }
16
+ if (projection !== "equirectangular") throw new TypeError(`Unknown map projection: ${projection}`);
17
+ return [lon * RAD, lat * RAD];
18
+ };
19
+ var isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
20
+ function featuresFor(spec, options) {
21
+ const ctx = options;
22
+ const map = spec.data.map;
23
+ let collection, idProperty;
24
+ if (map.geojson) {
25
+ collection = map.geojson;
26
+ idProperty = map.idProperty;
27
+ } else {
28
+ collection = options.assets?.[map.asset];
29
+ if (!collection) ctx.fit(`缺少注入的地图资产 ${map.asset}`, "由宿主通过 ctx.assets 注入对应 FeatureCollection");
30
+ idProperty = "id";
31
+ }
32
+ if (!collection || collection.type !== "FeatureCollection" || !Array.isArray(collection.features)) ctx.fit("地图资产不是 FeatureCollection", "传入合法 GeoJSON FeatureCollection");
33
+ for (const [i, f] of collection.features.entries()) {
34
+ if (!isObject(f) || f.type !== "Feature" || !isObject(f.properties) || !isObject(f.geometry) || !["Polygon", "MultiPolygon"].includes(f.geometry.type)) ctx.fit(`地图要素 ${i} 缺少合法 Polygon/MultiPolygon geometry`, "修正 GeoJSON geometry.type 与 coordinates");
35
+ const polys = f.geometry.type === "Polygon" ? [f.geometry.coordinates] : f.geometry.coordinates;
36
+ if (!Array.isArray(polys) || !polys.length || polys.some((poly) => !Array.isArray(poly) || !poly.length || poly.some((ring) => !Array.isArray(ring) || ring.length < 4 || ring.some((p) => !Array.isArray(p) || p.length < 2 || !Number.isFinite(p[0]) || !Number.isFinite(p[1]) || p[0] < -180 || p[0] > 180 || p[1] < -90 || p[1] > 90) || ring[0][0] !== ring.at(-1)[0] || ring[0][1] !== ring.at(-1)[1]))) ctx.fit(`地图要素 ${i} 含空或非法坐标环`, "每个 ring 须至少含4个合法经纬度位置且首尾闭合");
37
+ }
38
+ return {
39
+ features: collection.features,
40
+ idProperty
41
+ };
42
+ }
43
+ function splitRing(ring) {
44
+ const points = [];
45
+ for (const [lon, lat] of ring) {
46
+ let x = lon;
47
+ if (points.length) {
48
+ const prev = points.at(-1)[0];
49
+ while (x - prev > 180) x -= 360;
50
+ while (x - prev < -180) x += 360;
51
+ }
52
+ points.push([x, lat]);
53
+ }
54
+ const min = Math.floor((Math.min(...points.map((p) => p[0])) + 180) / 360);
55
+ const max = Math.floor((Math.max(...points.map((p) => p[0])) + 180) / 360);
56
+ const clip = (input, edge, keepGreater) => {
57
+ const out = [];
58
+ for (let i = 0; i < input.length; i += 1) {
59
+ const a = input[i], b = input[(i + 1) % input.length];
60
+ const insideA = keepGreater ? a[0] >= edge : a[0] <= edge;
61
+ const insideB = keepGreater ? b[0] >= edge : b[0] <= edge;
62
+ if (insideA) out.push(a);
63
+ if (insideA !== insideB) {
64
+ const t = (edge - a[0]) / (b[0] - a[0]);
65
+ out.push([edge, a[1] + (b[1] - a[1]) * t]);
66
+ }
67
+ }
68
+ return out;
69
+ };
70
+ const result = [];
71
+ for (let strip = min; strip <= max; strip += 1) {
72
+ const west = -180 + strip * 360, east = 180 + strip * 360;
73
+ const clipped = clip(clip(points, west, true), east, false);
74
+ if (clipped.length < 3) continue;
75
+ const shifted = clipped.map(([x, y]) => [x - strip * 360, y]);
76
+ shifted.push([...shifted[0]]);
77
+ result.push(shifted);
78
+ }
79
+ return result;
80
+ }
81
+ function ringPath(ring, point) {
82
+ return splitRing(ring).map((part) => {
83
+ const pts = part.map(([lon, lat]) => point(lon, lat)).filter(Boolean);
84
+ if (pts.length < 4) return "";
85
+ return `${pts.map(([x, y], i) => `${i ? "L" : "M"}${x.toFixed(2)},${y.toFixed(2)}`).join(" ")} Z`;
86
+ }).filter(Boolean).join(" ");
87
+ }
88
+ function geometryRings(geometry) {
89
+ return geometry.type === "Polygon" ? geometry.coordinates : geometry.coordinates.flat();
90
+ }
91
+ function coordinateBounds(features, rows, enc, inset, projection) {
92
+ if (inset) return {
93
+ west: -126,
94
+ east: -66,
95
+ south: 23,
96
+ north: 50
97
+ };
98
+ const coords = [];
99
+ for (const f of features) for (const ring of geometryRings(f.geometry)) for (const p of ring) coords.push(p);
100
+ if (!coords.length) {
101
+ for (const row of rows || []) if (Number.isFinite(row[enc?.longitude]) && Number.isFinite(row[enc?.latitude])) coords.push([row[enc.longitude], row[enc.latitude]]);
102
+ }
103
+ if (!coords.length) return {
104
+ west: -180,
105
+ east: 180,
106
+ south: -90,
107
+ north: 90
108
+ };
109
+ let west = Math.min(...coords.map((p) => p[0])), east = Math.max(...coords.map((p) => p[0]));
110
+ let south = Math.min(...coords.map((p) => p[1])), north = Math.max(...coords.map((p) => p[1]));
111
+ if (west === east) {
112
+ west -= 1;
113
+ east += 1;
114
+ }
115
+ if (south === north) {
116
+ south -= 1;
117
+ north += 1;
118
+ }
119
+ if (projection === "mercator") {
120
+ south = Math.max(south, -85.05112878);
121
+ north = Math.min(north, 85.05112878);
122
+ }
123
+ return {
124
+ west,
125
+ east,
126
+ south,
127
+ north
128
+ };
129
+ }
130
+ var INDEX_TOP = 98;
131
+ var INDEX_ROW = 16;
132
+ var INDEX_CAP = 24;
133
+ function makePlot(spec, ctx, features, rows = []) {
134
+ const width = ctx.width;
135
+ const inset = spec.data.map.asset === "us-states";
136
+ const projection = ctx.assets?.projection || "equirectangular";
137
+ const bounds = coordinateBounds(features, rows, spec.encode, inset, projection);
138
+ const ll = project(bounds.west, bounds.south, projection), ur = project(bounds.east, bounds.north, projection);
139
+ const margin = width <= 360 ? 12 : 32;
140
+ const indexRows = Math.min(rows.length, INDEX_CAP);
141
+ const bottom = indexRows ? 126 + Math.ceil(indexRows / (width < 420 ? 1 : 2)) * INDEX_ROW : 112;
142
+ const availableW = width - margin * 2;
143
+ const budgetH = Math.max(180, Math.round(width * .64) - (indexRows ? 120 : 80));
144
+ const scale = Math.min(availableW / Math.max(1e-9, ur[0] - ll[0]), budgetH / Math.max(1e-9, ur[1] - ll[1]));
145
+ const plot = {
146
+ x: margin + (availableW - (ur[0] - ll[0]) * scale) / 2,
147
+ y: 20,
148
+ w: (ur[0] - ll[0]) * scale,
149
+ h: (ur[1] - ll[1]) * scale
150
+ };
151
+ const height = Math.ceil(plot.y + plot.h + 4 + bottom);
152
+ const projectPoint = (lon, lat) => {
153
+ let p = project(lon, lat, projection);
154
+ if (inset && lon > bounds.east) return null;
155
+ return p && [plot.x + (p[0] - ll[0]) * scale, plot.y + (ur[1] - p[1]) * scale];
156
+ }, children = [], grid = ctx.palette.grid;
157
+ ctx.palette.muted;
158
+ const lonStep = inset ? 10 : Math.max(10, Math.round((bounds.east - bounds.west) / 6 / 10) * 10);
159
+ const latStep = inset ? 5 : Math.max(10, Math.round((bounds.north - bounds.south) / 5 / 10) * 10);
160
+ for (let lon = Math.ceil(bounds.west / lonStep) * lonStep; lon <= bounds.east; lon += lonStep) {
161
+ const a = projectPoint(lon, bounds.south), b = projectPoint(lon, bounds.north);
162
+ if (a && b) children.push(ctx.line(a[0], a[1], b[0], b[1], {
163
+ stroke: grid,
164
+ "stroke-width": .6,
165
+ "stroke-dasharray": "2 4"
166
+ }));
167
+ }
168
+ for (let lat = Math.ceil(bounds.south / latStep) * latStep; lat <= bounds.north; lat += latStep) {
169
+ const a = projectPoint(bounds.west, lat), b = projectPoint(bounds.east, lat);
170
+ if (a && b) children.push(ctx.line(a[0], a[1], b[0], b[1], {
171
+ stroke: grid,
172
+ "stroke-width": .6,
173
+ "stroke-dasharray": "2 4"
174
+ }));
175
+ }
176
+ return {
177
+ width,
178
+ height,
179
+ plot,
180
+ children,
181
+ projectPoint,
182
+ inset,
183
+ projection,
184
+ bounds,
185
+ bottom
186
+ };
187
+ }
188
+ function insetPoint(layout, id, lon, lat) {
189
+ const ak = id === "US-AK", west = ak ? -188 : -161, east = ak ? -129 : -154, south = ak ? 51 : 18, north = ak ? 72 : 23;
190
+ const cell = ak ? {
191
+ x: layout.plot.x,
192
+ y: layout.plot.y + layout.plot.h - 62,
193
+ w: 102,
194
+ h: 48
195
+ } : {
196
+ x: layout.plot.x + 112,
197
+ y: layout.plot.y + layout.plot.h - 28,
198
+ w: 36,
199
+ h: 22
200
+ };
201
+ const x = ak && lon > 0 ? lon - 360 : lon;
202
+ if (!ak && x < west - 1) return null;
203
+ return [cell.x + (x - west) / (east - west) * cell.w, cell.y + (north - lat) / (north - south) * cell.h];
204
+ }
205
+ function mapCredit(children, ctx, layout, spec) {
206
+ const source = spec.data.map.geojson ? ctx.lang.startsWith("en") ? "Source: supplied GeoJSON" : "地图来源:注入的 GeoJSON" : ctx.lang.startsWith("en") ? "Source: Natural Earth" : "地图来源:Natural Earth";
207
+ const text = ctx.lang.startsWith("en") ? `${source} · Boundaries are illustrative, not an endorsement.` : `${source} · 边界仅作示意,不代表立场或认可。`;
208
+ const lines = ctx.wrap(text, Math.max(120, layout.width - 24), 12, 400);
209
+ children.push(ctx.textLines(layout.width / 2, layout.height - 6, lines, {
210
+ anchor: "middle",
211
+ size: 12,
212
+ fill: palette(ctx).muted,
213
+ meta: { kind: "map-source" }
214
+ }));
215
+ layout.height += (lines.length - 1) * 16;
216
+ }
217
+ function palette(ctx) {
218
+ const { accent, bg, muted } = ctx.palette;
219
+ return {
220
+ low: ctx.mix(bg, accent, .3),
221
+ high: accent,
222
+ missing: bg,
223
+ border: ctx.mix(bg, muted, .5),
224
+ ink: ctx.palette.fg,
225
+ muted,
226
+ grid: ctx.palette.grid
227
+ };
228
+ }
229
+ function formattedValue(ctx, value, field) {
230
+ const meta = ctx.field(field), text = ctx.format(value, field);
231
+ return meta.unit && text !== "—" && !text.includes(meta.unit) ? `${text} ${meta.unit}` : text;
232
+ }
233
+ function bottomLegend(children, ctx, layout, min, max, field, withSize = false) {
234
+ const c = palette(ctx), y = layout.height - layout.bottom + 16;
235
+ let center = layout.width / 2;
236
+ const reserveLegendWidth = (width) => {
237
+ if (width + 24 > layout.width) {
238
+ layout.width = Math.ceil(width + 24);
239
+ center = layout.width / 2;
240
+ }
241
+ };
242
+ const fieldMeta = field ? ctx.field(field) : {};
243
+ const title = fieldMeta.label && fieldMeta.label !== field ? fieldMeta.label : fieldMeta.unit || "";
244
+ if (withSize) {
245
+ const low = formattedValue(ctx, min, field), high = formattedValue(ctx, max, field), note = ctx.lang.startsWith("en") ? "area ∝ value" : "面积 ∝ 数值";
246
+ if (min === max) {
247
+ const width = ctx.measure(low, 12, 400), noteWidth = ctx.measure(note, 12, 400);
248
+ const total = 40 + width + noteWidth;
249
+ reserveLegendWidth(total);
250
+ const start = center - total / 2;
251
+ children.push(ctx.circle(start + 5, y + 26, 5, {
252
+ fill: c.high,
253
+ stroke: c.ink,
254
+ "stroke-width": .7,
255
+ meta: { kind: "geo-scatter-size-legend" }
256
+ }));
257
+ children.push(ctx.text(start + 14, y + 30, low, {
258
+ size: 12,
259
+ fill: c.ink
260
+ }));
261
+ children.push(ctx.text(start + 22 + width + 18, y + 30, note, {
262
+ size: 12,
263
+ fill: c.muted
264
+ }));
265
+ } else {
266
+ const widths = [
267
+ low,
268
+ high,
269
+ note
270
+ ].map((value) => ctx.measure(value, 12, 400));
271
+ const total = 74 + widths[0] + widths[1] + widths[2];
272
+ reserveLegendWidth(total);
273
+ let x = center - total / 2;
274
+ children.push(ctx.circle(x + 5, y + 26, 5, {
275
+ fill: c.high,
276
+ stroke: c.ink,
277
+ "stroke-width": .7,
278
+ meta: { kind: "geo-scatter-size-legend" }
279
+ }));
280
+ x += 16;
281
+ children.push(ctx.text(x, y + 30, low, {
282
+ size: 12,
283
+ fill: c.ink
284
+ }));
285
+ x += widths[0] + 18;
286
+ children.push(ctx.circle(x + 9, y + 26, 9, {
287
+ fill: c.high,
288
+ stroke: c.ink,
289
+ "stroke-width": .7,
290
+ meta: { kind: "geo-scatter-size-legend" }
291
+ }));
292
+ x += 24;
293
+ children.push(ctx.text(x, y + 30, high, {
294
+ size: 12,
295
+ fill: c.ink
296
+ }));
297
+ x += widths[1] + 16;
298
+ children.push(ctx.text(x, y + 30, note, {
299
+ size: 12,
300
+ fill: c.muted
301
+ }));
302
+ }
303
+ children.push(ctx.text(center, y, title, {
304
+ anchor: "middle",
305
+ size: 12,
306
+ fill: c.ink
307
+ }));
308
+ } else {
309
+ children.push(ctx.text(center, y, title, {
310
+ anchor: "middle",
311
+ size: 12,
312
+ fill: c.ink
313
+ }));
314
+ children.push(ctx.rect(center - 100, y + 8, 200, 12, { fill: `url(#map-legend)` }));
315
+ children.push(ctx.text(center - 100, y + 38, formattedValue(ctx, min, field), {
316
+ size: 12,
317
+ fill: c.ink
318
+ }));
319
+ children.push(ctx.text(center + 100, y + 38, formattedValue(ctx, max, field), {
320
+ anchor: "end",
321
+ size: 12,
322
+ fill: c.ink
323
+ }));
324
+ children.push(ctx.rect(center - 100, y + 46, 12, 12, {
325
+ fill: c.missing,
326
+ stroke: c.border,
327
+ "stroke-width": 1
328
+ }));
329
+ children.push(ctx.text(center - 82, y + 57, ctx.lang.startsWith("en") ? "No data" : "无数据", {
330
+ size: 12,
331
+ fill: c.ink
332
+ }));
333
+ children.push(ctx.el("defs", {}, [ctx.el("linearGradient", {
334
+ id: "map-legend",
335
+ x1: "0%",
336
+ x2: "100%",
337
+ y1: "0%",
338
+ y2: "0%"
339
+ }, [ctx.el("stop", {
340
+ offset: "0%",
341
+ "stop-color": c.low
342
+ }), ctx.el("stop", {
343
+ offset: "100%",
344
+ "stop-color": c.high
345
+ })])]));
346
+ }
347
+ }
348
+ function validateUniqueRegionKeys(features, idProperty, ctx) {
349
+ const ids = /* @__PURE__ */ new Set();
350
+ for (const f of features) {
351
+ const id = f.properties[idProperty];
352
+ if (typeof id !== "string" || !id || ids.has(id)) ctx.fit(`地图地区键 ${String(id)} 缺失或重复`, "修正 idProperty 并确保每个地区键唯一");
353
+ ids.add(id);
354
+ }
355
+ return ids;
356
+ }
357
+ function choropleth(spec, ctx) {
358
+ const { features, idProperty } = featuresFor(spec, ctx), ids = validateUniqueRegionKeys(features, idProperty, ctx);
359
+ const [regionField, valueField] = [spec.encode.region, spec.encode.value], data = /* @__PURE__ */ new Map();
360
+ for (const [i, row] of spec.data.rows.entries()) {
361
+ const id = row?.[regionField];
362
+ if (typeof id !== "string" || !ids.has(id)) ctx.fit(`数据行 ${i} 的地区键 ${String(id)} 不存在于地图资产`, "使地区键与 GeoJSON id 完全一致;禁止按名称模糊匹配");
363
+ if (data.has(id)) ctx.fit(`地区键 ${id} 在数据中重复`, "每个地区只能提供一行数据");
364
+ const value = row[valueField];
365
+ if (value !== null && (typeof value !== "number" || !Number.isFinite(value))) ctx.fit(`地区 ${id} 的数值非法`, "value 必须是有限数值或 null");
366
+ data.set(id, value);
367
+ }
368
+ const field = ctx.field(valueField), domain = Array.isArray(field.domain) && field.domain.length === 2 && field.domain.every(Number.isFinite) ? field.domain : null;
369
+ const vals = [...data.values()].filter(Number.isFinite), min = domain?.[0] ?? (vals.length ? Math.min(...vals) : 0), max = domain?.[1] ?? (vals.length ? Math.max(...vals) : 0);
370
+ const layout = makePlot(spec, ctx, features), c = palette(ctx), { children, projectPoint } = layout;
371
+ for (const feature of features) {
372
+ const id = feature.properties[idProperty], hasValue = data.has(id) && Number.isFinite(data.get(id)), value = hasValue ? data.get(id) : null;
373
+ if (layout.inset && ["US-AK", "US-HI"].includes(id)) continue;
374
+ const ratio = !hasValue ? 0 : max === min ? .5 : Math.max(0, Math.min(1, (value - min) / (max - min)));
375
+ const fill = hasValue ? ctx.mix(c.low, c.high, ratio) : c.missing;
376
+ const d = geometryRings(feature.geometry).map((ring) => ringPath(ring, projectPoint)).filter(Boolean).join(" ");
377
+ if (d) children.push(ctx.path(d, {
378
+ fill,
379
+ "fill-rule": "evenodd",
380
+ stroke: c.border,
381
+ "stroke-width": .7,
382
+ "vector-effect": "non-scaling-stroke",
383
+ meta: {
384
+ kind: "choropleth",
385
+ region: id,
386
+ label: feature.properties.name || id,
387
+ value: hasValue ? value : null
388
+ }
389
+ }));
390
+ }
391
+ if (layout.inset) for (const feature of features) {
392
+ const id = feature.properties[idProperty];
393
+ if (!["US-AK", "US-HI"].includes(id)) continue;
394
+ const place = (lon, lat) => insetPoint(layout, id, lon, lat);
395
+ const d = geometryRings(feature.geometry).map((ring) => ringPath(ring, place)).filter(Boolean).join(" "), val = data.get(id), hasValue = data.has(id) && Number.isFinite(val);
396
+ if (d) children.push(ctx.path(d, {
397
+ fill: hasValue ? ctx.mix(c.low, c.high, max === min ? .5 : Math.max(0, Math.min(1, (val - min) / (max - min)))) : c.missing,
398
+ "fill-rule": "evenodd",
399
+ stroke: c.border,
400
+ "stroke-width": .7,
401
+ meta: {
402
+ kind: "choropleth",
403
+ region: id,
404
+ label: feature.properties.name || id,
405
+ value: hasValue ? val : null,
406
+ inset: true
407
+ }
408
+ }));
409
+ }
410
+ bottomLegend(children, ctx, layout, min, max, valueField);
411
+ mapCredit(children, ctx, layout, spec);
412
+ return {
413
+ width: layout.width,
414
+ height: layout.height,
415
+ title: spec.title || "Choropleth map",
416
+ children
417
+ };
418
+ }
419
+ function scatter(spec, ctx) {
420
+ const { features } = featuresFor(spec, ctx), rows = spec.data.rows, enc = spec.encode;
421
+ const layout = makePlot(spec, ctx, features, rows), c = palette(ctx), { children, projectPoint } = layout;
422
+ for (const feature of features) {
423
+ const id = feature.properties.id || null;
424
+ const point = layout.inset && ["US-AK", "US-HI"].includes(id) ? (lon, lat) => insetPoint(layout, id, lon, lat) : projectPoint;
425
+ for (const ring of geometryRings(feature.geometry)) {
426
+ const d = ringPath(ring, point);
427
+ if (d) children.push(ctx.path(d, {
428
+ fill: "none",
429
+ stroke: c.border,
430
+ "stroke-width": .7,
431
+ meta: {
432
+ kind: "geo-boundary",
433
+ region: id,
434
+ ...layout.inset && ["US-AK", "US-HI"].includes(id) ? { inset: true } : {}
435
+ }
436
+ }));
437
+ }
438
+ }
439
+ const valueField = enc.value, values = valueField ? rows.map((r) => r[valueField]).filter((v) => typeof v === "number" && Number.isFinite(v)) : [];
440
+ if (values.some((v) => v < 0)) ctx.fit("geo-scatter value 不可为负数", "协议规定非负数值按面积映射;修正 value");
441
+ const valueDomain = valueField ? ctx.field(valueField).domain : null;
442
+ const min = valueDomain?.length === 2 && valueDomain.every(Number.isFinite) ? valueDomain[0] : values.length ? Math.min(...values) : 0;
443
+ const max = valueDomain?.length === 2 && valueDomain.every(Number.isFinite) ? valueDomain[1] : values.length ? Math.max(...values) : 0;
444
+ const visible = [], outside = [];
445
+ rows.forEach((row, index) => {
446
+ const lon = row[enc.longitude], lat = row[enc.latitude], id = enc.id ? row[enc.id] : null, label = enc.label ? row[enc.label] : null;
447
+ const stateInset = layout.inset && (lat > 50 && lon <= -129 ? "US-AK" : lat >= 18 && lat <= 23 && lon >= -161 && lon <= -154 ? "US-HI" : null);
448
+ const p = stateInset ? insetPoint(layout, stateInset, lon, lat) : projectPoint(lon, lat), hasValue = !!valueField && typeof row[valueField] === "number" && Number.isFinite(row[valueField]);
449
+ const beyondBounds = !stateInset && (lon < layout.bounds.west || lon > layout.bounds.east || lat < layout.bounds.south || lat > layout.bounds.north);
450
+ if (!p || beyondBounds || p[0] < 0 || p[0] > layout.width || p[1] < 0 || p[1] > layout.height - layout.bottom) {
451
+ outside.push(label || id || `#${index + 1}`);
452
+ return;
453
+ }
454
+ if (hasValue && row[valueField] === 0) {
455
+ visible.push({
456
+ index,
457
+ label,
458
+ id,
459
+ value: 0,
460
+ zero: true
461
+ });
462
+ return;
463
+ }
464
+ const value = hasValue ? row[valueField] : null, radius = hasValue && max > 0 ? Math.sqrt(value / max) * 9 : 4.5;
465
+ children.push(ctx.circle(p[0], p[1], radius, {
466
+ fill: c.high,
467
+ "fill-opacity": .85,
468
+ stroke: c.ink,
469
+ "stroke-width": 1,
470
+ meta: {
471
+ kind: "geo-scatter",
472
+ ...id != null ? { id } : {},
473
+ ...label != null ? { label } : {},
474
+ value
475
+ }
476
+ }));
477
+ visible.push({
478
+ index,
479
+ label,
480
+ id,
481
+ value,
482
+ zero: false
483
+ });
484
+ });
485
+ if (valueField) bottomLegend(children, ctx, layout, min, max, valueField, true);
486
+ const indexY = layout.height - layout.bottom + INDEX_TOP, localized = ctx.lang.startsWith("en");
487
+ children.push(ctx.text(24, indexY, localized ? "Locations" : "地点索引", {
488
+ size: 12,
489
+ weight: 600,
490
+ fill: c.ink
491
+ }));
492
+ const columns = layout.width < 420 ? 1 : 2;
493
+ const shown = visible.slice(0, INDEX_CAP);
494
+ const omitted = visible.length - shown.length;
495
+ const columnWidth = Math.max(96, (layout.width - 48) / columns - 12);
496
+ let indexBottom = indexY + 8;
497
+ const columnHeights = Array.from({ length: columns }, () => indexY + 20);
498
+ shown.forEach((item, i) => {
499
+ const column = i % columns;
500
+ const x = 24 + column * ((layout.width - 48) / columns);
501
+ const y = columnHeights[column];
502
+ const name = item.label || item.id || `#${item.index + 1}`;
503
+ const readout = item.zero ? localized ? `${formattedValue(ctx, 0, valueField)} (no marker)` : `${formattedValue(ctx, 0, valueField)}(无点)` : item.value == null ? localized ? "no value" : "无数值" : formattedValue(ctx, item.value, valueField);
504
+ const lines = ctx.wrap(`${name} · ${readout}`, columnWidth, 12, 400);
505
+ lines.forEach((line, lineIndex) => children.push(ctx.text(x, y + lineIndex * INDEX_ROW, line, {
506
+ size: 12,
507
+ fill: c.ink,
508
+ meta: lineIndex === 0 ? {
509
+ kind: "geo-scatter-index",
510
+ id: item.id,
511
+ label: name,
512
+ value: item.value
513
+ } : void 0
514
+ })));
515
+ columnHeights[column] = y + lines.length * INDEX_ROW;
516
+ indexBottom = Math.max(indexBottom, columnHeights[column]);
517
+ });
518
+ if (omitted > 0) {
519
+ const more = localized ? `and ${omitted} more` : `另有 ${omitted} 处`;
520
+ children.push(ctx.text(24, indexBottom + 4, more, {
521
+ size: 12,
522
+ fill: c.muted,
523
+ meta: {
524
+ kind: "geo-scatter-more",
525
+ omitted
526
+ }
527
+ }));
528
+ indexBottom += 20;
529
+ }
530
+ const reservedIndex = layout.bottom - INDEX_TOP;
531
+ const usedIndex = indexBottom - indexY + 16;
532
+ if (usedIndex > reservedIndex) layout.height += usedIndex - reservedIndex;
533
+ if (outside.length) {
534
+ const outsideY = indexBottom + 8;
535
+ const outsideLines = ctx.wrap(`${localized ? "Outside map bounds" : "不在地图范围"}: ${outside.join(", ")}`, Math.max(120, layout.width - 48), 12, 400);
536
+ outsideLines.forEach((line, lineIndex) => children.push(ctx.text(24, outsideY + lineIndex * INDEX_ROW, line, {
537
+ size: 12,
538
+ fill: c.muted,
539
+ meta: lineIndex === 0 ? {
540
+ kind: "geo-scatter-outside",
541
+ ids: outside
542
+ } : void 0
543
+ })));
544
+ layout.height += outsideLines.length * INDEX_ROW + 8;
545
+ }
546
+ mapCredit(children, ctx, layout, spec);
547
+ return {
548
+ width: layout.width,
549
+ height: layout.height,
550
+ title: spec.title || "Geographic scatter",
551
+ children
552
+ };
553
+ }
554
+ function install({ register }) {
555
+ register("choropleth", choropleth);
556
+ register("geo-scatter", scatter);
557
+ }
558
+ install({ register });
559
+ var maps = Object.freeze({
560
+ project,
561
+ featuresFor
562
+ });
563
+ export { install, maps, maps_exports };