-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12bb2dd
commit 87a9478
Showing
7 changed files
with
211 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import type { EChartsOption, SetOptionOpts } from 'echarts'; | ||
import { BarChart, LineChart } from 'echarts/charts'; | ||
import { | ||
DatasetComponent, | ||
DataZoomComponent, | ||
GridComponent, | ||
LegendComponent, | ||
TitleComponent, | ||
ToolboxComponent, | ||
TooltipComponent, | ||
TransformComponent, | ||
} from 'echarts/components'; | ||
import * as echarts from 'echarts/core'; | ||
import { LabelLayout, UniversalTransition } from 'echarts/features'; | ||
import { CanvasRenderer } from 'echarts/renderers'; | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import type { CSSProperties } from 'react'; | ||
import useResize from 'hooks/useResize'; | ||
|
||
echarts.use([ | ||
BarChart, | ||
LineChart, | ||
DataZoomComponent, | ||
TitleComponent, | ||
TooltipComponent, | ||
ToolboxComponent, | ||
LegendComponent, | ||
GridComponent, | ||
DatasetComponent, | ||
TransformComponent, | ||
LabelLayout, | ||
UniversalTransition, | ||
CanvasRenderer, | ||
]); | ||
|
||
interface Props { | ||
onClick?: (param: echarts.ECElementEvent) => void; | ||
option: EChartsOption; | ||
settings?: SetOptionOpts; | ||
style?: CSSProperties; | ||
} | ||
|
||
const ReactECharts: React.FC<Props> = ({ option, style, settings = {}, onClick }: Props) => { | ||
const chartRef = useRef<HTMLDivElement>(null); | ||
const [width, height] = useResize(chartRef); | ||
const [echart, setEchart] = useState<echarts.ECharts | undefined>(undefined); | ||
|
||
useEffect(() => { | ||
if (chartRef.current && !echart) { | ||
const chart = echarts.init(chartRef.current, null, { renderer: 'canvas', useDirtyRect: false }); | ||
chart.on('click', onClick ?? (() => undefined)); | ||
setEchart(chart); | ||
} | ||
|
||
return () => { | ||
echart?.dispose(); | ||
}; | ||
}, [echart, onClick]); | ||
|
||
useEffect(() => { | ||
echart?.resize({ width, height }); | ||
}, [echart, height, width]); | ||
|
||
useEffect(() => { | ||
echart?.setOption(option, { ...settings, notMerge: false }); | ||
}, [echart, option, settings]); | ||
|
||
return ( | ||
<div | ||
ref={chartRef} | ||
style={{ | ||
height: '100%', | ||
position: 'relative', | ||
width: '100%', | ||
...style, | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export default ReactECharts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { RefObject, useCallback, useEffect, useState } from 'react'; | ||
|
||
const useResize = (ref: RefObject<HTMLElement>): [width: number, height: number] => { | ||
const [height, setHeight] = useState<number>(0); | ||
const [width, setWidth] = useState<number>(0); | ||
|
||
const handleResize = useCallback((entries: ResizeObserverEntry[]) => { | ||
if (!Array.isArray(entries)) { | ||
return; | ||
} | ||
|
||
const entry = entries[0]; | ||
setWidth(entry.contentRect.width); | ||
setHeight(entry.contentRect.height); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!ref.current) { | ||
return; | ||
} | ||
|
||
let resizeObserver = new ResizeObserver((entries: ResizeObserverEntry[]) => handleResize(entries)); | ||
resizeObserver.observe(ref.current); | ||
|
||
return () => { | ||
resizeObserver.disconnect(); | ||
resizeObserver = null; | ||
}; | ||
}, [handleResize, ref]); | ||
|
||
return [width, height]; | ||
}; | ||
|
||
export default useResize; |
Oops, something went wrong.