@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,27 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
import {
|
|
2
|
+
// src/charts/graph-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, estimateTextWidth } from "../../core/format";
|
|
6
7
|
import { withAlpha } from "../../theme";
|
|
7
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
layoutForce
|
|
10
|
+
} from "../../core/force";
|
|
8
11
|
import { useChartTheme } from "../../hooks/useChartTheme";
|
|
9
12
|
import { useElementSize } from "../../hooks/useElementSize";
|
|
10
13
|
import { useChartTransition } from "../../hooks/useChartTransition";
|
|
11
14
|
import { resolveAnimation, staggerProgress } from "../../core/animation";
|
|
12
15
|
import { Tooltip } from "../../components/Tooltip";
|
|
13
|
-
import { jsx
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 关系图(力导向)。
|
|
17
|
-
*
|
|
18
|
-
* 节点两两相斥、有连边的相互吸引,最终收敛成「抱团」的形状:
|
|
19
|
-
* 连得紧的聚成一簇,孤立的被推到边缘。这是一种**探索**型图表——
|
|
20
|
-
* 它的价值是让你看到结构,而不是读出精确数值。
|
|
21
|
-
*/
|
|
22
|
-
export function GraphChart({
|
|
16
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
17
|
+
function GraphChart({
|
|
23
18
|
nodes = [],
|
|
24
19
|
links = [],
|
|
25
20
|
iterations = 220,
|
|
26
21
|
nodeSize = 7,
|
|
27
22
|
showLabel = true,
|
|
28
23
|
scaleByDegree = true,
|
|
29
|
-
width =
|
|
24
|
+
width = "100%",
|
|
30
25
|
height = 380,
|
|
31
26
|
theme: themeInput,
|
|
32
27
|
palette,
|
|
@@ -35,73 +30,85 @@ export function GraphChart({
|
|
|
35
30
|
className,
|
|
36
31
|
style,
|
|
37
32
|
ariaLabel,
|
|
38
|
-
emptyText =
|
|
33
|
+
emptyText = "暂无数据"
|
|
39
34
|
}) {
|
|
40
35
|
const theme = useChartTheme(themeInput, palette);
|
|
41
|
-
const {
|
|
42
|
-
ref,
|
|
43
|
-
size
|
|
44
|
-
} = useElementSize();
|
|
36
|
+
const { ref, size } = useElementSize();
|
|
45
37
|
const [activeId, setActiveId] = useState(null);
|
|
46
|
-
const animKey = typeof animation ===
|
|
47
|
-
const animConfig = useMemo(
|
|
48
|
-
|
|
49
|
-
|
|
38
|
+
const animKey = typeof animation === "object" ? JSON.stringify(animation) : String(animation);
|
|
39
|
+
const animConfig = useMemo(
|
|
40
|
+
() => resolveAnimation(animation),
|
|
41
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
42
|
+
[animKey]
|
|
43
|
+
);
|
|
50
44
|
const groups = useMemo(() => {
|
|
51
45
|
const list = [];
|
|
52
|
-
nodes.forEach(node => {
|
|
53
|
-
const group = node.group ??
|
|
54
|
-
if (!list.includes(group))
|
|
46
|
+
nodes.forEach((node) => {
|
|
47
|
+
const group = node.group ?? "__default__";
|
|
48
|
+
if (!list.includes(group))
|
|
49
|
+
list.push(group);
|
|
55
50
|
});
|
|
56
51
|
return list;
|
|
57
52
|
}, [nodes]);
|
|
58
53
|
const positions = useMemo(() => {
|
|
59
|
-
if (!nodes.length || size.width <= 0)
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
height: size.height
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
radius: nodeSize + 4
|
|
68
|
-
});
|
|
54
|
+
if (!nodes.length || size.width <= 0)
|
|
55
|
+
return [];
|
|
56
|
+
return layoutForce(
|
|
57
|
+
nodes,
|
|
58
|
+
links,
|
|
59
|
+
{ x: 0, y: 0, width: size.width, height: size.height },
|
|
60
|
+
{ iterations, radius: nodeSize + 4 }
|
|
61
|
+
);
|
|
69
62
|
}, [nodes, links, size.width, size.height, iterations, nodeSize]);
|
|
70
|
-
const positionMap = useMemo(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
63
|
+
const positionMap = useMemo(
|
|
64
|
+
() => new Map(positions.map((item) => [item.id, item])),
|
|
65
|
+
[positions]
|
|
66
|
+
);
|
|
67
|
+
const maxDegree = Math.max(1, ...positions.map((item) => item.degree));
|
|
68
|
+
const radiusOf = useCallback(
|
|
69
|
+
(position) => {
|
|
70
|
+
if (!scaleByDegree)
|
|
71
|
+
return nodeSize;
|
|
72
|
+
return nodeSize * (1 + Math.min(1.2, position.degree / maxDegree) * 1.2);
|
|
73
|
+
},
|
|
74
|
+
[scaleByDegree, nodeSize, maxDegree]
|
|
75
|
+
);
|
|
76
|
+
const colorOf = useCallback(
|
|
77
|
+
(node) => {
|
|
78
|
+
const groupIndex = groups.indexOf(node.group ?? "__default__");
|
|
79
|
+
return theme.palette[Math.max(0, groupIndex) % theme.palette.length];
|
|
80
|
+
},
|
|
81
|
+
[groups, theme.palette]
|
|
82
|
+
);
|
|
83
|
+
const degreeMatrix = useMemo(
|
|
84
|
+
() => [positions.map((item) => item.degree)],
|
|
85
|
+
[positions]
|
|
86
|
+
);
|
|
82
87
|
const progress = useChartTransition(degreeMatrix, animConfig).progress;
|
|
83
88
|
const neighbors = useMemo(() => {
|
|
84
|
-
const map = new Map();
|
|
89
|
+
const map = /* @__PURE__ */ new Map();
|
|
85
90
|
for (const link of links) {
|
|
86
|
-
if (!map.has(link.source))
|
|
87
|
-
|
|
91
|
+
if (!map.has(link.source))
|
|
92
|
+
map.set(link.source, /* @__PURE__ */ new Set());
|
|
93
|
+
if (!map.has(link.target))
|
|
94
|
+
map.set(link.target, /* @__PURE__ */ new Set());
|
|
88
95
|
map.get(link.source).add(link.target);
|
|
89
96
|
map.get(link.target).add(link.source);
|
|
90
97
|
}
|
|
91
98
|
return map;
|
|
92
99
|
}, [links]);
|
|
93
100
|
const scene = useMemo(() => {
|
|
94
|
-
if (!positions.length || size.width <= 0)
|
|
101
|
+
if (!positions.length || size.width <= 0)
|
|
102
|
+
return [];
|
|
95
103
|
const nodes_ = [];
|
|
96
|
-
|
|
97
|
-
// 边
|
|
98
104
|
links.forEach((link, index) => {
|
|
99
105
|
const from = positionMap.get(link.source);
|
|
100
106
|
const to = positionMap.get(link.target);
|
|
101
|
-
if (!from || !to)
|
|
107
|
+
if (!from || !to)
|
|
108
|
+
return;
|
|
102
109
|
const related = activeId === null || activeId === link.source || activeId === link.target;
|
|
103
110
|
nodes_.push({
|
|
104
|
-
kind:
|
|
111
|
+
kind: "line",
|
|
105
112
|
key: `edge-${index}`,
|
|
106
113
|
x1: from.x,
|
|
107
114
|
y1: from.y,
|
|
@@ -110,56 +117,76 @@ export function GraphChart({
|
|
|
110
117
|
stroke: related && activeId !== null ? theme.emphasis : withAlpha(theme.axis.line, 0.9),
|
|
111
118
|
strokeWidth: related && activeId !== null ? 1.6 : 1,
|
|
112
119
|
opacity: (related ? 1 : 0.25) * progress,
|
|
113
|
-
pointerEvents:
|
|
120
|
+
pointerEvents: "none"
|
|
114
121
|
});
|
|
115
122
|
});
|
|
116
|
-
|
|
117
|
-
// 节点
|
|
118
123
|
positions.forEach((position, index) => {
|
|
119
|
-
|
|
120
|
-
|
|
124
|
+
var _a;
|
|
125
|
+
const node = nodes.find((item) => item.id === position.id);
|
|
126
|
+
if (!node)
|
|
127
|
+
return;
|
|
121
128
|
const baseRadius = radiusOf(position);
|
|
122
|
-
const local = staggerProgress(
|
|
129
|
+
const local = staggerProgress(
|
|
130
|
+
progress,
|
|
131
|
+
index,
|
|
132
|
+
positions.length,
|
|
133
|
+
animConfig.stagger
|
|
134
|
+
);
|
|
123
135
|
const radius = baseRadius * local;
|
|
124
136
|
const isActive = activeId === position.id;
|
|
125
|
-
const isNeighbor = activeId ? neighbors.get(activeId)
|
|
137
|
+
const isNeighbor = activeId ? ((_a = neighbors.get(activeId)) == null ? void 0 : _a.has(position.id)) ?? false : false;
|
|
126
138
|
const dim = activeId !== null && !isActive && !isNeighbor;
|
|
127
139
|
nodes_.push({
|
|
128
|
-
kind:
|
|
140
|
+
kind: "circle",
|
|
129
141
|
key: `node-${position.id}`,
|
|
130
142
|
cx: position.x,
|
|
131
143
|
cy: position.y,
|
|
132
144
|
r: Math.max(0.5, radius),
|
|
133
145
|
fill: colorOf(node),
|
|
134
146
|
opacity: dim ? 0.28 : 1,
|
|
135
|
-
stroke: isActive ? theme.text.primary :
|
|
147
|
+
stroke: isActive ? theme.text.primary : void 0,
|
|
136
148
|
strokeWidth: isActive ? 2 : 0,
|
|
137
|
-
pointerEvents:
|
|
149
|
+
pointerEvents: "none"
|
|
138
150
|
});
|
|
139
|
-
if (!showLabel || radius < 4)
|
|
151
|
+
if (!showLabel || radius < 4)
|
|
152
|
+
return;
|
|
140
153
|
const label = node.name ?? node.id;
|
|
141
|
-
// 标签最多占 20% 画布宽度,超了就截断
|
|
142
154
|
const maxWidth = size.width * 0.2;
|
|
143
155
|
const text = estimateTextWidth(label, theme.font.sizeSmall) > maxWidth ? `${label.slice(0, Math.max(2, Math.floor(maxWidth / theme.font.sizeSmall)))}…` : label;
|
|
144
156
|
nodes_.push({
|
|
145
|
-
kind:
|
|
157
|
+
kind: "text",
|
|
146
158
|
key: `label-${position.id}`,
|
|
147
159
|
x: position.x,
|
|
148
160
|
y: position.y + radius + theme.font.sizeSmall + 2,
|
|
149
161
|
text,
|
|
150
162
|
fill: theme.text.secondary,
|
|
151
163
|
fontSize: theme.font.sizeSmall,
|
|
152
|
-
anchor:
|
|
153
|
-
baseline:
|
|
164
|
+
anchor: "middle",
|
|
165
|
+
baseline: "middle",
|
|
154
166
|
opacity: dim ? 0.3 : local,
|
|
155
167
|
fontFamily: theme.font.family
|
|
156
168
|
});
|
|
157
169
|
});
|
|
158
170
|
return nodes_;
|
|
159
|
-
}, [
|
|
171
|
+
}, [
|
|
172
|
+
positions,
|
|
173
|
+
links,
|
|
174
|
+
positionMap,
|
|
175
|
+
nodes,
|
|
176
|
+
theme,
|
|
177
|
+
activeId,
|
|
178
|
+
neighbors,
|
|
179
|
+
progress,
|
|
180
|
+
animConfig.stagger,
|
|
181
|
+
showLabel,
|
|
182
|
+
size.width,
|
|
183
|
+
radiusOf,
|
|
184
|
+
colorOf
|
|
185
|
+
]);
|
|
160
186
|
const nodeFromPointer = (clientX, clientY) => {
|
|
161
187
|
const element = ref.current;
|
|
162
|
-
if (!element)
|
|
188
|
+
if (!element)
|
|
189
|
+
return null;
|
|
163
190
|
const rect = element.getBoundingClientRect();
|
|
164
191
|
const x = clientX - rect.left;
|
|
165
192
|
const y = clientY - rect.top;
|
|
@@ -175,50 +202,63 @@ export function GraphChart({
|
|
|
175
202
|
}
|
|
176
203
|
return nearest;
|
|
177
204
|
};
|
|
178
|
-
const activeNode = activeId ? nodes.find(node => node.id === activeId) : null;
|
|
205
|
+
const activeNode = activeId ? nodes.find((node) => node.id === activeId) : null;
|
|
179
206
|
const activePosition = activeId ? positionMap.get(activeId) : null;
|
|
180
207
|
const tooltipPayload = activeNode ? {
|
|
181
208
|
label: activeNode.name ?? activeNode.id,
|
|
182
209
|
row: null,
|
|
183
|
-
series: [
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
210
|
+
series: [
|
|
211
|
+
{
|
|
212
|
+
key: "degree",
|
|
213
|
+
name: "连接数",
|
|
214
|
+
color: colorOf(activeNode),
|
|
215
|
+
value: (activePosition == null ? void 0 : activePosition.degree) ?? 0,
|
|
216
|
+
formatted: formatNumber((activePosition == null ? void 0 : activePosition.degree) ?? 0)
|
|
217
|
+
}
|
|
218
|
+
]
|
|
190
219
|
} : null;
|
|
191
220
|
const autoAriaLabel = ariaLabel ?? `关系图:${nodes.length} 个节点,${links.length} 条关系`;
|
|
192
|
-
return
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
height,
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
221
|
+
return /* @__PURE__ */ jsx(
|
|
222
|
+
"div",
|
|
223
|
+
{
|
|
224
|
+
className: classNames(prefixCls("chart"), className),
|
|
225
|
+
style: { width, height, ...style },
|
|
226
|
+
"data-aura-chart": "graph",
|
|
227
|
+
children: /* @__PURE__ */ jsxs(
|
|
228
|
+
"div",
|
|
229
|
+
{
|
|
230
|
+
ref,
|
|
231
|
+
className: prefixCls("chart__plot"),
|
|
232
|
+
onPointerMove: (event) => setActiveId(nodeFromPointer(event.clientX, event.clientY)),
|
|
233
|
+
onPointerLeave: () => setActiveId(null),
|
|
234
|
+
children: [
|
|
235
|
+
!positions.length || size.width === 0 ? /* @__PURE__ */ jsx("div", { className: prefixCls("chart__empty"), children: emptyText }) : /* @__PURE__ */ jsx(
|
|
236
|
+
SvgScene,
|
|
237
|
+
{
|
|
238
|
+
nodes: scene,
|
|
239
|
+
width: size.width,
|
|
240
|
+
height: size.height,
|
|
241
|
+
ariaLabel: autoAriaLabel
|
|
242
|
+
}
|
|
243
|
+
),
|
|
244
|
+
/* @__PURE__ */ jsx(
|
|
245
|
+
Tooltip,
|
|
246
|
+
{
|
|
247
|
+
payload: tooltipPayload,
|
|
248
|
+
x: activePosition ? activePosition.x : 0,
|
|
249
|
+
y: activePosition ? activePosition.y - 20 : 0,
|
|
250
|
+
containerWidth: size.width,
|
|
251
|
+
containerHeight: size.height,
|
|
252
|
+
theme,
|
|
253
|
+
config: tooltip
|
|
254
|
+
}
|
|
255
|
+
)
|
|
256
|
+
]
|
|
257
|
+
}
|
|
258
|
+
)
|
|
259
|
+
}
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
export {
|
|
263
|
+
GraphChart
|
|
264
|
+
};
|