@aura-react-comp/charts 0.0.0-stage → 0.3.1
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/LICENSE +21 -0
- package/README.md +90 -2
- package/esm/charts/area-chart/index.d.ts +19 -0
- package/esm/charts/area-chart/index.d.ts.map +1 -0
- package/esm/charts/area-chart/index.js +31 -0
- package/esm/charts/bar-chart/index.d.ts +16 -0
- package/esm/charts/bar-chart/index.d.ts.map +1 -0
- package/esm/charts/bar-chart/index.js +28 -0
- package/esm/charts/candlestick-chart/index.d.ts +47 -0
- package/esm/charts/candlestick-chart/index.d.ts.map +1 -0
- package/esm/charts/candlestick-chart/index.js +347 -0
- package/esm/charts/combo-chart/index.d.ts +17 -0
- package/esm/charts/combo-chart/index.d.ts.map +1 -0
- package/esm/charts/combo-chart/index.js +26 -0
- package/esm/charts/funnel-chart/index.d.ts +35 -0
- package/esm/charts/funnel-chart/index.d.ts.map +1 -0
- package/esm/charts/funnel-chart/index.js +200 -0
- package/esm/charts/gauge-chart/index.d.ts +41 -0
- package/esm/charts/gauge-chart/index.d.ts.map +1 -0
- package/esm/charts/gauge-chart/index.js +217 -0
- package/esm/charts/graph-chart/index.d.ts +35 -0
- package/esm/charts/graph-chart/index.d.ts.map +1 -0
- package/esm/charts/graph-chart/index.js +224 -0
- package/esm/charts/heatmap-chart/index.d.ts +47 -0
- package/esm/charts/heatmap-chart/index.d.ts.map +1 -0
- package/esm/charts/heatmap-chart/index.js +281 -0
- package/esm/charts/line-chart/index.d.ts +23 -0
- package/esm/charts/line-chart/index.d.ts.map +1 -0
- package/esm/charts/line-chart/index.js +33 -0
- package/esm/charts/pie-chart/index.d.ts +38 -0
- package/esm/charts/pie-chart/index.d.ts.map +1 -0
- package/esm/charts/pie-chart/index.js +272 -0
- package/esm/charts/radar-chart/index.d.ts +44 -0
- package/esm/charts/radar-chart/index.d.ts.map +1 -0
- package/esm/charts/radar-chart/index.js +263 -0
- package/esm/charts/ring-progress/index.d.ts +38 -0
- package/esm/charts/ring-progress/index.d.ts.map +1 -0
- package/esm/charts/ring-progress/index.js +137 -0
- package/esm/charts/sankey-chart/index.d.ts +34 -0
- package/esm/charts/sankey-chart/index.d.ts.map +1 -0
- package/esm/charts/sankey-chart/index.js +248 -0
- package/esm/charts/scatter-chart/index.d.ts +34 -0
- package/esm/charts/scatter-chart/index.d.ts.map +1 -0
- package/esm/charts/scatter-chart/index.js +305 -0
- package/esm/charts/sparkline/index.d.ts +31 -0
- package/esm/charts/sparkline/index.d.ts.map +1 -0
- package/esm/charts/sparkline/index.js +152 -0
- package/esm/charts/tree-chart/index.d.ts +34 -0
- package/esm/charts/tree-chart/index.d.ts.map +1 -0
- package/esm/charts/tree-chart/index.js +200 -0
- package/esm/charts/treemap-chart/index.d.ts +35 -0
- package/esm/charts/treemap-chart/index.d.ts.map +1 -0
- package/esm/charts/treemap-chart/index.js +239 -0
- package/esm/charts/waterfall-chart/index.d.ts +41 -0
- package/esm/charts/waterfall-chart/index.d.ts.map +1 -0
- package/esm/charts/waterfall-chart/index.js +281 -0
- package/esm/components/Legend.d.ts +24 -0
- package/esm/components/Legend.d.ts.map +1 -0
- package/esm/components/Legend.js +50 -0
- package/esm/components/Tooltip.d.ts +24 -0
- package/esm/components/Tooltip.d.ts.map +1 -0
- package/esm/components/Tooltip.js +85 -0
- package/esm/components/chart/index.d.ts +71 -0
- package/esm/components/chart/index.d.ts.map +1 -0
- package/esm/components/chart/index.js +142 -0
- package/esm/components/chart.less +223 -0
- package/esm/core/animation.d.ts +49 -0
- package/esm/core/animation.d.ts.map +1 -0
- package/esm/core/animation.js +93 -0
- package/esm/core/force.d.ts +43 -0
- package/esm/core/force.d.ts.map +1 -0
- package/esm/core/force.js +134 -0
- package/esm/core/format.d.ts +34 -0
- package/esm/core/format.d.ts.map +1 -0
- package/esm/core/format.js +81 -0
- package/esm/core/hierarchy.d.ts +77 -0
- package/esm/core/hierarchy.d.ts.map +1 -0
- package/esm/core/hierarchy.js +233 -0
- package/esm/core/layout.d.ts +67 -0
- package/esm/core/layout.d.ts.map +1 -0
- package/esm/core/layout.js +121 -0
- package/esm/core/sampling.d.ts +32 -0
- package/esm/core/sampling.d.ts.map +1 -0
- package/esm/core/sampling.js +124 -0
- package/esm/core/sankey.d.ts +67 -0
- package/esm/core/sankey.d.ts.map +1 -0
- package/esm/core/sankey.js +136 -0
- package/esm/core/scale.d.ts +98 -0
- package/esm/core/scale.d.ts.map +1 -0
- package/esm/core/scale.js +207 -0
- package/esm/core/shape.d.ts +60 -0
- package/esm/core/shape.d.ts.map +1 -0
- package/esm/core/shape.js +246 -0
- package/esm/global.d.ts +6 -0
- package/esm/hooks/useChartTheme.d.ts +11 -0
- package/esm/hooks/useChartTheme.d.ts.map +1 -0
- package/esm/hooks/useChartTheme.js +35 -0
- package/esm/hooks/useChartTransition.d.ts +20 -0
- package/esm/hooks/useChartTransition.d.ts.map +1 -0
- package/esm/hooks/useChartTransition.js +120 -0
- package/esm/hooks/useElementSize.d.ts +17 -0
- package/esm/hooks/useElementSize.d.ts.map +1 -0
- package/esm/hooks/useElementSize.js +47 -0
- package/esm/index.d.ts +62 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +48 -0
- package/esm/internal/CartesianChart.d.ts +16 -0
- package/esm/internal/CartesianChart.d.ts.map +1 -0
- package/esm/internal/CartesianChart.js +811 -0
- package/esm/internal/model.d.ts +67 -0
- package/esm/internal/model.d.ts.map +1 -0
- package/esm/internal/model.js +146 -0
- package/esm/scene/canvas.d.ts +18 -0
- package/esm/scene/canvas.d.ts.map +1 -0
- package/esm/scene/canvas.js +138 -0
- package/esm/scene/svg.d.ts +12 -0
- package/esm/scene/svg.d.ts.map +1 -0
- package/esm/scene/svg.js +116 -0
- package/esm/scene/types.d.ts +89 -0
- package/esm/scene/types.d.ts.map +1 -0
- package/esm/scene/types.js +21 -0
- package/esm/style.css +196 -0
- package/esm/theme/index.d.ts +127 -0
- package/esm/theme/index.d.ts.map +1 -0
- package/esm/theme/index.js +300 -0
- package/esm/types.d.ts +168 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/types.js +2 -0
- package/package.json +64 -3
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import React, { useMemo } from 'react';
|
|
3
|
+
import { classNames, prefixCls } from '@aura-react-comp/shared';
|
|
4
|
+
import { LineChart } from "../../charts/line-chart";
|
|
5
|
+
import { BarChart } from "../../charts/bar-chart";
|
|
6
|
+
import { AreaChart } from "../../charts/area-chart";
|
|
7
|
+
import { PieChart } from "../../charts/pie-chart";
|
|
8
|
+
import { ComboChart } from "../../charts/combo-chart";
|
|
9
|
+
|
|
10
|
+
/** ECharts 风格的系列配置(支持子集) */
|
|
11
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
12
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
13
|
+
const X_KEY = '__aura_x__';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 配置式图表(兼容 ECharts 风格的 option)。
|
|
17
|
+
*
|
|
18
|
+
* 存在意义有两个:
|
|
19
|
+
* 1. 迁移成本 —— 已有 ECharts 配置的项目可以低成本试用;
|
|
20
|
+
* 2. **AI 友好** —— 让模型生成 JSON 配置比生成 JSX 组合可靠得多,
|
|
21
|
+
* 尤其在需要动态拼装系列时。
|
|
22
|
+
*
|
|
23
|
+
* 明确只支持常用子集(直角坐标系的 line / bar / area / scatter 与 pie)。
|
|
24
|
+
* 不做「假装全支持」:未识别的系列类型会被跳过,而不是静默画错。
|
|
25
|
+
*/
|
|
26
|
+
export function Chart({
|
|
27
|
+
option,
|
|
28
|
+
width = '100%',
|
|
29
|
+
height = 320,
|
|
30
|
+
theme,
|
|
31
|
+
palette,
|
|
32
|
+
animation = true,
|
|
33
|
+
className,
|
|
34
|
+
style
|
|
35
|
+
}) {
|
|
36
|
+
const xValues = useMemo(() => (option.xAxis?.data ?? []).map(item => String(item)), [option.xAxis]);
|
|
37
|
+
const paletteOrOption = palette ?? option.color;
|
|
38
|
+
const {
|
|
39
|
+
rows,
|
|
40
|
+
seriesConfig,
|
|
41
|
+
primaryType
|
|
42
|
+
} = useMemo(() => {
|
|
43
|
+
const list = option.series ?? [];
|
|
44
|
+
const nextRows = xValues.map((value, index) => {
|
|
45
|
+
const row = {
|
|
46
|
+
[X_KEY]: value
|
|
47
|
+
};
|
|
48
|
+
list.forEach((item, seriesIndex) => {
|
|
49
|
+
row[`s${seriesIndex}`] = item.data?.[index] ?? null;
|
|
50
|
+
});
|
|
51
|
+
return row;
|
|
52
|
+
});
|
|
53
|
+
const configs = list.map((item, index) => ({
|
|
54
|
+
key: `s${index}`,
|
|
55
|
+
name: item.name ?? `系列 ${index + 1}`,
|
|
56
|
+
color: item.itemStyle?.color,
|
|
57
|
+
type: item.type === 'area' ? 'area' : item.type === 'bar' ? 'bar' : item.type === 'scatter' ? 'scatter' : 'line',
|
|
58
|
+
stack: item.stack,
|
|
59
|
+
curve: item.smooth ? 'smooth' : 'monotone',
|
|
60
|
+
area: item.areaStyle !== undefined && item.areaStyle !== false
|
|
61
|
+
}));
|
|
62
|
+
const first = list[0]?.type ?? 'line';
|
|
63
|
+
const primary = first === 'bar' ? 'bar' : first === 'pie' ? 'pie' : first === 'area' ? 'area' : 'line';
|
|
64
|
+
return {
|
|
65
|
+
rows: nextRows,
|
|
66
|
+
seriesConfig: configs,
|
|
67
|
+
primaryType: primary
|
|
68
|
+
};
|
|
69
|
+
}, [option.series, xValues]);
|
|
70
|
+
const isPie = primaryType === 'pie';
|
|
71
|
+
const legendVisible = option.legend?.show !== false;
|
|
72
|
+
const tooltipVisible = option.tooltip?.show !== false;
|
|
73
|
+
const gridVisible = option.grid?.show !== false;
|
|
74
|
+
const titleNode = option.title?.text ? /*#__PURE__*/_jsxs("div", {
|
|
75
|
+
className: prefixCls('chart__title'),
|
|
76
|
+
children: [/*#__PURE__*/_jsx("div", {
|
|
77
|
+
className: prefixCls('chart__title-main'),
|
|
78
|
+
children: option.title.text
|
|
79
|
+
}), option.title.subtext ? /*#__PURE__*/_jsx("div", {
|
|
80
|
+
className: prefixCls('chart__title-sub'),
|
|
81
|
+
children: option.title.subtext
|
|
82
|
+
}) : null]
|
|
83
|
+
}) : null;
|
|
84
|
+
if (isPie) {
|
|
85
|
+
const pieData = (option.series?.[0]?.data ?? []).map((value, index) => ({
|
|
86
|
+
name: xValues[index] ?? `项 ${index + 1}`,
|
|
87
|
+
value: Number(value ?? 0)
|
|
88
|
+
}));
|
|
89
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
90
|
+
className: classNames(className),
|
|
91
|
+
style: style,
|
|
92
|
+
children: [titleNode, /*#__PURE__*/_jsx(PieChart, {
|
|
93
|
+
data: pieData,
|
|
94
|
+
width: width,
|
|
95
|
+
height: height,
|
|
96
|
+
theme: theme,
|
|
97
|
+
palette: paletteOrOption,
|
|
98
|
+
legend: legendVisible,
|
|
99
|
+
tooltip: tooltipVisible,
|
|
100
|
+
animation: animation
|
|
101
|
+
})]
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
const common = {
|
|
105
|
+
data: rows,
|
|
106
|
+
xKey: X_KEY,
|
|
107
|
+
series: seriesConfig,
|
|
108
|
+
width,
|
|
109
|
+
height,
|
|
110
|
+
theme,
|
|
111
|
+
palette: paletteOrOption,
|
|
112
|
+
legend: legendVisible,
|
|
113
|
+
tooltip: tooltipVisible ? {
|
|
114
|
+
trigger: option.tooltip?.trigger ?? 'axis'
|
|
115
|
+
} : false,
|
|
116
|
+
grid: gridVisible,
|
|
117
|
+
animation,
|
|
118
|
+
yAxis: {
|
|
119
|
+
title: option.yAxis?.name,
|
|
120
|
+
min: option.yAxis?.min,
|
|
121
|
+
max: option.yAxis?.max
|
|
122
|
+
},
|
|
123
|
+
xAxis: {
|
|
124
|
+
title: option.xAxis?.name
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
const hasMixed = seriesConfig.some(item => item.type !== seriesConfig[0].type);
|
|
128
|
+
const chart = primaryType === 'bar' && !hasMixed ? /*#__PURE__*/_jsx(BarChart, {
|
|
129
|
+
...common
|
|
130
|
+
}) : primaryType === 'area' ? /*#__PURE__*/_jsx(AreaChart, {
|
|
131
|
+
...common
|
|
132
|
+
}) : primaryType === 'line' && !hasMixed ? /*#__PURE__*/_jsx(LineChart, {
|
|
133
|
+
...common
|
|
134
|
+
}) : /*#__PURE__*/_jsx(ComboChart, {
|
|
135
|
+
...common
|
|
136
|
+
});
|
|
137
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
138
|
+
className: classNames(className),
|
|
139
|
+
style: style,
|
|
140
|
+
children: [titleNode, chart]
|
|
141
|
+
});
|
|
142
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
.aura-chart {
|
|
2
|
+
position: relative;
|
|
3
|
+
display: flex;
|
|
4
|
+
flex-direction: column;
|
|
5
|
+
width: 100%;
|
|
6
|
+
font-family: var(--aura-font-family, inherit);
|
|
7
|
+
color: var(--aura-text, #111827);
|
|
8
|
+
|
|
9
|
+
&__legend {
|
|
10
|
+
display: flex;
|
|
11
|
+
flex-wrap: wrap;
|
|
12
|
+
gap: 4px 16px;
|
|
13
|
+
padding: 0 4px 8px;
|
|
14
|
+
font-size: 12px;
|
|
15
|
+
line-height: 20px;
|
|
16
|
+
flex: 0 0 auto;
|
|
17
|
+
user-select: none;
|
|
18
|
+
|
|
19
|
+
&--bottom {
|
|
20
|
+
padding: 8px 4px 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
&--center {
|
|
24
|
+
justify-content: center;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
&--right {
|
|
28
|
+
justify-content: flex-end;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
&__legend-item {
|
|
33
|
+
display: inline-flex;
|
|
34
|
+
align-items: center;
|
|
35
|
+
gap: 6px;
|
|
36
|
+
padding: 0;
|
|
37
|
+
border: 0;
|
|
38
|
+
background: none;
|
|
39
|
+
font: inherit;
|
|
40
|
+
color: inherit;
|
|
41
|
+
cursor: pointer;
|
|
42
|
+
transition: opacity 0.15s ease;
|
|
43
|
+
|
|
44
|
+
&:hover {
|
|
45
|
+
opacity: 0.75;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
&:focus-visible {
|
|
49
|
+
outline: 2px solid var(--aura-primary-300, #d8b4fe);
|
|
50
|
+
outline-offset: 2px;
|
|
51
|
+
border-radius: 4px;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
&--inactive {
|
|
55
|
+
opacity: 0.45;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
&__legend-dot {
|
|
60
|
+
width: 8px;
|
|
61
|
+
height: 8px;
|
|
62
|
+
border-radius: 2px;
|
|
63
|
+
flex: 0 0 auto;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
&__plot {
|
|
67
|
+
position: relative;
|
|
68
|
+
flex: 1 1 auto;
|
|
69
|
+
min-height: 0;
|
|
70
|
+
width: 100%;
|
|
71
|
+
overflow: hidden;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
&__zoom {
|
|
75
|
+
flex: 0 0 auto;
|
|
76
|
+
padding: 6px 12px 2px;
|
|
77
|
+
box-sizing: content-box;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
&__zoom-track {
|
|
81
|
+
position: relative;
|
|
82
|
+
height: 100%;
|
|
83
|
+
border-radius: 4px;
|
|
84
|
+
background: var(--aura-bg-tertiary, #f3f4f6);
|
|
85
|
+
cursor: pointer;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
&__zoom-window {
|
|
89
|
+
position: absolute;
|
|
90
|
+
top: 0;
|
|
91
|
+
bottom: 0;
|
|
92
|
+
border-radius: 4px;
|
|
93
|
+
background: rgba(124, 58, 237, 0.12);
|
|
94
|
+
border: 1px solid var(--aura-primary-500, #a855f7);
|
|
95
|
+
cursor: grab;
|
|
96
|
+
|
|
97
|
+
&:active {
|
|
98
|
+
cursor: grabbing;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
&__zoom-handle {
|
|
103
|
+
position: absolute;
|
|
104
|
+
top: 50%;
|
|
105
|
+
width: 8px;
|
|
106
|
+
height: 16px;
|
|
107
|
+
margin-top: -8px;
|
|
108
|
+
border-radius: 3px;
|
|
109
|
+
background: var(--aura-primary-700, #7c3aed);
|
|
110
|
+
cursor: ew-resize;
|
|
111
|
+
|
|
112
|
+
&--start {
|
|
113
|
+
left: -4px;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
&--end {
|
|
117
|
+
right: -4px;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
&__canvas {
|
|
122
|
+
display: block;
|
|
123
|
+
width: 100%;
|
|
124
|
+
height: 100%;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
&__tooltip {
|
|
128
|
+
position: absolute;
|
|
129
|
+
z-index: 10;
|
|
130
|
+
min-width: 120px;
|
|
131
|
+
max-width: 280px;
|
|
132
|
+
padding: 8px 10px;
|
|
133
|
+
border-radius: 10px;
|
|
134
|
+
border: 1px solid transparent;
|
|
135
|
+
font-size: 12px;
|
|
136
|
+
line-height: 1.6;
|
|
137
|
+
pointer-events: none;
|
|
138
|
+
transition:
|
|
139
|
+
left 0.08s linear,
|
|
140
|
+
top 0.08s linear;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
&__tooltip-title {
|
|
144
|
+
font-weight: 500;
|
|
145
|
+
margin-bottom: 4px;
|
|
146
|
+
opacity: 0.75;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
&__tooltip-item {
|
|
150
|
+
display: flex;
|
|
151
|
+
align-items: center;
|
|
152
|
+
gap: 6px;
|
|
153
|
+
justify-content: space-between;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
&__tooltip-label {
|
|
157
|
+
display: inline-flex;
|
|
158
|
+
align-items: center;
|
|
159
|
+
gap: 6px;
|
|
160
|
+
min-width: 0;
|
|
161
|
+
opacity: 0.85;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
&__tooltip-value {
|
|
165
|
+
font-weight: 600;
|
|
166
|
+
font-variant-numeric: tabular-nums;
|
|
167
|
+
white-space: nowrap;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
&__empty {
|
|
171
|
+
display: flex;
|
|
172
|
+
align-items: center;
|
|
173
|
+
justify-content: center;
|
|
174
|
+
width: 100%;
|
|
175
|
+
height: 100%;
|
|
176
|
+
font-size: 13px;
|
|
177
|
+
color: var(--aura-text-tertiary, #9ca3af);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
&__title {
|
|
181
|
+
padding: 0 4px 8px;
|
|
182
|
+
flex: 0 0 auto;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
&__title-main {
|
|
186
|
+
font-size: 14px;
|
|
187
|
+
font-weight: 600;
|
|
188
|
+
line-height: 22px;
|
|
189
|
+
color: var(--aura-text, #111827);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
&__title-sub {
|
|
193
|
+
font-size: 12px;
|
|
194
|
+
line-height: 18px;
|
|
195
|
+
color: var(--aura-text-secondary, #6b7280);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/* 无障碍数据表:视觉上隐藏,屏幕阅读器可读 */
|
|
199
|
+
/**
|
|
200
|
+
* 视觉隐藏容器。
|
|
201
|
+
*
|
|
202
|
+
* 注意必须由 div 承担 position/overflow —— table 的 display 会忽略
|
|
203
|
+
* height 与 overflow,把 1px 直接加在 table 上挡不住它撑开文档滚动高度
|
|
204
|
+
* (8000 行数据曾把页面撑到 21 万像素,滚到底全是空白)。
|
|
205
|
+
*/
|
|
206
|
+
&__a11y {
|
|
207
|
+
position: absolute;
|
|
208
|
+
width: 1px;
|
|
209
|
+
height: 1px;
|
|
210
|
+
padding: 0;
|
|
211
|
+
margin: -1px;
|
|
212
|
+
overflow: hidden;
|
|
213
|
+
clip: rect(0, 0, 0, 0);
|
|
214
|
+
clip-path: inset(50%);
|
|
215
|
+
white-space: nowrap;
|
|
216
|
+
border: 0;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
&__a11y-table {
|
|
220
|
+
border-collapse: collapse;
|
|
221
|
+
width: 100%;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 动画基础设施。
|
|
3
|
+
*
|
|
4
|
+
* 设计目标(针对「生硬」的根因):
|
|
5
|
+
* 1. 缓动不能只有一种 —— 默认改用 easeOutQuint,尾部更慢,收得更柔和;
|
|
6
|
+
* 2. 图元不能同时到齐 —— 提供 stagger(错峰),让动画「扫过」而非「弹起」;
|
|
7
|
+
* 3. 数据更新不能重播入场 —— 交给 useChartTransition 做值插值。
|
|
8
|
+
*/
|
|
9
|
+
export type EasingName = 'linear' | 'easeOutCubic' | 'easeOutQuint' | 'easeInOutCubic' | 'easeOutBack';
|
|
10
|
+
export declare function getEasing(name: EasingName): (t: number) => number;
|
|
11
|
+
export interface ChartAnimationOptions {
|
|
12
|
+
/** 时长(毫秒) */
|
|
13
|
+
duration?: number;
|
|
14
|
+
/** 缓动曲线 */
|
|
15
|
+
easing?: EasingName;
|
|
16
|
+
/**
|
|
17
|
+
* 错峰强度 0~1:最后一个图元比第一个晚多少比例的总时长开始。
|
|
18
|
+
* 0 表示全部同步;0.35 表示最后一个晚 35% 开始。
|
|
19
|
+
*/
|
|
20
|
+
stagger?: number;
|
|
21
|
+
/** 整体延迟(毫秒) */
|
|
22
|
+
delay?: number;
|
|
23
|
+
}
|
|
24
|
+
export interface ResolvedAnimation {
|
|
25
|
+
enabled: boolean;
|
|
26
|
+
duration: number;
|
|
27
|
+
easing: EasingName;
|
|
28
|
+
stagger: number;
|
|
29
|
+
delay: number;
|
|
30
|
+
ease: (t: number) => number;
|
|
31
|
+
}
|
|
32
|
+
export declare const DEFAULT_ANIMATION: Required<ChartAnimationOptions>;
|
|
33
|
+
/** 统一解析 boolean | 配置对象 */
|
|
34
|
+
export declare function resolveAnimation(input?: boolean | ChartAnimationOptions, defaultEnabled?: boolean): ResolvedAnimation;
|
|
35
|
+
export declare function clamp01(value: number): number;
|
|
36
|
+
/**
|
|
37
|
+
* 计算第 index 个元素的局部进度。
|
|
38
|
+
*
|
|
39
|
+
* 把全局进度 t 切成 count 段:第 i 个元素从 `stagger * i / (count-1)` 开始,
|
|
40
|
+
* 在剩余 `1 - stagger` 的窗口内走完自己的 0→1。
|
|
41
|
+
*
|
|
42
|
+
* 这样整组图元不是「同时弹起」,而是像画笔从左到右扫过去。
|
|
43
|
+
*/
|
|
44
|
+
export declare function staggerProgress(t: number, index: number, count: number, stagger: number): number;
|
|
45
|
+
/** 数值插值 */
|
|
46
|
+
export declare function lerp(from: number, to: number, t: number): number;
|
|
47
|
+
/** 矩阵的轻量指纹,用于判断「数据是否真的变了」 */
|
|
48
|
+
export declare function hashMatrix(matrix: readonly (readonly (number | null)[])[]): number;
|
|
49
|
+
//# sourceMappingURL=animation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"animation.d.ts","sourceRoot":"","sources":["animation.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,MAAM,UAAU,GACpB,QAAQ,GAAG,cAAc,GAAG,cAAc,GAAG,gBAAgB,GAAG,aAAa,CAAC;AAgBhF,wBAAgB,SAAS,CAAC,IAAI,EAAE,UAAU,GAAG,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAEjE;AAED,MAAM,WAAW,qBAAqB;IACpC,aAAa;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW;IACX,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,eAAe;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,UAAU,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;CAC7B;AAED,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CAAC,qBAAqB,CAK7D,CAAC;AAEF,0BAA0B;AAC1B,wBAAgB,gBAAgB,CAC9B,KAAK,CAAC,EAAE,OAAO,GAAG,qBAAqB,EACvC,cAAc,UAAO,GACpB,iBAAiB,CAuBnB;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,CAAC,EAAE,MAAM,EACT,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,GACd,MAAM,CAMR;AAED,WAAW;AACX,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED,6BAA6B;AAC7B,wBAAgB,UAAU,CACxB,MAAM,EAAE,SAAS,CAAC,SAAS,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,CAAC,EAAE,GAC9C,MAAM,CAUR"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* 动画基础设施。
|
|
4
|
+
*
|
|
5
|
+
* 设计目标(针对「生硬」的根因):
|
|
6
|
+
* 1. 缓动不能只有一种 —— 默认改用 easeOutQuint,尾部更慢,收得更柔和;
|
|
7
|
+
* 2. 图元不能同时到齐 —— 提供 stagger(错峰),让动画「扫过」而非「弹起」;
|
|
8
|
+
* 3. 数据更新不能重播入场 —— 交给 useChartTransition 做值插值。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const easingFns = {
|
|
12
|
+
linear: t => t,
|
|
13
|
+
easeOutCubic: t => 1 - (1 - t) ** 3,
|
|
14
|
+
// 比 cubic 尾部更松:数字类、柱状生长用它的减速感最自然
|
|
15
|
+
easeOutQuint: t => 1 - (1 - t) ** 5,
|
|
16
|
+
easeInOutCubic: t => t < 0.5 ? 4 * t ** 3 : 1 - (-2 * t + 2) ** 3 / 2,
|
|
17
|
+
// 轻微回弹(约 10% 过冲),仅用于环形/雷达这类「展开」动作
|
|
18
|
+
easeOutBack: t => {
|
|
19
|
+
const c1 = 1.70158;
|
|
20
|
+
const c3 = c1 + 1;
|
|
21
|
+
return 1 + c3 * (t - 1) ** 3 + c1 * (t - 1) ** 2;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
export function getEasing(name) {
|
|
25
|
+
return easingFns[name] ?? easingFns.easeOutQuint;
|
|
26
|
+
}
|
|
27
|
+
export const DEFAULT_ANIMATION = {
|
|
28
|
+
duration: 620,
|
|
29
|
+
easing: 'easeOutQuint',
|
|
30
|
+
stagger: 0.35,
|
|
31
|
+
delay: 0
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** 统一解析 boolean | 配置对象 */
|
|
35
|
+
export function resolveAnimation(input, defaultEnabled = true) {
|
|
36
|
+
if (input === false) {
|
|
37
|
+
return {
|
|
38
|
+
enabled: false,
|
|
39
|
+
duration: 0,
|
|
40
|
+
easing: DEFAULT_ANIMATION.easing,
|
|
41
|
+
stagger: 0,
|
|
42
|
+
delay: 0,
|
|
43
|
+
ease: t => t
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const options = typeof input === 'object' ? input : {};
|
|
47
|
+
const easing = options.easing ?? DEFAULT_ANIMATION.easing;
|
|
48
|
+
return {
|
|
49
|
+
enabled: defaultEnabled,
|
|
50
|
+
duration: Math.max(0, options.duration ?? DEFAULT_ANIMATION.duration),
|
|
51
|
+
easing,
|
|
52
|
+
stagger: clamp01(options.stagger ?? DEFAULT_ANIMATION.stagger),
|
|
53
|
+
delay: Math.max(0, options.delay ?? DEFAULT_ANIMATION.delay),
|
|
54
|
+
ease: getEasing(easing)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export function clamp01(value) {
|
|
58
|
+
return value < 0 ? 0 : value > 1 ? 1 : value;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 计算第 index 个元素的局部进度。
|
|
63
|
+
*
|
|
64
|
+
* 把全局进度 t 切成 count 段:第 i 个元素从 `stagger * i / (count-1)` 开始,
|
|
65
|
+
* 在剩余 `1 - stagger` 的窗口内走完自己的 0→1。
|
|
66
|
+
*
|
|
67
|
+
* 这样整组图元不是「同时弹起」,而是像画笔从左到右扫过去。
|
|
68
|
+
*/
|
|
69
|
+
export function staggerProgress(t, index, count, stagger) {
|
|
70
|
+
if (count <= 1 || stagger <= 0) return clamp01(t);
|
|
71
|
+
const delay = stagger * index / (count - 1);
|
|
72
|
+
const span = 1 - stagger;
|
|
73
|
+
if (span <= 0) return t >= delay ? 1 : 0;
|
|
74
|
+
return clamp01((t - delay) / span);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** 数值插值 */
|
|
78
|
+
export function lerp(from, to, t) {
|
|
79
|
+
return from + (to - from) * t;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 矩阵的轻量指纹,用于判断「数据是否真的变了」 */
|
|
83
|
+
export function hashMatrix(matrix) {
|
|
84
|
+
let hash = matrix.length * 2654435761;
|
|
85
|
+
for (const row of matrix) {
|
|
86
|
+
hash = hash * 31 + row.length | 0;
|
|
87
|
+
for (let index = 0; index < row.length; index += 1) {
|
|
88
|
+
const value = row[index];
|
|
89
|
+
hash = hash * 31 + (value === null ? -1 : Math.round(value * 100)) | 0;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return hash;
|
|
93
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export interface ForceNode {
|
|
2
|
+
id: string;
|
|
3
|
+
name?: string;
|
|
4
|
+
/** 分组名,用于配色 */
|
|
5
|
+
group?: string;
|
|
6
|
+
/** 节点权重,影响半径(缺省按连接数) */
|
|
7
|
+
size?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface ForceLink {
|
|
10
|
+
source: string;
|
|
11
|
+
target: string;
|
|
12
|
+
value?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface ForceLayoutOptions {
|
|
15
|
+
/** 迭代次数,越多越稳定但越慢 */
|
|
16
|
+
iterations?: number;
|
|
17
|
+
/** 节点半径,用于把节点约束在画布内 */
|
|
18
|
+
radius?: number;
|
|
19
|
+
}
|
|
20
|
+
export interface ForceNodePosition {
|
|
21
|
+
id: string;
|
|
22
|
+
x: number;
|
|
23
|
+
y: number;
|
|
24
|
+
/** 连接数,可用于决定半径 */
|
|
25
|
+
degree: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 力导向布局(Fruchterman–Reingold)。
|
|
29
|
+
*
|
|
30
|
+
* 把图当成物理系统:节点两两相斥、有连边的相互吸引,
|
|
31
|
+
* 每轮按「温度」限制最大位移,温度线性冷却到 0 后收敛。
|
|
32
|
+
*
|
|
33
|
+
* 初始位置用「圆环 + 确定性扰动」而不是随机散布:
|
|
34
|
+
* 随机数会让同一份数据在每次刷新时长得不一样,也不利于截图对比;
|
|
35
|
+
* 圆环本身已经足够均匀,能让迭代更快收敛。
|
|
36
|
+
*/
|
|
37
|
+
export declare function layoutForce(nodes: ForceNode[], links: ForceLink[], rect: {
|
|
38
|
+
x: number;
|
|
39
|
+
y: number;
|
|
40
|
+
width: number;
|
|
41
|
+
height: number;
|
|
42
|
+
}, options?: ForceLayoutOptions): ForceNodePosition[];
|
|
43
|
+
//# sourceMappingURL=force.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"force.d.ts","sourceRoot":"","sources":["force.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,eAAe;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,wBAAwB;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,kBAAkB;IACjC,oBAAoB;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,uBAAuB;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,kBAAkB;IAClB,MAAM,EAAE,MAAM,CAAC;CAChB;AAQD;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,SAAS,EAAE,EAClB,KAAK,EAAE,SAAS,EAAE,EAClB,IAAI,EAAE;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAC7D,OAAO,GAAE,kBAAuB,GAC/B,iBAAiB,EAAE,CAsHrB"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/** 确定性伪随机:同一个 seed 恒定,保证 SSR 与截图一致 */
|
|
3
|
+
function pseudo(seed) {
|
|
4
|
+
const value = Math.sin(seed * 12.9898) * 43758.5453;
|
|
5
|
+
return value - Math.floor(value);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 力导向布局(Fruchterman–Reingold)。
|
|
10
|
+
*
|
|
11
|
+
* 把图当成物理系统:节点两两相斥、有连边的相互吸引,
|
|
12
|
+
* 每轮按「温度」限制最大位移,温度线性冷却到 0 后收敛。
|
|
13
|
+
*
|
|
14
|
+
* 初始位置用「圆环 + 确定性扰动」而不是随机散布:
|
|
15
|
+
* 随机数会让同一份数据在每次刷新时长得不一样,也不利于截图对比;
|
|
16
|
+
* 圆环本身已经足够均匀,能让迭代更快收敛。
|
|
17
|
+
*/
|
|
18
|
+
export function layoutForce(nodes, links, rect, options = {}) {
|
|
19
|
+
const count = nodes.length;
|
|
20
|
+
if (count === 0) return [];
|
|
21
|
+
const {
|
|
22
|
+
iterations = 220,
|
|
23
|
+
radius = 8
|
|
24
|
+
} = options;
|
|
25
|
+
const indexOf = new Map(nodes.map((node, index) => [node.id, index]));
|
|
26
|
+
const xs = new Array(count);
|
|
27
|
+
const ys = new Array(count);
|
|
28
|
+
const degrees = new Array(count).fill(0);
|
|
29
|
+
const valid = [];
|
|
30
|
+
for (const link of links) {
|
|
31
|
+
const a = indexOf.get(link.source);
|
|
32
|
+
const b = indexOf.get(link.target);
|
|
33
|
+
if (a === undefined || b === undefined || a === b) continue;
|
|
34
|
+
valid.push({
|
|
35
|
+
a,
|
|
36
|
+
b
|
|
37
|
+
});
|
|
38
|
+
degrees[a] += 1;
|
|
39
|
+
degrees[b] += 1;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 初始:圆环 + 轻微扰动(单节点时居中)
|
|
43
|
+
const cx = rect.x + rect.width / 2;
|
|
44
|
+
const cy = rect.y + rect.height / 2;
|
|
45
|
+
const ringRadius = Math.min(rect.width, rect.height) * 0.34;
|
|
46
|
+
for (let index = 0; index < count; index += 1) {
|
|
47
|
+
if (count === 1) {
|
|
48
|
+
xs[index] = cx;
|
|
49
|
+
ys[index] = cy;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const angle = index / count * Math.PI * 2;
|
|
53
|
+
const jitter = (pseudo(index + 1) - 0.5) * ringRadius * 0.35;
|
|
54
|
+
xs[index] = cx + Math.cos(angle) * (ringRadius + jitter);
|
|
55
|
+
ys[index] = cy + Math.sin(angle) * (ringRadius + jitter);
|
|
56
|
+
}
|
|
57
|
+
if (count === 1) {
|
|
58
|
+
return [{
|
|
59
|
+
id: nodes[0].id,
|
|
60
|
+
x: cx,
|
|
61
|
+
y: cy,
|
|
62
|
+
degree: 0
|
|
63
|
+
}];
|
|
64
|
+
}
|
|
65
|
+
const area = Math.max(1, rect.width * rect.height);
|
|
66
|
+
const k = Math.sqrt(area / count);
|
|
67
|
+
const margin = radius + 2;
|
|
68
|
+
let temperature = Math.min(rect.width, rect.height) / 8;
|
|
69
|
+
for (let step = 0; step < iterations; step += 1) {
|
|
70
|
+
const dx = new Array(count).fill(0);
|
|
71
|
+
const dy = new Array(count).fill(0);
|
|
72
|
+
|
|
73
|
+
// 斥力:所有节点两两排斥
|
|
74
|
+
for (let i = 0; i < count; i += 1) {
|
|
75
|
+
for (let j = i + 1; j < count; j += 1) {
|
|
76
|
+
let deltaX = xs[i] - xs[j];
|
|
77
|
+
let deltaY = ys[i] - ys[j];
|
|
78
|
+
let distance = Math.hypot(deltaX, deltaY);
|
|
79
|
+
if (distance < 0.01) {
|
|
80
|
+
// 完全重合时给一个确定性的偏移方向,避免除零
|
|
81
|
+
deltaX = (pseudo(i + j + 1) - 0.5) * 0.1;
|
|
82
|
+
deltaY = (pseudo(i + j + 2) - 0.5) * 0.1;
|
|
83
|
+
distance = 0.01;
|
|
84
|
+
}
|
|
85
|
+
const force = k * k / distance;
|
|
86
|
+
const ux = deltaX / distance;
|
|
87
|
+
const uy = deltaY / distance;
|
|
88
|
+
dx[i] += ux * force;
|
|
89
|
+
dy[i] += uy * force;
|
|
90
|
+
dx[j] -= ux * force;
|
|
91
|
+
dy[j] -= uy * force;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 引力:有连边的相互吸引
|
|
96
|
+
for (const {
|
|
97
|
+
a,
|
|
98
|
+
b
|
|
99
|
+
} of valid) {
|
|
100
|
+
let deltaX = xs[a] - xs[b];
|
|
101
|
+
let deltaY = ys[a] - ys[b];
|
|
102
|
+
let distance = Math.hypot(deltaX, deltaY);
|
|
103
|
+
if (distance < 0.01) {
|
|
104
|
+
deltaX = 0.01;
|
|
105
|
+
deltaY = 0;
|
|
106
|
+
distance = 0.01;
|
|
107
|
+
}
|
|
108
|
+
const force = distance * distance / k;
|
|
109
|
+
const ux = deltaX / distance;
|
|
110
|
+
const uy = deltaY / distance;
|
|
111
|
+
dx[a] -= ux * force;
|
|
112
|
+
dy[a] -= uy * force;
|
|
113
|
+
dx[b] += ux * force;
|
|
114
|
+
dy[b] += uy * force;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 按温度限制位移,并夹在画布内
|
|
118
|
+
for (let index = 0; index < count; index += 1) {
|
|
119
|
+
const length = Math.hypot(dx[index], dy[index]) || 1;
|
|
120
|
+
const limited = Math.min(length, temperature);
|
|
121
|
+
xs[index] += dx[index] / length * limited;
|
|
122
|
+
ys[index] += dy[index] / length * limited;
|
|
123
|
+
xs[index] = Math.min(rect.x + rect.width - margin, Math.max(rect.x + margin, xs[index]));
|
|
124
|
+
ys[index] = Math.min(rect.y + rect.height - margin, Math.max(rect.y + margin, ys[index]));
|
|
125
|
+
}
|
|
126
|
+
temperature = Math.max(0.5, temperature * 0.975);
|
|
127
|
+
}
|
|
128
|
+
return nodes.map((node, index) => ({
|
|
129
|
+
id: node.id,
|
|
130
|
+
x: xs[index],
|
|
131
|
+
y: ys[index],
|
|
132
|
+
degree: degrees[index]
|
|
133
|
+
}));
|
|
134
|
+
}
|