当前位置:首页 > vue > 正文内容

用于vue3的EchartsResize Hook函数

admin14小时前vue7
import { onMounted, onUnmounted, nextTick } from 'vue'

export function useEchartsResize(chartInstances) {
  const debounce = (fn, delay) => {
    let timer
    return function () {
      if (timer) {
        clearTimeout(timer)
      }
      timer = setTimeout(() => {
        fn()
      }, delay)
    }
  }

  const handleResize = () => {
    nextTick(() => {
      // 支持函数形式获取图表实例
      const charts = typeof chartInstances === 'function' ? chartInstances() : chartInstances
      if (Array.isArray(charts)) {
        charts.forEach(it => {
          const chart = typeof it === 'function' ? it() : it;
          if (chart && typeof chart.resize === 'function') {
            chart.resize()
          }
        })
      } else if (charts && typeof charts.resize === 'function') {
        charts.resize()
      }
    })
  }

  const debouncedResize = debounce(handleResize, 100)

  onMounted(() => {
    window.addEventListener('resize', debouncedResize)
  })

  onUnmounted(() => {
    window.removeEventListener('resize', debouncedResize)
  })

  return {
    handleResize: debouncedResize
  }
}

使用方式简单

import { useEchartsResize } from "./useEchartsResize";
const chart = shallowRef(null);

useEchartsResize(() => chart.value);

图标自动变化大小

扫描二维码推送至手机访问。

版权声明:本文由一棵低调的葱发布,如需转载请注明出处。

本文链接:https://duanxianze.site/?id=25

分享给朋友:
返回列表

上一篇:fpr配置文件写法

没有最新的文章了...

“用于vue3的EchartsResize Hook函数” 的相关文章

fpr配置文件写法

客户端  frpc.toml serverAddr = "***"  //服务器域名 serverPort = 7*** //服务器监听端口 auth.method =&nb...