@liujitcn/kratos-admin-bi 0.0.0-stage → 0.1.6

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 (166) hide show
  1. package/README.md +43 -2
  2. package/dist/package/declarations/index.d.ts +1 -0
  3. package/dist/package/declarations/src/api/bi/admin/v1/bi_card.d.ts +18 -0
  4. package/dist/package/declarations/src/api/bi/admin/v1/bi_card.js +46 -0
  5. package/dist/package/declarations/src/api/bi/admin/v1/bi_dashboard.d.ts +17 -0
  6. package/dist/package/declarations/src/api/bi/admin/v1/bi_dashboard.js +30 -0
  7. package/dist/package/declarations/src/api/bi/admin/v1/bi_query.d.ts +11 -0
  8. package/dist/package/declarations/src/api/bi/admin/v1/bi_query.js +41 -0
  9. package/dist/package/declarations/src/api/bi/admin/v1/bi_query_record.d.ts +10 -0
  10. package/dist/package/declarations/src/api/bi/admin/v1/bi_query_record.js +14 -0
  11. package/dist/package/declarations/src/api/bi/admin/v1/bi_source.d.ts +26 -0
  12. package/dist/package/declarations/src/api/bi/admin/v1/bi_source.js +46 -0
  13. package/dist/package/declarations/src/bi-core.d.ts +17 -0
  14. package/dist/package/declarations/src/bi-core.js +22 -0
  15. package/dist/package/declarations/src/bi-dashboard-schema.d.ts +31 -0
  16. package/dist/package/declarations/src/bi-dashboard-schema.js +39 -0
  17. package/dist/package/declarations/src/bi-messages.d.ts +247 -0
  18. package/dist/package/declarations/src/bi-messages.js +247 -0
  19. package/dist/package/declarations/src/bi-response.d.ts +10 -0
  20. package/dist/package/declarations/src/bi-response.js +33 -0
  21. package/dist/package/declarations/src/bi-vue.d.ts +10 -0
  22. package/dist/package/declarations/src/bi-vue.js +10 -0
  23. package/dist/package/declarations/src/card-schema.d.ts +30 -0
  24. package/dist/package/declarations/src/card-schema.js +87 -0
  25. package/dist/package/declarations/src/components/BiCardChartEditor.vue.d.ts +17 -0
  26. package/dist/package/declarations/src/components/BiCardChartEditor.vue.js +527 -0
  27. package/dist/package/declarations/src/components/BiCardFormDialog.vue.d.ts +20 -0
  28. package/dist/package/declarations/src/components/BiCardFormDialog.vue.js +495 -0
  29. package/dist/package/declarations/src/components/BiCardLibrary.vue.d.ts +3 -0
  30. package/dist/package/declarations/src/components/BiCardLibrary.vue.js +807 -0
  31. package/dist/package/declarations/src/components/BiCardVisualization.vue.d.ts +10 -0
  32. package/dist/package/declarations/src/components/BiCardVisualization.vue.js +97 -0
  33. package/dist/package/declarations/src/components/BiChart.vue.d.ts +20 -0
  34. package/dist/package/declarations/src/components/BiChart.vue.js +112 -0
  35. package/dist/package/declarations/src/components/BiDashboard.vue.d.ts +9 -0
  36. package/dist/package/declarations/src/components/BiDashboard.vue.js +81 -0
  37. package/dist/package/declarations/src/components/BiDashboardMenuDialog.vue.d.ts +12 -0
  38. package/dist/package/declarations/src/components/BiDashboardMenuDialog.vue.js +252 -0
  39. package/dist/package/declarations/src/components/BiDataCard.vue.d.ts +36 -0
  40. package/dist/package/declarations/src/components/BiDataCard.vue.js +372 -0
  41. package/dist/package/declarations/src/components/BiDesigner.vue.d.ts +17 -0
  42. package/dist/package/declarations/src/components/BiDesigner.vue.js +666 -0
  43. package/dist/package/declarations/src/components/BiSourceModelDialog.vue.d.ts +17 -0
  44. package/dist/package/declarations/src/components/BiSourceModelDialog.vue.js +2118 -0
  45. package/dist/package/declarations/src/components/BiWorkbenchPreview.vue.d.ts +7 -0
  46. package/dist/package/declarations/src/components/BiWorkbenchPreview.vue.js +176 -0
  47. package/dist/package/declarations/src/components/source-model-layout.d.ts +3 -0
  48. package/dist/package/declarations/src/components/source-model-layout.js +66 -0
  49. package/dist/package/declarations/src/dataset/index.d.ts +7 -0
  50. package/dist/package/declarations/src/dataset/index.js +25 -0
  51. package/dist/package/declarations/src/dataset/labels.d.ts +2 -0
  52. package/dist/package/declarations/src/dataset/labels.js +21 -0
  53. package/dist/package/declarations/src/dataset/transform.d.ts +20 -0
  54. package/dist/package/declarations/src/dataset/transform.js +173 -0
  55. package/dist/package/declarations/src/engine/chart-types.d.ts +14 -0
  56. package/dist/package/declarations/src/engine/chart-types.js +37 -0
  57. package/dist/package/declarations/src/engine/echarts.d.ts +16 -0
  58. package/dist/package/declarations/src/engine/echarts.js +491 -0
  59. package/dist/package/declarations/src/engine/types.d.ts +36 -0
  60. package/dist/package/declarations/src/engine/types.js +20 -0
  61. package/dist/package/declarations/src/env.d.ts +17 -0
  62. package/dist/package/declarations/src/env.js +67 -0
  63. package/dist/package/declarations/src/index.d.ts +1 -0
  64. package/dist/package/declarations/src/index.js +1 -0
  65. package/dist/package/declarations/src/locales/en-US.json +254 -0
  66. package/dist/package/declarations/src/locales/generated.d.ts +1021 -0
  67. package/dist/package/declarations/src/locales/generated.js +13 -0
  68. package/dist/package/declarations/src/locales/ja-JP.json +254 -0
  69. package/dist/package/declarations/src/locales/zh-CN.json +254 -0
  70. package/dist/package/declarations/src/locales/zh-TW.json +254 -0
  71. package/dist/package/declarations/src/module.d.ts +2 -0
  72. package/dist/package/declarations/src/module.js +9 -0
  73. package/dist/package/declarations/src/rpc/bi/admin/v1/bi_card.d.ts +145 -0
  74. package/dist/package/declarations/src/rpc/bi/admin/v1/bi_card.js +6 -0
  75. package/dist/package/declarations/src/rpc/bi/admin/v1/bi_dashboard.d.ts +107 -0
  76. package/dist/package/declarations/src/rpc/bi/admin/v1/bi_dashboard.js +6 -0
  77. package/dist/package/declarations/src/rpc/bi/admin/v1/bi_query.d.ts +157 -0
  78. package/dist/package/declarations/src/rpc/bi/admin/v1/bi_query.js +6 -0
  79. package/dist/package/declarations/src/rpc/bi/admin/v1/bi_query_record.d.ts +83 -0
  80. package/dist/package/declarations/src/rpc/bi/admin/v1/bi_query_record.js +15 -0
  81. package/dist/package/declarations/src/rpc/bi/admin/v1/bi_source.d.ts +301 -0
  82. package/dist/package/declarations/src/rpc/bi/admin/v1/bi_source.js +6 -0
  83. package/dist/package/declarations/src/rpc/common/v1/types.d.ts +60 -0
  84. package/dist/package/declarations/src/rpc/common/v1/types.js +6 -0
  85. package/dist/package/declarations/src/schema/layout.d.ts +7 -0
  86. package/dist/package/declarations/src/schema/layout.js +52 -0
  87. package/dist/package/declarations/src/schema/types.d.ts +83 -0
  88. package/dist/package/declarations/src/schema/types.js +1 -0
  89. package/dist/package/declarations/src/theme/index.d.ts +29 -0
  90. package/dist/package/declarations/src/theme/index.js +83 -0
  91. package/dist/package/declarations/src/theme/palettes.d.ts +7 -0
  92. package/dist/package/declarations/src/theme/palettes.js +28 -0
  93. package/dist/package/declarations/src/theme/presets.d.ts +73 -0
  94. package/dist/package/declarations/src/theme/presets.js +235 -0
  95. package/dist/package/declarations/src/views/card/index.vue.d.ts +3 -0
  96. package/dist/package/declarations/src/views/card/index.vue.js +18 -0
  97. package/dist/package/declarations/src/views/dashboard/index.vue.d.ts +3 -0
  98. package/dist/package/declarations/src/views/dashboard/index.vue.js +107 -0
  99. package/dist/package/declarations/src/views/designer/index.vue.d.ts +3 -0
  100. package/dist/package/declarations/src/views/designer/index.vue.js +332 -0
  101. package/dist/package/declarations/src/views/manage/index.vue.d.ts +3 -0
  102. package/dist/package/declarations/src/views/manage/index.vue.js +325 -0
  103. package/dist/package/declarations/src/views/query/index.vue.d.ts +3 -0
  104. package/dist/package/declarations/src/views/query/index.vue.js +876 -0
  105. package/dist/package/declarations/src/views/source/index.vue.d.ts +3 -0
  106. package/dist/package/declarations/src/views/source/index.vue.js +343 -0
  107. package/dist/package/declarations/src/views/workbench/index.vue.d.ts +3 -0
  108. package/dist/package/declarations/src/views/workbench/index.vue.js +362 -0
  109. package/dist/package/src/api/bi/admin/v1/bi_card.ts +67 -0
  110. package/dist/package/src/api/bi/admin/v1/bi_dashboard.ts +51 -0
  111. package/dist/package/src/api/bi/admin/v1/bi_query.ts +55 -0
  112. package/dist/package/src/api/bi/admin/v1/bi_query_record.ts +25 -0
  113. package/dist/package/src/api/bi/admin/v1/bi_source.ts +80 -0
  114. package/dist/package/src/bi-core.ts +43 -0
  115. package/dist/package/src/bi-dashboard-schema.ts +69 -0
  116. package/dist/package/src/bi-messages.ts +249 -0
  117. package/dist/package/src/bi-response.ts +39 -0
  118. package/dist/package/src/bi-vue.ts +13 -0
  119. package/dist/package/src/card-schema.ts +111 -0
  120. package/dist/package/src/components/BiCardChartEditor.vue +139 -0
  121. package/dist/package/src/components/BiCardFormDialog.vue +289 -0
  122. package/dist/package/src/components/BiCardLibrary.vue +310 -0
  123. package/dist/package/src/components/BiCardVisualization.vue +95 -0
  124. package/dist/package/src/components/BiChart.vue +134 -0
  125. package/dist/package/src/components/BiDashboard.vue +53 -0
  126. package/dist/package/src/components/BiDashboardMenuDialog.vue +177 -0
  127. package/dist/package/src/components/BiDataCard.vue +231 -0
  128. package/dist/package/src/components/BiDesigner.vue +287 -0
  129. package/dist/package/src/components/BiSourceModelDialog.vue +846 -0
  130. package/dist/package/src/components/BiWorkbenchPreview.vue +131 -0
  131. package/dist/package/src/components/source-model-layout.ts +62 -0
  132. package/dist/package/src/dataset/index.ts +28 -0
  133. package/dist/package/src/dataset/labels.ts +19 -0
  134. package/dist/package/src/dataset/transform.ts +189 -0
  135. package/dist/package/src/engine/chart-types.ts +44 -0
  136. package/dist/package/src/engine/echarts.ts +568 -0
  137. package/dist/package/src/engine/types.ts +60 -0
  138. package/dist/package/src/env.ts +82 -0
  139. package/dist/package/src/index.ts +1 -0
  140. package/dist/package/src/locales/en-US.json +254 -0
  141. package/dist/package/src/locales/generated.ts +16 -0
  142. package/dist/package/src/locales/ja-JP.json +254 -0
  143. package/dist/package/src/locales/zh-CN.json +254 -0
  144. package/dist/package/src/locales/zh-TW.json +254 -0
  145. package/dist/package/src/module.ts +12 -0
  146. package/dist/package/src/rpc/bi/admin/v1/bi_card.ts +169 -0
  147. package/dist/package/src/rpc/bi/admin/v1/bi_dashboard.ts +126 -0
  148. package/dist/package/src/rpc/bi/admin/v1/bi_query.ts +187 -0
  149. package/dist/package/src/rpc/bi/admin/v1/bi_query_record.ts +98 -0
  150. package/dist/package/src/rpc/bi/admin/v1/bi_source.ts +338 -0
  151. package/dist/package/src/rpc/common/v1/types.ts +77 -0
  152. package/dist/package/src/schema/layout.ts +54 -0
  153. package/dist/package/src/schema/types.ts +103 -0
  154. package/dist/package/src/styles.scss +1 -0
  155. package/dist/package/src/theme/index.ts +108 -0
  156. package/dist/package/src/theme/palettes.ts +34 -0
  157. package/dist/package/src/theme/presets.ts +278 -0
  158. package/dist/package/src/views/card/index.vue +5 -0
  159. package/dist/package/src/views/dashboard/index.vue +58 -0
  160. package/dist/package/src/views/designer/index.vue +164 -0
  161. package/dist/package/src/views/manage/index.vue +276 -0
  162. package/dist/package/src/views/query/index.vue +593 -0
  163. package/dist/package/src/views/source/index.vue +214 -0
  164. package/dist/package/src/views/workbench/index.vue +208 -0
  165. package/package.json +67 -3
  166. package/tsconfig.json +12 -0
@@ -0,0 +1,568 @@
1
+ /**
2
+ * ECharts 引擎适配器(默认内置引擎)。
3
+ * 按需注册图表与组件(素材:go-wind-admin ele 端 plugins/echarts 注册表),
4
+ * 避免 import * as echarts from 'echarts' 的全量引入。
5
+ */
6
+ import * as echarts from "echarts/core";
7
+ import {
8
+ BarChart,
9
+ FunnelChart,
10
+ GaugeChart,
11
+ HeatmapChart,
12
+ LineChart,
13
+ PieChart,
14
+ RadarChart,
15
+ SankeyChart,
16
+ ScatterChart,
17
+ SunburstChart,
18
+ TreemapChart,
19
+ } from "echarts/charts";
20
+ import {
21
+ DataZoomComponent,
22
+ VisualMapComponent,
23
+ DataZoomInsideComponent,
24
+ DataZoomSliderComponent,
25
+ DatasetComponent,
26
+ GridComponent,
27
+ LegendComponent,
28
+ TitleComponent,
29
+ TooltipComponent,
30
+ TransformComponent,
31
+ } from "echarts/components";
32
+ import { LabelLayout, UniversalTransition } from "echarts/features";
33
+ import { CanvasRenderer } from "echarts/renderers";
34
+
35
+ import type {
36
+ BarSeriesOption,
37
+ GaugeSeriesOption,
38
+ LineSeriesOption,
39
+ PieSeriesOption,
40
+ RadarSeriesOption,
41
+ ScatterSeriesOption,
42
+ } from "echarts/charts";
43
+ import type { ComposeOption } from "echarts/core";
44
+ import type {
45
+ GridComponentOption,
46
+ LegendComponentOption,
47
+ TitleComponentOption,
48
+ TooltipComponentOption,
49
+ } from "echarts/components";
50
+
51
+ import { getColumns, toNumber, unique } from "../dataset";
52
+ import { createDimensionLabelFormatter } from "../dataset/labels";
53
+ import { formatNumber } from "../dataset/transform";
54
+ import { resolvePalette } from "../theme/palettes";
55
+ import type { BiResolvedTheme } from "../theme";
56
+ import type { BiChartSchema, BiRow } from "../schema/types";
57
+ import type { BiChartEvent, ChartEngine, ChartHandle } from "./types";
58
+ import { registerEngine } from "./types";
59
+
60
+ let registered = false;
61
+
62
+ function ensureRegistered() {
63
+ if (registered) return;
64
+ registered = true;
65
+ echarts.use([
66
+ BarChart,
67
+ LineChart,
68
+ PieChart,
69
+ ScatterChart,
70
+ RadarChart,
71
+ GaugeChart,
72
+ FunnelChart,
73
+ TreemapChart,
74
+ SankeyChart,
75
+ HeatmapChart,
76
+ SunburstChart,
77
+ DatasetComponent,
78
+ GridComponent,
79
+ LegendComponent,
80
+ TitleComponent,
81
+ TooltipComponent,
82
+ TransformComponent,
83
+ VisualMapComponent,
84
+ DataZoomComponent,
85
+ DataZoomInsideComponent,
86
+ DataZoomSliderComponent,
87
+ LabelLayout,
88
+ UniversalTransition,
89
+ CanvasRenderer,
90
+ ]);
91
+ }
92
+
93
+ export type BiEChartsOption = ComposeOption<
94
+ | BarSeriesOption
95
+ | LineSeriesOption
96
+ | PieSeriesOption
97
+ | ScatterSeriesOption
98
+ | RadarSeriesOption
99
+ | GaugeSeriesOption
100
+ | TitleComponentOption
101
+ | LegendComponentOption
102
+ | TooltipComponentOption
103
+ | GridComponentOption
104
+ >;
105
+
106
+ /** 轴最大值取整到「好看的刻度」(1/2/5×10^n) */
107
+ function niceMax(value: number): number {
108
+ if (value <= 0) return 100;
109
+ const exp = Math.floor(Math.log10(value));
110
+ const base = Math.pow(10, exp);
111
+ const scaled = value / base;
112
+ const nice = scaled <= 1 ? 1 : scaled <= 2 ? 2 : scaled <= 5 ? 5 : 10;
113
+ return nice * base;
114
+ }
115
+
116
+ function axisStyles(theme: BiResolvedTheme) {
117
+ return {
118
+ axisLabel: { color: theme.subText },
119
+ axisLine: { lineStyle: { color: theme.axisLine } },
120
+ axisTick: { lineStyle: { color: theme.axisLine } },
121
+ };
122
+ }
123
+
124
+ function tooltipStyles(theme: BiResolvedTheme) {
125
+ return {
126
+ backgroundColor: theme.tooltipBg,
127
+ borderColor: theme.axisLine,
128
+ textStyle: { color: theme.text },
129
+ };
130
+ }
131
+
132
+ /** schema + 行数据 + 主题模式 → ECharts option */
133
+ export function compileEcharts(
134
+ chart: BiChartSchema,
135
+ rows: BiRow[],
136
+ theme: BiResolvedTheme
137
+ ): BiEChartsOption {
138
+ const enc = chart.encoding ?? {};
139
+ const style = chart.style ?? {};
140
+ const cols = getColumns(rows);
141
+ const dimensionLabel = createDimensionLabelFormatter(rows.map(row => row[enc.dimension ?? cols[0] ?? ""]), enc.dimension);
142
+ const seriesLabel = createDimensionLabelFormatter(rows.map(row => row[enc.series ?? cols[1] ?? ""]), enc.series);
143
+ const fmt = style.format;
144
+ const palette = resolvePalette(style.palette, theme);
145
+ const fmtTooltip = fmt ? { valueFormatter: (v: unknown) => formatNumber(toNumber(v), fmt) } : {};
146
+ const base: BiEChartsOption = {
147
+ backgroundColor: "transparent",
148
+ color: palette,
149
+ textStyle: { color: theme.text },
150
+ tooltip: { ...tooltipStyles(theme), ...fmtTooltip },
151
+ };
152
+ // 图例显隐:显式 style.showLegend 优先,否则多系列自动显示
153
+ const multiSeries = !!(enc.series || (enc.measures?.length ?? 0) > 1);
154
+ const showLegend = style.showLegend ?? multiSeries;
155
+ // 引擎原生透传:浅合并每个顶层键(嵌套覆盖用整键替换语义)
156
+ const passthrough = (chart.options ?? {}) as object;
157
+ // y 轴数值格式化
158
+ const yAxisLabel = fmt ? { formatter: (v: number) => formatNumber(v, fmt) } : {};
159
+
160
+ const labelOption = style.showLabel ? { label: { show: true, color: theme.subText } } : {};
161
+ const cartesian = (seriesIn: any[], categories: string[], area: boolean): BiEChartsOption => {
162
+ const series = seriesIn.map((sr) => ({
163
+ ...labelOption,
164
+ ...(style.stack ? { stack: "total" } : {}),
165
+ ...sr,
166
+ }));
167
+ const hasLegend = showLegend && (multiSeries || seriesIn.length > 1 || enc.series);
168
+ return ({
169
+ ...base,
170
+ legend: hasLegend
171
+ ? { top: 0, textStyle: { color: theme.subText }, itemWidth: 14, itemHeight: 8, formatter: (name: string) => name.split(" · ").map(seriesLabel).join(" · ") }
172
+ : undefined,
173
+ grid: { left: 8, right: 16, top: hasLegend ? 32 : 20, bottom: style.dataZoom ? 32 : 8, outerBoundsMode: "same", outerBoundsContain: "axisLabel" },
174
+ xAxis: {
175
+ ...(chart.type === "scatter" ? { type: "value" } : { type: "category", data: categories }),
176
+ ...axisStyles(theme),
177
+ ...(chart.type !== "scatter" ? { axisLabel: { color: theme.subText, formatter: dimensionLabel } } : {}),
178
+ splitLine: { show: false },
179
+ },
180
+ yAxis: {
181
+ type: "value",
182
+ ...axisStyles(theme),
183
+ axisLabel: { ...axisStyles(theme).axisLabel, ...yAxisLabel },
184
+ splitLine: { lineStyle: { color: theme.splitLine } },
185
+ },
186
+ ...(style.dataZoom
187
+ ? { dataZoom: [{ type: "inside" }, { type: "slider", height: 16, bottom: 4, ...(chart.type !== "scatter" ? { labelFormatter: (_value: number, text: string) => dimensionLabel(text) } : {}) }] }
188
+ : {}),
189
+ series: series.map((s) => ({
190
+ smooth: true,
191
+ ...(area ? { areaStyle: { opacity: 0.15 } } : {}),
192
+ ...s,
193
+ })),
194
+ ...passthrough,
195
+ } as BiEChartsOption);
196
+ };
197
+
198
+ switch (chart.type) {
199
+ case "bar":
200
+ case "line":
201
+ case "area":
202
+ case "scatter": {
203
+ const dim = enc.dimension ?? cols[0] ?? "";
204
+ const measure = enc.measure ?? cols.find((c) => c !== dim) ?? "";
205
+ // ECharts 无 area 系列:面积 = line + areaStyle(cartesian 已按 area 注入)
206
+ const seriesType = chart.type === "area" ? "line" : chart.type;
207
+ const categories = unique(rows.map((r) => String(r[dim] ?? "")));
208
+ if (["line", "area"].includes(chart.type) && !chart.sort && categories.every(value => /^\d{4}-\d{2}(?:-\d{2})?(?:[ T].*)?$/.test(value)))
209
+ categories.sort();
210
+
211
+ let series: any[];
212
+ if (chart.type === "scatter") {
213
+ const group = (name: string, subset: BiRow[]) => ({
214
+ name,
215
+ type: "scatter" as const,
216
+ data: subset.map((r) => [toNumber(r[dim]), toNumber(r[measure])]),
217
+ symbolSize: 10,
218
+ });
219
+ series = enc.series
220
+ ? unique(rows.map((r) => String(r[enc.series!]))).map((sn) =>
221
+ group(sn, rows.filter((r) => String(r[enc.series!]) === sn))
222
+ )
223
+ : [group(chart.title ?? measure, rows)];
224
+ } else {
225
+ const seriesNames = enc.series ? unique(rows.map((r) => String(r[enc.series!]))) : [""];
226
+ const measures = enc.measures?.length ? enc.measures : [measure];
227
+ series = seriesNames.flatMap((sn) => measures.map((m) => ({
228
+ name: enc.series ? (measures.length > 1 ? `${sn} · ${m}` : sn) : (enc.measures?.length ? m : chart.title ?? m),
229
+ type: seriesType,
230
+ data: categories.map((c) => {
231
+ const hit = rows.find((r) => String(r[dim]) === c && (!enc.series || String(r[enc.series]) === sn));
232
+ return hit ? toNumber(hit[m]) : null;
233
+ }),
234
+ })));
235
+ }
236
+ return {
237
+ ...cartesian(series, categories, chart.type === "area"),
238
+ ...passthrough,
239
+ } as BiEChartsOption;
240
+ }
241
+
242
+ case "pie":
243
+ case "donut": {
244
+ const dim = enc.dimension ?? cols[0] ?? "name";
245
+ const measure = enc.measure ?? cols.find((c) => c !== dim) ?? "value";
246
+ return {
247
+ ...base,
248
+ legend: style.showLegend === false
249
+ ? undefined
250
+ : { bottom: 0, textStyle: { color: theme.subText }, itemWidth: 14, itemHeight: 8, formatter: dimensionLabel },
251
+ series: [
252
+ {
253
+ type: "pie",
254
+ radius: chart.type === "donut" ? ["42%", "68%"] : "68%",
255
+ center: ["50%", "46%"],
256
+ itemStyle: { borderColor: "transparent", borderWidth: 2 },
257
+ label: {
258
+ show: style.showLabel ?? true,
259
+ color: theme.subText,
260
+ formatter: (p: any) => dimensionLabel(p.name) + (fmt ? ` ${formatNumber(toNumber(p.value), fmt)}` : ""),
261
+ },
262
+ data: rows.map((r) => ({
263
+ name: String(r[dim] ?? ""),
264
+ value: toNumber(r[measure]),
265
+ })),
266
+ },
267
+ ],
268
+ ...passthrough,
269
+ } as BiEChartsOption;
270
+ }
271
+
272
+ case "radar": {
273
+ const dim = enc.dimension ?? cols[0] ?? "name";
274
+ const measure = enc.measure ?? cols.find((c) => c !== dim) ?? "value";
275
+ const max = niceMax(Math.max(...rows.map((x) => toNumber(x[measure])), 1));
276
+ const categories = unique(rows.map((r) => String(r[dim] ?? "")));
277
+ const indicators = categories.map((name) => ({ name, max }));
278
+
279
+ let data: any[];
280
+ if (enc.series) {
281
+ const seriesNames = unique(rows.map((r) => String(r[enc.series!])));
282
+ data = seriesNames.map((sn) => ({
283
+ name: sn,
284
+ value: categories.map((category) => {
285
+ const hit = rows.find((r) => String(r[dim]) === category && String(r[enc.series!]) === sn);
286
+ return hit ? toNumber(hit[measure]) : null;
287
+ }),
288
+ }));
289
+ } else {
290
+ data = [{ name: chart.title ?? measure, value: rows.map((r) => toNumber(r[measure])) }];
291
+ }
292
+ return {
293
+ ...base,
294
+ legend: enc.series
295
+ ? { bottom: 0, textStyle: { color: theme.subText }, itemWidth: 14, itemHeight: 8, formatter: seriesLabel }
296
+ : undefined,
297
+ radar: {
298
+ indicator: indicators,
299
+ center: ["50%", "50%"],
300
+ radius: "62%",
301
+ axisName: { color: theme.subText, formatter: dimensionLabel },
302
+ axisLine: { lineStyle: { color: theme.axisLine } },
303
+ splitLine: { lineStyle: { color: theme.splitLine } },
304
+ splitArea: { show: false },
305
+ },
306
+ series: [{ type: "radar", data, areaStyle: { opacity: 0.12 } }],
307
+ ...passthrough,
308
+ } as BiEChartsOption;
309
+ }
310
+
311
+ case "gauge": {
312
+ const measure = enc.measure ?? cols[0] ?? "value";
313
+ const value = toNumber(rows[0]?.[measure]);
314
+ const max = niceMax(Math.max(value, 100));
315
+ return {
316
+ ...base,
317
+ series: [
318
+ {
319
+ type: "gauge",
320
+ min: 0,
321
+ max,
322
+ startAngle: 210,
323
+ endAngle: -30,
324
+ progress: { show: true, width: 12 },
325
+ axisLine: { lineStyle: { width: 12, color: [[1, theme.splitLine]] } },
326
+ axisTick: { show: false },
327
+ splitLine: { length: 6, lineStyle: { color: theme.axisLine } },
328
+ axisLabel: { color: theme.subText, distance: 16, fontSize: 10 },
329
+ pointer: { itemStyle: { color: "auto" } },
330
+ detail: {
331
+ valueAnimation: true,
332
+ formatter: fmt
333
+ ? (v: number) => formatNumber(v, fmt)
334
+ : "{value}",
335
+ color: theme.text,
336
+ fontSize: 22,
337
+ offsetCenter: [0, "62%"],
338
+ },
339
+ data: [{ value }],
340
+ },
341
+ ],
342
+ ...passthrough,
343
+ } as BiEChartsOption;
344
+ }
345
+
346
+ case "funnel": {
347
+ const dim = enc.dimension ?? cols[0] ?? "name";
348
+ const measure = enc.measure ?? cols.find((c) => c !== dim) ?? "value";
349
+ // 漏斗要求降序
350
+ const sorted = [...rows]
351
+ .map((r) => ({ name: String(r[dim] ?? ""), value: toNumber(r[measure]) }))
352
+ .sort((a, b) => b.value - a.value);
353
+ return {
354
+ ...base,
355
+ tooltip: { ...tooltipStyles(theme), trigger: "item", ...fmtTooltip },
356
+ series: [
357
+ {
358
+ type: "funnel",
359
+ left: "10%",
360
+ top: 12,
361
+ bottom: 12,
362
+ width: "80%",
363
+ sort: "descending",
364
+ gap: 2,
365
+ label: { show: true, position: "inside", color: "#fff", formatter: (p: any) => dimensionLabel(p.name) + (fmt ? ` ${formatNumber(toNumber(p.value), fmt)}` : "") },
366
+ data: sorted,
367
+ },
368
+ ],
369
+ ...passthrough,
370
+ } as BiEChartsOption;
371
+ }
372
+
373
+ case "treemap": {
374
+ const dim = enc.dimension ?? cols[0] ?? "name";
375
+ const measure = enc.measure ?? cols.find((c) => c !== dim) ?? "value";
376
+ return {
377
+ ...base,
378
+ series: [
379
+ {
380
+ type: "treemap",
381
+ left: 8,
382
+ right: 8,
383
+ top: 12,
384
+ bottom: 8,
385
+ roam: false,
386
+ nodeClick: false,
387
+ breadcrumb: { show: false },
388
+ label: { show: true, color: "#fff", formatter: (p: any) => dimensionLabel(p.name) + (fmt ? ` ${formatNumber(toNumber(p.value), fmt)}` : "") },
389
+ data: rows.map((r) => ({
390
+ name: String(r[dim] ?? ""),
391
+ value: toNumber(r[measure]),
392
+ })),
393
+ },
394
+ ],
395
+ ...passthrough,
396
+ } as BiEChartsOption;
397
+ }
398
+
399
+ case "sunburst": {
400
+ // 两层旭日:series 分组为内环,维度为外环叶子
401
+ const dim = enc.dimension ?? cols[0] ?? "name";
402
+ const measure = enc.measure ?? cols.find((c) => c !== dim) ?? "value";
403
+ let data: any[];
404
+ if (enc.series) {
405
+ const seriesNames = unique(rows.map((r) => String(r[enc.series!])));
406
+ data = seriesNames.map((sn) => {
407
+ const subset = rows.filter((r) => String(r[enc.series!]) === sn);
408
+ return {
409
+ name: sn,
410
+ children: subset.map((r) => ({
411
+ name: String(r[dim] ?? ""),
412
+ value: toNumber(r[measure]),
413
+ })),
414
+ };
415
+ });
416
+ } else {
417
+ data = rows.map((r) => ({
418
+ name: String(r[dim] ?? ""),
419
+ value: toNumber(r[measure]),
420
+ }));
421
+ }
422
+ return {
423
+ ...base,
424
+ series: [
425
+ {
426
+ type: "sunburst",
427
+ radius: [enc.series ? "18%" : "0%", "82%"],
428
+ label: { color: theme.subText, rotate: "radial", formatter: (p: any) => dimensionLabel(seriesLabel(p.name)) },
429
+ itemStyle: { borderColor: "transparent", borderWidth: 1 },
430
+ data,
431
+ },
432
+ ],
433
+ ...passthrough,
434
+ } as BiEChartsOption;
435
+ }
436
+
437
+ case "sankey": {
438
+ // 边列表:dimension=起点,series=终点,measure=流量
439
+ const sourceField = enc.dimension ?? cols[0] ?? "source";
440
+ const targetField = enc.series ?? cols[1] ?? "target";
441
+ const valueField = enc.measure ?? cols[2] ?? "value";
442
+ const nodeIds = new Set<string>();
443
+ const links: any[] = [];
444
+ for (const row of rows) {
445
+ const source = String(row[sourceField] ?? "");
446
+ const target = String(row[targetField] ?? "");
447
+ if (!source || !target) continue;
448
+ nodeIds.add(source);
449
+ nodeIds.add(target);
450
+ links.push({ source, target, value: toNumber(row[valueField]) });
451
+ }
452
+ return {
453
+ ...base,
454
+ series: [
455
+ {
456
+ type: "sankey",
457
+ left: 16,
458
+ right: 16,
459
+ top: 12,
460
+ bottom: 12,
461
+ nodeWidth: 14,
462
+ nodeGap: 10,
463
+ emphasis: { focus: "adjacency" },
464
+ lineStyle: { color: "gradient", curveness: 0.5 },
465
+ label: { color: theme.subText, formatter: (p: any) => dimensionLabel(seriesLabel(p.name)) },
466
+ data: [...nodeIds].map((id) => ({ name: id })),
467
+ links,
468
+ },
469
+ ],
470
+ ...passthrough,
471
+ } as BiEChartsOption;
472
+ }
473
+
474
+ case "heatmap": {
475
+ // 矩阵三元组:dimension=x 轴,series=y 轴,measure=值
476
+ const xField = enc.dimension ?? cols[0] ?? "x";
477
+ const yField = enc.series ?? cols[1] ?? "y";
478
+ const valueField = enc.measure ?? cols[2] ?? "value";
479
+ const xCats = unique(rows.map((r) => String(r[xField] ?? "")));
480
+ const yCats = unique(rows.map((r) => String(r[yField] ?? "")));
481
+ const data = rows.map((r) => [
482
+ xCats.indexOf(String(r[xField] ?? "")),
483
+ yCats.indexOf(String(r[yField] ?? "")),
484
+ toNumber(r[valueField]),
485
+ ]);
486
+ const max = Math.max(...rows.map((r) => toNumber(r[valueField])), 1);
487
+ return {
488
+ ...base,
489
+ // 为横向色阶和坐标标签分别留出空间,避免覆盖最后一行数据。
490
+ grid: { left: 8, right: 24, top: 12, bottom: 68, outerBoundsMode: "same", outerBoundsContain: "axisLabel" },
491
+ xAxis: { type: "category", data: xCats, ...axisStyles(theme), axisLabel: { color: theme.subText, formatter: dimensionLabel }, splitArea: { show: true } },
492
+ yAxis: { type: "category", data: yCats, ...axisStyles(theme), axisLabel: { color: theme.subText, formatter: seriesLabel }, splitArea: { show: true } },
493
+ visualMap: {
494
+ min: 0,
495
+ max,
496
+ calculable: true,
497
+ orient: "horizontal",
498
+ left: "center",
499
+ bottom: 0,
500
+ textStyle: { color: theme.subText },
501
+ inRange: { color: theme.palette.slice(0, 5) },
502
+ },
503
+ series: [
504
+ {
505
+ type: "heatmap",
506
+ data,
507
+ label: { show: chart.style?.showLabel ?? true, color: theme.text, fontSize: 10 },
508
+ },
509
+ ],
510
+ ...passthrough,
511
+ } as BiEChartsOption;
512
+ }
513
+
514
+ default:
515
+ throw new Error(`[bi] unsupported chart type: ${(chart as any).type}`);
516
+ }
517
+ }
518
+
519
+ /**
520
+ * 创建 ECharts 引擎实例(幂等注册 echarts 模块)。
521
+ * handle 上带内部约定字段 __echartsInstance 供 bindEvents 取实例。
522
+ */
523
+ export function createEchartsEngine(): ChartEngine {
524
+ ensureRegistered();
525
+ return {
526
+ mount(el, option) {
527
+ const instance = echarts.init(el);
528
+ instance.setOption(option as any);
529
+ const handle: ChartHandle & { __echartsInstance?: echarts.ECharts } = {
530
+ __echartsInstance: instance,
531
+ update(next) {
532
+ instance.setOption(next as any, { notMerge: true });
533
+ },
534
+ resize() {
535
+ instance.resize();
536
+ },
537
+ dispose() {
538
+ instance.dispose();
539
+ },
540
+ getDataURL() {
541
+ return instance.getDataURL({ type: "png", pixelRatio: 2, backgroundColor: "transparent" });
542
+ },
543
+ };
544
+ return handle;
545
+ },
546
+ compile(chart, rows, theme) {
547
+ return compileEcharts(chart, rows, theme);
548
+ },
549
+ bindEvents(handle, cb) {
550
+ const inst = (handle as any).__echartsInstance as echarts.ECharts | undefined;
551
+ inst?.on("click", (params: any) => {
552
+ const e: BiChartEvent = {
553
+ chartId: "",
554
+ seriesName: params?.seriesName,
555
+ name: params?.name,
556
+ value: params?.value,
557
+ raw: params,
558
+ };
559
+ cb(e);
560
+ });
561
+ },
562
+ };
563
+ }
564
+
565
+ /** 默认注册(调用方引入本模块后执行一次;重复注册无害) */
566
+ export function installEchartsEngine(): void {
567
+ registerEngine("echarts", createEchartsEngine());
568
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * 图表引擎抽象:任何图表库实现这三个方法即可接入。
3
+ * 主题在编译期注入 option(引擎层不管主题切换,换主题 = 重新 compile + update)。
4
+ */
5
+
6
+ import type { BiChartSchema } from "../schema/types";
7
+ import type { BiRow } from "../schema/types";
8
+ import type { BiResolvedTheme } from "../theme";
9
+
10
+ /** 归一化图表事件(跨引擎联动的基础) */
11
+ export interface BiChartEvent {
12
+ chartId: string;
13
+ /** 命中的系列名(饼=系列即数据名;柱线=系列字段值) */
14
+ seriesName?: string;
15
+ /** 命中的类目名(dimension 值) */
16
+ name?: string;
17
+ value?: unknown;
18
+ /** 引擎原始事件参数(逃生口) */
19
+ raw?: unknown;
20
+ }
21
+
22
+ export interface ChartHandle {
23
+ update(option: unknown): void;
24
+ resize(): void;
25
+ dispose(): void;
26
+ /** 导出当前画布为 PNG data URL(引擎不支持时返回 undefined) */
27
+ getDataURL?(): string | undefined;
28
+ }
29
+
30
+ export interface ChartEngine {
31
+ /** 挂载容器并渲染首帧(option 由 compile 产出) */
32
+ mount(el: HTMLElement, option: unknown): ChartHandle;
33
+ /** schema + 行数据 + 主题 → 引擎原生 option */
34
+ compile(chart: BiChartSchema, rows: BiRow[], theme: BiResolvedTheme): unknown;
35
+ /** 订阅归一化点击事件 */
36
+ bindEvents(handle: ChartHandle, cb: (e: BiChartEvent) => void): void;
37
+ }
38
+
39
+ // ============ 注册表 ============
40
+
41
+ const engines = new Map<string, ChartEngine>();
42
+
43
+ export function registerEngine(name: string, engine: ChartEngine): void {
44
+ engines.set(name, engine);
45
+ }
46
+
47
+ export function getEngine(name: string | undefined): ChartEngine {
48
+ const key = name || "echarts";
49
+ const engine = engines.get(key);
50
+ if (!engine) {
51
+ throw new Error(
52
+ `[bi] chart engine "${key}" is not registered. Install and register the corresponding adapter package (e.g. registerEngine("${key}", create${key[0]?.toUpperCase() + key.slice(1)}Engine())).`
53
+ );
54
+ }
55
+ return engine;
56
+ }
57
+
58
+ export function hasEngine(name: string | undefined): boolean {
59
+ return engines.has(name || "echarts");
60
+ }