@aura-react-comp/charts 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/charts/area-chart/index.d.ts +2 -2
- package/esm/charts/area-chart/index.js +28 -23
- package/esm/charts/bar-chart/index.d.ts +1 -1
- package/esm/charts/bar-chart/index.js +18 -21
- package/esm/charts/candlestick-chart/index.d.ts +2 -2
- package/esm/charts/candlestick-chart/index.js +243 -180
- package/esm/charts/combo-chart/index.d.ts +1 -1
- package/esm/charts/combo-chart/index.js +9 -20
- package/esm/charts/funnel-chart/index.d.ts +2 -2
- package/esm/charts/funnel-chart/index.js +134 -108
- package/esm/charts/gauge-chart/index.d.ts +1 -1
- package/esm/charts/gauge-chart/index.js +103 -105
- package/esm/charts/graph-chart/index.d.ts +3 -3
- package/esm/charts/graph-chart/index.js +155 -114
- package/esm/charts/heatmap-chart/index.d.ts +2 -2
- package/esm/charts/heatmap-chart/index.js +180 -135
- package/esm/charts/line-chart/index.d.ts +2 -2
- package/esm/charts/line-chart/index.js +29 -24
- package/esm/charts/pie-chart/index.d.ts +2 -2
- package/esm/charts/pie-chart/index.d.ts.map +1 -1
- package/esm/charts/pie-chart/index.js +199 -139
- package/esm/charts/radar-chart/index.d.ts +2 -2
- package/esm/charts/radar-chart/index.d.ts.map +1 -1
- package/esm/charts/radar-chart/index.js +155 -128
- package/esm/charts/ring-progress/index.d.ts +1 -1
- package/esm/charts/ring-progress/index.js +82 -82
- package/esm/charts/sankey-chart/index.d.ts +3 -3
- package/esm/charts/sankey-chart/index.js +172 -141
- package/esm/charts/scatter-chart/index.d.ts +3 -3
- package/esm/charts/scatter-chart/index.d.ts.map +1 -1
- package/esm/charts/scatter-chart/index.js +226 -159
- package/esm/charts/sparkline/index.d.ts +1 -1
- package/esm/charts/sparkline/index.js +82 -66
- package/esm/charts/tree-chart/index.d.ts +3 -3
- package/esm/charts/tree-chart/index.js +129 -102
- package/esm/charts/treemap-chart/index.d.ts +3 -3
- package/esm/charts/treemap-chart/index.js +163 -141
- package/esm/charts/waterfall-chart/index.d.ts +2 -2
- package/esm/charts/waterfall-chart/index.js +166 -121
- package/esm/components/Legend.d.ts +5 -4
- package/esm/components/Legend.d.ts.map +1 -1
- package/esm/components/Legend.js +80 -48
- package/esm/components/Tooltip.d.ts +2 -2
- package/esm/components/Tooltip.js +63 -71
- package/esm/components/chart/index.d.ts +5 -2
- package/esm/components/chart/index.d.ts.map +1 -1
- package/esm/components/chart/index.js +81 -102
- package/esm/components/chart.less +13 -0
- package/esm/core/animation.js +32 -43
- package/esm/core/export.d.ts +34 -0
- package/esm/core/export.d.ts.map +1 -0
- package/esm/core/export.js +390 -0
- package/esm/core/force.js +23 -45
- package/esm/core/format.js +49 -52
- package/esm/core/hierarchy.d.ts +1 -1
- package/esm/core/hierarchy.js +61 -92
- package/esm/core/layout.d.ts +1 -1
- package/esm/core/layout.js +33 -50
- package/esm/core/sampling.js +31 -53
- package/esm/core/sankey.js +36 -55
- package/esm/core/scale.js +73 -86
- package/esm/core/shape.js +102 -133
- package/esm/core/sync.d.ts +41 -0
- package/esm/core/sync.d.ts.map +1 -0
- package/esm/core/sync.js +120 -0
- package/esm/hooks/useChartExport.d.ts +19 -0
- package/esm/hooks/useChartExport.d.ts.map +1 -0
- package/esm/hooks/useChartExport.js +35 -0
- package/esm/hooks/useChartTheme.d.ts +6 -5
- package/esm/hooks/useChartTheme.d.ts.map +1 -1
- package/esm/hooks/useChartTheme.js +18 -18
- package/esm/hooks/useChartTransition.d.ts +1 -1
- package/esm/hooks/useChartTransition.js +59 -73
- package/esm/hooks/useElementSize.js +20 -28
- package/esm/hooks/usePlotPointer.d.ts +23 -0
- package/esm/hooks/usePlotPointer.d.ts.map +1 -0
- package/esm/hooks/usePlotPointer.js +94 -0
- package/esm/index.d.ts +56 -50
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +137 -47
- package/esm/internal/CartesianChart.d.ts +1 -1
- package/esm/internal/CartesianChart.d.ts.map +1 -1
- package/esm/internal/CartesianChart.js +667 -485
- package/esm/internal/model.d.ts +2 -2
- package/esm/internal/model.js +45 -60
- package/esm/scene/canvas.d.ts +1 -1
- package/esm/scene/canvas.js +101 -107
- package/esm/scene/svg.d.ts +1 -1
- package/esm/scene/svg.js +107 -99
- package/esm/scene/types.js +9 -15
- package/esm/style.css +10 -0
- package/esm/theme/index.d.ts +2 -2
- package/esm/theme/index.d.ts.map +1 -1
- package/esm/theme/index.js +145 -170
- package/esm/types.d.ts +7 -3
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js +1 -1
- package/package.json +2 -2
|
@@ -1,31 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
// src/charts/graph-chart/index.tsx
|
|
3
|
+
import { useCallback, useMemo, useState } from "react";
|
|
4
|
+
import { classNames, prefixCls } from "@aura-react-comp/shared";
|
|
3
5
|
import { SvgScene } from "../../scene/svg";
|
|
4
6
|
import { formatNumber, estimateTextWidth } from "../../core/format";
|
|
5
7
|
import { withAlpha } from "../../theme";
|
|
6
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
layoutForce
|
|
10
|
+
} from "../../core/force";
|
|
7
11
|
import { useChartTheme } from "../../hooks/useChartTheme";
|
|
8
12
|
import { useElementSize } from "../../hooks/useElementSize";
|
|
9
13
|
import { useChartTransition } from "../../hooks/useChartTransition";
|
|
10
14
|
import { resolveAnimation, staggerProgress } from "../../core/animation";
|
|
11
15
|
import { Tooltip } from "../../components/Tooltip";
|
|
12
|
-
import { jsx
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 关系图(力导向)。
|
|
16
|
-
*
|
|
17
|
-
* 节点两两相斥、有连边的相互吸引,最终收敛成「抱团」的形状:
|
|
18
|
-
* 连得紧的聚成一簇,孤立的被推到边缘。这是一种**探索**型图表——
|
|
19
|
-
* 它的价值是让你看到结构,而不是读出精确数值。
|
|
20
|
-
*/
|
|
21
|
-
export function GraphChart({
|
|
16
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
17
|
+
function GraphChart({
|
|
22
18
|
nodes = [],
|
|
23
19
|
links = [],
|
|
24
20
|
iterations = 220,
|
|
25
21
|
nodeSize = 7,
|
|
26
22
|
showLabel = true,
|
|
27
23
|
scaleByDegree = true,
|
|
28
|
-
width =
|
|
24
|
+
width = "100%",
|
|
29
25
|
height = 380,
|
|
30
26
|
theme: themeInput,
|
|
31
27
|
palette,
|
|
@@ -34,73 +30,85 @@ export function GraphChart({
|
|
|
34
30
|
className,
|
|
35
31
|
style,
|
|
36
32
|
ariaLabel,
|
|
37
|
-
emptyText =
|
|
33
|
+
emptyText = "暂无数据"
|
|
38
34
|
}) {
|
|
39
35
|
const theme = useChartTheme(themeInput, palette);
|
|
40
|
-
const {
|
|
41
|
-
ref,
|
|
42
|
-
size
|
|
43
|
-
} = useElementSize();
|
|
36
|
+
const { ref, size } = useElementSize();
|
|
44
37
|
const [activeId, setActiveId] = useState(null);
|
|
45
|
-
const animKey = typeof animation ===
|
|
46
|
-
const animConfig = useMemo(
|
|
47
|
-
|
|
48
|
-
|
|
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
|
+
);
|
|
49
44
|
const groups = useMemo(() => {
|
|
50
45
|
const list = [];
|
|
51
|
-
nodes.forEach(node => {
|
|
52
|
-
const group = node.group ??
|
|
53
|
-
if (!list.includes(group))
|
|
46
|
+
nodes.forEach((node) => {
|
|
47
|
+
const group = node.group ?? "__default__";
|
|
48
|
+
if (!list.includes(group))
|
|
49
|
+
list.push(group);
|
|
54
50
|
});
|
|
55
51
|
return list;
|
|
56
52
|
}, [nodes]);
|
|
57
53
|
const positions = useMemo(() => {
|
|
58
|
-
if (!nodes.length || size.width <= 0)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
height: size.height
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
radius: nodeSize + 4
|
|
67
|
-
});
|
|
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
|
+
);
|
|
68
62
|
}, [nodes, links, size.width, size.height, iterations, nodeSize]);
|
|
69
|
-
const positionMap = useMemo(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
+
);
|
|
81
87
|
const progress = useChartTransition(degreeMatrix, animConfig).progress;
|
|
82
88
|
const neighbors = useMemo(() => {
|
|
83
|
-
const map = new Map();
|
|
89
|
+
const map = /* @__PURE__ */ new Map();
|
|
84
90
|
for (const link of links) {
|
|
85
|
-
if (!map.has(link.source))
|
|
86
|
-
|
|
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());
|
|
87
95
|
map.get(link.source).add(link.target);
|
|
88
96
|
map.get(link.target).add(link.source);
|
|
89
97
|
}
|
|
90
98
|
return map;
|
|
91
99
|
}, [links]);
|
|
92
100
|
const scene = useMemo(() => {
|
|
93
|
-
if (!positions.length || size.width <= 0)
|
|
101
|
+
if (!positions.length || size.width <= 0)
|
|
102
|
+
return [];
|
|
94
103
|
const nodes_ = [];
|
|
95
|
-
|
|
96
|
-
// 边
|
|
97
104
|
links.forEach((link, index) => {
|
|
98
105
|
const from = positionMap.get(link.source);
|
|
99
106
|
const to = positionMap.get(link.target);
|
|
100
|
-
if (!from || !to)
|
|
107
|
+
if (!from || !to)
|
|
108
|
+
return;
|
|
101
109
|
const related = activeId === null || activeId === link.source || activeId === link.target;
|
|
102
110
|
nodes_.push({
|
|
103
|
-
kind:
|
|
111
|
+
kind: "line",
|
|
104
112
|
key: `edge-${index}`,
|
|
105
113
|
x1: from.x,
|
|
106
114
|
y1: from.y,
|
|
@@ -109,56 +117,76 @@ export function GraphChart({
|
|
|
109
117
|
stroke: related && activeId !== null ? theme.emphasis : withAlpha(theme.axis.line, 0.9),
|
|
110
118
|
strokeWidth: related && activeId !== null ? 1.6 : 1,
|
|
111
119
|
opacity: (related ? 1 : 0.25) * progress,
|
|
112
|
-
pointerEvents:
|
|
120
|
+
pointerEvents: "none"
|
|
113
121
|
});
|
|
114
122
|
});
|
|
115
|
-
|
|
116
|
-
// 节点
|
|
117
123
|
positions.forEach((position, index) => {
|
|
118
|
-
|
|
119
|
-
|
|
124
|
+
var _a;
|
|
125
|
+
const node = nodes.find((item) => item.id === position.id);
|
|
126
|
+
if (!node)
|
|
127
|
+
return;
|
|
120
128
|
const baseRadius = radiusOf(position);
|
|
121
|
-
const local = staggerProgress(
|
|
129
|
+
const local = staggerProgress(
|
|
130
|
+
progress,
|
|
131
|
+
index,
|
|
132
|
+
positions.length,
|
|
133
|
+
animConfig.stagger
|
|
134
|
+
);
|
|
122
135
|
const radius = baseRadius * local;
|
|
123
136
|
const isActive = activeId === position.id;
|
|
124
|
-
const isNeighbor = activeId ? neighbors.get(activeId)
|
|
137
|
+
const isNeighbor = activeId ? ((_a = neighbors.get(activeId)) == null ? void 0 : _a.has(position.id)) ?? false : false;
|
|
125
138
|
const dim = activeId !== null && !isActive && !isNeighbor;
|
|
126
139
|
nodes_.push({
|
|
127
|
-
kind:
|
|
140
|
+
kind: "circle",
|
|
128
141
|
key: `node-${position.id}`,
|
|
129
142
|
cx: position.x,
|
|
130
143
|
cy: position.y,
|
|
131
144
|
r: Math.max(0.5, radius),
|
|
132
145
|
fill: colorOf(node),
|
|
133
146
|
opacity: dim ? 0.28 : 1,
|
|
134
|
-
stroke: isActive ? theme.text.primary :
|
|
147
|
+
stroke: isActive ? theme.text.primary : void 0,
|
|
135
148
|
strokeWidth: isActive ? 2 : 0,
|
|
136
|
-
pointerEvents:
|
|
149
|
+
pointerEvents: "none"
|
|
137
150
|
});
|
|
138
|
-
if (!showLabel || radius < 4)
|
|
151
|
+
if (!showLabel || radius < 4)
|
|
152
|
+
return;
|
|
139
153
|
const label = node.name ?? node.id;
|
|
140
|
-
// 标签最多占 20% 画布宽度,超了就截断
|
|
141
154
|
const maxWidth = size.width * 0.2;
|
|
142
155
|
const text = estimateTextWidth(label, theme.font.sizeSmall) > maxWidth ? `${label.slice(0, Math.max(2, Math.floor(maxWidth / theme.font.sizeSmall)))}…` : label;
|
|
143
156
|
nodes_.push({
|
|
144
|
-
kind:
|
|
157
|
+
kind: "text",
|
|
145
158
|
key: `label-${position.id}`,
|
|
146
159
|
x: position.x,
|
|
147
160
|
y: position.y + radius + theme.font.sizeSmall + 2,
|
|
148
161
|
text,
|
|
149
162
|
fill: theme.text.secondary,
|
|
150
163
|
fontSize: theme.font.sizeSmall,
|
|
151
|
-
anchor:
|
|
152
|
-
baseline:
|
|
164
|
+
anchor: "middle",
|
|
165
|
+
baseline: "middle",
|
|
153
166
|
opacity: dim ? 0.3 : local,
|
|
154
167
|
fontFamily: theme.font.family
|
|
155
168
|
});
|
|
156
169
|
});
|
|
157
170
|
return nodes_;
|
|
158
|
-
}, [
|
|
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
|
+
]);
|
|
159
186
|
const nodeFromPointer = (clientX, clientY) => {
|
|
160
187
|
const element = ref.current;
|
|
161
|
-
if (!element)
|
|
188
|
+
if (!element)
|
|
189
|
+
return null;
|
|
162
190
|
const rect = element.getBoundingClientRect();
|
|
163
191
|
const x = clientX - rect.left;
|
|
164
192
|
const y = clientY - rect.top;
|
|
@@ -174,50 +202,63 @@ export function GraphChart({
|
|
|
174
202
|
}
|
|
175
203
|
return nearest;
|
|
176
204
|
};
|
|
177
|
-
const activeNode = activeId ? nodes.find(node => node.id === activeId) : null;
|
|
205
|
+
const activeNode = activeId ? nodes.find((node) => node.id === activeId) : null;
|
|
178
206
|
const activePosition = activeId ? positionMap.get(activeId) : null;
|
|
179
207
|
const tooltipPayload = activeNode ? {
|
|
180
208
|
label: activeNode.name ?? activeNode.id,
|
|
181
209
|
row: null,
|
|
182
|
-
series: [
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
+
]
|
|
189
219
|
} : null;
|
|
190
220
|
const autoAriaLabel = ariaLabel ?? `关系图:${nodes.length} 个节点,${links.length} 条关系`;
|
|
191
|
-
return
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
height,
|
|
196
|
-
|
|
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
|
-
|
|
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
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { type ChartThemeInput } from '../../theme';
|
|
3
|
-
import type { ChartAnimationInput, TooltipConfig } from '../../types';
|
|
2
|
+
import { type ChartThemeInput } from '../../theme/index.js';
|
|
3
|
+
import type { ChartAnimationInput, TooltipConfig } from '../../types.js';
|
|
4
4
|
export interface HeatmapCell {
|
|
5
5
|
/** 横轴分类 */
|
|
6
6
|
x: string;
|