Skip to content

Commit 0502128

Browse files
committed
docs: 优化 sankey 示例
1 parent d3c611e commit 0502128

File tree

1 file changed

+68
-43
lines changed

1 file changed

+68
-43
lines changed

docs/demos/config/demo/Sankey/basic.tsx

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { IVChart, SankeyProps } from '@sensoro-design/chart';
22
import { Sankey } from '@sensoro-design/chart';
3-
import { uniqueId } from 'es-toolkit/compat';
43
import React from 'react';
54
import tree from 'tree-lodash';
65

@@ -20,57 +19,72 @@ const values = [
2019
{
2120
nodes: [
2221
{
23-
key: uniqueId(),
2422
name: '今日巡航次数',
2523
value: 160,
2624
total: 1087875,
2725
children: [
2826
{
29-
key: uniqueId(),
3027
name: '公共安全',
3128
value: 40,
3229
total: 287875,
3330
children: [
34-
{ key: uniqueId(), name: '高风险', value: 10, total: 18764 },
35-
{ key: uniqueId(), name: '中风险', value: 10, total: 22329 },
36-
{ key: uniqueId(), name: '低风险', value: 10, total: 43093 },
37-
{ key: uniqueId(), name: '无风险', value: 10, total: 763875 },
31+
{
32+
name: '紧急响应',
33+
value: 10,
34+
total: 18764,
35+
children: [
36+
{ name: '暴力行为', value: 10, total: 1864 },
37+
{ name: '突发状况', value: 10, total: 1864 },
38+
],
39+
},
40+
{
41+
name: '异常跟踪',
42+
value: 10,
43+
total: 22329,
44+
},
45+
{ name: '持续监测', value: 10, total: 43093 },
46+
{ name: '巡航正常', value: 10, total: 763875 },
3847
],
3948
},
4049
{
41-
key: uniqueId(),
4250
name: '交通安全',
4351
value: 40,
4452
total: 200653,
4553
children: [
46-
{ key: uniqueId(''), name: '高风险', value: 10, total: 18764 },
47-
{ key: uniqueId(), name: '中风险', value: 10, total: 22329 },
48-
{ key: uniqueId(), name: '低风险', value: 10, total: 43093 },
49-
{ key: uniqueId(), name: '无风险', value: 10, total: 763875 },
54+
{
55+
name: '紧急响应',
56+
value: 10,
57+
total: 18764,
58+
children: [
59+
{ name: '暴力行为', value: 10, total: 1864 },
60+
{ name: '突发状况', value: 10, total: 1864 },
61+
],
62+
},
63+
{ name: '异常跟踪', value: 10, total: 22329 },
64+
{ name: '持续监测', value: 10, total: 43093 },
65+
{ name: '巡航正常', value: 10, total: 763875 },
5066
],
5167
},
5268
{
53-
key: uniqueId(),
5469
name: '市容市政',
5570
value: 40,
5671
total: 406765,
5772
children: [
58-
{ key: uniqueId(), name: '高风险', value: 10, total: 18764 },
59-
{ key: uniqueId(), name: '中风险', value: 10, total: 22329 },
60-
{ key: uniqueId(), name: '低风险', value: 10, total: 43093 },
61-
{ key: uniqueId(), name: '无风险', value: 10, total: 763875 },
73+
{ name: '紧急响应', value: 10, total: 18764 },
74+
{ name: '异常跟踪', value: 10, total: 22329 },
75+
{ name: '持续监测', value: 10, total: 43093 },
76+
{ name: '巡航正常', value: 10, total: 763875 },
6277
],
6378
},
6479
{
65-
key: uniqueId(),
6680
name: '城市秩序',
6781
value: 40,
6882
total: 82854,
6983
children: [
70-
{ key: uniqueId(), name: '高风险', value: 10, total: 18764 },
71-
{ key: uniqueId(), name: '中风险', value: 10, total: 22329 },
72-
{ key: uniqueId(), name: '低风险', value: 10, total: 43093 },
73-
{ key: uniqueId(), name: '无风险', value: 10, total: 763875 },
84+
{ name: '紧急响应', value: 10, total: 18764 },
85+
{ name: '异常跟踪', value: 10, total: 22329 },
86+
{ name: '持续监测', value: 10, total: 43093 },
87+
{ name: '巡航正常', value: 10, total: 763875 },
7488
],
7589
},
7690
],
@@ -81,10 +95,10 @@ const values = [
8195

8296
const colors = ['#423CD1', '#E7474D', '#E7AF2C', '#51C2E1', '#3AC583'];
8397
const defaultSpecified = {
84-
高风险: colors[1],
85-
中风险: colors[2],
86-
低风险: colors[3],
87-
无风险: colors[4],
98+
紧急响应: colors[1],
99+
异常跟踪: colors[2],
100+
持续监测: colors[3],
101+
巡航正常: colors[4],
88102
};
89103
const specified: Record<string, string> = {
90104
...defaultSpecified,
@@ -131,7 +145,7 @@ function Example() {
131145
const timeoutRef = React.useRef<NodeJS.Timeout>();
132146
const intervalRef = React.useRef<NodeJS.Timeout | null>(null);
133147
const [tooltipStyles, setTooltipStyles] = React.useState<React.CSSProperties>({});
134-
const [hideLabel, setHideLabel] = React.useState(false);
148+
const [isAutoHover, setIsAutoHover] = React.useState(false);
135149

136150
const spec: SankeyProps = {
137151
height: 600,
@@ -155,8 +169,27 @@ function Example() {
155169
// @ts-expect-error 忽略报错
156170
return datum.name;
157171
},
158-
nodeHeight(node) {
159-
return node.depth === 0 ? 160 : 40;
172+
nodeHeight: (node) => {
173+
if (node.depth === 0) {
174+
return 160;
175+
}
176+
177+
if (node.depth === 1 || node.depth === 2) {
178+
return 40;
179+
}
180+
181+
return 20;
182+
},
183+
linkHeight(_, sourceNode) {
184+
if (sourceNode.depth === 0) {
185+
return 40;
186+
}
187+
188+
if (sourceNode.depth === 1) {
189+
return 10;
190+
}
191+
192+
return 20;
160193
},
161194
node: {
162195
style: {
@@ -175,8 +208,6 @@ function Example() {
175208
visible: true,
176209
offset: -12,
177210
formatMethod(_, datum) {
178-
if (hideLabel)
179-
return ``;
180211
return `${datum?.name} ${datum?.total?.toLocaleString()}`;
181212
},
182213
style: {
@@ -223,7 +254,7 @@ function Example() {
223254
emphasis: {
224255
enable: true,
225256
trigger: 'hover',
226-
effect: 'related',
257+
effect: isAutoHover ? 'adjacency' : 'related',
227258
},
228259
};
229260

@@ -239,6 +270,8 @@ function Example() {
239270
if (chart) {
240271
chart.setHovered(null);
241272
}
273+
274+
setIsAutoHover(false);
242275
};
243276

244277
const startAutoHover = () => {
@@ -261,6 +294,8 @@ function Example() {
261294
]);
262295
}
263296
}, 2 * 1000);
297+
298+
setIsAutoHover(true);
264299
};
265300

266301
const handlePointerEnter = () => {
@@ -338,16 +373,6 @@ function Example() {
338373
[],
339374
);
340375

341-
React.useEffect(() => {
342-
const timeout = setTimeout(() => {
343-
setHideLabel(true);
344-
}, 5 * 1000);
345-
346-
return () => {
347-
timeout && clearTimeout(timeout);
348-
};
349-
}, []);
350-
351376
const handleClick: SankeyProps['onClick'] = (e) => {
352377
if (e.node?.type === 'rect') {
353378
const { datum } = e;
@@ -365,7 +390,6 @@ function Example() {
365390
if (datum.datum.name === item.name) {
366391
item.children = [
367392
{
368-
key: uniqueId(),
369393
name: '测试节点',
370394
value: 5,
371395
total: 1000,
@@ -389,6 +413,7 @@ function Example() {
389413
<Sankey
390414
{...spec}
391415
ref={chartRef}
416+
skipFunctionDiff
392417
onClick={handleClick}
393418
onDblClick={handleDblClick}
394419
/>

0 commit comments

Comments
 (0)