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

237 lines
6.1 KiB
Vue
Raw Normal View History

2024-02-19 13:44:32 +08:00
<template>
2024-02-28 15:26:31 +08:00
<div ref="chartRef" class="my-chart" />
2024-02-19 13:44:32 +08:00
</template>
2024-02-28 15:26:31 +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'
2024-02-28 15:26:31 +08:00
import { color, gradeColor3 } from './color'
2024-02-19 13:44:32 +08:00
const chartRef = ref<HTMLDivElement>()
const props = defineProps(['options'])
let chart: echarts.ECharts | any = null
const resizeHandler = () => {
// 不在视野中的时候不进行resize
if(chartRef.value!.offsetHeight == 0) return
2024-02-19 13:44:32 +08:00
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-27 14:18:01 +08:00
const options = {
2024-02-26 16:12:09 +08:00
title: {
left: 'center',
textStyle: {
color: '#000',
fontSize: 18
},
...(props.options.title || null)
},
tooltip: {
trigger: 'axis',
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-27 14:18:01 +08:00
dataZoom: props.options.dataZoom || [
2024-02-19 13:44:32 +08:00
{
type: 'inside',
height: 13,
start: 0,
bottom: '20px',
2024-02-27 14:18:01 +08:00
end: 100
2024-02-19 13:44:32 +08:00
},
{
start: 0,
height: 13,
bottom: '20px',
2024-02-27 14:18:01 +08:00
end: 100
2024-02-19 13:44:32 +08:00
}
],
2024-02-27 14:18:01 +08:00
color: props.options.color || color,
2024-02-26 16:12:09 +08:00
series: props.options.series,
2024-02-19 13:44:32 +08:00
...props.options.options
2024-02-27 14:18:01 +08:00
}
handlerBar(options)
// 处理柱状图
chart.setOption(options)
setTimeout(() => {
chart.resize()
2024-02-26 19:17:08 +08:00
}, 0)
2024-02-19 13:44:32 +08:00
}
2024-02-27 14:18:01 +08:00
const handlerBar = (options: any) => {
if (Array.isArray(options.series)) {
options.series.forEach((item: any) => {
if (item.type === 'bar') {
item.barMinHeight = 10
item.itemStyle = Object.assign(
{
color: (params: any) => {
if (params.value == 0 || params.value == 3.14159) {
return '#ccc'
} else {
2024-02-28 15:26:31 +08:00
return props.options.color
? props.options.color[params.seriesIndex]
: color[params.seriesIndex]
2024-02-27 14:18:01 +08:00
}
}
2024-02-27 14:47:45 +08:00
},
item.itemStyle
2024-02-27 14:18:01 +08:00
)
}
})
}
}
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
}
}
}
let throttle: ReturnType<typeof setTimeout>
// 动态计算table高度
const resizeObserver = new ResizeObserver(entries => {
for (const entry of entries) {
if (throttle) {
clearTimeout(throttle)
}
throttle = setTimeout(() => {
resizeHandler()
}, 100)
}
})
2024-02-19 13:44:32 +08:00
onMounted(() => {
initChart()
resizeObserver.observe(chartRef.value!)
2024-02-19 13:44:32 +08:00
})
defineExpose({ initChart })
onBeforeUnmount(() => {
resizeObserver.unobserve(chartRef.value!)
2024-02-19 13:44:32 +08:00
chart?.dispose()
})
watch(
() => props.options,
(newVal, oldVal) => {
initChart()
}
)
</script>
2024-02-28 15:26:31 +08:00
<style lang="scss" scoped>
2024-02-19 13:44:32 +08:00
.my-chart {
height: 100%;
width: 100%;
}
</style>