index.vue
5.17 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<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 cityMap from '../OverrideMapBase/mapGeojson/china-main-city-map.json'
type historyDataType = { name: string; code: string }
const props = defineProps({
chartConfig: {
type: Object as PropType<config>,
required: true
}
})
const iconStr = ref(
'path://M853.333333 245.333333H245.333333l93.866667-93.866666c12.8-12.8 12.8-34.133333 0-46.933334-12.8-12.8-34.133333-12.8-46.933333 0l-145.066667 145.066667c-12.8 12.8-12.8 34.133333 0 46.933333l145.066667 145.066667c6.4 6.4 14.933333 10.666667 23.466666 10.666667s17.066667-4.266667 23.466667-10.666667c12.8-12.8 12.8-34.133333 0-46.933333L256 311.466667h597.333333c6.4 0 10.666667 4.266667 10.666667 10.666666v426.666667c0 6.4-4.266667 10.666667-10.666667 10.666667H170.666667c-17.066667 0-32 14.933333-32 32s14.933333 32 32 32h682.666666c40.533333 0 74.666667-34.133333 74.666667-74.666667V320c0-40.533333-34.133333-74.666667-74.666667-74.666667z'
)
const historyData = ref<historyDataType[]>([])
const { w, h } = toRefs(props.chartConfig.attr)
const map3DRef = ref()
const chartInstance = ref()
const saveSelectValue = ref('')
const toolBoxOption = ref({
show: true,
right: 110,
top: 20,
feature: {
myFullButton: {
show: true,
title: '返回',
icon: iconStr.value,
iconStyle: {
color: ''
},
onclick: () => watchAdcode()
}
}
})
watch(
() => props.chartConfig.option,
newData => {
const { iconColor, drillingIn, iconDistanceRight, iconDistanceTop } = newData
toolBoxOption.value.feature.myFullButton.show = drillingIn
toolBoxOption.value.feature.myFullButton.iconStyle.color = iconColor
toolBoxOption.value.right = iconDistanceRight
toolBoxOption.value.top = iconDistanceTop
},
{
deep: true
}
)
props.chartConfig.option = {
...props.chartConfig.option,
...{ toolbox: toolBoxOption.value }
}
//地图点击返回
const watchAdcode = () => {
if (props.chartConfig.option.drillingIn) {
const code = historyData.value.at(-2)?.code
props.chartConfig.option.mapRegion.adcode = code ? code : 'china'
historyData.value.pop()
}
}
//地图点击
const handleMap3DClick = async (params: any) => {
if (props.chartConfig.option.drillingIn) {
const { name } = params
saveSelectValue.value = name
const findAdcode = (cityMap as any)[name]
if (!findAdcode) return
props.chartConfig.option.mapRegion.adcode = findAdcode
historyData.value.push({
name,
code: findAdcode
})
}
}
//动态获取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(() => {
handleSetOption(chartInstance.value, props.chartConfig.option)
})
chartInstance.value.on('click', (e: any) => {
console.log(`地图点击`, e)
// handleMap3DClick(e)
})
}
// 手动触发渲染
const handleSetOption = (instance: any, option: any) => {
if (!instance) return
try {
instance.clear()
instance.setOption(option)
// eslint-disable-next-line no-empty
} finally {
}
}
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 = props.chartConfig.option.dataset
}
})
handleSetOption(chartInstance.value, props.chartConfig.option)
} catch (error) {
console.log(error)
}
},
{
immediate: true
}
)
// 监听地图右侧配置项变化
watch(
props.chartConfig.option,
async newData => {
try {
handleSetOption(chartInstance.value, newData)
} catch (error) {
console.log(error)
}
},
{
deep: true
}
)
// 监听地图dataset配置项变化
watch(
() => props.chartConfig.option.dataset,
newData => {
try {
props.chartConfig.option.series.forEach((item: any) => {
if (item.type === 'map3D') {
item.data = newData
}
})
handleSetOption(chartInstance.value, props.chartConfig.option)
} catch (error) {
console.log(error)
}
},
{
deep: true
}
)
</script>