Skip to content
This repository was archived by the owner on Dec 10, 2021. It is now read-only.

Commit 8e0435e

Browse files
committed
clean up Echart component
1 parent fe67a77 commit 8e0435e

File tree

1 file changed

+13
-28
lines changed
  • plugins/plugin-chart-echarts/src/components

1 file changed

+13
-28
lines changed

plugins/plugin-chart-echarts/src/components/Echart.tsx

+13-28
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
import React, { createRef, useEffect, useState } from 'react';
19+
import React, { useRef, useEffect } from 'react';
2020
import styled from '@superset-ui/style';
2121
import echarts from 'echarts';
2222
import { EchartsProps, EchartsStylesProps } from '../types';
@@ -26,38 +26,23 @@ const Styles = styled.div<EchartsStylesProps>`
2626
width: ${({ width }) => width};
2727
`;
2828

29-
export default function Echart(props: EchartsProps) {
30-
const { height, width, echartOptions } = props;
31-
const [rootElem, setRootElem] = useState({ obj: undefined } as {
32-
obj?: React.RefObject<HTMLDivElement>;
33-
});
34-
const [chart, setChart] = useState({ obj: undefined } as { obj?: echarts.ECharts });
29+
export default function Echart({ width, height, echartOptions }: EchartsProps) {
30+
const divRef = useRef<HTMLDivElement>();
31+
const chartRef = useRef<echarts.ECharts>();
3532

3633
useEffect(() => {
37-
setRootElem({ obj: createRef<HTMLDivElement>() });
38-
}, []);
39-
40-
useEffect(() => {
41-
if (rootElem.obj) {
42-
const root = rootElem.obj.current as HTMLDivElement;
43-
setChart({ obj: echarts.init(root) });
44-
}
45-
}, [rootElem]);
46-
47-
useEffect(() => {
48-
if (chart.obj) {
49-
chart.obj.setOption(echartOptions, true);
34+
if (!divRef.current) return;
35+
if (!chartRef.current) {
36+
chartRef.current = echarts.init(divRef.current);
5037
}
51-
}, [chart, echartOptions]);
38+
chartRef.current.setOption(echartOptions, true);
39+
}, [echartOptions]);
5240

5341
useEffect(() => {
54-
if (chart.obj) {
55-
chart.obj.resize({
56-
width,
57-
height,
58-
});
42+
if (chartRef.current) {
43+
chartRef.current.resize({ width, height });
5944
}
60-
}, [chart, width, height]);
45+
}, [width, height]);
6146

62-
return <Styles ref={rootElem.obj} height={height} width={width} />;
47+
return <Styles ref={divRef} height={height} width={width} />;
6348
}

0 commit comments

Comments
 (0)