2025-01-21 03:53:24 +00:00
|
|
|
import { unref, nextTick, watch, computed, ref } from 'vue';
|
|
|
|
import { useDebounceFn, tryOnUnmounted } from '@vueuse/core';
|
|
|
|
import { useTimeoutFn } from './useTimeout';
|
|
|
|
import { useEventListener } from './useEventListener';
|
|
|
|
import { useBreakpoint } from './useBreakpoint';
|
|
|
|
import echarts from '../utils/echarts';
|
|
|
|
|
|
|
|
export const useEcharts = (elRef, theme = 'default') => {
|
|
|
|
const getDarkMode = computed(() => {
|
|
|
|
return theme === 'default' ? 'dark' : theme;
|
|
|
|
});
|
|
|
|
let chartInstance = null;
|
|
|
|
let resizeFn = resize;
|
|
|
|
const cacheOptions = ref({});
|
2025-01-25 02:44:29 +00:00
|
|
|
let removeResizeFn = null;
|
2025-01-21 03:53:24 +00:00
|
|
|
|
|
|
|
resizeFn = useDebounceFn(resize, 200);
|
|
|
|
|
|
|
|
const getOptions = computed(() => {
|
|
|
|
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, t);
|
|
|
|
const { removeEvent } = useEventListener({
|
|
|
|
el: window,
|
|
|
|
name: 'resize',
|
|
|
|
listener: resizeFn,
|
|
|
|
});
|
|
|
|
removeResizeFn = removeEvent;
|
|
|
|
const { widthRef } = useBreakpoint();
|
|
|
|
if (unref(widthRef) <= 768 || el.offsetHeight === 0) {
|
|
|
|
useTimeoutFn(() => {
|
|
|
|
resizeFn();
|
|
|
|
}, 30);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setOptions(options = {}, clear = true) {
|
|
|
|
cacheOptions.value = options;
|
|
|
|
if (unref(elRef)?.offsetHeight === 0) {
|
|
|
|
useTimeoutFn(() => {
|
|
|
|
setOptions(unref(getOptions));
|
|
|
|
}, 30);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nextTick(() => {
|
|
|
|
useTimeoutFn(() => {
|
|
|
|
if (!chartInstance) {
|
|
|
|
initCharts(getDarkMode.value ?? 'default');
|
|
|
|
|
|
|
|
if (!chartInstance) return;
|
|
|
|
}
|
|
|
|
clear && chartInstance?.clear();
|
|
|
|
|
|
|
|
chartInstance?.setOption(unref(getOptions));
|
|
|
|
}, 30);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function resize() {
|
|
|
|
chartInstance?.resize();
|
|
|
|
}
|
|
|
|
|
2025-03-27 16:04:07 +08:00
|
|
|
/**
|
|
|
|
* 注册地图数据
|
|
|
|
* @param {string} mapName - 地图名称
|
|
|
|
* @param {object} geoJSON - GeoJSON 数据
|
|
|
|
*/
|
|
|
|
function registerMap(mapName, geoJSON) {
|
|
|
|
if (!mapName || !geoJSON) {
|
|
|
|
console.warn('地图名称或 GeoJSON 数据无效');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
echarts.registerMap(mapName, geoJSON);
|
|
|
|
}
|
|
|
|
|
2025-01-21 03:53:24 +00:00
|
|
|
watch(
|
|
|
|
() => getDarkMode.value,
|
|
|
|
(theme) => {
|
|
|
|
if (chartInstance) {
|
|
|
|
chartInstance.dispose();
|
|
|
|
initCharts(theme);
|
|
|
|
setOptions(cacheOptions.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
tryOnUnmounted(() => {
|
|
|
|
if (!chartInstance) return;
|
|
|
|
removeResizeFn();
|
|
|
|
chartInstance.dispose();
|
|
|
|
chartInstance = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
function getInstance() {
|
|
|
|
if (!chartInstance) {
|
|
|
|
initCharts(getDarkMode.value ?? 'default');
|
|
|
|
}
|
|
|
|
return chartInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
setOptions,
|
|
|
|
resize,
|
|
|
|
echarts,
|
|
|
|
getInstance,
|
2025-03-27 16:04:07 +08:00
|
|
|
registerMap,
|
2025-01-21 03:53:24 +00:00
|
|
|
};
|
|
|
|
};
|