刘光辉
9 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<script lang="ts" setup>
  import { onBeforeUnmount, onMounted, ref, watch } from 'vue';
 
  import * as echarts from 'echarts';
  import { debounce } from 'lodash-es';
  import { storeToRefs } from 'pinia';
 
  import { useJnpfUniverStore } from '../../../store';
 
  interface Data {
    id: string;
    piniaStoreId: string;
  }
  interface PropsType {
    data: Data;
  }
 
  defineOptions({ name: 'JnpfUniverFloatEchart' });
 
  const props = defineProps<PropsType>();
 
  const floatChartContainerRef = ref<HTMLDivElement | null>(null);
 
  const jnpfUniverStore = useJnpfUniverStore(props?.data?.piniaStoreId);
  const { focusedFloatEchartDataCache } = storeToRefs(jnpfUniverStore);
 
  let floatChartInstance: echarts.ECharts | null = null;
  let resizeObserver: null | ResizeObserver = null;
 
  // 处理窗口大小变化的函数,带300ms 防抖
  const handleResize = debounce(() => {
    floatChartInstance?.resize();
  }, 300);
 
  // 初始化或更新图表
  function initialize(option: echarts.EChartsOption) {
    if (!floatChartContainerRef.value) {
      return;
    }
 
    if (!floatChartInstance) {
      floatChartInstance = echarts?.init(floatChartContainerRef.value);
    }
 
    floatChartInstance?.clear();
 
    // 使用传入的 options 配置图表
    floatChartInstance?.setOption(option);
 
    // 设置 ResizeObserver 监听容器大小变化
    if (!resizeObserver) {
      resizeObserver = new ResizeObserver(handleResize);
      resizeObserver?.observe(floatChartContainerRef.value);
    }
  }
 
  onMounted(() => {
    const { id: domId, piniaStoreId } = props.data ?? {};
 
    if (!floatChartContainerRef.value || !domId || !piniaStoreId) {
      return;
    }
 
    const floatEchartDataCaches = jnpfUniverStore?.floatEchartDataCaches ?? {};
 
    const targetOption = Object.values(floatEchartDataCaches as Record<string, { domId: string; option: any }>)?.find(item => item?.domId === domId)?.option;
    initialize(targetOption);
  });
 
  // 销毁
  onBeforeUnmount(() => {
    if (resizeObserver && floatChartContainerRef.value) {
      resizeObserver?.unobserve(floatChartContainerRef.value);
    }
 
    resizeObserver?.disconnect();
    floatChartInstance?.dispose();
  });
 
  watch(
    () => focusedFloatEchartDataCache.value,
    value => {
      const { domId, drawingId, option } = value;
 
      if (!domId && !drawingId) {
        // store置空的情况下,不响应
        return;
      }
 
      const containerId = floatChartContainerRef.value?.getAttribute('id');
      if (domId !== containerId) {
        return;
      }
 
      initialize(option);
    },
  );
</script>
 
<template>
  <div ref="floatChartContainerRef" :id="data.id" class="jnpf-univer-float-echart-ele"></div>
</template>
 
<style lang="scss" scoped>
  .jnpf-univer-float-echart-ele {
    position: absolute;
    width: 100%;
    height: 100%;
    background: #f9f9f9;
  }
</style>