Echarts中国地图
# 效果图

# 所需文件
# 组件源码
<template>
<div id="mapChart"></div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import * as echarts from 'echarts'
import chinaJson from './china.json'
const provinceData = ref([]);
const geoCoordMap = ref({})
chinaJson.features.forEach(e => {
if(e.properties.name) {
const name = e.properties.name;
// 地区经纬度
geoCoordMap.value[name] = e.properties.center;
provinceData.value.push({
name: e.properties.name,
value: parseInt(Math.random() * 1000)
})
}
})
const convertData = (data) => {
let res = [];
for (let i = 0; i < data.length; i++) {
let geoCoord = geoCoordMap.value[data[i].name];
if (geoCoord) {
res.push({
name: data[i].name,
value: geoCoord.concat(data[i].value),
});
}
}
return res;
}
const options = {
backgroundColor: '#0F1C3C',
tooltip: {
show: true,
formatter: function(params) {
if (params.value.length > 1) {
return `${params.name} ${params.value[2]} 人`
} else {
return `${params.name} ${params.value ? params.value + '人' : ''}`
}
},
},
geo: {
map: 'china',
show: true,
roam: false,
label: {
emphasis: {
show: false
}
},
layoutSize: "100%",
itemStyle: {
normal: {
borderColor: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: '#00F6FF'
}, {
offset: 1,
color: '#53D9FF'
}], false),
borderWidth: 3,
shadowColor: 'rgba(10,76,139,1)',
shadowOffsetY: 0,
shadowBlur: 60
}
}
},
series: [{
symbolSize: 1,
label: {
normal: {
formatter: function(para) {
return '{name|' + para.data.name + '}'
},
rich: {
cnNum: {
fontSize: 14,
color: '#D4EEFF',
align: 'center',
},
name: {
fontSize: 12,
color: '#D4EEFF',
align: 'center',
lineHeight:20,
},
},
color: '#D4EEFF',
show: true
},
emphasis: {
show: true
}
},
itemStyle: {
normal: {
color: '#D4EEFF'
}
},
name: 'light',
type: 'scatter',
coordinateSystem: 'geo',
data: convertData(provinceData.value),
}, {
type: 'map',
map: 'china',
aspectScale: 0.75,
label: {
normal: {
show: false
},
emphasis: {
show: false,
}
},
itemStyle: {
normal: {
areaColor: {
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [{
offset: 0,
color: '#073684' // 0% 处的颜色
}, {
offset: 1,
color: '#061E3D' // 100% 处的颜色
}],
},
borderColor: '#215495',
borderWidth: 1,
},
emphasis: {
areaColor: {
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [{
offset: 0,
color: '#073684' // 0% 处的颜色
}, {
offset: 1,
color: '#061E3D' // 100% 处的颜色
}],
},
}
},
data: provinceData.value,
}]
}
const renderEcharts = (elementId, options, isMap = true) => {
if(isMap){
echarts.registerMap('china', chinaJson)
}
let myChart = echarts.getInstanceByDom(document.getElementById(elementId))
if (!myChart) {
myChart = echarts.init(document.getElementById(elementId));
}
myChart.setOption(options);
window.addEventListener("resize", () => myChart && myChart.resize());
}
onMounted(() => {
renderEcharts('mapChart', options)
})
</script>
<style lang="less" scoped>
#mapChart {
width: 80%;
height: 800px;
}
</style>
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
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
上次更新: 2023/08/07, 10:37:54