@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,6 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
import {
|
|
2
|
+
// src/charts/funnel-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 { formatNumber, formatPercent } from "../../core/format";
|
|
6
7
|
import { withAlpha } from "../../theme";
|
|
@@ -9,21 +10,12 @@ import { useElementSize } from "../../hooks/useElementSize";
|
|
|
9
10
|
import { useChartTransition } from "../../hooks/useChartTransition";
|
|
10
11
|
import { resolveAnimation, staggerProgress } from "../../core/animation";
|
|
11
12
|
import { Tooltip } from "../../components/Tooltip";
|
|
12
|
-
import { jsx
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 漏斗图。
|
|
16
|
-
*
|
|
17
|
-
* 每一项的宽度按数值比例决定,因此它能直观表达「逐层流失」。
|
|
18
|
-
*
|
|
19
|
-
* 注意:漏斗图的面积编码是**非线性的**(梯形面积 ∝ 上下宽之和 × 高),
|
|
20
|
-
* 所以不要用它做精确的比例判断——它擅长的是「看出哪一层掉得最狠」。
|
|
21
|
-
*/
|
|
22
|
-
export function FunnelChart({
|
|
13
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
14
|
+
function FunnelChart({
|
|
23
15
|
data = [],
|
|
24
|
-
width =
|
|
16
|
+
width = "100%",
|
|
25
17
|
height = 320,
|
|
26
|
-
label =
|
|
18
|
+
label = "inside",
|
|
27
19
|
gap = 4,
|
|
28
20
|
theme: themeInput,
|
|
29
21
|
palette,
|
|
@@ -32,46 +24,50 @@ export function FunnelChart({
|
|
|
32
24
|
className,
|
|
33
25
|
style,
|
|
34
26
|
ariaLabel,
|
|
35
|
-
emptyText =
|
|
27
|
+
emptyText = "暂无数据"
|
|
36
28
|
}) {
|
|
29
|
+
var _a;
|
|
37
30
|
const theme = useChartTheme(themeInput, palette);
|
|
38
|
-
const {
|
|
39
|
-
ref,
|
|
40
|
-
size
|
|
41
|
-
} = useElementSize();
|
|
31
|
+
const { ref, size } = useElementSize();
|
|
42
32
|
const [activeIndex, setActiveIndex] = useState(null);
|
|
43
|
-
const items = useMemo(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
33
|
+
const items = useMemo(
|
|
34
|
+
() => data.map((item, index) => ({
|
|
35
|
+
...item,
|
|
36
|
+
color: item.color ?? theme.palette[index % theme.palette.length],
|
|
37
|
+
index
|
|
38
|
+
})),
|
|
39
|
+
[data, theme.palette]
|
|
40
|
+
);
|
|
41
|
+
const maxValue = useMemo(
|
|
42
|
+
() => Math.max(...items.map((item) => item.value), 1),
|
|
43
|
+
[items]
|
|
44
|
+
);
|
|
45
|
+
const baseValue = ((_a = items[0]) == null ? void 0 : _a.value) ?? 1;
|
|
46
|
+
const animKey = typeof animation === "object" ? JSON.stringify(animation) : String(animation);
|
|
47
|
+
const animConfig = useMemo(
|
|
48
|
+
() => resolveAnimation(animation),
|
|
49
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
50
|
+
[animKey]
|
|
51
|
+
);
|
|
52
|
+
const valueMatrix = useMemo(() => [items.map((item) => item.value)], [items]);
|
|
55
53
|
const transition = useChartTransition(valueMatrix, animConfig);
|
|
56
54
|
const progress = transition.progress;
|
|
57
55
|
const scene = useMemo(() => {
|
|
58
|
-
if (!items.length || size.width <= 0 || size.height <= 0)
|
|
56
|
+
if (!items.length || size.width <= 0 || size.height <= 0)
|
|
57
|
+
return [];
|
|
59
58
|
const nodes = [];
|
|
60
59
|
const count = items.length;
|
|
61
60
|
const slot = (size.height - gap * (count - 1)) / count;
|
|
62
|
-
|
|
63
|
-
const labelSpace = label === 'right' ? Math.min(160, size.width * 0.32) : 0;
|
|
61
|
+
const labelSpace = label === "right" ? Math.min(160, size.width * 0.32) : 0;
|
|
64
62
|
const maxWidth = Math.max(40, (size.width - labelSpace) * 0.82);
|
|
65
63
|
const cx = (size.width - labelSpace) / 2;
|
|
66
|
-
|
|
67
|
-
// 每层按自己的局部进度展开,整体呈「从上往下倾泻」的节奏
|
|
68
64
|
const widthOf = (value, local) => Math.max(4, value / maxValue * maxWidth * local);
|
|
69
65
|
items.forEach((item, index) => {
|
|
66
|
+
var _a2, _b, _c;
|
|
70
67
|
const local = staggerProgress(progress, index, count, animConfig.stagger);
|
|
71
|
-
const animatedValue = transition.values[0]
|
|
68
|
+
const animatedValue = ((_a2 = transition.values[0]) == null ? void 0 : _a2[index]) ?? item.value;
|
|
72
69
|
const topWidth = widthOf(animatedValue, local);
|
|
73
|
-
|
|
74
|
-
const nextValue = transition.values[0]?.[index + 1] ?? items[index + 1]?.value ?? 0;
|
|
70
|
+
const nextValue = ((_b = transition.values[0]) == null ? void 0 : _b[index + 1]) ?? ((_c = items[index + 1]) == null ? void 0 : _c.value) ?? 0;
|
|
75
71
|
const nextWidth = index < count - 1 ? widthOf(nextValue, local) : topWidth * 0.6;
|
|
76
72
|
const y = index * (slot + gap);
|
|
77
73
|
const isActive = activeIndex === index;
|
|
@@ -80,34 +76,33 @@ export function FunnelChart({
|
|
|
80
76
|
const x3 = cx + nextWidth / 2;
|
|
81
77
|
const x4 = cx - nextWidth / 2;
|
|
82
78
|
nodes.push({
|
|
83
|
-
kind:
|
|
79
|
+
kind: "path",
|
|
84
80
|
key: `funnel-${index}`,
|
|
85
81
|
d: `M${x1},${y}L${x2},${y}L${x3},${y + slot}L${x4},${y + slot}Z`,
|
|
86
82
|
fill: item.color,
|
|
87
83
|
opacity: activeIndex === null || isActive ? 0.92 : 0.5,
|
|
88
|
-
pointerEvents:
|
|
84
|
+
pointerEvents: "none"
|
|
89
85
|
});
|
|
90
|
-
if (label ===
|
|
91
|
-
// 文字放在梯形上沿,宽度不足时跳过,避免文字溢出图形
|
|
86
|
+
if (label === "inside") {
|
|
92
87
|
if (topWidth > 90 && slot > 26) {
|
|
93
88
|
nodes.push({
|
|
94
|
-
kind:
|
|
89
|
+
kind: "text",
|
|
95
90
|
key: `inside-${index}`,
|
|
96
91
|
x: cx,
|
|
97
92
|
y: y + slot / 2,
|
|
98
93
|
text: `${item.name} ${formatNumber(item.value)}`,
|
|
99
|
-
fill:
|
|
94
|
+
fill: "#fff",
|
|
100
95
|
fontSize: theme.font.size,
|
|
101
96
|
fontWeight: theme.font.weightMedium,
|
|
102
|
-
anchor:
|
|
103
|
-
baseline:
|
|
97
|
+
anchor: "middle",
|
|
98
|
+
baseline: "middle",
|
|
104
99
|
opacity: progress
|
|
105
100
|
});
|
|
106
101
|
}
|
|
107
102
|
}
|
|
108
|
-
if (label ===
|
|
103
|
+
if (label === "right") {
|
|
109
104
|
nodes.push({
|
|
110
|
-
kind:
|
|
105
|
+
kind: "line",
|
|
111
106
|
key: `leader-${index}`,
|
|
112
107
|
x1: cx + topWidth / 2,
|
|
113
108
|
y1: y + slot / 2,
|
|
@@ -115,86 +110,116 @@ export function FunnelChart({
|
|
|
115
110
|
y2: y + slot / 2,
|
|
116
111
|
stroke: withAlpha(theme.axis.line, 0.9),
|
|
117
112
|
strokeWidth: 1,
|
|
118
|
-
pointerEvents:
|
|
113
|
+
pointerEvents: "none"
|
|
119
114
|
});
|
|
120
115
|
nodes.push({
|
|
121
|
-
kind:
|
|
116
|
+
kind: "text",
|
|
122
117
|
key: `label-${index}`,
|
|
123
118
|
x: cx + maxWidth / 2 + 14,
|
|
124
119
|
y: y + slot / 2,
|
|
125
120
|
text: `${item.name}`,
|
|
126
121
|
fill: theme.text.primary,
|
|
127
122
|
fontSize: theme.font.size,
|
|
128
|
-
anchor:
|
|
129
|
-
baseline:
|
|
123
|
+
anchor: "start",
|
|
124
|
+
baseline: "middle"
|
|
130
125
|
});
|
|
131
126
|
nodes.push({
|
|
132
|
-
kind:
|
|
127
|
+
kind: "text",
|
|
133
128
|
key: `value-${index}`,
|
|
134
129
|
x: cx + maxWidth / 2 + 14,
|
|
135
130
|
y: y + slot / 2 + theme.font.size + 2,
|
|
136
131
|
text: `${formatNumber(item.value)} · ${formatPercent(item.value, baseValue, 1)}`,
|
|
137
132
|
fill: theme.text.secondary,
|
|
138
133
|
fontSize: theme.font.sizeSmall,
|
|
139
|
-
anchor:
|
|
140
|
-
baseline:
|
|
134
|
+
anchor: "start",
|
|
135
|
+
baseline: "middle"
|
|
141
136
|
});
|
|
142
137
|
}
|
|
143
138
|
});
|
|
144
139
|
return nodes;
|
|
145
|
-
}, [
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
140
|
+
}, [
|
|
141
|
+
items,
|
|
142
|
+
size.width,
|
|
143
|
+
size.height,
|
|
144
|
+
gap,
|
|
145
|
+
label,
|
|
146
|
+
maxValue,
|
|
147
|
+
baseValue,
|
|
148
|
+
progress,
|
|
149
|
+
theme,
|
|
150
|
+
activeIndex,
|
|
151
|
+
transition.values,
|
|
152
|
+
animConfig.stagger
|
|
153
|
+
]);
|
|
154
|
+
const indexFromPointer = useCallback(
|
|
155
|
+
(clientY) => {
|
|
156
|
+
const element = ref.current;
|
|
157
|
+
if (!element || !items.length)
|
|
158
|
+
return null;
|
|
159
|
+
const rect = element.getBoundingClientRect();
|
|
160
|
+
const localY = clientY - rect.top;
|
|
161
|
+
const slot = (size.height - gap * (items.length - 1)) / items.length;
|
|
162
|
+
const index = Math.floor(localY / (slot + gap));
|
|
163
|
+
return index >= 0 && index < items.length ? index : null;
|
|
164
|
+
},
|
|
165
|
+
[ref, items.length, size.height, gap]
|
|
166
|
+
);
|
|
155
167
|
const active = activeIndex !== null ? items[activeIndex] : null;
|
|
156
168
|
const tooltipPayload = active ? {
|
|
157
169
|
label: active.name,
|
|
158
170
|
row: null,
|
|
159
|
-
series: [
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
171
|
+
series: [
|
|
172
|
+
{
|
|
173
|
+
key: active.name,
|
|
174
|
+
name: active.name,
|
|
175
|
+
color: active.color,
|
|
176
|
+
value: active.value,
|
|
177
|
+
formatted: `${formatNumber(active.value)}(${formatPercent(active.value, baseValue, 1)})`
|
|
178
|
+
}
|
|
179
|
+
]
|
|
166
180
|
} : null;
|
|
167
|
-
const autoAriaLabel = ariaLabel ?? `漏斗图:${items.map(item => item.name).join(
|
|
168
|
-
return
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
height,
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
181
|
+
const autoAriaLabel = ariaLabel ?? `漏斗图:${items.map((item) => item.name).join("、")}`;
|
|
182
|
+
return /* @__PURE__ */ jsx(
|
|
183
|
+
"div",
|
|
184
|
+
{
|
|
185
|
+
className: classNames(prefixCls("chart"), className),
|
|
186
|
+
style: { width, height, ...style },
|
|
187
|
+
"data-aura-chart": "funnel",
|
|
188
|
+
children: /* @__PURE__ */ jsxs(
|
|
189
|
+
"div",
|
|
190
|
+
{
|
|
191
|
+
ref,
|
|
192
|
+
className: prefixCls("chart__plot"),
|
|
193
|
+
onPointerMove: (event) => setActiveIndex(indexFromPointer(event.clientY)),
|
|
194
|
+
onPointerLeave: () => setActiveIndex(null),
|
|
195
|
+
children: [
|
|
196
|
+
items.length === 0 || size.width === 0 ? /* @__PURE__ */ jsx("div", { className: prefixCls("chart__empty"), children: emptyText }) : /* @__PURE__ */ jsx(
|
|
197
|
+
SvgScene,
|
|
198
|
+
{
|
|
199
|
+
nodes: scene,
|
|
200
|
+
width: size.width,
|
|
201
|
+
height: size.height,
|
|
202
|
+
ariaLabel: autoAriaLabel
|
|
203
|
+
}
|
|
204
|
+
),
|
|
205
|
+
/* @__PURE__ */ jsx(
|
|
206
|
+
Tooltip,
|
|
207
|
+
{
|
|
208
|
+
payload: tooltipPayload,
|
|
209
|
+
x: size.width / 2,
|
|
210
|
+
y: activeIndex !== null ? size.height / items.length * activeIndex + 4 : 0,
|
|
211
|
+
containerWidth: size.width,
|
|
212
|
+
containerHeight: size.height,
|
|
213
|
+
theme,
|
|
214
|
+
config: tooltip
|
|
215
|
+
}
|
|
216
|
+
)
|
|
217
|
+
]
|
|
218
|
+
}
|
|
219
|
+
)
|
|
220
|
+
}
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
export {
|
|
224
|
+
FunnelChart
|
|
225
|
+
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
import {
|
|
2
|
+
// src/charts/gauge-chart/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 { formatNumber } from "../../core/format";
|
|
@@ -9,27 +10,19 @@ import { useChartTheme } from "../../hooks/useChartTheme";
|
|
|
9
10
|
import { useElementSize } from "../../hooks/useElementSize";
|
|
10
11
|
import { useChartTransition } from "../../hooks/useChartTransition";
|
|
11
12
|
import { resolveAnimation } from "../../core/animation";
|
|
12
|
-
import { jsx
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 仪表盘。
|
|
17
|
-
*
|
|
18
|
-
* 只表达「一个值在量程中的位置」,不承载多指标比较。
|
|
19
|
-
* 数值以外不加指针:现代仪表盘用填充弧表达进度,指针图形的可读性反而更差
|
|
20
|
-
* (细长指针在 240° 弧上要判断角度,误差远大于判断弧长)。
|
|
21
|
-
*/
|
|
22
|
-
export function GaugeChart({
|
|
13
|
+
import { jsx } from "react/jsx-runtime";
|
|
14
|
+
var DEG = Math.PI / 180;
|
|
15
|
+
function GaugeChart({
|
|
23
16
|
value,
|
|
24
17
|
min = 0,
|
|
25
18
|
max = 100,
|
|
26
|
-
unit =
|
|
19
|
+
unit = "",
|
|
27
20
|
label,
|
|
28
21
|
ranges,
|
|
29
22
|
thickness = 0.14,
|
|
30
23
|
startAngle = 150,
|
|
31
24
|
sweepAngle = 240,
|
|
32
|
-
width =
|
|
25
|
+
width = "100%",
|
|
33
26
|
height = 260,
|
|
34
27
|
theme: themeInput,
|
|
35
28
|
palette,
|
|
@@ -38,34 +31,33 @@ export function GaugeChart({
|
|
|
38
31
|
style,
|
|
39
32
|
ariaLabel
|
|
40
33
|
}) {
|
|
34
|
+
var _a;
|
|
41
35
|
const theme = useChartTheme(themeInput, palette);
|
|
42
|
-
const {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
[animKey]);
|
|
50
|
-
|
|
51
|
-
// 数值做插值:更新时弧长平滑滑到新位置,中心数字同时滚动
|
|
36
|
+
const { ref, size } = useElementSize();
|
|
37
|
+
const animKey = typeof animation === "object" ? JSON.stringify(animation) : String(animation);
|
|
38
|
+
const animConfig = useMemo(
|
|
39
|
+
() => resolveAnimation(animation),
|
|
40
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
41
|
+
[animKey]
|
|
42
|
+
);
|
|
52
43
|
const transition = useChartTransition([[value]], animConfig);
|
|
53
44
|
const progress = transition.progress;
|
|
54
|
-
const animatedValue = transition.values[0]
|
|
45
|
+
const animatedValue = ((_a = transition.values[0]) == null ? void 0 : _a[0]) ?? value;
|
|
55
46
|
const ratio = useMemo(() => {
|
|
56
|
-
if (max === min)
|
|
47
|
+
if (max === min)
|
|
48
|
+
return 0;
|
|
57
49
|
return Math.min(1, Math.max(0, (animatedValue - min) / (max - min)));
|
|
58
50
|
}, [animatedValue, min, max]);
|
|
59
51
|
const color = useMemo(() => {
|
|
60
|
-
if (ranges
|
|
61
|
-
const hit = ranges.find(range => value <= range.to);
|
|
62
|
-
if (hit)
|
|
52
|
+
if (ranges == null ? void 0 : ranges.length) {
|
|
53
|
+
const hit = ranges.find((range) => value <= range.to);
|
|
54
|
+
if (hit)
|
|
55
|
+
return hit.color;
|
|
63
56
|
return ranges[ranges.length - 1].color;
|
|
64
57
|
}
|
|
65
58
|
return theme.emphasis;
|
|
66
59
|
}, [ranges, value, theme.emphasis]);
|
|
67
60
|
const geometry = useMemo(() => {
|
|
68
|
-
// 240° 弧在竖直方向的占用约为 0.87R,据此反推半径,避免上下裁切
|
|
69
61
|
const outer = Math.max(0, Math.min(size.width / 2, size.height / 1.72)) - 6;
|
|
70
62
|
const ring = Math.max(6, outer * thickness);
|
|
71
63
|
return {
|
|
@@ -79,51 +71,39 @@ export function GaugeChart({
|
|
|
79
71
|
const start = startAngle * DEG;
|
|
80
72
|
const sweep = sweepAngle * DEG;
|
|
81
73
|
const scene = useMemo(() => {
|
|
82
|
-
const {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
outer,
|
|
86
|
-
inner,
|
|
87
|
-
thickness: ring
|
|
88
|
-
} = geometry;
|
|
89
|
-
if (outer <= 0) return [];
|
|
74
|
+
const { cx, cy, outer, inner, thickness: ring } = geometry;
|
|
75
|
+
if (outer <= 0)
|
|
76
|
+
return [];
|
|
90
77
|
const nodes = [];
|
|
91
|
-
|
|
92
|
-
// 轨道
|
|
93
78
|
nodes.push({
|
|
94
|
-
kind:
|
|
95
|
-
key:
|
|
79
|
+
kind: "path",
|
|
80
|
+
key: "track",
|
|
96
81
|
d: arcLinePath(cx, cy, (inner + outer) / 2, start, start + sweep),
|
|
97
|
-
fill:
|
|
82
|
+
fill: "none",
|
|
98
83
|
stroke: withAlpha(theme.axis.line, 0.55),
|
|
99
84
|
strokeWidth: ring,
|
|
100
|
-
linecap:
|
|
101
|
-
pointerEvents:
|
|
85
|
+
linecap: "round",
|
|
86
|
+
pointerEvents: "none"
|
|
102
87
|
});
|
|
103
|
-
|
|
104
|
-
// 进度弧
|
|
105
88
|
const end = start + sweep * ratio * progress;
|
|
106
|
-
if (end > start +
|
|
89
|
+
if (end > start + 1e-3) {
|
|
107
90
|
nodes.push({
|
|
108
|
-
kind:
|
|
109
|
-
key:
|
|
91
|
+
kind: "path",
|
|
92
|
+
key: "value-arc",
|
|
110
93
|
d: arcLinePath(cx, cy, (inner + outer) / 2, start, end),
|
|
111
|
-
fill:
|
|
94
|
+
fill: "none",
|
|
112
95
|
stroke: color,
|
|
113
96
|
strokeWidth: ring,
|
|
114
|
-
linecap:
|
|
115
|
-
pointerEvents:
|
|
97
|
+
linecap: "round",
|
|
98
|
+
pointerEvents: "none"
|
|
116
99
|
});
|
|
117
100
|
}
|
|
118
|
-
|
|
119
|
-
// 刻度:11 条短线,每 10% 一条
|
|
120
|
-
// 刻度落在环外侧,长度取环宽的 45%,太短会淹没在环的边缘里
|
|
121
101
|
const tickOuter = outer + ring * 1.05;
|
|
122
102
|
const tickInner = outer + ring * 0.6;
|
|
123
103
|
for (let index = 0; index <= 10; index += 1) {
|
|
124
104
|
const angle = start + sweep * index / 10;
|
|
125
105
|
nodes.push({
|
|
126
|
-
kind:
|
|
106
|
+
kind: "line",
|
|
127
107
|
key: `tick-${index}`,
|
|
128
108
|
x1: cx + Math.cos(angle) * tickInner,
|
|
129
109
|
y1: cy + Math.sin(angle) * tickInner,
|
|
@@ -132,86 +112,103 @@ export function GaugeChart({
|
|
|
132
112
|
stroke: index / 10 <= ratio ? color : theme.axis.tick,
|
|
133
113
|
strokeWidth: index % 5 === 0 ? 2 : 1,
|
|
134
114
|
opacity: index / 10 <= ratio ? 0.9 : 0.5,
|
|
135
|
-
pointerEvents:
|
|
115
|
+
pointerEvents: "none"
|
|
136
116
|
});
|
|
137
117
|
}
|
|
138
|
-
|
|
139
|
-
// 量程端点数值
|
|
140
|
-
// 端点标签按容器边界收敛:小卡片里「100」这类标签溢出会被裁成「10C」
|
|
141
|
-
const clampToCard = (value, max) => Math.min(Math.max(value, 10), Math.max(10, max - 10));
|
|
118
|
+
const clampToCard = (value2, max2) => Math.min(Math.max(value2, 10), Math.max(10, max2 - 10));
|
|
142
119
|
nodes.push({
|
|
143
|
-
kind:
|
|
144
|
-
key:
|
|
120
|
+
kind: "text",
|
|
121
|
+
key: "min-label",
|
|
145
122
|
x: clampToCard(cx + Math.cos(start) * (outer + ring * 0.9), size.width),
|
|
146
123
|
y: clampToCard(cy + Math.sin(start) * (outer + ring * 0.9), size.height),
|
|
147
124
|
text: formatNumber(min),
|
|
148
125
|
fill: theme.text.secondary,
|
|
149
126
|
fontSize: theme.font.sizeSmall,
|
|
150
|
-
anchor:
|
|
151
|
-
baseline:
|
|
127
|
+
anchor: "middle",
|
|
128
|
+
baseline: "middle",
|
|
152
129
|
fontFamily: theme.font.family
|
|
153
130
|
});
|
|
154
131
|
nodes.push({
|
|
155
|
-
kind:
|
|
156
|
-
key:
|
|
157
|
-
x: clampToCard(
|
|
158
|
-
|
|
132
|
+
kind: "text",
|
|
133
|
+
key: "max-label",
|
|
134
|
+
x: clampToCard(
|
|
135
|
+
cx + Math.cos(start + sweep) * (outer + ring * 0.9),
|
|
136
|
+
size.width
|
|
137
|
+
),
|
|
138
|
+
y: clampToCard(
|
|
139
|
+
cy + Math.sin(start + sweep) * (outer + ring * 0.9),
|
|
140
|
+
size.height
|
|
141
|
+
),
|
|
159
142
|
text: formatNumber(max),
|
|
160
143
|
fill: theme.text.secondary,
|
|
161
144
|
fontSize: theme.font.sizeSmall,
|
|
162
|
-
anchor:
|
|
163
|
-
baseline:
|
|
145
|
+
anchor: "middle",
|
|
146
|
+
baseline: "middle",
|
|
164
147
|
fontFamily: theme.font.family
|
|
165
148
|
});
|
|
166
|
-
|
|
167
|
-
// 中心数值
|
|
168
149
|
nodes.push({
|
|
169
|
-
kind:
|
|
170
|
-
key:
|
|
150
|
+
kind: "text",
|
|
151
|
+
key: "value",
|
|
171
152
|
x: cx,
|
|
172
153
|
y: cy - 6,
|
|
173
154
|
text: `${formatNumber(Math.round(animatedValue * 10) / 10)}${unit}`,
|
|
174
155
|
fill: theme.text.primary,
|
|
175
156
|
fontSize: Math.max(20, Math.min(34, outer * 0.3)),
|
|
176
157
|
fontWeight: theme.font.weightMedium,
|
|
177
|
-
anchor:
|
|
178
|
-
baseline:
|
|
158
|
+
anchor: "middle",
|
|
159
|
+
baseline: "middle",
|
|
179
160
|
fontFamily: theme.font.family
|
|
180
161
|
});
|
|
181
162
|
if (label) {
|
|
182
163
|
nodes.push({
|
|
183
|
-
kind:
|
|
184
|
-
key:
|
|
164
|
+
kind: "text",
|
|
165
|
+
key: "label",
|
|
185
166
|
x: cx,
|
|
186
167
|
y: cy + Math.max(20, outer * 0.16),
|
|
187
168
|
text: label,
|
|
188
169
|
fill: theme.text.secondary,
|
|
189
170
|
fontSize: theme.font.size,
|
|
190
|
-
anchor:
|
|
191
|
-
baseline:
|
|
171
|
+
anchor: "middle",
|
|
172
|
+
baseline: "middle",
|
|
192
173
|
fontFamily: theme.font.family
|
|
193
174
|
});
|
|
194
175
|
}
|
|
195
176
|
return nodes;
|
|
196
|
-
}, [
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}
|
|
177
|
+
}, [
|
|
178
|
+
geometry,
|
|
179
|
+
start,
|
|
180
|
+
sweep,
|
|
181
|
+
ratio,
|
|
182
|
+
progress,
|
|
183
|
+
color,
|
|
184
|
+
theme,
|
|
185
|
+
animatedValue,
|
|
186
|
+
unit,
|
|
187
|
+
size.width,
|
|
188
|
+
size.height,
|
|
189
|
+
label,
|
|
190
|
+
min,
|
|
191
|
+
max
|
|
192
|
+
]);
|
|
193
|
+
const autoAriaLabel = ariaLabel ?? `${label ?? "仪表盘"}:${value}${unit},量程 ${min}~${max}`;
|
|
194
|
+
return /* @__PURE__ */ jsx(
|
|
195
|
+
"div",
|
|
196
|
+
{
|
|
197
|
+
className: classNames(prefixCls("chart"), className),
|
|
198
|
+
style: { width, height, ...style },
|
|
199
|
+
"data-aura-chart": "gauge",
|
|
200
|
+
children: /* @__PURE__ */ jsx("div", { ref, className: prefixCls("chart__plot"), children: size.width > 0 ? /* @__PURE__ */ jsx(
|
|
201
|
+
SvgScene,
|
|
202
|
+
{
|
|
203
|
+
nodes: scene,
|
|
204
|
+
width: size.width,
|
|
205
|
+
height: size.height,
|
|
206
|
+
ariaLabel: autoAriaLabel
|
|
207
|
+
}
|
|
208
|
+
) : null })
|
|
209
|
+
}
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
export {
|
|
213
|
+
GaugeChart
|
|
214
|
+
};
|