mirror of
https://github.com/Tencent/tdesign-vue-next-starter.git
synced 2024-12-24 02:46:29 +08:00
27 lines
719 B
TypeScript
27 lines
719 B
TypeScript
|
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);
|
||
|
});
|
||
|
};
|