AnalysisSystemForRadionucli.../src/components/CustomChart/index.vue

90 lines
1.7 KiB
Vue
Raw Normal View History

<template>
<div class="custom-chart" ref="containerRef" :style="{ height: height + 'px' }"></div>
</template>
<script>
import * as echarts from 'echarts'
import 'echarts-gl'
2023-07-06 19:40:48 +08:00
const events = ['click', 'brushEnd', 'dataZoom']
const zrEvents = ['mousemove', 'mousedown', 'mouseup', 'click', 'dblclick', 'contextmenu']
export default {
props: {
option: {
type: Object,
default: () => ({}),
},
opts: {
type: Object,
default: () => {},
},
height: {
type: Number,
default: null,
},
},
data() {
return {}
},
mounted() {
2023-07-06 19:40:48 +08:00
this._chart = echarts.init(this.$refs.containerRef)
this._chart.setOption(this.option, this.opts)
this.initEventListener()
},
beforeDestroy() {
if (this._chart) {
2023-08-08 19:11:37 +08:00
this._chart.dispose()
this._chart = null
2023-08-08 19:11:37 +08:00
}
},
methods: {
initEventListener() {
events.forEach((eventName) => {
2023-07-06 19:40:48 +08:00
this._chart.on(eventName, (params) => {
this.$emit(eventName, params)
})
})
const zr = this.getZRender()
zrEvents.forEach((eventName) => {
zr.on(eventName, (params) => {
2023-07-06 19:40:48 +08:00
this.$emit(`zr:${eventName}`, params)
})
})
},
resize() {
2023-07-06 19:40:48 +08:00
this._chart && this._chart.resize()
},
// 获取echart实例
getChartInstance() {
2023-07-06 19:40:48 +08:00
return this._chart
},
getZRender() {
2023-07-06 19:40:48 +08:00
return this._chart.getZr()
},
},
2023-07-06 19:40:48 +08:00
watch: {
option: {
handler() {
2023-07-06 19:40:48 +08:00
if (this._chart) {
this._chart.setOption(this.option, this.opts)
2023-06-25 14:18:45 +08:00
}
},
deep: true,
},
height() {
this.$nextTick(() => {
2023-07-06 19:40:48 +08:00
this._chart && this._chart.resize()
})
},
},
}
</script>
<style lang="less" scoped>
.custom-chart {
2023-09-26 15:41:42 +08:00
height: 100%;
}
</style>