|
@@ -1,5 +1,8 @@
|
1
|
|
-import React from 'react';
|
2
|
|
-import * as echarts from '@/subpkg1/components/ec-canvas/echarts';
|
|
1
|
+import React from "react";
|
|
2
|
+// import * as echarts from '@/subpkg1/components/ec-canvas/echarts';
|
|
3
|
+import * as echarts from "echarts";
|
|
4
|
+
|
|
5
|
+import { View } from "@tarojs/components";
|
3
|
6
|
|
4
|
7
|
/**
|
5
|
8
|
* 使用本组件前, 页面组件必须引入 ec-canvas 组件
|
|
@@ -7,47 +10,37 @@ import * as echarts from '@/subpkg1/components/ec-canvas/echarts';
|
7
|
10
|
* 'ec-canvas': '@/subpkg1/components/ec-canvas/ec-canvas'
|
8
|
11
|
* }
|
9
|
12
|
*/
|
10
|
|
-const Chart = (props) => {
|
|
13
|
+export default function Chart(props) {
|
11
|
14
|
const { option, className, style } = props;
|
12
|
|
-
|
|
15
|
+
|
13
|
16
|
const [inited, setInited] = React.useState(false);
|
14
|
17
|
const chartRef = React.useRef();
|
15
|
|
- const canvasId = React.useMemo(() => `canvas-${Math.random().toString(36).substring(2, 7)}`, []);
|
16
|
|
- const ec = React.useMemo(() => {
|
17
|
|
- const onInit = (canvas, width, height, dpr) => {
|
18
|
|
- const chart = echarts.init(canvas, null, {
|
19
|
|
- width: width,
|
20
|
|
- height: height,
|
21
|
|
- devicePixelRatio: dpr // new
|
22
|
|
- });
|
23
|
|
- canvas.setChart(chart);
|
24
|
|
- chartRef.current = chart;
|
25
|
|
- setInited(true);
|
26
|
|
- }
|
|
18
|
+ const domRef = React.useRef();
|
27
|
19
|
|
28
|
|
- return {
|
29
|
|
- onInit
|
30
|
|
- }
|
31
|
|
- }, []);
|
32
|
20
|
|
33
|
21
|
React.useEffect(() => {
|
34
|
|
- if (inited) {
|
35
|
|
- chartRef.current.setOption(option);
|
|
22
|
+ if (!chartRef.current) {
|
|
23
|
+ chartRef.current = echarts.init(domRef.current);
|
36
|
24
|
}
|
37
|
|
- }, [option, inited]);
|
38
|
25
|
|
39
|
|
- return (
|
40
|
|
- <View className={className} style={style}>
|
41
|
|
- <ec-canvas canvasId={canvasId} ec={ec}></ec-canvas>
|
42
|
|
- </View>
|
43
|
|
- )
|
44
|
|
-}
|
|
26
|
+ const resize = () => {
|
|
27
|
+ const t = setTimeout(() => {
|
|
28
|
+ clearTimeout(t);
|
|
29
|
+ chartRef.current.resize();
|
|
30
|
+ }, 100);
|
|
31
|
+ };
|
|
32
|
+
|
|
33
|
+ chartRef.current.setOption(option);
|
|
34
|
+ resize();
|
45
|
35
|
|
46
|
|
-export default Chart;
|
|
36
|
+ window.addEventListener("resize", resize);
|
|
37
|
+ return () => window.removeEventListener("resize", resize);
|
|
38
|
+ }, [option]);
|
|
39
|
+ console.log(option);
|
|
40
|
+ return <div className={className} style={style} ref={domRef}></div>;
|
|
41
|
+}
|
47
|
42
|
|
48
|
|
-const getLinearGradient = (...args) => new echarts.graphic.LinearGradient(...args);
|
|
43
|
+const getLinearGradient = (...args) =>
|
|
44
|
+ new echarts.graphic.LinearGradient(...args);
|
49
|
45
|
|
50
|
|
-export {
|
51
|
|
- echarts,
|
52
|
|
- getLinearGradient,
|
53
|
|
-}
|
|
46
|
+export { echarts, getLinearGradient };
|