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'
|
2024-01-12 09:55:55 +08:00
|
|
|
// import echarts from './echarts'
|
2024-01-18 20:29:38 +08:00
|
|
|
import * as echarts from 'echarts' // 全引入
|
2024-01-12 09:55:55 +08:00
|
|
|
|
2023-12-26 14:15:05 +08:00
|
|
|
import 'echarts/lib/component/dataZoom'
|
2024-01-11 16:06:05 +08:00
|
|
|
|
2023-12-29 11:50:53 +08:00
|
|
|
const chartRef = ref<HTMLDivElement>()
|
2023-12-26 14:15:05 +08:00
|
|
|
|
|
|
|
|
const props = defineProps(['options'])
|
2024-01-02 13:45:20 +08:00
|
|
|
let chart: echarts.ECharts | any = null
|
2023-12-26 14:15:05 +08:00
|
|
|
const resizeHandler = () => {
|
2024-01-05 16:32:18 +08:00
|
|
|
chart.getZr().painter.getViewportRoot().style.display = 'none'
|
2024-01-02 13:45:20 +08:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
|
chart.resize()
|
2024-01-05 16:32:18 +08:00
|
|
|
chart.getZr().painter.getViewportRoot().style.display = ''
|
2024-01-02 13:45:20 +08:00
|
|
|
})
|
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: {
|
|
|
|
|
left: 'center',
|
|
|
|
|
textStyle: {
|
|
|
|
|
color: '#000',
|
|
|
|
|
fontSize: 18
|
2023-12-29 16:25:26 +08:00
|
|
|
},
|
2024-01-05 16:32:18 +08:00
|
|
|
...(props.options.title || null)
|
2023-12-27 15:06:23 +08:00
|
|
|
},
|
2023-12-26 14:15:05 +08:00
|
|
|
tooltip: {
|
|
|
|
|
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)',
|
2023-12-29 16:25:26 +08:00
|
|
|
borderWidth: 0,
|
|
|
|
|
...(props.options.tooltip || null)
|
2023-12-27 15:06:23 +08:00
|
|
|
},
|
|
|
|
|
legend: {
|
2023-12-28 14:23:22 +08:00
|
|
|
right: 20,
|
2023-12-27 15:06:23 +08:00
|
|
|
top: 0,
|
2023-12-28 14:23:22 +08:00
|
|
|
itemGap: 10,
|
2023-12-27 15:06:23 +08:00
|
|
|
itemStyle: {},
|
|
|
|
|
textStyle: {
|
2023-12-28 14:23:22 +08:00
|
|
|
fontSize: 12,
|
2023-12-27 15:06:23 +08:00
|
|
|
padding: [2, 0, 0, 0] //[上、右、下、左]
|
2023-12-28 14:23:22 +08:00
|
|
|
},
|
|
|
|
|
itemWidth: 15,
|
2023-12-29 16:25:26 +08:00
|
|
|
itemHeight: 10,
|
|
|
|
|
...(props.options.legend || null)
|
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',
|
2024-01-02 16:34:56 +08:00
|
|
|
right: '60px',
|
2023-12-26 14:15:05 +08:00
|
|
|
bottom: '40px',
|
|
|
|
|
containLabel: true
|
|
|
|
|
},
|
2024-01-11 16:06:05 +08:00
|
|
|
xAxis: handlerXAxis(),
|
|
|
|
|
yAxis: handlerYAxis(),
|
2023-12-26 14:15:05 +08:00
|
|
|
dataZoom: [
|
|
|
|
|
{
|
|
|
|
|
type: 'inside',
|
|
|
|
|
height: 13,
|
|
|
|
|
start: 0,
|
|
|
|
|
bottom: '20px',
|
2024-01-05 16:32:18 +08:00
|
|
|
end: 100,
|
|
|
|
|
...(props.options.dataZoom || null)
|
2023-12-26 14:15:05 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
start: 0,
|
|
|
|
|
height: 13,
|
|
|
|
|
bottom: '20px',
|
2024-01-05 16:32:18 +08:00
|
|
|
end: 100,
|
2024-01-11 16:06:05 +08:00
|
|
|
...(props.options.dataZoom || null)
|
2023-12-26 14:15:05 +08:00
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
}
|
2024-01-11 16:06:05 +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 {
|
2024-01-18 20:29:38 +08:00
|
|
|
...temp,
|
|
|
|
|
...props.options.yAxis
|
2024-01-11 16:06:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
|
...item,
|
|
|
|
|
...temp
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
2024-01-18 20:29:38 +08:00
|
|
|
...temp,
|
|
|
|
|
...props.options.xAxis
|
2024-01-11 16:06:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-26 14:15:05 +08:00
|
|
|
onMounted(() => {
|
2024-01-02 13:45:20 +08:00
|
|
|
initChart()
|
2023-12-26 14:15:05 +08:00
|
|
|
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>
|