@aura-react-comp/charts 0.3.1 → 0.4.0

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 (68) hide show
  1. package/esm/charts/area-chart/index.js +27 -23
  2. package/esm/charts/bar-chart/index.js +17 -21
  3. package/esm/charts/candlestick-chart/index.js +242 -180
  4. package/esm/charts/combo-chart/index.js +8 -20
  5. package/esm/charts/funnel-chart/index.js +133 -108
  6. package/esm/charts/gauge-chart/index.js +102 -105
  7. package/esm/charts/graph-chart/index.js +154 -114
  8. package/esm/charts/heatmap-chart/index.js +179 -135
  9. package/esm/charts/line-chart/index.js +28 -24
  10. package/esm/charts/pie-chart/index.d.ts.map +1 -1
  11. package/esm/charts/pie-chart/index.js +198 -139
  12. package/esm/charts/radar-chart/index.d.ts.map +1 -1
  13. package/esm/charts/radar-chart/index.js +154 -128
  14. package/esm/charts/ring-progress/index.js +81 -82
  15. package/esm/charts/sankey-chart/index.js +171 -141
  16. package/esm/charts/scatter-chart/index.d.ts.map +1 -1
  17. package/esm/charts/scatter-chart/index.js +225 -159
  18. package/esm/charts/sparkline/index.js +81 -66
  19. package/esm/charts/tree-chart/index.js +128 -102
  20. package/esm/charts/treemap-chart/index.js +162 -141
  21. package/esm/charts/waterfall-chart/index.js +165 -121
  22. package/esm/components/Legend.d.ts +3 -2
  23. package/esm/components/Legend.d.ts.map +1 -1
  24. package/esm/components/Legend.js +79 -48
  25. package/esm/components/Tooltip.js +62 -71
  26. package/esm/components/chart/index.d.ts +4 -1
  27. package/esm/components/chart/index.d.ts.map +1 -1
  28. package/esm/components/chart/index.js +80 -102
  29. package/esm/components/chart.less +13 -0
  30. package/esm/core/animation.js +31 -43
  31. package/esm/core/export.d.ts +34 -0
  32. package/esm/core/export.d.ts.map +1 -0
  33. package/esm/core/export.js +390 -0
  34. package/esm/core/force.js +22 -45
  35. package/esm/core/format.js +48 -52
  36. package/esm/core/hierarchy.js +60 -92
  37. package/esm/core/layout.js +32 -50
  38. package/esm/core/sampling.js +30 -53
  39. package/esm/core/sankey.js +35 -55
  40. package/esm/core/scale.js +72 -86
  41. package/esm/core/shape.js +101 -133
  42. package/esm/core/sync.d.ts +41 -0
  43. package/esm/core/sync.d.ts.map +1 -0
  44. package/esm/core/sync.js +120 -0
  45. package/esm/hooks/useChartExport.d.ts +19 -0
  46. package/esm/hooks/useChartExport.d.ts.map +1 -0
  47. package/esm/hooks/useChartExport.js +35 -0
  48. package/esm/hooks/useChartTheme.js +17 -19
  49. package/esm/hooks/useChartTransition.js +58 -73
  50. package/esm/hooks/useElementSize.js +19 -28
  51. package/esm/hooks/usePlotPointer.d.ts +23 -0
  52. package/esm/hooks/usePlotPointer.d.ts.map +1 -0
  53. package/esm/hooks/usePlotPointer.js +94 -0
  54. package/esm/index.d.ts +6 -0
  55. package/esm/index.d.ts.map +1 -1
  56. package/esm/index.js +136 -47
  57. package/esm/internal/CartesianChart.d.ts.map +1 -1
  58. package/esm/internal/CartesianChart.js +666 -485
  59. package/esm/internal/model.js +44 -60
  60. package/esm/scene/canvas.js +100 -107
  61. package/esm/scene/svg.js +106 -99
  62. package/esm/scene/types.js +8 -15
  63. package/esm/style.css +10 -0
  64. package/esm/theme/index.js +143 -171
  65. package/esm/types.d.ts +4 -0
  66. package/esm/types.d.ts.map +1 -1
  67. package/esm/types.js +0 -1
  68. package/package.json +1 -1
@@ -1,32 +1,17 @@
1
1
  'use client';
2
+ // src/internal/model.ts
2
3
  import { extentOf } from "../core/scale";
3
4
  import { colorAt } from "../theme";
4
-
5
- /**
6
- * 数据模型归一化。
7
- *
8
- * 图表组件不直接消费用户传入的原始 props,而是先过这一层:
9
- * · 补齐缺省值(颜色、名称、类型);
10
- * · 计算堆叠基线(柱状图堆叠、面积图堆叠共用一套逻辑);
11
- * · 判定 y 轴值域。
12
- * 好处是所有边界情况(null、全零、单点、负值)只在这一处处理。
13
- */
14
-
15
5
  function toNumber(value) {
16
- if (value === null || value === undefined || value === '') return null;
17
- const num = typeof value === 'number' ? value : Number(value);
6
+ if (value === null || value === void 0 || value === "")
7
+ return null;
8
+ const num = typeof value === "number" ? value : Number(value);
18
9
  return Number.isNaN(num) ? null : num;
19
10
  }
20
- export function resolveSeries(options) {
21
- const {
22
- data,
23
- series,
24
- theme,
25
- defaultType,
26
- hidden
27
- } = options;
11
+ function resolveSeries(options) {
12
+ const { data, series, theme, defaultType, hidden } = options;
28
13
  return series.map((config, index) => {
29
- const values = config.data ? config.data.map(v => toNumber(v)) : data.map(row => toNumber(row[config.key]));
14
+ const values = config.data ? config.data.map((v) => toNumber(v)) : data.map((row) => toNumber(row[config.key]));
30
15
  return {
31
16
  config,
32
17
  index,
@@ -37,23 +22,15 @@ export function resolveSeries(options) {
37
22
  values,
38
23
  base: values.map(() => 0),
39
24
  top: values.slice(),
40
- visible: !config.hidden && !hidden?.has(config.key),
25
+ visible: !config.hidden && !(hidden == null ? void 0 : hidden.has(config.key)),
41
26
  stackId: config.stack
42
27
  };
43
28
  });
44
29
  }
45
-
46
- /**
47
- * 就地计算堆叠基线。
48
- *
49
- * 正负值分开累加(与 ECharts 行为一致):正值从 0 往上堆,
50
- * 负值从 0 往下堆,两者互不干扰 —— 否则「收入正、支出负」的图会错位。
51
- */
52
- export function applyStacking(series) {
53
- const groups = new Map();
30
+ function applyStacking(series) {
31
+ const groups = /* @__PURE__ */ new Map();
54
32
  for (const item of series) {
55
- if (!item.stackId) {
56
- // 非堆叠系列:base 归零,top 即原值
33
+ if (!item.stackId || !item.visible) {
57
34
  item.base = item.values.map(() => 0);
58
35
  item.top = item.values.slice();
59
36
  continue;
@@ -63,7 +40,7 @@ export function applyStacking(series) {
63
40
  groups.set(item.stackId, list);
64
41
  }
65
42
  for (const group of groups.values()) {
66
- const length = Math.max(0, ...group.map(item => item.values.length));
43
+ const length = Math.max(0, ...group.map((item) => item.values.length));
67
44
  const positive = new Array(length).fill(0);
68
45
  const negative = new Array(length).fill(0);
69
46
  for (const item of group) {
@@ -91,22 +68,24 @@ export function applyStacking(series) {
91
68
  }
92
69
  }
93
70
  }
94
-
95
- /** 为未显式指定 stack 的系列自动编组(图表级 stack 开关) */
96
- export function assignDefaultStack(series, enabled) {
97
- if (!enabled) return;
71
+ function assignDefaultStack(series, enabled) {
72
+ if (!enabled)
73
+ return;
98
74
  for (const item of series) {
99
- if (!item.stackId) item.stackId = '__default__';
75
+ if (!item.stackId)
76
+ item.stackId = "__default__";
100
77
  }
101
78
  }
102
- /** 计算 y 轴值域 */
103
- export function resolveYDomain(series, options = {}) {
104
- const visible = series.filter(item => item.visible && (options.yAxisIndex === undefined || (item.config.yAxisIndex ?? 0) === options.yAxisIndex));
79
+ function resolveYDomain(series, options = {}) {
80
+ const visible = series.filter(
81
+ (item) => item.visible && (options.yAxisIndex === void 0 || (item.config.yAxisIndex ?? 0) === options.yAxisIndex)
82
+ );
105
83
  const pool = [];
106
84
  for (const item of visible) {
107
85
  for (let i = 0; i < item.values.length; i += 1) {
108
86
  pool.push(item.top[i]);
109
- if (options.includeBase) pool.push(item.base[i]);
87
+ if (options.includeBase)
88
+ pool.push(item.base[i]);
110
89
  }
111
90
  }
112
91
  let [min, max] = extentOf(pool);
@@ -114,25 +93,22 @@ export function resolveYDomain(series, options = {}) {
114
93
  min = Math.min(0, min);
115
94
  max = Math.max(0, max);
116
95
  }
117
- if (options.min !== undefined) min = options.min;
118
- if (options.max !== undefined) max = options.max;
119
-
120
- // 全零数据的退化处理:给一个固定的可视高度,否则所有内容压在同一条线上
121
- if (min === 0 && max === 0) return [0, 1];
96
+ if (options.min !== void 0)
97
+ min = options.min;
98
+ if (options.max !== void 0)
99
+ max = options.max;
100
+ if (min === 0 && max === 0)
101
+ return [0, 1];
122
102
  return [min, max];
123
103
  }
124
-
125
- /** 提取 x 轴分类值,并做去重保护(重复分类会让 band 定位错乱) */
126
- export function resolveXValues(data, xKey) {
127
- return data.map(row => {
104
+ function resolveXValues(data, xKey) {
105
+ return data.map((row) => {
128
106
  const raw = row[xKey];
129
- return raw === null || raw === undefined ? '' : String(raw);
107
+ return raw === null || raw === void 0 ? "" : String(raw);
130
108
  });
131
109
  }
132
-
133
- /** 按 x 索引取整行的 tooltip 载荷 */
134
- export function buildTooltipItems(series, index) {
135
- return series.filter(item => item.visible).map(item => {
110
+ function buildTooltipItems(series, index) {
111
+ return series.filter((item) => item.visible).map((item) => {
136
112
  const value = item.values[index] ?? null;
137
113
  const formatter = item.config.valueFormatter;
138
114
  return {
@@ -140,7 +116,15 @@ export function buildTooltipItems(series, index) {
140
116
  name: item.name,
141
117
  color: item.color,
142
118
  value,
143
- formatted: value === null ? '—' : formatter ? formatter(value) : String(value)
119
+ formatted: value === null ? "—" : formatter ? formatter(value) : String(value)
144
120
  };
145
121
  });
146
- }
122
+ }
123
+ export {
124
+ applyStacking,
125
+ assignDefaultStack,
126
+ buildTooltipItems,
127
+ resolveSeries,
128
+ resolveXValues,
129
+ resolveYDomain
130
+ };
@@ -1,23 +1,12 @@
1
1
  'use client';
2
- /**
3
- * 场景 → Canvas 2D 绘制。
4
- *
5
- * 使用场景:单系列点数超过 `canvasThreshold`(默认 2000)时,
6
- * SVG 的 DOM 节点数会拖垮布局与重绘,切 Canvas 后只保留一次 draw call 批次。
7
- *
8
- * 注意:Canvas 里没有 DOM 节点,也就没有逐图元的事件与无障碍语义。
9
- * 因此启用 Canvas 时,图表仍会渲染一份**隐藏的无障碍数据表**(见 Chart 组件),
10
- * tooltip 也退化为按 x 轴索引命中,而非逐 mark 命中。
11
- */
12
-
2
+ // src/scene/canvas.ts
13
3
  function applyPaint(ctx, node) {
14
- ctx.fillStyle = node.fill ?? 'transparent';
15
- ctx.strokeStyle = node.stroke ?? 'transparent';
4
+ ctx.fillStyle = node.fill ?? "transparent";
5
+ ctx.strokeStyle = node.stroke ?? "transparent";
16
6
  ctx.lineWidth = node.strokeWidth ?? 1;
17
7
  ctx.globalAlpha = node.opacity ?? 1;
18
8
  if (node.strokeDasharray) {
19
- // "3 3" → [3, 3]
20
- const parts = node.strokeDasharray.split(/[\s,]+/).map(Number).filter(n => !Number.isNaN(n));
9
+ const parts = node.strokeDasharray.split(/[\s,]+/).map(Number).filter((n) => !Number.isNaN(n));
21
10
  ctx.setLineDash(parts);
22
11
  } else {
23
12
  ctx.setLineDash([]);
@@ -26,106 +15,107 @@ function applyPaint(ctx, node) {
26
15
  function drawNodes(ctx, nodes) {
27
16
  for (const node of nodes) {
28
17
  switch (node.kind) {
29
- case 'group':
30
- {
31
- ctx.save();
32
- // group 的 transform 是 SVG 写法,这里只支持 translate(...)(当前图表未用到更复杂的)
33
- if (node.transform) {
34
- const matched = /translate\(\s*(-?[\d.]+)[,\s]+(-?[\d.]+)\s*\)/.exec(node.transform);
35
- if (matched) ctx.translate(Number(matched[1]), Number(matched[2]));
36
- }
37
- drawNodes(ctx, node.children);
38
- ctx.restore();
39
- break;
40
- }
41
- case 'rect':
42
- {
43
- applyPaint(ctx, node);
44
- const radius = Math.min(node.radius ?? 0, Math.abs(node.width) / 2, Math.abs(node.height) / 2);
45
- ctx.beginPath();
46
- if (radius > 0) {
47
- // roundRect 在部分旧环境不可用,手写等价路径
48
- const {
49
- x,
50
- y,
51
- width,
52
- height
53
- } = node;
54
- ctx.moveTo(x + radius, y);
55
- ctx.lineTo(x + width - radius, y);
56
- ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
57
- ctx.lineTo(x + width, y + height - radius);
58
- ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
59
- ctx.lineTo(x + radius, y + height);
60
- ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
61
- ctx.lineTo(x, y + radius);
62
- ctx.quadraticCurveTo(x, y, x + radius, y);
63
- ctx.closePath();
64
- } else {
65
- ctx.rect(node.x, node.y, node.width, node.height);
66
- }
67
- if (node.fill && node.fill !== 'none') ctx.fill();
68
- if (node.stroke) ctx.stroke();
69
- break;
70
- }
71
- case 'path':
72
- {
73
- applyPaint(ctx, node);
74
- const path = new Path2D(node.d);
75
- if (node.fill && node.fill !== 'none') ctx.fill(path);
76
- if (node.stroke) ctx.stroke(path);
77
- break;
18
+ case "group": {
19
+ ctx.save();
20
+ if (node.transform) {
21
+ const matched = /translate\(\s*(-?[\d.]+)[,\s]+(-?[\d.]+)\s*\)/.exec(
22
+ node.transform
23
+ );
24
+ if (matched)
25
+ ctx.translate(Number(matched[1]), Number(matched[2]));
78
26
  }
79
- case 'circle':
80
- {
81
- applyPaint(ctx, node);
82
- ctx.beginPath();
83
- ctx.arc(node.cx, node.cy, Math.max(0, node.r), 0, Math.PI * 2);
84
- if (node.fill && node.fill !== 'none') ctx.fill();
85
- if (node.stroke) ctx.stroke();
86
- break;
27
+ drawNodes(ctx, node.children);
28
+ ctx.restore();
29
+ break;
30
+ }
31
+ case "rect": {
32
+ applyPaint(ctx, node);
33
+ const radius = Math.min(
34
+ node.radius ?? 0,
35
+ Math.abs(node.width) / 2,
36
+ Math.abs(node.height) / 2
37
+ );
38
+ ctx.beginPath();
39
+ if (radius > 0) {
40
+ const { x, y, width, height } = node;
41
+ ctx.moveTo(x + radius, y);
42
+ ctx.lineTo(x + width - radius, y);
43
+ ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
44
+ ctx.lineTo(x + width, y + height - radius);
45
+ ctx.quadraticCurveTo(
46
+ x + width,
47
+ y + height,
48
+ x + width - radius,
49
+ y + height
50
+ );
51
+ ctx.lineTo(x + radius, y + height);
52
+ ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
53
+ ctx.lineTo(x, y + radius);
54
+ ctx.quadraticCurveTo(x, y, x + radius, y);
55
+ ctx.closePath();
56
+ } else {
57
+ ctx.rect(node.x, node.y, node.width, node.height);
87
58
  }
88
- case 'line':
89
- {
90
- applyPaint(ctx, node);
91
- ctx.beginPath();
92
- ctx.moveTo(node.x1, node.y1);
93
- ctx.lineTo(node.x2, node.y2);
59
+ if (node.fill && node.fill !== "none")
60
+ ctx.fill();
61
+ if (node.stroke)
94
62
  ctx.stroke();
95
- break;
96
- }
97
- case 'text':
98
- {
99
- applyPaint(ctx, {
100
- ...node,
101
- stroke: undefined
102
- });
103
- ctx.font = `${node.fontWeight ?? 400} ${node.fontSize ?? 12}px ${node.fontFamily ?? 'sans-serif'}`;
104
- ctx.textAlign = node.anchor === 'middle' ? 'center' : node.anchor === 'end' ? 'right' : 'left';
105
- ctx.textBaseline = node.baseline === 'middle' ? 'middle' : node.baseline === 'hanging' ? 'top' : 'alphabetic';
106
- if (node.rotate) {
107
- ctx.save();
108
- ctx.translate(node.x, node.y);
109
- ctx.rotate(node.rotate * Math.PI / 180);
110
- ctx.fillText(node.text, 0, 0);
111
- ctx.restore();
112
- } else {
113
- ctx.fillText(node.text, node.x, node.y);
114
- }
115
- ctx.globalAlpha = 1;
116
- break;
63
+ break;
64
+ }
65
+ case "path": {
66
+ applyPaint(ctx, node);
67
+ const path = new Path2D(node.d);
68
+ if (node.fill && node.fill !== "none")
69
+ ctx.fill(path);
70
+ if (node.stroke)
71
+ ctx.stroke(path);
72
+ break;
73
+ }
74
+ case "circle": {
75
+ applyPaint(ctx, node);
76
+ ctx.beginPath();
77
+ ctx.arc(node.cx, node.cy, Math.max(0, node.r), 0, Math.PI * 2);
78
+ if (node.fill && node.fill !== "none")
79
+ ctx.fill();
80
+ if (node.stroke)
81
+ ctx.stroke();
82
+ break;
83
+ }
84
+ case "line": {
85
+ applyPaint(ctx, node);
86
+ ctx.beginPath();
87
+ ctx.moveTo(node.x1, node.y1);
88
+ ctx.lineTo(node.x2, node.y2);
89
+ ctx.stroke();
90
+ break;
91
+ }
92
+ case "text": {
93
+ applyPaint(ctx, { ...node, stroke: void 0 });
94
+ ctx.font = `${node.fontWeight ?? 400} ${node.fontSize ?? 12}px ${node.fontFamily ?? "sans-serif"}`;
95
+ ctx.textAlign = node.anchor === "middle" ? "center" : node.anchor === "end" ? "right" : "left";
96
+ ctx.textBaseline = node.baseline === "middle" ? "middle" : node.baseline === "hanging" ? "top" : "alphabetic";
97
+ if (node.rotate) {
98
+ ctx.save();
99
+ ctx.translate(node.x, node.y);
100
+ ctx.rotate(node.rotate * Math.PI / 180);
101
+ ctx.fillText(node.text, 0, 0);
102
+ ctx.restore();
103
+ } else {
104
+ ctx.fillText(node.text, node.x, node.y);
117
105
  }
106
+ ctx.globalAlpha = 1;
107
+ break;
108
+ }
118
109
  default:
119
110
  break;
120
111
  }
121
112
  }
122
113
  }
123
-
124
- /** 把场景画到 canvas 上(会先清空并处理 DPR 缩放) */
125
- export function paintScene(canvas, nodes, options = {}) {
126
- const ctx = canvas.getContext('2d');
127
- if (!ctx) return;
128
- const dpr = options.dpr ?? (typeof window !== 'undefined' ? window.devicePixelRatio || 1 : 1);
114
+ function paintScene(canvas, nodes, options = {}) {
115
+ const ctx = canvas.getContext("2d");
116
+ if (!ctx)
117
+ return;
118
+ const dpr = options.dpr ?? (typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1);
129
119
  const width = canvas.clientWidth || canvas.width;
130
120
  const height = canvas.clientHeight || canvas.height;
131
121
  if (canvas.width !== Math.floor(width * dpr) || canvas.height !== Math.floor(height * dpr)) {
@@ -135,4 +125,7 @@ export function paintScene(canvas, nodes, options = {}) {
135
125
  ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
136
126
  ctx.clearRect(0, 0, width, height);
137
127
  drawNodes(ctx, nodes);
138
- }
128
+ }
129
+ export {
130
+ paintScene
131
+ };
package/esm/scene/svg.js CHANGED
@@ -1,116 +1,123 @@
1
1
  'use client';
2
- import React from 'react';
3
- import { jsx as _jsx } from "react/jsx-runtime";
4
- /**
5
- * 场景 → SVG 元素。
6
- *
7
- * 为什么 SVG 是默认渲染器:
8
- * · 每个图元都是真实 DOM,可被测试查询、可被无障碍工具读取;
9
- * · 打印/导出为矢量不失真;
10
- * · 主题换色只是改属性,无需重绘位图。
11
- * 数据量超过阈值时切 Canvas(见 canvas.ts)。
12
- */
2
+ // src/scene/svg.tsx
3
+ import { jsx } from "react/jsx-runtime";
13
4
  function renderNode(node) {
14
5
  const common = {
15
6
  opacity: node.opacity,
16
7
  pointerEvents: node.pointerEvents
17
8
  };
18
9
  switch (node.kind) {
19
- case 'group':
20
- return /*#__PURE__*/_jsx("g", {
21
- transform: node.transform,
22
- ...common,
23
- children: node.children.map(renderNode)
24
- }, node.key);
25
- case 'rect':
26
- return /*#__PURE__*/_jsx("rect", {
27
- x: node.x,
28
- y: node.y,
29
- width: Math.max(0, node.width),
30
- height: Math.max(0, node.height),
31
- rx: node.radius,
32
- ry: node.radius,
33
- fill: node.fill ?? 'none',
34
- stroke: node.stroke,
35
- strokeWidth: node.strokeWidth,
36
- strokeDasharray: node.strokeDasharray,
37
- ...common
38
- }, node.key);
39
- case 'path':
40
- return /*#__PURE__*/_jsx("path", {
41
- d: node.d,
42
- fill: node.fill ?? 'none',
43
- stroke: node.stroke,
44
- strokeWidth: node.strokeWidth,
45
- strokeDasharray: node.strokeDasharray,
46
- strokeLinecap: node.linecap,
47
- strokeLinejoin: node.linejoin,
48
- ...common
49
- }, node.key);
50
- case 'circle':
51
- return /*#__PURE__*/_jsx("circle", {
52
- cx: node.cx,
53
- cy: node.cy,
54
- r: Math.max(0, node.r),
55
- fill: node.fill ?? 'none',
56
- stroke: node.stroke,
57
- strokeWidth: node.strokeWidth,
58
- ...common
59
- }, node.key);
60
- case 'line':
61
- return /*#__PURE__*/_jsx("line", {
62
- x1: node.x1,
63
- y1: node.y1,
64
- x2: node.x2,
65
- y2: node.y2,
66
- stroke: node.stroke,
67
- strokeWidth: node.strokeWidth ?? 1,
68
- strokeDasharray: node.strokeDasharray,
69
- ...common
70
- }, node.key);
71
- case 'text':
72
- return /*#__PURE__*/_jsx("text", {
73
- x: node.x,
74
- y: node.y,
75
- fill: node.fill,
76
- fontSize: node.fontSize,
77
- fontWeight: node.fontWeight,
78
- fontFamily: node.fontFamily,
79
- textAnchor: node.anchor ?? 'start',
80
- dominantBaseline: node.baseline === 'middle' ? 'central' : node.baseline === 'hanging' ? 'hanging' : 'auto',
81
- transform: node.rotate ? `rotate(${node.rotate} ${node.x} ${node.y})` : undefined,
82
- stroke: node.stroke,
83
- strokeWidth: node.strokeWidth
84
- // 描边画在填充下面,形成光晕而不是把字吃细
85
- ,
86
- paintOrder: "stroke",
87
- strokeLinejoin: "round",
88
- ...common,
89
- children: node.text
90
- }, node.key);
10
+ case "group":
11
+ return /* @__PURE__ */ jsx("g", { transform: node.transform, ...common, children: node.children.map(renderNode) }, node.key);
12
+ case "rect":
13
+ return /* @__PURE__ */ jsx(
14
+ "rect",
15
+ {
16
+ x: node.x,
17
+ y: node.y,
18
+ width: Math.max(0, node.width),
19
+ height: Math.max(0, node.height),
20
+ rx: node.radius,
21
+ ry: node.radius,
22
+ fill: node.fill ?? "none",
23
+ stroke: node.stroke,
24
+ strokeWidth: node.strokeWidth,
25
+ strokeDasharray: node.strokeDasharray,
26
+ ...common
27
+ },
28
+ node.key
29
+ );
30
+ case "path":
31
+ return /* @__PURE__ */ jsx(
32
+ "path",
33
+ {
34
+ d: node.d,
35
+ fill: node.fill ?? "none",
36
+ stroke: node.stroke,
37
+ strokeWidth: node.strokeWidth,
38
+ strokeDasharray: node.strokeDasharray,
39
+ strokeLinecap: node.linecap,
40
+ strokeLinejoin: node.linejoin,
41
+ ...common
42
+ },
43
+ node.key
44
+ );
45
+ case "circle":
46
+ return /* @__PURE__ */ jsx(
47
+ "circle",
48
+ {
49
+ cx: node.cx,
50
+ cy: node.cy,
51
+ r: Math.max(0, node.r),
52
+ fill: node.fill ?? "none",
53
+ stroke: node.stroke,
54
+ strokeWidth: node.strokeWidth,
55
+ ...common
56
+ },
57
+ node.key
58
+ );
59
+ case "line":
60
+ return /* @__PURE__ */ jsx(
61
+ "line",
62
+ {
63
+ x1: node.x1,
64
+ y1: node.y1,
65
+ x2: node.x2,
66
+ y2: node.y2,
67
+ stroke: node.stroke,
68
+ strokeWidth: node.strokeWidth ?? 1,
69
+ strokeDasharray: node.strokeDasharray,
70
+ ...common
71
+ },
72
+ node.key
73
+ );
74
+ case "text":
75
+ return /* @__PURE__ */ jsx(
76
+ "text",
77
+ {
78
+ x: node.x,
79
+ y: node.y,
80
+ fill: node.fill,
81
+ fontSize: node.fontSize,
82
+ fontWeight: node.fontWeight,
83
+ fontFamily: node.fontFamily,
84
+ textAnchor: node.anchor ?? "start",
85
+ dominantBaseline: node.baseline === "middle" ? "central" : node.baseline === "hanging" ? "hanging" : "auto",
86
+ transform: node.rotate ? `rotate(${node.rotate} ${node.x} ${node.y})` : void 0,
87
+ stroke: node.stroke,
88
+ strokeWidth: node.strokeWidth,
89
+ paintOrder: "stroke",
90
+ strokeLinejoin: "round",
91
+ ...common,
92
+ children: node.text
93
+ },
94
+ node.key
95
+ );
91
96
  default:
92
97
  return null;
93
98
  }
94
99
  }
95
- export function SvgScene({
100
+ function SvgScene({
96
101
  nodes,
97
102
  width,
98
103
  height,
99
104
  ariaLabel,
100
105
  className
101
106
  }) {
102
- return /*#__PURE__*/_jsx("svg", {
103
- className: className,
104
- width: width,
105
- height: height,
106
- viewBox: `0 0 ${width} ${height}`,
107
- role: "img",
108
- "aria-label": ariaLabel,
109
- style: {
110
- display: 'block',
111
- maxWidth: '100%',
112
- overflow: 'visible'
113
- },
114
- children: nodes.map(renderNode)
115
- });
116
- }
107
+ return /* @__PURE__ */ jsx(
108
+ "svg",
109
+ {
110
+ className,
111
+ width,
112
+ height,
113
+ viewBox: `0 0 ${width} ${height}`,
114
+ role: "img",
115
+ "aria-label": ariaLabel,
116
+ style: { display: "block", maxWidth: "100%", overflow: "visible" },
117
+ children: nodes.map(renderNode)
118
+ }
119
+ );
120
+ }
121
+ export {
122
+ SvgScene
123
+ };
@@ -1,21 +1,14 @@
1
1
  'use client';
2
- /**
3
- * 场景图节点定义。
4
- *
5
- * 图表不复用 React 元素树做几何描述(那样 Canvas 渲染器无从下手),
6
- * 而是先算出一份纯数据化的「场景」:一组带坐标的图元。
7
- * 于是:
8
- * · SVG 渲染器 = 场景 → React 元素;
9
- * · Canvas 渲染器 = 场景 → 2D 绘制指令。
10
- * 两者消费同一份场景,天然保持一致。
11
- */
12
-
13
- /** 递归统计节点数,便于性能告警与测试断言 */
14
- export function countNodes(nodes) {
2
+ // src/scene/types.ts
3
+ function countNodes(nodes) {
15
4
  let total = 0;
16
5
  for (const node of nodes) {
17
6
  total += 1;
18
- if (node.kind === 'group') total += countNodes(node.children);
7
+ if (node.kind === "group")
8
+ total += countNodes(node.children);
19
9
  }
20
10
  return total;
21
- }
11
+ }
12
+ export {
13
+ countNodes
14
+ };