import * as echarts from 'echarts';
|
import type { Ref } from 'vue';
|
import { onMounted, shallowRef } from 'vue';
|
import { debounce } from '/@/utils/util';
|
|
export type UseDrawChatChartOption = {
|
chartRef: Ref<HTMLDivElement>;
|
drawChart: () => void;
|
};
|
export const useDrawChatChart = (option: UseDrawChatChartOption) => {
|
const { chartRef, drawChart } = option;
|
const chartInstance = shallowRef<echarts.ECharts>(null);
|
|
let resizeChart = null;
|
const chartContainerResize = ({ width, height }) => {
|
resizeChart?.(width, height);
|
};
|
|
onMounted(() => {
|
setTimeout(() => {
|
const parent = chartRef.value.parentElement;
|
const parentBound = parent.getBoundingClientRect();
|
chartInstance.value = echarts.init(chartRef.value, undefined, {
|
width: parentBound.width,
|
height: parentBound.height,
|
});
|
resizeChart = debounce((width, height) => {
|
chartInstance.value.resize({
|
width: width,
|
height: height,
|
});
|
});
|
|
drawChart();
|
}, 100);
|
});
|
|
return {
|
chartContainerResize,
|
chartInstance,
|
};
|
};
|