18
18
19
19
import * as chart from '~/utils/chart' ;
20
20
21
- import type {
22
- EChartsOption ,
23
- ECharts ,
24
- CustomSeriesOption ,
25
- CustomSeriesRenderItem ,
26
- AxisPointerComponentOption ,
27
- TooltipComponentOption ,
28
- GridComponentOption ,
29
- Color as ZRColor
30
- } from 'echarts' ;
21
+ import type { EChartsOption as EChartOption , ECharts } from 'echarts' ;
31
22
import React , { useCallback , useEffect , useImperativeHandle , useMemo , useRef , useState } from 'react' ;
32
23
import { WithStyled , primaryColor , transitionProps } from '~/utils/style' ;
33
24
import useECharts , { Options , Wrapper , useChartTheme } from '~/hooks/useECharts' ;
@@ -36,7 +27,6 @@ import GridLoader from 'react-spinners/GridLoader';
36
27
import defaultsDeep from 'lodash/defaultsDeep' ;
37
28
import styled from 'styled-components' ;
38
29
import useThrottleFn from '~/hooks/useThrottleFn' ;
39
- import type { LinesSeriesOption } from 'echarts/charts' ;
40
30
41
31
const Tooltip = styled . div `
42
32
position: absolute;
@@ -48,14 +38,23 @@ const Tooltip = styled.div`
48
38
display: none;
49
39
${ transitionProps ( [ 'color' , 'background-color' ] ) }
50
40
` ;
51
-
52
- type RenderItem = CustomSeriesRenderItem ;
41
+ // type RenderItem = EChartOption.SeriesCustom.RenderItem;
42
+ type RenderItem = any ;
53
43
type GetValue = ( i : number ) => number ;
54
44
type GetCoord = ( p : [ number , number ] ) => [ number , number ] ;
55
45
56
46
export type StackChartProps = {
57
- options ?: EChartsOption ;
47
+ options ?: EChartOption ;
58
48
title ?: string ;
49
+ // data?: Partial<Omit<NonNullable<EChartOption<EChartOption.SeriesCustom>['series']>[number], 'data'>> & {
50
+ // minZ: number;
51
+ // maxZ: number;
52
+ // minX: number;
53
+ // maxX: number;
54
+ // minY: number;
55
+ // maxY: number;
56
+ // data: number[][];
57
+ // };
59
58
data ?: any ;
60
59
loading ?: boolean ;
61
60
zoom ?: boolean ;
@@ -116,11 +115,11 @@ const StackChart = React.forwardRef<StackChartRef, StackChartProps & WithStyled>
116
115
[ getPoint , rawData ]
117
116
) ;
118
117
119
- const renderItem = useCallback (
120
- ( params , api ) => {
118
+ const renderItem = useCallback < RenderItem > (
119
+ ( params : any , api : any ) => {
121
120
const points = makePolyPoints ( params . dataIndex as number , api . value as GetValue , api . coord as GetCoord ) ;
122
121
return {
123
- type : 'path ' ,
122
+ type : 'polygon ' ,
124
123
silent : true ,
125
124
z : api . value ?.( 1 ) ,
126
125
shape : {
@@ -147,9 +146,8 @@ const StackChart = React.forwardRef<StackChartRef, StackChartProps & WithStyled>
147
146
useEffect ( ( ) => {
148
147
dotsRef . current = dots ;
149
148
} , [ dots ] ) ;
150
-
151
- const AxisPointer = options ?. axisPointer as AxisPointerComponentOption ;
152
- const pointerLabelFormatter = AxisPointer . label ?. formatter ;
149
+ const axisPointer : any = options ?. axisPointer ;
150
+ const pointerLabelFormatter = axisPointer ?. label ?. formatter ;
153
151
154
152
// formatter change will cause echarts rerender axis pointer label
155
153
// so we need to use 2 refs instead of dots and highlight to get rid of dependencies of these two variables
@@ -161,15 +159,14 @@ const StackChart = React.forwardRef<StackChartRef, StackChartProps & WithStyled>
161
159
if ( 'string' === typeof pointerLabelFormatter ) {
162
160
return pointerLabelFormatter ;
163
161
}
164
- // return pointerLabelFormatter(params, dotsRef.current[highLightRef.current]);
165
- return pointerLabelFormatter ( params ) ;
162
+ return pointerLabelFormatter ( params , dotsRef . current [ highLightRef . current ] ) ;
166
163
} ,
167
164
[ pointerLabelFormatter ]
168
165
) ;
169
166
170
167
const theme = useChartTheme ( ) ;
171
168
172
- const chartOptions = useMemo < EChartsOption > ( ( ) => {
169
+ const chartOptions : any = useMemo < EChartOption > ( ( ) => {
173
170
// eslint-disable-next-line @typescript-eslint/no-unused-vars
174
171
const { color, colorAlt, toolbox, series, ...defaults } = chart ;
175
172
@@ -240,8 +237,7 @@ const StackChart = React.forwardRef<StackChartRef, StackChartProps & WithStyled>
240
237
const mouseout = useCallback ( ( ) => {
241
238
setHighlight ( null ) ;
242
239
setDots ( [ ] ) ;
243
- const formatters = chartOptions . tooltip as TooltipComponentOption ;
244
- if ( formatters . formatter ) {
240
+ if ( chartOptions . tooltip ?. formatter ) {
245
241
setTooltip ( '' ) ;
246
242
if ( tooltipRef . current ) {
247
243
tooltipRef . current . style . display = 'none' ;
@@ -257,13 +253,13 @@ const StackChart = React.forwardRef<StackChartRef, StackChartProps & WithStyled>
257
253
}
258
254
259
255
const { offsetX, offsetY} = e ;
260
- if ( offsetY < negativeY + ( ( chartOptions [ ' grid' ] as GridComponentOption ) ?. top as number ) ?? 0 ) {
256
+ if ( offsetY < negativeY + ( chartOptions ?. grid ?. top as number ) ?? 0 ) {
261
257
mouseout ( ) ;
262
258
return ;
263
259
}
264
- const [ x , y ] = echarts . convertFromPixel ( 'grid' , [ offsetX , offsetY ] ) as [ number , number ] ;
265
- const seriesData = echart ? .getOption ( ) . series as LinesSeriesOption ;
266
- const data = ( seriesData . data as number [ ] [ ] ) ?? [ ] ;
260
+ const [ x , y ] = echarts . convertFromPixel ( 'grid' as any , [ offsetX , offsetY ] ) as [ number , number ] ;
261
+ const seriesData : any = echarts . getOption ( ) . series ;
262
+ const data = ( seriesData [ 0 ] . data as number [ ] [ ] ) ?? [ ] ;
267
263
268
264
// find right on top step
269
265
const steps = data . map ( row => row [ 1 ] ) . sort ( ( a , b ) => a - b ) ;
@@ -300,11 +296,10 @@ const StackChart = React.forwardRef<StackChartRef, StackChartProps & WithStyled>
300
296
}
301
297
302
298
// set tooltip
303
- const formatters = chartOptions . tooltip as TooltipComponentOption ;
304
- if ( formatters . formatter ) {
299
+ if ( chartOptions . tooltip ?. formatter ) {
305
300
setTooltip (
306
301
// eslint-disable-next-line @typescript-eslint/no-explicit-any
307
- highlight == null ? '' : ( formatters . formatter as any ) ?.( dots [ highlight ] )
302
+ highlight == null ? '' : ( chartOptions . tooltip ? .formatter as any ) ?.( dots [ highlight ] )
308
303
) ;
309
304
if ( tooltipRef . current ) {
310
305
if ( step == null ) {
@@ -384,9 +379,9 @@ const StackChart = React.forwardRef<StackChartRef, StackChartProps & WithStyled>
384
379
}
385
380
} ) ;
386
381
} else {
387
- const seriesData = echart . getOption ( ) . series as LinesSeriesOption ;
388
- const data = ( seriesData . data as number [ ] [ ] ) ?? [ ] ;
389
- const getCoord : GetCoord = pt => echart . convertToPixel ( 'grid' , pt ) as [ number , number ] ;
382
+ const seriesData : any = echart . getOption ( ) . series ;
383
+ const data = ( seriesData [ 0 ] ? .data as number [ ] [ ] ) ?? [ ] ;
384
+ const getCoord : GetCoord = pt => echart . convertToPixel ( 'grid' as any , pt ) as [ number , number ] ;
390
385
const getValue : GetValue = i => data [ highlight ] [ i ] ;
391
386
echart . setOption ( {
392
387
graphic : {
@@ -430,7 +425,7 @@ const StackChart = React.forwardRef<StackChartRef, StackChartProps & WithStyled>
430
425
}
431
426
} ) ;
432
427
} else {
433
- const getCoord : GetCoord = pt => echart . convertToPixel ( 'grid' , pt ) as [ number , number ] ;
428
+ const getCoord : GetCoord = pt => echart . convertToPixel ( 'grid' as any , pt ) as [ number , number ] ;
434
429
echart . setOption ( {
435
430
graphic : {
436
431
elements : dots . map ( ( dot , i ) => {
@@ -449,7 +444,7 @@ const StackChart = React.forwardRef<StackChartRef, StackChartProps & WithStyled>
449
444
} ,
450
445
style : {
451
446
fill : '#fff' ,
452
- stroke : ( chartOptions . color as ZRColor [ ] ) ?. [ 0 ] ,
447
+ stroke : chartOptions . color ?. [ 0 ] ,
453
448
lineWidth : 2
454
449
}
455
450
} ;
0 commit comments