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

210 lines
5.7 KiB
Vue
Raw Normal View History

2023-12-26 14:15:05 +08:00
<!-- 地图组件 -->
<template>
2023-12-29 16:25:26 +08:00
<div style="position: relative">
<div class="bars_w" ref="chartMap" id="chartMap"></div>
<span @click="circle" v-show="showCircle" class="iconfont icon-back" style="color: #003078"></span>
</div>
2023-12-26 14:15:05 +08:00
</template>
2023-12-29 11:50:53 +08:00
<script setup lang="ts">
2023-12-29 16:25:26 +08:00
import { onBeforeUnmount, ref, watch, onMounted, defineEmits } from 'vue'
2024-01-02 16:34:56 +08:00
import * as echarts from 'echarts4'
import { useDictData } from '@/stores/dictData'
const dictData = useDictData()
2023-12-29 16:25:26 +08:00
const props = defineProps(['options'])
2023-12-26 14:15:05 +08:00
const myCharts = ref()
2023-12-29 16:25:26 +08:00
const showCircle = ref(false)
2023-12-29 11:50:53 +08:00
const fetchConfig = async (name: string) => {
const res = await import(`../../assets/map/${name}.json`)
return res.default
// GetEchar(res.default)
}
// fetchConfig()
2024-01-02 16:34:56 +08:00
const emit = defineEmits(['getRegionByRegion'])
2023-12-29 16:25:26 +08:00
onMounted(() => {})
2023-12-26 14:15:05 +08:00
2023-12-29 16:25:26 +08:00
const GetEchar = async (name: string) => {
2023-12-29 11:50:53 +08:00
let chartDom = document.getElementById('chartMap')
2023-12-29 16:25:26 +08:00
myCharts.value?.resize()
2023-12-26 14:15:05 +08:00
myCharts.value = echarts.init(chartDom)
2023-12-29 13:22:27 +08:00
echarts.registerMap('china', await fetchConfig(name)) //注册可用的地图
2023-12-29 16:25:26 +08:00
let option = {
title: {
left: 'center',
top: '3%',
...props.options.title
},
tooltip: {
trigger: 'item',
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)',
...(props.options.tooltip || null)
},
legend: {
orient: 'vertical',
left: 26,
bottom: 40,
itemWidth: 16,
itemHeight: 16,
...(props.options.legend || null)
},
color: [
...(props.options.color || ''),
'#07CCCA ',
'#00BFF5',
'#FFBF00',
'#77DA63',
'#D5FF6B',
'#Ff6600',
'#FF9100',
'#5B6E96',
'#66FFCC',
'#B3B3B3'
],
2023-12-26 14:15:05 +08:00
geo: {
map: 'china',
2023-12-29 11:50:53 +08:00
zoom: 1,
2023-12-29 16:25:26 +08:00
2023-12-29 11:50:53 +08:00
roam: true,
2023-12-26 14:15:05 +08:00
label: {
2023-12-29 11:50:53 +08:00
normal: {
show: true,
fontSize: '14',
color: 'rgba(0,0,0,0.7)'
2023-12-26 14:15:05 +08:00
}
},
itemStyle: {
2023-12-29 11:50:53 +08:00
normal: {
color: 'rgba(51, 69, 129, .8)', //地图背景色
borderColor: '#999999',
borderWidth: 1,
areaColor: {
type: 'radial',
x: 0.5,
y: 0.5,
r: 0.8,
colorStops: [
{
offset: 0,
color: 'rgba(147, 235, 248, 0)' // 0% 处的颜色
},
{
offset: 1,
color: 'rgba(147, 135, 148, .2)' // 100% 处的颜色
}
],
globalCoord: false // 缺省为 false
},
shadowColor: 'rgba(128, 217, 248, 1)',
// shadowColor: 'rgba(255, 255, 255, 1)',
shadowOffsetX: -2,
shadowOffsetY: 2,
shadowBlur: 10
},
emphasis: {
areaColor: '#ccc',
shadowOffsetX: 0,
shadowOffsetY: 0,
borderWidth: 0
}
2023-12-26 14:15:05 +08:00
},
2023-12-29 11:50:53 +08:00
regions: [
{
name: '南海诸岛',
itemStyle: {
// 隐藏地图
normal: {
opacity: 0 // 为 0 时不绘制该图形
}
},
label: {
show: false // 隐藏文字
}
}
]
2023-12-29 16:25:26 +08:00
},
...props.options.options
2023-12-29 11:50:53 +08:00
}
2023-12-26 14:15:05 +08:00
2023-12-29 11:50:53 +08:00
myCharts.value.setOption(option)
window.addEventListener('resize', resizeHandler)
2023-12-26 14:15:05 +08:00
// 点击事件
2023-12-29 16:25:26 +08:00
myCharts.value.off('click')
myCharts.value.on('click', (e: any) => {
if (name == '中国' && e.componentIndex == 0) {
GetEchar(e.name)
2024-01-02 16:34:56 +08:00
MapReturn(e.name)
2023-12-29 16:25:26 +08:00
showCircle.value = true
2024-01-02 16:34:56 +08:00
// console.log('🚀 ~ file: MyEchartMap.vue:156 ~ myCharts.value.on ~ MapReturn(e.name):', MapReturn(e.name))
2023-12-29 11:50:53 +08:00
}
2023-12-26 14:15:05 +08:00
})
}
2024-01-02 16:34:56 +08:00
const MapReturn = (name: string) => {
let area = dictData.state.area[0].children
let list = {}
// break;
for (let i = 0; i < area.length; i++) {
if (area[i].name == name) {
list = area[i]
break
} else {
list = dictData.state.area[0]
}
}
emit('getRegionByRegion', list)
}
2023-12-29 16:25:26 +08:00
// 返回
const circle = () => {
GetEchar('中国')
showCircle.value = false
}
2023-12-29 11:50:53 +08:00
const resizeHandler = () => {
myCharts.value?.resize()
}
onBeforeUnmount(() => {
window.removeEventListener('resize', resizeHandler)
myCharts.value?.dispose()
})
2024-01-02 16:34:56 +08:00
defineExpose({ GetEchar })
2023-12-29 16:25:26 +08:00
watch(
() => props.options,
(newVal, oldVal) => {
2024-01-02 16:34:56 +08:00
// GetEchar('中国')
2023-12-29 16:25:26 +08:00
}
)
2023-12-26 14:15:05 +08:00
</script>
<style lang="scss" scoped>
.bars_w {
2023-12-29 11:50:53 +08:00
width: 100%;
height: 100%;
2023-12-26 14:15:05 +08:00
}
2023-12-29 16:25:26 +08:00
.iconfont {
cursor: pointer;
position: absolute;
top: 10px;
left: 10px;
z-index: 2;
font-size: 20px;
}
2023-12-26 14:15:05 +08:00
</style>