@@ -3,6 +3,10 @@ import { Sankey } from '@sensoro-design/chart';
3
3
import React from 'react' ;
4
4
import { foreach } from 'tree-lodash' ;
5
5
6
+ /**
7
+ * 从数组获取随机数
8
+ * @param arr
9
+ */
6
10
function getRandomElement < T = any > ( arr : T [ ] ) {
7
11
if ( arr . length === 0 ) {
8
12
return null ;
@@ -106,6 +110,8 @@ function Tooltip(props: { style?: React.CSSProperties }) {
106
110
107
111
function Example ( ) {
108
112
const chartRef = React . useRef < IVChart > ( null ) ;
113
+ const timeoutRef = React . useRef < NodeJS . Timeout > ( ) ;
114
+ const intervalRef = React . useRef < NodeJS . Timeout > ( ) ;
109
115
const [ tooltipStyles , setTooltipStyles ] = React . useState < React . CSSProperties > ( { } ) ;
110
116
111
117
const spec : SankeyProps = {
@@ -188,19 +194,14 @@ function Example() {
188
194
189
195
React . useEffect (
190
196
( ) => {
191
- const timeout = setTimeout ( ( ) => {
197
+ timeoutRef . current = setTimeout ( ( ) => {
192
198
// eslint-disable-next-line no-console
193
199
console . log ( chartRef . current ) ;
194
200
195
201
const chart = chartRef . current ;
196
202
197
203
if ( ! chart )
198
204
return ;
199
- // chart.setHovered([
200
- // {
201
- // name: '高风险',
202
- // },
203
- // ]);
204
205
205
206
const names : string [ ] = [ ] ;
206
207
@@ -217,7 +218,7 @@ function Example() {
217
218
} ,
218
219
) ;
219
220
220
- setInterval ( ( ) => {
221
+ intervalRef . current = setInterval ( ( ) => {
221
222
const name = getRandomElement ( names ) ;
222
223
223
224
if ( name ) {
@@ -228,6 +229,7 @@ function Example() {
228
229
] ) ;
229
230
}
230
231
} , 2 * 1000 ) ;
232
+
231
233
chart . setTooltipHandler ( {
232
234
showTooltip : ( activeType , data , params ) => {
233
235
// console.log(activeType, data, params);
@@ -267,7 +269,8 @@ function Example() {
267
269
} , 500 ) ;
268
270
269
271
return ( ) => {
270
- timeout && clearTimeout ( timeout ) ;
272
+ timeoutRef . current && clearTimeout ( timeoutRef . current ) ;
273
+ intervalRef . current && clearInterval ( intervalRef . current ) ;
271
274
} ;
272
275
} ,
273
276
[ ] ,
0 commit comments