Skip to content

Commit 93cbf40

Browse files
committed
docs: 优化随机 hover 示例
1 parent a06f68e commit 93cbf40

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { Sankey } from '@sensoro-design/chart';
33
import React from 'react';
44
import { foreach } from 'tree-lodash';
55

6+
/**
7+
* 从数组获取随机数
8+
* @param arr
9+
*/
610
function getRandomElement<T = any>(arr: T[]) {
711
if (arr.length === 0) {
812
return null;
@@ -106,6 +110,8 @@ function Tooltip(props: { style?: React.CSSProperties }) {
106110

107111
function Example() {
108112
const chartRef = React.useRef<IVChart>(null);
113+
const timeoutRef = React.useRef<NodeJS.Timeout>();
114+
const intervalRef = React.useRef<NodeJS.Timeout>();
109115
const [tooltipStyles, setTooltipStyles] = React.useState<React.CSSProperties>({});
110116

111117
const spec: SankeyProps = {
@@ -188,19 +194,14 @@ function Example() {
188194

189195
React.useEffect(
190196
() => {
191-
const timeout = setTimeout(() => {
197+
timeoutRef.current = setTimeout(() => {
192198
// eslint-disable-next-line no-console
193199
console.log(chartRef.current);
194200

195201
const chart = chartRef.current;
196202

197203
if (!chart)
198204
return;
199-
// chart.setHovered([
200-
// {
201-
// name: '高风险',
202-
// },
203-
// ]);
204205

205206
const names: string[] = [];
206207

@@ -217,7 +218,7 @@ function Example() {
217218
},
218219
);
219220

220-
setInterval(() => {
221+
intervalRef.current = setInterval(() => {
221222
const name = getRandomElement(names);
222223

223224
if (name) {
@@ -228,6 +229,7 @@ function Example() {
228229
]);
229230
}
230231
}, 2 * 1000);
232+
231233
chart.setTooltipHandler({
232234
showTooltip: (activeType, data, params) => {
233235
// console.log(activeType, data, params);
@@ -267,7 +269,8 @@ function Example() {
267269
}, 500);
268270

269271
return () => {
270-
timeout && clearTimeout(timeout);
272+
timeoutRef.current && clearTimeout(timeoutRef.current);
273+
intervalRef.current && clearInterval(intervalRef.current);
271274
};
272275
},
273276
[],

0 commit comments

Comments
 (0)