Skip to content

Commit 7c6aa02

Browse files
committed
docs: 示例优化
1 parent 93cbf40 commit 7c6aa02

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

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

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,48 @@ function Example() {
192192
},
193193
};
194194

195+
const startAutoHover = () => {
196+
const chart = chartRef.current;
197+
const names: string[] = [];
198+
199+
foreach(
200+
values[0].nodes,
201+
(item) => {
202+
const name = item.name as unknown as string;
203+
if (!names.includes(name) && name !== '今日巡航次数') {
204+
names.push(name);
205+
}
206+
},
207+
{
208+
strategy: 'breadth',
209+
},
210+
);
211+
212+
if (!chart)
213+
return;
214+
215+
intervalRef.current = setInterval(() => {
216+
const name = getRandomElement(names);
217+
218+
if (name) {
219+
chart.setHovered([
220+
{
221+
name,
222+
},
223+
]);
224+
}
225+
}, 2 * 1000);
226+
};
227+
228+
const closeAutoHover = () => {
229+
const chart = chartRef.current;
230+
231+
intervalRef.current && clearInterval(intervalRef.current);
232+
if (chart) {
233+
chart.setHovered(null);
234+
}
235+
};
236+
195237
React.useEffect(
196238
() => {
197239
timeoutRef.current = setTimeout(() => {
@@ -203,32 +245,7 @@ function Example() {
203245
if (!chart)
204246
return;
205247

206-
const names: string[] = [];
207-
208-
foreach(
209-
values[0].nodes,
210-
(item) => {
211-
const name = item.name as unknown as string;
212-
if (!names.includes(name) && name !== '今日巡航次数') {
213-
names.push(name);
214-
}
215-
},
216-
{
217-
strategy: 'breadth',
218-
},
219-
);
220-
221-
intervalRef.current = setInterval(() => {
222-
const name = getRandomElement(names);
223-
224-
if (name) {
225-
chart.setHovered([
226-
{
227-
name,
228-
},
229-
]);
230-
}
231-
}, 2 * 1000);
248+
startAutoHover();
232249

233250
chart.setTooltipHandler({
234251
showTooltip: (activeType, data, params) => {
@@ -270,7 +287,7 @@ function Example() {
270287

271288
return () => {
272289
timeoutRef.current && clearTimeout(timeoutRef.current);
273-
intervalRef.current && clearInterval(intervalRef.current);
290+
closeAutoHover();
274291
};
275292
},
276293
[],

0 commit comments

Comments
 (0)