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