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

163 lines
4.3 KiB
Vue
Raw Normal View History

2023-12-26 14:15:05 +08:00
<!-- 地图组件 -->
<template>
2023-12-29 11:50:53 +08:00
<div class="bars_w" ref="chartMap" id="chartMap"></div>
2023-12-26 14:15:05 +08:00
</template>
2023-12-29 11:50:53 +08:00
<script setup lang="ts">
import { onBeforeUnmount, ref, nextTick, onMounted, defineEmits } from 'vue'
2023-12-26 14:15:05 +08:00
import * as echarts from 'echarts'
2023-12-29 11:50:53 +08:00
import chinaJson from '@/assets/map/zh.json'
import axios from 'axios'
2023-12-26 14:15:05 +08:00
const props = defineProps({
2023-12-29 11:50:53 +08:00
datas: {
type: Array,
required: true
}
2023-12-26 14:15:05 +08:00
})
2023-12-29 11:50:53 +08:00
2023-12-26 14:15:05 +08:00
const myCharts = ref()
2023-12-29 11:50:53 +08:00
const mapJson = ref()
const fetchConfig = async (name: string) => {
const res = await import(`../../assets/map/${name}.json`)
return res.default
// GetEchar(res.default)
}
// fetchConfig()
const emit = defineEmits(['getRegionByRegionId'])
2023-12-26 14:15:05 +08:00
onMounted(() => {
2023-12-29 11:50:53 +08:00
GetEchar()
2023-12-26 14:15:05 +08:00
})
2023-12-29 11:50:53 +08:00
const GetEchar = async () => {
let chartDom = document.getElementById('chartMap')
2023-12-26 14:15:05 +08:00
myCharts.value = echarts.init(chartDom)
2023-12-29 11:50:53 +08:00
echarts.registerMap('china', await fetchConfig('中国')) //注册可用的地图
var option = {
2023-12-26 14:15:05 +08:00
geo: {
map: 'china',
2023-12-29 11:50:53 +08:00
zoom: 1,
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-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
//设置默认选中高亮部分
let index = 11
myCharts.value.dispatchAction({
2023-12-29 11:50:53 +08:00
type: 'highlight',
seriesIndex: 0,
dataIndex: index
2023-12-26 14:15:05 +08:00
})
// 点击事件
2023-12-29 11:50:53 +08:00
myCharts.value.on('click', e => {
emit('getRegionByRegionId', e.data)
2023-12-26 14:15:05 +08:00
myCharts.value.dispatchAction({
2023-12-29 11:50:53 +08:00
type: 'downplay',
seriesIndex: 0,
dataIndex: 0
2023-12-26 14:15:05 +08:00
})
2023-12-29 11:50:53 +08:00
if (e.dataIndex != index) {
myCharts.value.dispatchAction({
type: 'downplay',
seriesIndex: 0,
dataIndex: index
})
}
myCharts.value.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: e.dataIndex
})
index = e.dataIndex
2023-12-26 14:15:05 +08:00
})
// 当鼠标离开时
myCharts.value.on('mouseout', function (e) {
2023-12-29 11:50:53 +08:00
myCharts.value.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: index
})
2023-12-26 14:15:05 +08:00
})
}
2023-12-29 11:50:53 +08:00
const resizeHandler = () => {
myCharts.value?.resize()
}
onBeforeUnmount(() => {
window.removeEventListener('resize', resizeHandler)
myCharts.value?.dispose()
})
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
}
</style>