<template>
<div>
<slot name="header"></slot>
<div :id="id" :style="chartStyle"></div>
<slot name="footer"></slot>
</div>
</template>
<script setup lang="ts" name="CommonChart">
import {
onMounted,
markRaw,
defineProps,
defineEmits,
ref,
nextTick,
watch,
computed,
onUnmounted
} from 'vue';
import * as echarts from 'echarts';
const props = defineProps({
id: {
type: String,
required: true,
default: 'commonChart'
},
width: {
type: String,
default: '100%'
},
height: {
type: String,
default: '100%'
},
Styles: {
type: String,
default: ''
},
setOption: {
type: Object[Array],
required: true,
default: null
},
showLoading: {
type: Boolean,
default: false
},
loadConfig: {
type: Object,
default: () => {
return { text: 'loading' };
}
}
});
const emits = defineEmits(['getClickEvent']);
const myChart = ref(null);
const chartStyle = computed(() => {
return `width: ${props.width};height: ${props.height};${props.Styles}`;
});
watch(
() => props.setOption,
() => {
nextTick(() => {
myChart.value = markRaw(echarts.init(document.getElementById(props.id)));
if (isPlainObject(props.setOption)) {
initCharts();
props.showLoading ? myChart.value.hideLoading() : null;
} else {
props.showLoading ? myChart.value.showLoading(props.loadConfig) : null;
}
});
},
{
deep: true,
immediate: true
}
);
onMounted(() => {
myChart.value = markRaw(echarts.init(document.getElementById(props.id)));
if (isPlainObject(props.setOption)) {
initCharts();
props.showLoading ? myChart.value.hideLoading() : null;
} else {
props.showLoading ? myChart.value.showLoading(props.loadConfig) : null;
}
window.addEventListener('resize', () => {
myChart.value.resize();
});
});
onUnmounted(() => {
window.removeEventListener('resize', () => {
myChart.value.resize();
});
});
const initCharts = () => {
myChart.value.on('click', event => {
emits('getClickEvent', event);
});
myChart.value.getZr().on('mousemove', param => {
let pointInPixel = [param.offsetX, param.offsetY];
if (myChart.value.containPixel('grid', pointInPixel)) {
myChart.value.getZr().setCursorStyle('pointer');
} else {
myChart.value.getZr().setCursorStyle('default');
}
});
myChart.value.setOption(props.setOption, true);
window.addEventListener('resize', () => {
myChart.value.resize();
});
};
const isPlainObject = val => {
return Object.prototype.toString.call(val) === '[object Object]';
};
</script>
<style scoped></style>