index.vue
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<template>
<!-- 原生方式,没有使用vue-echarts -->
<div :style="`width:${w}px;height:${h}px;`" ref="map3DRef"></div>
</template>
<script setup lang="ts">
import { onMounted, ref, nextTick, PropType, toRefs, watch } from 'vue'
import * as echarts from 'echarts'
import { registerMap } from 'echarts/core'
import 'echarts-gl'
import config from './config'
import { dataMaps } from './mock'
const props = defineProps({
chartConfig: {
type: Object as PropType<config>,
required: true
}
})
const { w, h } = toRefs(props.chartConfig.attr)
const map3DRef = ref()
const chartInstance = ref()
//动态获取geojson文件进行注册地图
const getGeojson = (regionId: string) => {
try {
return new Promise<boolean>(resolve => {
import(`../OverrideMapBase/mapGeojson/${regionId}.json`).then(data => {
registerMap(regionId, { geoJSON: data.default as any, specialAreas: {} })
resolve(true)
})
})
} catch (e) {
console.log(e)
}
}
watch(
() => w.value,
(value: number) => {
chartInstance.value.resize({
width: value + 'px',
height: h.value + 'px'
})
}
)
const initMap = async () => {
chartInstance.value = echarts.init(map3DRef.value)
await nextTick()
await getGeojson(props.chartConfig.option.mapRegion.adcode)
await nextTick().then(() => {
vEchartsSetOption(props.chartConfig.option)
})
chartInstance.value.on('click', (e: any) => {
console.log(e)
})
}
// 手动触发渲染
const vEchartsSetOption = (option: any) => {
chartInstance.value.clear()
chartInstance.value.setOption(option)
}
onMounted(() => {
initMap()
})
//监听地图展示区域发生变化
watch(
() => `${props.chartConfig.option.mapRegion.adcode}`,
async (newData: any) => {
try {
await getGeojson(newData)
props.chartConfig.option.geo3D.map = newData
props.chartConfig.option.series.forEach((item: any) => {
if (item.type === 'map3D') {
item.map = newData
item.data = dataMaps
}
})
initMap()
vEchartsSetOption(props.chartConfig.option)
} catch (error) {
console.log(error)
}
},
{
immediate: true
}
)
// 监听地图右侧配置项变化
watch(
props.chartConfig.option,
newData => {
try {
nextTick(() => {
initMap()
vEchartsSetOption(newData)
})
} catch (error) {
console.log(error)
}
},
{
deep: true
}
)
</script>