Files
fengjie-datascreen/src/hooks/echart.js
2024-12-26 13:46:27 +08:00

29 lines
627 B
JavaScript

import { ref } from 'vue'
import * as echarts from 'echarts'
import { guid } from '@/utils/util'
export function useEchart() {
let chart = ref(null)
let id = ref(guid())
const initChart = () => {
const dom = document.getElementById(id.value)
chart.value = echarts.init(dom)
}
const setOption = (params) => {
chart.value.setOption(params)
}
const resize = () => {
if (chart) {
chart.dispose()
chart = null
initChart()
}
}
onMounted(() => {
// 监听窗口大小变化
window.addEventListener('resize', resize)
})
return { id, chart, setOption, initChart }
}