vince
2024-08-01 d9bfe96dad098c5e72c39ed6727bcf6f2e8df595
src/hooks/web/useECharts.ts
@@ -1,19 +1,20 @@
import type { EChartsOption } from 'echarts';
import type { Ref } from 'vue';
import { useTimeoutFn } from '/@/hooks/core/useTimeout';
import { tryOnUnmounted } from '@vueuse/core';
import { unref, nextTick, watch, computed, ref } from 'vue';
import { useDebounceFn } from '@vueuse/core';
import { useEventListener } from '/@/hooks/event/useEventListener';
import { useBreakpoint } from '/@/hooks/event/useBreakpoint';
import echarts from '/@/utils/lib/echarts';
import { useRootSetting } from '/@/hooks/setting/useRootSetting';
import { computed, nextTick, ref, unref, watch } from 'vue';
import { useTimeoutFn } from '@vben/hooks';
import { tryOnUnmounted, useDebounceFn } from '@vueuse/core';
import { useEventListener } from '@/hooks/event/useEventListener';
import { useBreakpoint } from '@/hooks/event/useBreakpoint';
import echarts from '@/utils/lib/echarts';
import { useRootSetting } from '@/hooks/setting/useRootSetting';
import { useMenuSetting } from '@/hooks/setting/useMenuSetting';
export function useECharts(
  elRef: Ref<HTMLDivElement>,
  theme: 'light' | 'dark' | 'default' = 'default'
  theme: 'light' | 'dark' | 'default' = 'default',
) {
  const { getDarkMode: getSysDarkMode } = useRootSetting();
  const { getCollapsed } = useMenuSetting();
  const getDarkMode = computed(() => {
    return theme === 'default' ? getSysDarkMode.value : theme;
@@ -48,6 +49,10 @@
      listener: resizeFn,
    });
    removeResizeFn = removeEvent;
    const resizeObserver = new ResizeObserver(resizeFn);
    resizeObserver.observe(el);
    const { widthRef, screenEnum } = useBreakpoint();
    if (unref(widthRef) <= screenEnum.MD || el.offsetHeight === 0) {
      useTimeoutFn(() => {
@@ -58,28 +63,36 @@
  function setOptions(options: EChartsOption, clear = true) {
    cacheOptions.value = options;
    if (unref(elRef)?.offsetHeight === 0) {
      useTimeoutFn(() => {
        setOptions(unref(getOptions));
      }, 30);
      return;
    }
    nextTick(() => {
      useTimeoutFn(() => {
        if (!chartInstance) {
          initCharts(getDarkMode.value as 'default');
    return new Promise((resolve) => {
      if (unref(elRef)?.offsetHeight === 0) {
        useTimeoutFn(() => {
          setOptions(unref(getOptions));
          resolve(null);
        }, 50);
      }
      nextTick(() => {
        useTimeoutFn(() => {
          if (!chartInstance) {
            initCharts(getDarkMode.value as 'default');
          if (!chartInstance) return;
        }
        clear && chartInstance?.clear();
            if (!chartInstance) return;
          }
          clear && chartInstance?.clear();
        chartInstance?.setOption(unref(getOptions));
      }, 30);
          chartInstance?.setOption(unref(getOptions));
          resolve(null);
        }, 30);
      });
    });
  }
  function resize() {
    chartInstance?.resize();
    chartInstance?.resize({
      animation: {
        duration: 300,
        easing: 'quadraticIn',
      },
    });
  }
  watch(
@@ -90,9 +103,15 @@
        initCharts(theme as 'default');
        setOptions(cacheOptions.value);
      }
    }
    },
  );
  watch(getCollapsed, (_) => {
    useTimeoutFn(() => {
      resizeFn();
    }, 300);
  });
  tryOnUnmounted(() => {
    if (!chartInstance) return;
    removeResizeFn();