index.vue
8.19 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<template>
<!-- 原生方式,没有使用vue-echarts -->
<n-space vertical>
<n-spin :show="show">
<div :style="`width:${w}px;height:${h}px;`" ref="map3DRef"></div>
</n-spin>
</n-space>
</template>
<script setup lang="ts">
import { onMounted, ref, nextTick, PropType, toRefs, watch, reactive } from 'vue'
import * as echarts from 'echarts'
import { registerMap } from 'echarts/core'
import 'echarts-gl'
import config, { areaEnum } from './config'
import { getGeoJsonMap } from '@/api/external/common'
import dataMaps from './data.json'
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 { w, h } = toRefs(props.chartConfig.attr)
const map3DRef = ref()
const show = ref(true)
const chartInstance = 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 = async () => {
if (props.chartConfig.option.drillingIn) {
//如果是从右边配置里设置的,比如点击四川省,然后点击返回
const savePopParent = saveHistoryParent.value.pop()
let saveAdcode: any = savePopParent?.adcode
saveLevelStr.level = savePopParent?.level as string
if (!savePopParent) {
saveAdcode = getParentAdcode(props.chartConfig.option.mapRegion.adcode)
saveLevelStr.level = (regionMapParentArea as any)[props.chartConfig.option.mapRegion.saveSelect.levelStr]
}
if (saveAdcode === 0) {
saveAdcode = 'china'
saveLevelStr.level = 'COUNTRY'
}
await getGeojson(saveAdcode)
const adcode = saveAdcode === 100000 ? 'china' : saveAdcode
props.chartConfig.option.geo3D.map = adcode
props.chartConfig.option.series.forEach((item: any) => {
if (item.type === 'map3D') item.map = adcode
item.data = props.chartConfig.option.dataset
})
handleSetOption(chartInstance.value, props.chartConfig.option)
handleDataPoint(adcode)
}
}
const regionMapParentArea = {
[areaEnum.PROVINCE]: areaEnum.COUNTRY, //省份的上一级 中国
[areaEnum.CITY]: areaEnum.PROVINCE, //城市的上一级 省份
[areaEnum.COUNTY]: areaEnum.CITY, //县或者区的上一级 城市
[areaEnum.TOWN]: areaEnum.COUNTY //镇的上一级 县或者区
}
//地图点击
const handleMap3DClick = async (params: any) => {
if (props.chartConfig.option.drillingIn) {
const { name } = params
saveGeojson.value?.features.forEach((item: any) => {
if (item.properties.name === name) {
const level = item.properties.level.toUpperCase()
if (level === 'DISTRICT') return
const adcode = item.properties.adcode
props.chartConfig.option.mapRegion.adcode = adcode
saveLevelStr.level = level
handleDataPoint(adcode)
saveHistoryParent.value.push({
adcode: item.properties.parent.adcode,
level: (regionMapParentArea as any)[level]
})
}
})
}
}
const saveGeojson: any = ref({}) // 保存geojson
const chinaDefaultRegionId = ref(100000) //如果是china则adcode为100000
const saveLevelStr = reactive({
// 地区级别
level: ''
})
//父级地区编码和级别接口
interface HistoryParentType {
adcode: number
level: string
}
const saveHistoryParent = ref<HistoryParentType[]>([])
//动态注册地图
const getGeojson = (regionId: any) => {
try {
return new Promise<boolean>(resolve => {
const { levelStr } = props.chartConfig.option.mapRegion.saveSelect //右侧配置项获取的行政级别
getGeoJsonMap(
regionId === 'china' ? chinaDefaultRegionId.value : regionId,
!saveLevelStr.level ? levelStr : saveLevelStr.level
).then(res => {
const { geoJson, name, code, level } = res.data
const geoJsonFile = JSON.parse(geoJson)
if (!geoJsonFile) return
saveGeojson.value = geoJsonFile
const nameChina = name === '中国' ? 'china' : name
registerMap(level === areaEnum.COUNTRY ? nameChina : code, { geoJSON: geoJsonFile as any, specialAreas: {} })
show.value = false
resolve(true)
})
})
} catch (error) {
show.value = false
console.error('注册地图出错', error)
//注册出错则注册空的,不然在选择正确的adcode,则视图无法更新
registerMap(props.chartConfig.option.mapRegion.adcode, { geoJSON: {} as any, specialAreas: {} })
}
}
//传adcode 获取上级
const getParentAdcode = (adcode: number) => {
let adcodeNum = 100000
saveGeojson.value?.features.forEach((item: any) => {
if (item.properties.adcode === adcode) {
adcodeNum = item.properties.parent.adcode
}
})
return adcodeNum
}
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) => {
handleMap3DClick(e)
})
}
// 手动触发渲染
const handleSetOption = (instance: any, option: any) => {
if (!instance) return
try {
instance.clear()
instance.setOption(option)
} catch (error) {
console.error('触发渲染出错', error)
}
}
onMounted(() => {
initMap()
})
//处理数据标点
const handleDataPoint = (newData: any) => {
if (newData === 'china') {
props.chartConfig.option.dataset = dataMaps
} else {
props.chartConfig.option.dataset = dataMaps.filter((item: any) => item.adcode === newData)
}
}
//监听地图展示区域发生变化
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)
handleDataPoint(newData)
} 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>