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