|
| 1 | +<!-- |
| 2 | + * @Description: echarts图表初始化 |
| 3 | + * @Author: hutu |
| 4 | + * @Date: 2021-12-30 10:32:19 |
| 5 | + * @LastEditors: hutu |
| 6 | + * @LastEditTime: 2022-01-11 08:50:29 |
| 7 | +--> |
| 8 | +<template> |
| 9 | + <div :id="prop.id" :style="{ width: prop.width, height: prop.height }"></div> |
| 10 | +</template> |
| 11 | +<script lang="ts" setup> |
| 12 | +import * as echarts from 'echarts/core' |
| 13 | +import { BarChart, LineChart, PieChart } from 'echarts/charts' |
| 14 | +import { TitleComponent, TooltipComponent, GridComponent, LegendComponent, ToolboxComponent } from 'echarts/components' |
| 15 | +import { LabelLayout, UniversalTransition } from 'echarts/features' |
| 16 | +import { CanvasRenderer } from 'echarts/renderers' |
| 17 | +import { IEchartsOption } from '@/interface' |
| 18 | +echarts.use([BarChart, LineChart, PieChart, TitleComponent, TooltipComponent, ToolboxComponent, GridComponent, LegendComponent, LabelLayout, UniversalTransition, CanvasRenderer]) |
| 19 | +
|
| 20 | +import { ref, onMounted, onBeforeUnmount, onActivated, onDeactivated } from 'vue' |
| 21 | +import { debounce } from 'throttle-debounce' |
| 22 | +
|
| 23 | +const prop = defineProps<{ |
| 24 | + id: string |
| 25 | + width: string |
| 26 | + height: string |
| 27 | + option: IEchartsOption |
| 28 | +}>() |
| 29 | +
|
| 30 | +const chart = ref() |
| 31 | +/** |
| 32 | + * @desc: 初始化图表 |
| 33 | + */ |
| 34 | +const initChart = () => { |
| 35 | + const charts = echarts.init(document.getElementById(prop.id) as HTMLElement) |
| 36 | + charts.setOption(prop.option) |
| 37 | + chart.value = charts |
| 38 | +} |
| 39 | +/** |
| 40 | + * @desc: 监听窗口变化重置图表 |
| 41 | + */ |
| 42 | +const chartResizeHandler = debounce(100, false, () => { |
| 43 | + if (chart.value) { |
| 44 | + chart.value.resize({ |
| 45 | + animation: { |
| 46 | + duration: 200 |
| 47 | + } |
| 48 | + }) |
| 49 | + } |
| 50 | +}) |
| 51 | +/** |
| 52 | + * @desc: 添加监听窗口变化事件 |
| 53 | + */ |
| 54 | +const initResizeEvent = () => { |
| 55 | + window.addEventListener('resize', chartResizeHandler) |
| 56 | +} |
| 57 | +/** |
| 58 | + * @desc: 移除监听窗口变化事件 |
| 59 | + */ |
| 60 | +const destroyResizeEvent = () => { |
| 61 | + window.addEventListener('resize', chartResizeHandler) |
| 62 | +} |
| 63 | +
|
| 64 | +/** |
| 65 | + * @desc: 添加监听侧边栏变化事件 |
| 66 | + */ |
| 67 | +let sidebarElm: HTMLElement |
| 68 | +const initSidebarResizeEvent = () => { |
| 69 | + sidebarElm = document.getElementsByClassName('sidebar-container')[0] as HTMLElement |
| 70 | + sidebarElm && sidebarElm.addEventListener('transitionend', chartResizeHandler) |
| 71 | +} |
| 72 | +/** |
| 73 | + * @desc: 移除监听侧边栏变化事件 |
| 74 | + */ |
| 75 | +const destroySidebarResizeEvent = () => { |
| 76 | + sidebarElm && sidebarElm.removeEventListener('transitionend', chartResizeHandler) |
| 77 | +} |
| 78 | +
|
| 79 | +const mounted = () => { |
| 80 | + initChart() |
| 81 | + initResizeEvent() |
| 82 | + initSidebarResizeEvent() |
| 83 | +} |
| 84 | +const beforeDestroy = () => { |
| 85 | + destroyResizeEvent() |
| 86 | + destroySidebarResizeEvent() |
| 87 | +} |
| 88 | +
|
| 89 | +let firstFlag = false //首次渲染 |
| 90 | +onMounted(() => { |
| 91 | + firstFlag = true |
| 92 | + mounted() |
| 93 | +}) |
| 94 | +onBeforeUnmount(() => { |
| 95 | + beforeDestroy() |
| 96 | +}) |
| 97 | +onActivated(() => { |
| 98 | + //如果首次渲染不重复执行 |
| 99 | + if (firstFlag) { |
| 100 | + firstFlag = false |
| 101 | + return false |
| 102 | + } |
| 103 | + mounted() |
| 104 | +}) |
| 105 | +onDeactivated(() => { |
| 106 | + beforeDestroy() |
| 107 | +}) |
| 108 | +</script> |
0 commit comments