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

212 lines
5.1 KiB
Vue
Raw Normal View History

2024-02-19 13:44:32 +08:00
<template>
2024-02-26 10:43:33 +08:00
<div ref='chartRef' class='my-chart' />
2024-02-19 13:44:32 +08:00
</template>
2024-02-26 10:43:33 +08:00
<script setup lang='ts'>
2024-02-19 13:44:32 +08:00
import { onBeforeUnmount, onMounted, ref, defineExpose, watch } from 'vue'
// import echarts from './echarts'
import * as echarts from 'echarts' // 全引入
2024-02-26 19:17:08 +08:00
import 'echarts-gl'
2024-02-19 13:44:32 +08:00
import 'echarts/lib/component/dataZoom'
const chartRef = ref<HTMLDivElement>()
const props = defineProps(['options'])
let chart: echarts.ECharts | any = null
const resizeHandler = () => {
chart.getZr().painter.getViewportRoot().style.display = 'none'
requestAnimationFrame(() => {
chart.resize()
chart.getZr().painter.getViewportRoot().style.display = ''
})
}
const initChart = () => {
chart?.dispose()
chart = echarts.init(chartRef.value as HTMLDivElement)
2024-02-26 16:12:09 +08:00
chart.setOption({
title: {
left: 'center',
textStyle: {
color: '#000',
fontSize: 18
},
...(props.options.title || null)
},
tooltip: {
trigger: 'axis',
2024-02-19 13:44:32 +08:00
2024-02-26 16:12:09 +08:00
axisPointer: {
type: 'shadow',
label: {
color: '#fff',
fontSize: 16
}
},
textStyle: {
color: '#fff',
fontStyle: 'normal',
opacity: 0.35,
fontSize: 14
},
backgroundColor: 'rgba(0,0,0,0.35)',
borderWidth: 0,
...(props.options.tooltip || null)
},
legend: {
right: 20,
top: 0,
itemGap: 10,
itemStyle: {},
textStyle: {
fontSize: 12,
padding: [2, 0, 0, 0] //[上、右、下、左]
},
itemWidth: 15,
itemHeight: 10,
...(props.options.legend || null)
},
2024-02-19 13:44:32 +08:00
grid: {
top: '50px',
left: '10px',
right: '60px',
2024-02-26 10:43:33 +08:00
bottom: props.options.options?.dataZoom === null ? '10px' : '40px',
2024-02-19 13:44:32 +08:00
containLabel: true
},
2024-02-26 19:17:08 +08:00
xAxis: props.options.xAxis ? handlerXAxis() : null,
yAxis: props.options.yAxis ? handlerYAxis() : null,
2024-02-19 13:44:32 +08:00
dataZoom: [
{
type: 'inside',
height: 13,
start: 0,
bottom: '20px',
end: 100,
...(props.options.dataZoom || null)
},
{
start: 0,
height: 13,
bottom: '20px',
end: 100,
...(props.options.dataZoom || null)
}
],
color: [
...(props.options.color || ''),
2024-02-26 19:17:08 +08:00
'#07CCCA',
2024-02-19 13:44:32 +08:00
'#00BFF5',
'#FFBF00',
'#77DA63',
'#D5FF6B',
'#Ff6600',
'#FF9100',
'#5B6E96',
'#66FFCC',
'#B3B3B3'
],
2024-02-26 16:12:09 +08:00
series: props.options.series,
2024-02-19 13:44:32 +08:00
...props.options.options
})
setTimeout(() => {
chart.resize()
2024-02-26 19:17:08 +08:00
}, 0)
2024-02-19 13:44:32 +08:00
}
const handlerYAxis = () => {
let temp = {
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
}
}
}
// props.options.xAxis 是数组还是对象
if (Array.isArray(props.options.yAxis)) {
return props.options.yAxis.map((item: any) => {
return {
...item,
...temp
}
})
} else {
return {
...temp,
...props.options.yAxis
}
}
}
const handlerXAxis = () => {
let temp = {
type: 'category',
axisTick: { show: false },
axisLine: {
lineStyle: {
color: '#000'
}
},
axisLabel: {
textStyle: {
fontFamily: 'dinproRegular',
color: '#000',
fontSize: '12'
}
}
}
// props.options.xAxis 是数组还是对象
if (Array.isArray(props.options.xAxis)) {
return props.options.xAxis.map((item: any) => {
return {
2024-02-26 10:43:33 +08:00
...temp,
...item
2024-02-19 13:44:32 +08:00
}
})
} else {
return {
...temp,
...props.options.xAxis
}
}
}
onMounted(() => {
initChart()
window.addEventListener('resize', resizeHandler)
})
defineExpose({ initChart })
onBeforeUnmount(() => {
window.removeEventListener('resize', resizeHandler)
chart?.dispose()
})
watch(
() => props.options,
(newVal, oldVal) => {
initChart()
}
)
</script>
2024-02-26 10:43:33 +08:00
<style lang='scss' scoped>
2024-02-19 13:44:32 +08:00
.my-chart {
height: 100%;
width: 100%;
}
</style>