Vben
2021-04-13 c9089c324384dbf0f138f8861a2776810fc0efbf
src/hooks/web/useECharts.ts
@@ -1,31 +1,47 @@
import type { EChartsOption } from 'echarts';
import type { Ref } from 'vue';
import { useTimeoutFn } from '/@/hooks/core/useTimeout';
import { tryOnUnmounted } from '/@/utils/helper/vueHelper';
import { unref, Ref, nextTick } from 'vue';
import type { EChartOption, ECharts } from 'echarts';
import echarts from 'echarts';
import { useDebounce } from '/@/hooks/core/useDebounce';
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';
export type { EChartOption, ECharts };
import echarts from '/@/utils/lib/echarts';
import { useRootSetting } from '/@/hooks/setting/useRootSetting';
export function useECharts(
  elRef: Ref<HTMLDivElement>,
  theme: 'light' | 'dark' | 'default' = 'light'
) {
  let chartInstance: Nullable<ECharts> = null;
  const { getDarkMode } = useRootSetting();
  let chartInstance: echarts.ECharts | null = null;
  let resizeFn: Fn = resize;
  const cacheOptions = ref<EChartsOption>({});
  let removeResizeFn: Fn = () => {};
  const [debounceResize] = useDebounce(resize, 200);
  resizeFn = debounceResize;
  resizeFn = useDebounceFn(resize, 200);
  function init() {
  const getOptions = computed(
    (): EChartsOption => {
      if (getDarkMode.value !== 'dark') {
        return cacheOptions.value;
      }
      return {
        backgroundColor: 'transparent',
        ...cacheOptions.value,
      };
    }
  );
  function initCharts(t = theme) {
    const el = unref(elRef);
    if (!el || !unref(el)) {
      return;
    }
    chartInstance = echarts.init(el, theme);
    chartInstance = echarts.init(el, t);
    const { removeEvent } = useEventListener({
      el: window,
      name: 'resize',
@@ -33,32 +49,49 @@
    });
    removeResizeFn = removeEvent;
    const { widthRef, screenEnum } = useBreakpoint();
    if (unref(widthRef) <= screenEnum.MD) {
    if (unref(widthRef) <= screenEnum.MD || el.offsetHeight === 0) {
      useTimeoutFn(() => {
        resizeFn();
      }, 30);
    }
  }
  function setOptions(options: any, clear = true) {
  function setOptions(options: EChartsOption, clear = true) {
    cacheOptions.value = options;
    if (unref(elRef)?.offsetHeight === 0) {
      useTimeoutFn(() => {
        setOptions(unref(getOptions));
      }, 30);
      return;
    }
    nextTick(() => {
      useTimeoutFn(() => {
        if (!chartInstance) {
          init();
          initCharts(getDarkMode.value as 'default');
          if (!chartInstance) return;
        }
        clear && chartInstance.clear();
        clear && chartInstance?.clear();
        chartInstance && chartInstance.setOption(options);
        chartInstance?.setOption(unref(getOptions));
      }, 30);
    });
  }
  function resize() {
    if (!chartInstance) return;
    chartInstance.resize();
    chartInstance?.resize();
  }
  watch(
    () => getDarkMode.value,
    (theme) => {
      if (chartInstance) {
        chartInstance.dispose();
        initCharts(theme as 'default');
        setOptions(cacheOptions.value);
      }
    }
  );
  tryOnUnmounted(() => {
    if (!chartInstance) return;
@@ -69,7 +102,7 @@
  return {
    setOptions,
    echarts,
    resize,
    echarts,
  };
}