tdesign-vue-next-starter/src/utils/hooks.ts

27 lines
719 B
TypeScript
Raw Normal View History

2021-11-16 19:01:03 +08:00
import { Ref, onUnmounted, onMounted } from 'vue';
import * as echarts from 'echarts/core';
export const useChart = (domId: string, chart: Ref) => {
let chartContainer: HTMLCanvasElement;
const selfChart = chart;
const updateContainer = () => {
selfChart.value.resize({
width: chartContainer.clientWidth,
height: chartContainer.clientHeight,
});
};
onMounted(() => {
if (!chartContainer) {
chartContainer = document.getElementById(domId) as HTMLCanvasElement;
}
selfChart.value = echarts.init(chartContainer);
});
window.addEventListener('resize', updateContainer, false);
onUnmounted(() => {
window.removeEventListener('resize', updateContainer);
});
};