@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.
- package/esm/charts/area-chart/index.d.ts +2 -2
- package/esm/charts/area-chart/index.js +28 -23
- package/esm/charts/bar-chart/index.d.ts +1 -1
- package/esm/charts/bar-chart/index.js +18 -21
- package/esm/charts/candlestick-chart/index.d.ts +2 -2
- package/esm/charts/candlestick-chart/index.js +243 -180
- package/esm/charts/combo-chart/index.d.ts +1 -1
- package/esm/charts/combo-chart/index.js +9 -20
- package/esm/charts/funnel-chart/index.d.ts +2 -2
- package/esm/charts/funnel-chart/index.js +134 -108
- package/esm/charts/gauge-chart/index.d.ts +1 -1
- package/esm/charts/gauge-chart/index.js +103 -105
- package/esm/charts/graph-chart/index.d.ts +3 -3
- package/esm/charts/graph-chart/index.js +155 -114
- package/esm/charts/heatmap-chart/index.d.ts +2 -2
- package/esm/charts/heatmap-chart/index.js +180 -135
- package/esm/charts/line-chart/index.d.ts +2 -2
- package/esm/charts/line-chart/index.js +29 -24
- package/esm/charts/pie-chart/index.d.ts +2 -2
- package/esm/charts/pie-chart/index.d.ts.map +1 -1
- package/esm/charts/pie-chart/index.js +199 -139
- package/esm/charts/radar-chart/index.d.ts +2 -2
- package/esm/charts/radar-chart/index.d.ts.map +1 -1
- package/esm/charts/radar-chart/index.js +155 -128
- package/esm/charts/ring-progress/index.d.ts +1 -1
- package/esm/charts/ring-progress/index.js +82 -82
- package/esm/charts/sankey-chart/index.d.ts +3 -3
- package/esm/charts/sankey-chart/index.js +172 -141
- package/esm/charts/scatter-chart/index.d.ts +3 -3
- package/esm/charts/scatter-chart/index.d.ts.map +1 -1
- package/esm/charts/scatter-chart/index.js +226 -159
- package/esm/charts/sparkline/index.d.ts +1 -1
- package/esm/charts/sparkline/index.js +82 -66
- package/esm/charts/tree-chart/index.d.ts +3 -3
- package/esm/charts/tree-chart/index.js +129 -102
- package/esm/charts/treemap-chart/index.d.ts +3 -3
- package/esm/charts/treemap-chart/index.js +163 -141
- package/esm/charts/waterfall-chart/index.d.ts +2 -2
- package/esm/charts/waterfall-chart/index.js +166 -121
- package/esm/components/Legend.d.ts +5 -4
- package/esm/components/Legend.d.ts.map +1 -1
- package/esm/components/Legend.js +80 -48
- package/esm/components/Tooltip.d.ts +2 -2
- package/esm/components/Tooltip.js +63 -71
- package/esm/components/chart/index.d.ts +5 -2
- package/esm/components/chart/index.d.ts.map +1 -1
- package/esm/components/chart/index.js +81 -102
- package/esm/components/chart.less +13 -0
- package/esm/core/animation.js +32 -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 +23 -45
- package/esm/core/format.js +49 -52
- package/esm/core/hierarchy.d.ts +1 -1
- package/esm/core/hierarchy.js +61 -92
- package/esm/core/layout.d.ts +1 -1
- package/esm/core/layout.js +33 -50
- package/esm/core/sampling.js +31 -53
- package/esm/core/sankey.js +36 -55
- package/esm/core/scale.js +73 -86
- package/esm/core/shape.js +102 -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.d.ts +6 -5
- package/esm/hooks/useChartTheme.d.ts.map +1 -1
- package/esm/hooks/useChartTheme.js +18 -18
- package/esm/hooks/useChartTransition.d.ts +1 -1
- package/esm/hooks/useChartTransition.js +59 -73
- package/esm/hooks/useElementSize.js +20 -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 +56 -50
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +137 -47
- package/esm/internal/CartesianChart.d.ts +1 -1
- package/esm/internal/CartesianChart.d.ts.map +1 -1
- package/esm/internal/CartesianChart.js +667 -485
- package/esm/internal/model.d.ts +2 -2
- package/esm/internal/model.js +45 -60
- package/esm/scene/canvas.d.ts +1 -1
- package/esm/scene/canvas.js +101 -107
- package/esm/scene/svg.d.ts +1 -1
- package/esm/scene/svg.js +107 -99
- package/esm/scene/types.js +9 -15
- package/esm/style.css +10 -0
- package/esm/theme/index.d.ts +2 -2
- package/esm/theme/index.d.ts.map +1 -1
- package/esm/theme/index.js +145 -170
- package/esm/types.d.ts +7 -3
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js +1 -1
- package/package.json +2 -2
package/esm/internal/model.d.ts
CHANGED
|
@@ -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
|
*
|
package/esm/internal/model.js
CHANGED
|
@@ -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 ===
|
|
16
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
95
|
-
|
|
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)
|
|
75
|
+
if (!item.stackId)
|
|
76
|
+
item.stackId = "__default__";
|
|
99
77
|
}
|
|
100
78
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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)
|
|
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 !==
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
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];
|
|
121
102
|
return [min, max];
|
|
122
103
|
}
|
|
123
|
-
|
|
124
|
-
|
|
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 ===
|
|
107
|
+
return raw === null || raw === void 0 ? "" : String(raw);
|
|
129
108
|
});
|
|
130
109
|
}
|
|
131
|
-
|
|
132
|
-
|
|
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 ?
|
|
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
|
+
};
|
package/esm/scene/canvas.d.ts
CHANGED
package/esm/scene/canvas.js
CHANGED
|
@@ -1,22 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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 ??
|
|
14
|
-
ctx.strokeStyle = node.stroke ??
|
|
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
|
-
|
|
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
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
case
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
ctx.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
+
};
|
package/esm/scene/svg.d.ts
CHANGED
package/esm/scene/svg.js
CHANGED
|
@@ -1,115 +1,123 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
|
19
|
-
return
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
case
|
|
39
|
-
return
|
|
40
|
-
|
|
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
|
-
|
|
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
|
-
|
|
100
|
+
function SvgScene({
|
|
95
101
|
nodes,
|
|
96
102
|
width,
|
|
97
103
|
height,
|
|
98
104
|
ariaLabel,
|
|
99
105
|
className
|
|
100
106
|
}) {
|
|
101
|
-
return
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
maxWidth:
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
|
|
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
|
+
};
|