75 lines
1.3 KiB
Vue
75 lines
1.3 KiB
Vue
<template>
|
|
<l-echart ref="chartRef" class="chart"></l-echart>
|
|
</template>
|
|
|
|
|
|
<script setup>
|
|
import { onMounted, ref, onBeforeUnmount, nextTick, watch } from 'vue';
|
|
import * as echarts from 'echarts';
|
|
|
|
const chartRef = ref(null)
|
|
const myChart = ref(null)
|
|
let hasSetOption = false
|
|
|
|
const props = defineProps({
|
|
options: {
|
|
type: Object,
|
|
default: () => ({})
|
|
},
|
|
});
|
|
|
|
// 初始化图表
|
|
const initChart = async () => {
|
|
if (!chartRef.value) return
|
|
|
|
if (!myChart.value) {
|
|
myChart.value = await chartRef.value.init(echarts)
|
|
}
|
|
|
|
// 默认获取容器尺寸
|
|
myChart.value.resize();
|
|
|
|
if (!hasSetOption) {
|
|
myChart.value?.setOption(props.options, true);
|
|
hasSetOption = true
|
|
return
|
|
}
|
|
myChart.value?.setOption(props.options);
|
|
}
|
|
|
|
|
|
// 监听图表数据时候变化,重新渲染图表
|
|
watch(
|
|
() => props.options,
|
|
() => {
|
|
nextTick(() => {
|
|
initChart();
|
|
});
|
|
},
|
|
{ deep: true }
|
|
);
|
|
|
|
// 页面挂载,开始绘制图表
|
|
onMounted(() => {
|
|
nextTick(() => {
|
|
initChart();
|
|
})
|
|
})
|
|
|
|
|
|
// 组件销毁时销毁图表实例
|
|
onBeforeUnmount(() => {
|
|
if (myChart.value) {
|
|
echarts.dispose(myChart.value);
|
|
myChart.value = null;
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<style scoped>
|
|
.chart {
|
|
width: 100%;
|
|
height: 100%;
|
|
}
|
|
</style>
|