Files
admin-govern/src/components/echarts/MyEchart.vue

180 lines
4.3 KiB
Vue
Raw Normal View History

2023-12-26 14:15:05 +08:00
<template>
<div ref="chartRef" class="my-chart" />
</template>
<script setup lang="ts">
2023-12-27 15:06:23 +08:00
import { onBeforeUnmount, onMounted, ref, defineExpose, watch } from 'vue'
2023-12-26 14:15:05 +08:00
import echarts from './echarts'
import 'echarts/lib/component/dataZoom'
const props = defineProps(['options'])
const chartRef = ref<HTMLDivElement>()
let chart: echarts.ECharts | null = null
const resizeHandler = () => {
2023-12-27 15:06:23 +08:00
setTimeout(() => {
chart?.resize()
}, 100)
2023-12-26 14:15:05 +08:00
}
const initChart = () => {
2023-12-27 15:06:23 +08:00
chart?.dispose()
2023-12-26 14:15:05 +08:00
chart = echarts.init(chartRef.value as HTMLDivElement)
chart.setOption({
2023-12-27 15:06:23 +08:00
title: {
...props.options.title,
left: 'center',
textStyle: {
color: '#000',
fontSize: 18
}
},
2023-12-26 14:15:05 +08:00
tooltip: {
2023-12-27 15:06:23 +08:00
...(props.options.tooltip || null),
2023-12-26 14:15:05 +08:00
trigger: 'axis',
2023-12-27 15:06:23 +08:00
2023-12-26 14:15:05 +08:00
axisPointer: {
2023-12-27 15:06:23 +08:00
type: 'shadow',
label: {
color: '#fff',
fontSize: 16
}
2023-12-26 14:15:05 +08:00
},
textStyle: {
2023-12-27 15:06:23 +08:00
color: '#fff',
fontStyle: 'normal',
opacity: 0.35,
fontSize: 14
},
backgroundColor: 'rgba(0,0,0,0.35)',
borderWidth: 0
},
legend: {
...(props.options.xAxis.legend || null),
right: 80,
top: 0,
itemGap: 28,
itemStyle: {},
textStyle: {
rich: {
a: {
verticalAlign: 'middle'
}
},
padding: [2, 0, 0, 0] //[上、右、下、左]
2023-12-26 14:15:05 +08:00
}
},
2023-12-27 15:06:23 +08:00
2023-12-26 14:15:05 +08:00
grid: {
2023-12-27 15:06:23 +08:00
top: '50px',
left: '10px',
2023-12-26 14:15:05 +08:00
right: '40px',
bottom: '40px',
containLabel: true
},
2023-12-27 15:06:23 +08:00
xAxis: [
{
...(props.options.xAxis || null),
type: 'category',
axisTick: { show: false },
axisLine: {
lineStyle: {
color: '#000'
}
},
axisLabel: {
textStyle: {
fontFamily: 'dinproRegular',
color: '#000',
fontSize: '12'
}
}
}
],
yAxis: [
{
...(props.options.yAxis || null),
type: 'value',
nameTextStyle: {
color: '#000'
},
minInterval: 1,
axisLine: {
show: true,
lineStyle: {
color: '#000'
}
},
axisLabel: {
color: '#000',
fontSize: 14
},
splitLine: {
lineStyle: {
// 使用深浅的间隔色
color: ['#000'],
type: 'dashed',
opacity: 0.5
}
}
}
],
2023-12-26 14:15:05 +08:00
dataZoom: [
{
type: 'inside',
height: 13,
start: 0,
bottom: '20px',
end: 100
},
{
start: 0,
height: 13,
bottom: '20px',
end: 100
}
],
color: [
2023-12-27 15:06:23 +08:00
...(props.options.color || ''),
2023-12-26 14:15:05 +08:00
'#07CCCA ',
'#00BFF5',
'#FFBF00',
'#77DA63',
'#D5FF6B',
'#Ff6600',
'#FF9100',
'#5B6E96',
'#66FFCC',
'#B3B3B3'
],
2023-12-27 15:06:23 +08:00
...props.options.options
2023-12-26 14:15:05 +08:00
})
}
onMounted(() => {
setTimeout(() => {
2023-12-27 15:06:23 +08:00
// initChart()
2023-12-26 14:15:05 +08:00
}, 20)
window.addEventListener('resize', resizeHandler)
})
2023-12-27 15:06:23 +08:00
defineExpose({ initChart })
2023-12-26 14:15:05 +08:00
onBeforeUnmount(() => {
window.removeEventListener('resize', resizeHandler)
chart?.dispose()
})
2023-12-27 15:06:23 +08:00
watch(
() => props.options,
(newVal, oldVal) => {
initChart()
}
)
2023-12-26 14:15:05 +08:00
</script>
<style lang="scss" scoped>
.my-chart {
height: 100%;
width: 100%;
}
</style>