@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
|
@@ -1,32 +1,23 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
import {
|
|
2
|
+
// src/charts/radar-chart/index.tsx
|
|
3
|
+
import { useCallback, useMemo, useState } from "react";
|
|
4
|
+
import { classNames, prefixCls } from "@aura-react-comp/shared";
|
|
4
5
|
import { SvgScene } from "../../scene/svg";
|
|
5
6
|
import { withAlpha } from "../../theme";
|
|
6
7
|
import { useChartTheme } from "../../hooks/useChartTheme";
|
|
7
8
|
import { useElementSize } from "../../hooks/useElementSize";
|
|
8
9
|
import { useChartTransition } from "../../hooks/useChartTransition";
|
|
10
|
+
import { usePlotPointer } from "../../hooks/usePlotPointer";
|
|
9
11
|
import { resolveAnimation, staggerProgress } from "../../core/animation";
|
|
10
12
|
import { Legend } from "../../components/Legend";
|
|
11
13
|
import { Tooltip } from "../../components/Tooltip";
|
|
12
14
|
import { estimateTextWidth } from "../../core/format";
|
|
13
|
-
import { jsx
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 雷达图。
|
|
19
|
-
*
|
|
20
|
-
* 几何本质是极坐标:n 个指标把圆周等分,每个指标一条轴,
|
|
21
|
-
* 数值映射为「沿轴距圆心的距离」。
|
|
22
|
-
*
|
|
23
|
-
* 与饼图相反,雷达图不编码总量,它编码的是**形状**——多指标的综合轮廓。
|
|
24
|
-
* 因此各指标的 max 必须由使用者显式给出:自动取最大值会让「谁更好」失去参照。
|
|
25
|
-
*/
|
|
26
|
-
export function RadarChart({
|
|
15
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
16
|
+
var GRID_LABEL_GAP = 10;
|
|
17
|
+
function RadarChart({
|
|
27
18
|
indicators = [],
|
|
28
19
|
series = [],
|
|
29
|
-
width =
|
|
20
|
+
width = "100%",
|
|
30
21
|
height = 320,
|
|
31
22
|
theme: themeInput,
|
|
32
23
|
palette,
|
|
@@ -37,71 +28,74 @@ export function RadarChart({
|
|
|
37
28
|
className,
|
|
38
29
|
style,
|
|
39
30
|
ariaLabel,
|
|
40
|
-
emptyText =
|
|
31
|
+
emptyText = "暂无数据"
|
|
41
32
|
}) {
|
|
42
33
|
const theme = useChartTheme(themeInput, palette);
|
|
43
|
-
const {
|
|
44
|
-
ref,
|
|
45
|
-
size
|
|
46
|
-
} = useElementSize();
|
|
34
|
+
const { ref, size } = useElementSize();
|
|
47
35
|
const [hiddenKeys, setHiddenKeys] = useState([]);
|
|
48
36
|
const [activeKey, setActiveKey] = useState(null);
|
|
49
|
-
const visible = useMemo(
|
|
50
|
-
|
|
37
|
+
const visible = useMemo(
|
|
38
|
+
() => series.filter((item) => !hiddenKeys.includes(item.key)),
|
|
39
|
+
[series, hiddenKeys]
|
|
40
|
+
);
|
|
41
|
+
const colors = useMemo(
|
|
42
|
+
() => series.map(
|
|
43
|
+
(item, index) => item.color ?? theme.palette[index % theme.palette.length]
|
|
44
|
+
),
|
|
45
|
+
[series, theme.palette]
|
|
46
|
+
);
|
|
51
47
|
const geometry = useMemo(() => {
|
|
52
48
|
const count = Math.max(indicators.length, 3);
|
|
53
|
-
|
|
54
|
-
|
|
49
|
+
const maxLabel = indicators.reduce(
|
|
50
|
+
(max, item) => Math.max(max, estimateTextWidth(item.name, theme.font.size)),
|
|
51
|
+
0
|
|
52
|
+
);
|
|
55
53
|
const padding = Math.min(64, maxLabel / 2 + GRID_LABEL_GAP + 6);
|
|
56
54
|
const radius = Math.max(0, Math.min(size.width, size.height) / 2 - padding);
|
|
57
|
-
return {
|
|
58
|
-
cx: size.width / 2,
|
|
59
|
-
cy: size.height / 2,
|
|
60
|
-
radius,
|
|
61
|
-
count
|
|
62
|
-
};
|
|
55
|
+
return { cx: size.width / 2, cy: size.height / 2, radius, count };
|
|
63
56
|
}, [indicators, size.width, size.height, theme.font.size]);
|
|
64
|
-
const angleAt = useCallback(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const
|
|
57
|
+
const angleAt = useCallback(
|
|
58
|
+
(index) => -Math.PI / 2 + index * Math.PI * 2 / geometry.count,
|
|
59
|
+
[geometry.count]
|
|
60
|
+
);
|
|
61
|
+
const animKey = typeof animation === "object" ? JSON.stringify(animation) : String(animation);
|
|
62
|
+
const animConfig = useMemo(
|
|
63
|
+
() => resolveAnimation(animation),
|
|
64
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
65
|
+
[animKey]
|
|
66
|
+
);
|
|
67
|
+
const valueMatrix = useMemo(
|
|
68
|
+
() => series.map((item) => item.values),
|
|
69
|
+
[series]
|
|
70
|
+
);
|
|
70
71
|
const transition = useChartTransition(valueMatrix, animConfig);
|
|
71
72
|
const progress = transition.progress;
|
|
72
73
|
const scene = useMemo(() => {
|
|
73
|
-
const {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
radius
|
|
77
|
-
} = geometry;
|
|
78
|
-
if (radius <= 0 || indicators.length < 3) return [];
|
|
74
|
+
const { cx, cy, radius } = geometry;
|
|
75
|
+
if (radius <= 0 || indicators.length < 3)
|
|
76
|
+
return [];
|
|
79
77
|
const nodes = [];
|
|
80
|
-
|
|
81
|
-
// 同心多边形网格
|
|
82
78
|
for (let level = 1; level <= levels; level += 1) {
|
|
83
79
|
const ratio = level / levels;
|
|
84
80
|
const points = indicators.map((_, index) => {
|
|
85
81
|
const angle = angleAt(index);
|
|
86
82
|
return `${cx + Math.cos(angle) * radius * ratio},${cy + Math.sin(angle) * radius * ratio}`;
|
|
87
|
-
}).join(
|
|
83
|
+
}).join("L");
|
|
88
84
|
nodes.push({
|
|
89
|
-
kind:
|
|
85
|
+
kind: "path",
|
|
90
86
|
key: `grid-${level}`,
|
|
91
87
|
d: `M${points}Z`,
|
|
92
|
-
fill: level % 2 === 0 ? withAlpha(theme.grid.line, 0.05) :
|
|
88
|
+
fill: level % 2 === 0 ? withAlpha(theme.grid.line, 0.05) : "none",
|
|
93
89
|
stroke: theme.grid.line,
|
|
94
90
|
strokeWidth: theme.grid.lineWidth,
|
|
95
|
-
strokeDasharray: level === levels ?
|
|
96
|
-
pointerEvents:
|
|
91
|
+
strokeDasharray: level === levels ? void 0 : theme.grid.dash.join(" "),
|
|
92
|
+
pointerEvents: "none"
|
|
97
93
|
});
|
|
98
94
|
}
|
|
99
|
-
|
|
100
|
-
// 轴线
|
|
101
95
|
indicators.forEach((_, index) => {
|
|
102
96
|
const angle = angleAt(index);
|
|
103
97
|
nodes.push({
|
|
104
|
-
kind:
|
|
98
|
+
kind: "line",
|
|
105
99
|
key: `axis-${index}`,
|
|
106
100
|
x1: cx,
|
|
107
101
|
y1: cy,
|
|
@@ -109,42 +103,41 @@ export function RadarChart({
|
|
|
109
103
|
y2: cy + Math.sin(angle) * radius,
|
|
110
104
|
stroke: theme.axis.line,
|
|
111
105
|
strokeWidth: 1,
|
|
112
|
-
pointerEvents:
|
|
106
|
+
pointerEvents: "none"
|
|
113
107
|
});
|
|
114
108
|
});
|
|
115
|
-
|
|
116
|
-
// 指标名
|
|
117
109
|
indicators.forEach((indicator, index) => {
|
|
118
110
|
const angle = angleAt(index);
|
|
119
111
|
const x = cx + Math.cos(angle) * (radius + GRID_LABEL_GAP);
|
|
120
112
|
const y = cy + Math.sin(angle) * (radius + GRID_LABEL_GAP);
|
|
121
113
|
const cos = Math.cos(angle);
|
|
122
114
|
nodes.push({
|
|
123
|
-
kind:
|
|
115
|
+
kind: "text",
|
|
124
116
|
key: `label-${index}`,
|
|
125
117
|
x,
|
|
126
118
|
y,
|
|
127
119
|
text: indicator.name,
|
|
128
120
|
fill: theme.text.secondary,
|
|
129
121
|
fontSize: theme.font.size,
|
|
130
|
-
anchor: Math.abs(cos) < 0.2 ?
|
|
131
|
-
baseline:
|
|
122
|
+
anchor: Math.abs(cos) < 0.2 ? "middle" : cos > 0 ? "start" : "end",
|
|
123
|
+
baseline: "middle",
|
|
132
124
|
fontFamily: theme.font.family
|
|
133
125
|
});
|
|
134
126
|
});
|
|
135
|
-
|
|
136
|
-
// 数据多边形
|
|
137
|
-
visible.forEach(item => {
|
|
127
|
+
visible.forEach((item) => {
|
|
138
128
|
const seriesIndex = series.indexOf(item);
|
|
139
129
|
const color = colors[seriesIndex] ?? theme.palette[0];
|
|
140
130
|
const isActive = activeKey === item.key;
|
|
141
|
-
|
|
142
|
-
|
|
131
|
+
const local = staggerProgress(
|
|
132
|
+
progress,
|
|
133
|
+
seriesIndex,
|
|
134
|
+
visible.length,
|
|
135
|
+
animConfig.stagger
|
|
136
|
+
);
|
|
143
137
|
const row = transition.values[seriesIndex] ?? item.values;
|
|
144
138
|
const points = indicators.map((indicator, index) => {
|
|
145
139
|
const raw = row[index] ?? 0;
|
|
146
140
|
const max = indicator.max || 1;
|
|
147
|
-
// 超出 max 的数值会画到网格外,视觉上必须夹紧而不是溢出画布
|
|
148
141
|
const ratio = Math.min(1.2, Math.max(0, raw / max)) * local;
|
|
149
142
|
const angle = angleAt(index);
|
|
150
143
|
return {
|
|
@@ -153,19 +146,19 @@ export function RadarChart({
|
|
|
153
146
|
};
|
|
154
147
|
});
|
|
155
148
|
nodes.push({
|
|
156
|
-
kind:
|
|
149
|
+
kind: "path",
|
|
157
150
|
key: `area-${item.key}`,
|
|
158
|
-
d: `M${points.map(p => `${p.x},${p.y}`).join(
|
|
151
|
+
d: `M${points.map((p) => `${p.x},${p.y}`).join("L")}Z`,
|
|
159
152
|
fill: withAlpha(color, isActive ? 0.28 : 0.16),
|
|
160
153
|
stroke: color,
|
|
161
154
|
strokeWidth: isActive ? theme.series.emphasisWidth : theme.series.lineWidth,
|
|
162
|
-
linejoin:
|
|
155
|
+
linejoin: "round",
|
|
163
156
|
opacity: activeKey === null || isActive ? 1 : 0.5,
|
|
164
|
-
pointerEvents:
|
|
157
|
+
pointerEvents: "none"
|
|
165
158
|
});
|
|
166
159
|
points.forEach((point, index) => {
|
|
167
160
|
nodes.push({
|
|
168
|
-
kind:
|
|
161
|
+
kind: "circle",
|
|
169
162
|
key: `dot-${item.key}-${index}`,
|
|
170
163
|
cx: point.x,
|
|
171
164
|
cy: point.y,
|
|
@@ -174,28 +167,46 @@ export function RadarChart({
|
|
|
174
167
|
stroke: color,
|
|
175
168
|
strokeWidth: 2,
|
|
176
169
|
opacity: activeKey === null || isActive ? 1 : 0.5,
|
|
177
|
-
pointerEvents:
|
|
170
|
+
pointerEvents: "none"
|
|
178
171
|
});
|
|
179
172
|
});
|
|
180
173
|
});
|
|
181
174
|
return nodes;
|
|
182
|
-
}, [
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
175
|
+
}, [
|
|
176
|
+
geometry,
|
|
177
|
+
indicators,
|
|
178
|
+
levels,
|
|
179
|
+
angleAt,
|
|
180
|
+
theme,
|
|
181
|
+
visible,
|
|
182
|
+
colors,
|
|
183
|
+
series,
|
|
184
|
+
activeKey,
|
|
185
|
+
progress,
|
|
186
|
+
transition.values,
|
|
187
|
+
animConfig.stagger
|
|
188
|
+
]);
|
|
189
|
+
const findNearest = useCallback(
|
|
190
|
+
(clientX, clientY) => {
|
|
191
|
+
const element = ref.current;
|
|
192
|
+
if (!element || visible.length === 0)
|
|
193
|
+
return null;
|
|
194
|
+
const rect = element.getBoundingClientRect();
|
|
195
|
+
const localX = clientX - rect.left;
|
|
196
|
+
const localY = clientY - rect.top;
|
|
197
|
+
const distance = Math.hypot(localX - geometry.cx, localY - geometry.cy);
|
|
198
|
+
if (distance > geometry.radius + 12)
|
|
199
|
+
return null;
|
|
200
|
+
return visible[0].key;
|
|
201
|
+
},
|
|
202
|
+
[ref, visible, geometry]
|
|
203
|
+
);
|
|
195
204
|
const active = useMemo(() => {
|
|
196
|
-
if (!activeKey)
|
|
197
|
-
|
|
198
|
-
|
|
205
|
+
if (!activeKey)
|
|
206
|
+
return null;
|
|
207
|
+
const item = series.find((entry) => entry.key === activeKey);
|
|
208
|
+
if (!item)
|
|
209
|
+
return null;
|
|
199
210
|
const color = colors[series.indexOf(item)] ?? theme.palette[0];
|
|
200
211
|
return {
|
|
201
212
|
item,
|
|
@@ -219,45 +230,60 @@ export function RadarChart({
|
|
|
219
230
|
color: colors[index],
|
|
220
231
|
visible: !hiddenKeys.includes(item.key)
|
|
221
232
|
}));
|
|
222
|
-
const toggle = useCallback(key => {
|
|
223
|
-
setHiddenKeys(
|
|
233
|
+
const toggle = useCallback((key) => {
|
|
234
|
+
setHiddenKeys(
|
|
235
|
+
(prev) => prev.includes(key) ? prev.filter((item) => item !== key) : [...prev, key]
|
|
236
|
+
);
|
|
224
237
|
}, []);
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
width,
|
|
230
|
-
height,
|
|
231
|
-
...style
|
|
232
|
-
},
|
|
233
|
-
"data-aura-chart": "radar",
|
|
234
|
-
children: [/*#__PURE__*/_jsx(Legend, {
|
|
235
|
-
items: legendItems,
|
|
236
|
-
theme: theme,
|
|
237
|
-
config: legend,
|
|
238
|
-
onToggle: toggle
|
|
239
|
-
}), /*#__PURE__*/_jsxs("div", {
|
|
240
|
-
ref: ref,
|
|
241
|
-
className: prefixCls('chart__plot'),
|
|
242
|
-
onPointerMove: event => setActiveKey(findNearest(event.clientX, event.clientY)),
|
|
243
|
-
onPointerLeave: () => setActiveKey(null),
|
|
244
|
-
children: [indicators.length < 3 || visible.length === 0 ? /*#__PURE__*/_jsx("div", {
|
|
245
|
-
className: prefixCls('chart__empty'),
|
|
246
|
-
children: emptyText
|
|
247
|
-
}) : /*#__PURE__*/_jsx(SvgScene, {
|
|
248
|
-
nodes: scene,
|
|
249
|
-
width: size.width,
|
|
250
|
-
height: size.height,
|
|
251
|
-
ariaLabel: autoAriaLabel
|
|
252
|
-
}), /*#__PURE__*/_jsx(Tooltip, {
|
|
253
|
-
payload: active?.payload ?? null,
|
|
254
|
-
x: geometry.cx,
|
|
255
|
-
y: geometry.cy - geometry.radius * 0.6,
|
|
256
|
-
containerWidth: size.width,
|
|
257
|
-
containerHeight: size.height,
|
|
258
|
-
theme: theme,
|
|
259
|
-
config: tooltip
|
|
260
|
-
})]
|
|
261
|
-
})]
|
|
238
|
+
const pointerHandlers = usePlotPointer({
|
|
239
|
+
disabled: tooltip === false || visible.length === 0,
|
|
240
|
+
onMove: (clientX, clientY) => setActiveKey(findNearest(clientX, clientY)),
|
|
241
|
+
onLeave: () => setActiveKey(null)
|
|
262
242
|
});
|
|
263
|
-
}
|
|
243
|
+
const autoAriaLabel = ariaLabel ?? `雷达图:${indicators.map((item) => item.name).join("、")}`;
|
|
244
|
+
return /* @__PURE__ */ jsxs(
|
|
245
|
+
"div",
|
|
246
|
+
{
|
|
247
|
+
className: classNames(prefixCls("chart"), className),
|
|
248
|
+
style: { width, height, ...style },
|
|
249
|
+
"data-aura-chart": "radar",
|
|
250
|
+
children: [
|
|
251
|
+
/* @__PURE__ */ jsx(
|
|
252
|
+
Legend,
|
|
253
|
+
{
|
|
254
|
+
items: legendItems,
|
|
255
|
+
theme,
|
|
256
|
+
config: legend,
|
|
257
|
+
onToggle: toggle
|
|
258
|
+
}
|
|
259
|
+
),
|
|
260
|
+
/* @__PURE__ */ jsxs("div", { ref, className: prefixCls("chart__plot"), ...pointerHandlers, children: [
|
|
261
|
+
indicators.length < 3 || visible.length === 0 ? /* @__PURE__ */ jsx("div", { className: prefixCls("chart__empty"), children: emptyText }) : /* @__PURE__ */ jsx(
|
|
262
|
+
SvgScene,
|
|
263
|
+
{
|
|
264
|
+
nodes: scene,
|
|
265
|
+
width: size.width,
|
|
266
|
+
height: size.height,
|
|
267
|
+
ariaLabel: autoAriaLabel
|
|
268
|
+
}
|
|
269
|
+
),
|
|
270
|
+
/* @__PURE__ */ jsx(
|
|
271
|
+
Tooltip,
|
|
272
|
+
{
|
|
273
|
+
payload: (active == null ? void 0 : active.payload) ?? null,
|
|
274
|
+
x: geometry.cx,
|
|
275
|
+
y: geometry.cy - geometry.radius * 0.6,
|
|
276
|
+
containerWidth: size.width,
|
|
277
|
+
containerHeight: size.height,
|
|
278
|
+
theme,
|
|
279
|
+
config: tooltip
|
|
280
|
+
}
|
|
281
|
+
)
|
|
282
|
+
] })
|
|
283
|
+
]
|
|
284
|
+
}
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
export {
|
|
288
|
+
RadarChart
|
|
289
|
+
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
import {
|
|
2
|
+
// src/charts/ring-progress/index.tsx
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { classNames, prefixCls } from "@aura-react-comp/shared";
|
|
4
5
|
import { SvgScene } from "../../scene/svg";
|
|
5
6
|
import { arcLinePath } from "../../core/shape";
|
|
6
7
|
import { formatPercent } from "../../core/format";
|
|
@@ -8,17 +9,9 @@ import { withAlpha } from "../../theme";
|
|
|
8
9
|
import { useChartTheme } from "../../hooks/useChartTheme";
|
|
9
10
|
import { useChartTransition } from "../../hooks/useChartTransition";
|
|
10
11
|
import { resolveAnimation } from "../../core/animation";
|
|
11
|
-
import { jsx
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 环形进度。
|
|
17
|
-
*
|
|
18
|
-
* 与 GaugeChart 的分工:仪表盘强调「在量程中的位置」(带刻度与端点),
|
|
19
|
-
* 环形进度强调「完成度」(无刻度、更紧凑),常用于表格单元格与卡片角落。
|
|
20
|
-
*/
|
|
21
|
-
export function RingProgress({
|
|
12
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
13
|
+
var DEG = Math.PI / 180;
|
|
14
|
+
function RingProgress({
|
|
22
15
|
value,
|
|
23
16
|
max = 100,
|
|
24
17
|
size = 160,
|
|
@@ -36,16 +29,20 @@ export function RingProgress({
|
|
|
36
29
|
style,
|
|
37
30
|
ariaLabel
|
|
38
31
|
}) {
|
|
32
|
+
var _a;
|
|
39
33
|
const theme = useChartTheme(themeInput, palette);
|
|
40
|
-
const animKey = typeof animation ===
|
|
41
|
-
const animConfig = useMemo(
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
const animKey = typeof animation === "object" ? JSON.stringify(animation) : String(animation);
|
|
35
|
+
const animConfig = useMemo(
|
|
36
|
+
() => resolveAnimation(animation),
|
|
37
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
38
|
+
[animKey]
|
|
39
|
+
);
|
|
44
40
|
const transition = useChartTransition([[value]], animConfig);
|
|
45
41
|
const progress = transition.progress;
|
|
46
|
-
const animatedValue = transition.values[0]
|
|
42
|
+
const animatedValue = ((_a = transition.values[0]) == null ? void 0 : _a[0]) ?? value;
|
|
47
43
|
const ratio = useMemo(() => {
|
|
48
|
-
if (max === 0)
|
|
44
|
+
if (max === 0)
|
|
45
|
+
return 0;
|
|
49
46
|
return Math.min(1, Math.max(0, animatedValue / max));
|
|
50
47
|
}, [animatedValue, max]);
|
|
51
48
|
const accent = color ?? theme.emphasis;
|
|
@@ -53,85 +50,87 @@ export function RingProgress({
|
|
|
53
50
|
const geometry = useMemo(() => {
|
|
54
51
|
const center = size / 2;
|
|
55
52
|
const radius = Math.max(0, center - thickness / 2 - 1);
|
|
56
|
-
return {
|
|
57
|
-
cx: center,
|
|
58
|
-
cy: center,
|
|
59
|
-
radius,
|
|
60
|
-
arc: radius * 2 * Math.PI
|
|
61
|
-
};
|
|
53
|
+
return { cx: center, cy: center, radius, arc: radius * 2 * Math.PI };
|
|
62
54
|
}, [size, thickness]);
|
|
63
55
|
const start = startAngle * DEG;
|
|
64
56
|
const end = start + Math.PI * 2 * ratio * progress;
|
|
65
57
|
const scene = useMemo(() => {
|
|
66
|
-
const {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
radius
|
|
70
|
-
} = geometry;
|
|
71
|
-
if (radius <= 0) return [];
|
|
58
|
+
const { cx, cy, radius } = geometry;
|
|
59
|
+
if (radius <= 0)
|
|
60
|
+
return [];
|
|
72
61
|
const nodes = [];
|
|
73
62
|
nodes.push({
|
|
74
|
-
kind:
|
|
75
|
-
key:
|
|
63
|
+
kind: "path",
|
|
64
|
+
key: "track",
|
|
76
65
|
d: arcLinePath(cx, cy, radius, 0, Math.PI * 2),
|
|
77
|
-
fill:
|
|
66
|
+
fill: "none",
|
|
78
67
|
stroke: track,
|
|
79
68
|
strokeWidth: thickness,
|
|
80
|
-
pointerEvents:
|
|
69
|
+
pointerEvents: "none"
|
|
81
70
|
});
|
|
82
|
-
if (end > start +
|
|
71
|
+
if (end > start + 1e-4) {
|
|
83
72
|
nodes.push({
|
|
84
|
-
kind:
|
|
85
|
-
key:
|
|
73
|
+
kind: "path",
|
|
74
|
+
key: "value",
|
|
86
75
|
d: arcLinePath(cx, cy, radius, start, end),
|
|
87
|
-
fill:
|
|
76
|
+
fill: "none",
|
|
88
77
|
stroke: accent,
|
|
89
78
|
strokeWidth: thickness,
|
|
90
|
-
linecap: round ?
|
|
91
|
-
pointerEvents:
|
|
79
|
+
linecap: round ? "round" : "butt",
|
|
80
|
+
pointerEvents: "none"
|
|
92
81
|
});
|
|
93
82
|
}
|
|
94
83
|
return nodes;
|
|
95
84
|
}, [geometry, start, end, track, accent, thickness, round]);
|
|
96
85
|
const autoAriaLabel = ariaLabel ?? `进度:${Math.round(ratio * 100)}%`;
|
|
97
|
-
return
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
height: size,
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
86
|
+
return /* @__PURE__ */ jsxs(
|
|
87
|
+
"div",
|
|
88
|
+
{
|
|
89
|
+
className: classNames(prefixCls("chart"), className),
|
|
90
|
+
style: { width: size, height: size, position: "relative", ...style },
|
|
91
|
+
"data-aura-chart": "ring-progress",
|
|
92
|
+
role: "img",
|
|
93
|
+
"aria-label": autoAriaLabel,
|
|
94
|
+
children: [
|
|
95
|
+
/* @__PURE__ */ jsx("div", { className: prefixCls("chart__plot"), children: /* @__PURE__ */ jsx(
|
|
96
|
+
SvgScene,
|
|
97
|
+
{
|
|
98
|
+
nodes: scene,
|
|
99
|
+
width: size,
|
|
100
|
+
height: size,
|
|
101
|
+
ariaLabel: autoAriaLabel
|
|
102
|
+
}
|
|
103
|
+
) }),
|
|
104
|
+
/* @__PURE__ */ jsx(
|
|
105
|
+
"div",
|
|
106
|
+
{
|
|
107
|
+
style: {
|
|
108
|
+
position: "absolute",
|
|
109
|
+
inset: 0,
|
|
110
|
+
display: "flex",
|
|
111
|
+
flexDirection: "column",
|
|
112
|
+
alignItems: "center",
|
|
113
|
+
justifyContent: "center",
|
|
114
|
+
pointerEvents: "none"
|
|
115
|
+
},
|
|
116
|
+
children: children ?? (showPercent ? /* @__PURE__ */ jsx(
|
|
117
|
+
"span",
|
|
118
|
+
{
|
|
119
|
+
style: {
|
|
120
|
+
fontSize: Math.max(14, size * 0.16),
|
|
121
|
+
fontWeight: theme.font.weightMedium,
|
|
122
|
+
color: theme.text.primary,
|
|
123
|
+
fontVariantNumeric: "tabular-nums"
|
|
124
|
+
},
|
|
125
|
+
children: formatPercent(value, max || 1, 0)
|
|
126
|
+
}
|
|
127
|
+
) : null)
|
|
128
|
+
}
|
|
129
|
+
)
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
export {
|
|
135
|
+
RingProgress
|
|
136
|
+
};
|