@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.
- package/esm/charts/area-chart/index.js +27 -23
- package/esm/charts/bar-chart/index.js +17 -21
- package/esm/charts/candlestick-chart/index.js +242 -180
- package/esm/charts/combo-chart/index.js +8 -20
- package/esm/charts/funnel-chart/index.js +133 -108
- package/esm/charts/gauge-chart/index.js +102 -105
- package/esm/charts/graph-chart/index.js +154 -114
- package/esm/charts/heatmap-chart/index.js +179 -135
- package/esm/charts/line-chart/index.js +28 -24
- package/esm/charts/pie-chart/index.d.ts.map +1 -1
- package/esm/charts/pie-chart/index.js +198 -139
- package/esm/charts/radar-chart/index.d.ts.map +1 -1
- package/esm/charts/radar-chart/index.js +154 -128
- package/esm/charts/ring-progress/index.js +81 -82
- package/esm/charts/sankey-chart/index.js +171 -141
- package/esm/charts/scatter-chart/index.d.ts.map +1 -1
- package/esm/charts/scatter-chart/index.js +225 -159
- package/esm/charts/sparkline/index.js +81 -66
- package/esm/charts/tree-chart/index.js +128 -102
- package/esm/charts/treemap-chart/index.js +162 -141
- package/esm/charts/waterfall-chart/index.js +165 -121
- package/esm/components/Legend.d.ts +3 -2
- package/esm/components/Legend.d.ts.map +1 -1
- package/esm/components/Legend.js +79 -48
- package/esm/components/Tooltip.js +62 -71
- package/esm/components/chart/index.d.ts +4 -1
- package/esm/components/chart/index.d.ts.map +1 -1
- package/esm/components/chart/index.js +80 -102
- package/esm/components/chart.less +13 -0
- package/esm/core/animation.js +31 -43
- package/esm/core/export.d.ts +34 -0
- package/esm/core/export.d.ts.map +1 -0
- package/esm/core/export.js +390 -0
- package/esm/core/force.js +22 -45
- package/esm/core/format.js +48 -52
- package/esm/core/hierarchy.js +60 -92
- package/esm/core/layout.js +32 -50
- package/esm/core/sampling.js +30 -53
- package/esm/core/sankey.js +35 -55
- package/esm/core/scale.js +72 -86
- package/esm/core/shape.js +101 -133
- package/esm/core/sync.d.ts +41 -0
- package/esm/core/sync.d.ts.map +1 -0
- package/esm/core/sync.js +120 -0
- package/esm/hooks/useChartExport.d.ts +19 -0
- package/esm/hooks/useChartExport.d.ts.map +1 -0
- package/esm/hooks/useChartExport.js +35 -0
- package/esm/hooks/useChartTheme.js +17 -19
- package/esm/hooks/useChartTransition.js +58 -73
- package/esm/hooks/useElementSize.js +19 -28
- package/esm/hooks/usePlotPointer.d.ts +23 -0
- package/esm/hooks/usePlotPointer.d.ts.map +1 -0
- package/esm/hooks/usePlotPointer.js +94 -0
- package/esm/index.d.ts +6 -0
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +136 -47
- package/esm/internal/CartesianChart.d.ts.map +1 -1
- package/esm/internal/CartesianChart.js +666 -485
- package/esm/internal/model.js +44 -60
- package/esm/scene/canvas.js +100 -107
- package/esm/scene/svg.js +106 -99
- package/esm/scene/types.js +8 -15
- package/esm/style.css +10 -0
- package/esm/theme/index.js +143 -171
- package/esm/types.d.ts +4 -0
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js +0 -1
- package/package.json +1 -1
package/esm/internal/model.js
CHANGED
|
@@ -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 ===
|
|
17
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
96
|
-
|
|
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)
|
|
75
|
+
if (!item.stackId)
|
|
76
|
+
item.stackId = "__default__";
|
|
100
77
|
}
|
|
101
78
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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)
|
|
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 !==
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (min === 0 && max === 0)
|
|
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
|
-
|
|
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 ===
|
|
107
|
+
return raw === null || raw === void 0 ? "" : String(raw);
|
|
130
108
|
});
|
|
131
109
|
}
|
|
132
|
-
|
|
133
|
-
|
|
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 ?
|
|
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
|
+
};
|
package/esm/scene/canvas.js
CHANGED
|
@@ -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 ??
|
|
15
|
-
ctx.strokeStyle = node.stroke ??
|
|
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
|
-
|
|
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
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
case
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
ctx.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
|
|
3
|
-
import { jsx
|
|
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
|
|
20
|
-
return
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
case
|
|
40
|
-
return
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
,
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
100
|
+
function SvgScene({
|
|
96
101
|
nodes,
|
|
97
102
|
width,
|
|
98
103
|
height,
|
|
99
104
|
ariaLabel,
|
|
100
105
|
className
|
|
101
106
|
}) {
|
|
102
|
-
return
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
maxWidth:
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
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
|
+
};
|
package/esm/scene/types.js
CHANGED
|
@@ -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 ===
|
|
7
|
+
if (node.kind === "group")
|
|
8
|
+
total += countNodes(node.children);
|
|
19
9
|
}
|
|
20
10
|
return total;
|
|
21
|
-
}
|
|
11
|
+
}
|
|
12
|
+
export {
|
|
13
|
+
countNodes
|
|
14
|
+
};
|