学习过程中将笔记整理跟大家分享,希望对大家也有所帮助,共同成长进步💪~
如果大家喜欢,可以点赞或留言💕~~,谢谢大家⭐️⭐️⭐️~
功能是中国地图和世界地图的展示,并可以互相切换的功能。(无法上传视频格式没有做动图,具体效果大家可以复制代码到本地实操看)
效果图如下:
准备工作:
首先先引入echars.js、china.js、world.js
html部分
分别写两个容器放地图,一个中国地图容器、一个放世界地图容器
<div class="chinaMap" ref="chinaMap"></div>
<div class="miniWorldMap" ref="miniWorldMap"></div>
地图部分
主要是chinaConfigure、worldConfigure、chgMap、三个函数,代码上有备注,这里不再重复做讲解了,以往发过一篇带下钻的地图有过详细介绍地图一步步创建讲解等介绍,基本上大同小异,大家也可以去看😜
data: function () {
return {
chinaMap: "",//中国地图
worldMap: "",//世界地图
worldoption: {},
chinaoption: {},
}
},
mounted: function () {
setTimeout(() => {
this.$nextTick(function () {
this.chinaConfigure();
this.worldConfigure();
});
}, 0)
},
methods: {
// 中国地图配置代码
chinaConfigure() {
this.chinaMap = echarts.init(document.querySelector('.chinaMap'));
var data = [
{ name: "台湾", value: 1, ndtzjh: 188, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "北京", value: 2, ndtzjh: 488, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "天津", value: 3, ndtzjh: 288, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "河北", value: 4, ndtzjh: 388, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "山西", value: 5, ndtzjh: 188, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "内蒙古", value: 6, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "辽宁", value: 7, ndtzjh: 488, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "吉林", value: 8, ndtzjh: 588, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "黑龙江", value: 9, ndtzjh: 688, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "上海", value: 10, ndtzjh: 788, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "江苏", value: 11, ndtzjh: 388, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "浙江", value: 12, ndtzjh: 58, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "安徽", value: 13, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "福建", value: 14, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "江西", value: 15, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "山东", value: 16, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "河南", value: 17, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "湖北", value: 18, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "湖南", value: 19, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "重庆", value: 20, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "四川", value: 21, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "贵州", value: 22, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "云南", value: 23, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "西藏", value: 24, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "陕西", value: 25, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "甘肃", value: 26, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "青海", value: 27, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "宁夏", value: 28, ndtzjh: 118, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "新疆", value: 29, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "广东", value: 30, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "广西", value: 31, ndtzjh: 88, gsze: "3,330,156", xmsl: "9,01,302" },
{ name: "海南", value: 32, ndtzjh: 28, gsze: "3,330,156", xmsl: "9,01,302", },
];
window.onresize = this.chinaMap.resize //实现地图自适应
var geoCoordMap = {
'台湾': [122, 23],
'黑龙江': [126.661669, 45.742347],
'内蒙古': [110.3467, 41.4899],
"吉林": [125.8154, 44.2584],
'北京': [116.4551, 40.2539],
"辽宁": [123.1238, 42.1216],
"河北": [114.4995, 38.1006],
"天津": [117.4219, 39.4189],
"山西": [112.3352, 37.9413],
"陕西": [109.1162, 34.2004],
"甘肃": [103.5901, 36.3043],
"宁夏": [106.3586, 38.1775],
"青海": [101.4038, 36.8207],
"新疆": [87.9236, 43.5883],
"西藏": [88.388277, 31.56375],
"四川": [103.9526, 30.7617],
"重庆": [108.384366, 30.439702],
"山东": [117.1582, 36.8701],
"河南": [113.4668, 34.6234],
"江苏": [118.8062, 31.9208],
"安徽": [117.29, 32.0581],
"湖北": [114.3896, 30.6628],
"浙江": [119.5313, 29.8773],
"福建": [119.4543, 25.9222],
"江西": [116.0046, 28.6633],
"湖南": [113.0823, 28.2568],
"贵州": [106.6992, 26.7682],
"云南": [102.9199, 25.4663],
"广东": [113.12244, 23.009505],
"广西": [108.479, 23.1152],
"海南": [110.3893, 19.8516],
'上海': [121.4648, 31.2891],
};
//设置每个区域的值
var dotData = [1, 2, 3, 4, 5];
//给每个地区赋值
var convertData = function (data) {
//定义一个数组
var res = [];
//循环遍历每个区域值
for (var i = 0; i < data.length; i++) {
//获取坐标
var geoCoord = geoCoordMap[data[i].name];
//判断是否有坐标
if (geoCoord) {
//往数组里设置值
res.push({
name: data[i].name,
value: geoCoord.concat(i + 1),
ndtzjh: data[i].ndtzjh,//年度投资计划
gsze: data[i].gsze,//概算总额
xmsl: data[i].xmsl,//项目数量
});
}
}
return res;
};
this.chinaoption = {
backgroundColor: 'transparent',
geo: {
map: 'china',
roam: false,
zoom: 1.2,
label: {
normal: {
show: true, // 是否显示对应地名
textStyle: {
color: '#fff'
}
},
emphasis: { // 对应的鼠标悬浮效果
show: true,
textStyle: {
color: '#fff'
}
}
},
itemStyle: {
normal: {
areaColor: "rgba(2,68,158, .5)",//地图颜色
borderWidth: 1,//设置外层边框
borderColor: "rgba(65,154,225, 1)",//地图外边框颜色
label: {
show: true,//是否显示标签
textStyle: {
color: "rgba(255,255,255,.5)"//地图文字的颜色
}
}
},
emphasis: {
areaColor: '#01215c',
}
},
regions: [
{
name: "南海诸岛",
itemStyle: {
// 隐藏地图南海诸岛
normal: {
// opacity: 0, // 为 0 时不绘制该图形
}
},
label: {
show: false // 隐藏文字
}
}
],
},
tooltip: {
trigger: 'item',
show: true,
backgroundColor: "transparent",
formatter: function (params) {
if (params.data) {
var htmlStr = '';
htmlStr += '<div class="bgboder">';
htmlStr += '<div id="city">' + params.name + '</div><div id="trans">年度投资计划:' + params.data.ndtzjh + '<span>万元</span>' + '</div><div id="trans">概算总额:' + params.data.gsze + '<span>万元</span>' + '</div><div id="trans">项目数量:' + params.data.xmsl + '<span>万元</span>' + '</div>';
htmlStr += '</div>';
return htmlStr;
} else {
}
},
textStyle: {
width: 300,
height: 300,
}
},
visualMap: {
type: 'piecewise',
inRange: {
color: ['#02F6F9', '#02F6F9', '#02F6F9']
},
pieces: [
{ min: 0, max: 99, color: 'transparent' }, //把地图小点设置成透明颜色
{
min: 100,
max: 10000,
color: {
type: 'radial',
x: 0.5,
y: 0.5,
r: 0.5,
colorStops: [
{ offset: 0, color: 'rgba(11,28,92,0)' },
{ offset: 0.8, color: 'rgba(10,94,137, 0.5)' },
{ offset: 1, color: 'rgba(2, 246, 249, 1)' }
],
globalCoord: false // 缺省为 false
}
}
],
show: false,
textStyle: {
color: '#ffffff',
fontSize: 16
}
},
series: [
{
type: 'scatter',
coordinateSystem: 'geo',
data: convertData(data),
},
]
}
this.chinaMap.setOption(this.chinaoption);
window.onresize = () => {
this.chinaMap.resize()
}
window.addEventListener('resize', () => {
this.chinaMap.resize()
})
},
// 世界地图
worldConfigure() {
this.worldMap = echarts.init(document.querySelector('.miniWorldMap'));
this.worldoption = {
series: [
{
name: 'World Population (2010)',
type: 'map',
mapType: 'world',
clickable: false,
// selectedMode:true,
silent: true,
// zoom:2,//地图放大1.2倍
// roam: true,
itemStyle: {
normal: {
areaColor: '#1044A9',
borderColor: 'none'
},
emphasis: {//对应的鼠标悬浮效果
show: true,
itemStyle: { areaColor: '#1044A9' }
}
},
label: {
show: false,
},
emphasis: {
show: false
},
data: [
{ name: 'Afghanistan', value: 28397.812 },
{ name: 'Angola', value: 19549.124 },
{ name: 'Albania', value: 3150.143 },
{ name: 'United Arab Emirates', value: 8441.537 },
{ name: 'Argentina', value: 40374.224 },
{ name: 'Armenia', value: 2963.496 },
{ name: 'French Southern and Antarctic Lands', value: 268.065 },
{ name: 'Australia', value: 22404.488 },
{ name: 'Austria', value: 8401.924 },
{ name: 'Azerbaijan', value: 9094.718 },
{ name: 'Burundi', value: 9232.753 },
{ name: 'Belgium', value: 10941.288 },
{ name: 'Benin', value: 9509.798 },
{ name: 'Burkina Faso', value: 15540.284 },
{ name: 'Bangladesh', value: 151125.475 },
{ name: 'Bulgaria', value: 7389.175 },
{ name: 'The Bahamas', value: 66402.316 },
{ name: 'Bosnia and Herzegovina', value: 3845.929 },
{ name: 'Belarus', value: 9491.07 },
{ name: 'Belize', value: 308.595 },
{ name: 'Bermuda', value: 64.951 },
{ name: 'Bolivia', value: 716.939 },
{ name: 'Brazil', value: 195210.154 },
{ name: 'Brunei', value: 27.223 },
{ name: 'Bhutan', value: 716.939 },
{ name: 'Botswana', value: 1969.341 },
{ name: 'Central African Republic', value: 4349.921 },
{ name: 'Canada', value: 34126.24 },
{ name: 'Switzerland', value: 7830.534 },
{ name: 'Chile', value: 17150.76 },
{ name: 'China', value: 1359821.465 },
{ name: 'Ivory Coast', value: 60508.978 },
{ name: 'Cameroon', value: 20624.343 },
{ name: 'Democratic Republic of the Congo', value: 62191.161 },
{ name: 'Republic of the Congo', value: 3573.024 },
{ name: 'Colombia', value: 46444.798 },
{ name: 'Costa Rica', value: 4669.685 },
{ name: 'Cuba', value: 11281.768 },
{ name: 'Northern Cyprus', value: 1.468 },
{ name: 'Cyprus', value: 1103.685 },
{ name: 'Czech Republic', value: 10553.701 },
{ name: 'Germany', value: 83017.404 },
{ name: 'Djibouti', value: 834.036 },
{ name: 'Denmark', value: 5550.959 },
{ name: 'Dominican Republic', value: 10016.797 },
{ name: 'Algeria', value: 37062.82 },
{ name: 'Ecuador', value: 15001.072 },
{ name: 'Egypt', value: 78075.705 },
{ name: 'Eritrea', value: 5741.159 },
{ name: 'Spain', value: 46182.038 },
{ name: 'Estonia', value: 1298.533 },
{ name: 'Ethiopia', value: 87095.281 },
{ name: 'Finland', value: 5367.693 },
{ name: 'Fiji', value: 860.559 },
{ name: 'Falkland Islands', value: 49.581 },
{ name: 'France', value: 63230.866 },
{ name: 'Gabon', value: 1556.222 },
{ name: 'United Kingdom', value: 62066.35 },
{ name: 'Georgia', value: 4388.674 },
{ name: 'Ghana', value: 24262.901 },
{ name: 'Guinea', value: 10876.033 },
{ name: 'Gambia', value: 1680.64 },
{ name: 'Guinea Bissau', value: 10876.033 },
{ name: 'Equatorial Guinea', value: 696.167 },
{ name: 'Greece', value: 11109.999 },
{ name: 'Greenland', value: 56.546 },
{ name: 'Guatemala', value: 14341.576 },
{ name: 'French Guiana', value: 231.169 },
{ name: 'Guyana', value: 786.126 },
{ name: 'Honduras', value: 7621.204 },
{ name: 'Croatia', value: 4338.027 },
{ name: 'Haiti', value: 9896.4 },
{ name: 'Hungary', value: 10014.633 },
{ name: 'Indonesia', value: 240676.485 },
{ name: 'India', value: 1205624.648 },
{ name: 'Ireland', value: 4467.561 },
{ name: 'Iran', value: 240676.485 },
{ name: 'Iraq', value: 30962.38 },
{ name: 'Iceland', value: 318.042 },
{ name: 'Israel', value: 7420.368 },
{ name: 'Italy', value: 60508.978 },
{ name: 'Jamaica', value: 2741.485 },
{ name: 'Jordan', value: 6454.554 },
{ name: 'Japan', value: 127352.833 },
{ name: 'Kazakhstan', value: 15921.127 },
{ name: 'Kenya', value: 40909.194 },
{ name: 'Kyrgyzstan', value: 5334.223 },
{ name: 'Cambodia', value: 14364.931 },
{ name: 'South Korea', value: 51452.352 },
{ name: 'Kosovo', value: 97.743 },
{ name: 'Kuwait', value: 2991.58 },
{ name: 'Laos', value: 6395.713 },
{ name: 'Lebanon', value: 4341.092 },
{ name: 'Liberia', value: 3957.99 },
{ name: 'Libya', value: 6040.612 },
{ name: 'Sri Lanka', value: 20758.779 },
{ name: 'Lesotho', value: 2008.921 },
{ name: 'Lithuania', value: 3068.457 },
{ name: 'Luxembourg', value: 507.885 },
{ name: 'Latvia', value: 2090.519 },
{ name: 'Morocco', value: 31642.36 },
{ name: 'Moldova', value: 103.619 },
{ name: 'Madagascar', value: 21079.532 },
{ name: 'Mexico', value: 117886.404 },
{ name: 'Macedonia', value: 507.885 },
{ name: 'Mali', value: 13985.961 },
{ name: 'Myanmar', value: 51931.231 },
{ name: 'Montenegro', value: 620.078 },
{ name: 'Mongolia', value: 2712.738 },
{ name: 'Mozambique', value: 23967.265 },
{ name: 'Mauritania', value: 3609.42 },
{ name: 'Malawi', value: 15013.694 },
{ name: 'Malaysia', value: 28275.835 },
{ name: 'Namibia', value: 2178.967 },
{ name: 'New Caledonia', value: 246.379 },
{ name: 'Niger', value: 15893.746 },
{ name: 'Nigeria', value: 159707.78 },
{ name: 'Nicaragua', value: 5822.209 },
{ name: 'Netherlands', value: 16615.243 },
{ name: 'Norway', value: 4891.251 },
{ name: 'Nepal', value: 26846.016 },
{ name: 'New Zealand', value: 4368.136 },
{ name: 'Oman', value: 2802.768 },
{ name: 'Pakistan', value: 173149.306 },
{ name: 'Panama', value: 3678.128 },
{ name: 'Peru', value: 29262.83 },
{ name: 'Philippines', value: 93444.322 },
{ name: 'Papua New Guinea', value: 6858.945 },
{ name: 'Poland', value: 38198.754 },
{ name: 'Puerto Rico', value: 3709.671 },
{ name: 'North Korea', value: 1.468 },
{ name: 'Portugal', value: 10589.792 },
{ name: 'Paraguay', value: 6459.721 },
{ name: 'Qatar', value: 1749.713 },
{ name: 'Romania', value: 21861.476 },
{ name: 'Russia', value: 21861.476 },
{ name: 'Rwanda', value: 10836.732 },
{ name: 'Western Sahara', value: 514.648 },
{ name: 'Saudi Arabia', value: 27258.387 },
{ name: 'Sudan', value: 35652.002 },
{ name: 'South Sudan', value: 9940.929 },
{ name: 'Senegal', value: 12950.564 },
{ name: 'Solomon Islands', value: 526.447 },
{ name: 'Sierra Leone', value: 5751.976 },
{ name: 'El Salvador', value: 6218.195 },
{ name: 'Somaliland', value: 9636.173 },
{ name: 'Somalia', value: 9636.173 },
{ name: 'Republic of Serbia', value: 3573.024 },
{ name: 'Suriname', value: 524.96 },
{ name: 'Slovakia', value: 5433.437 },
{ name: 'Slovenia', value: 2054.232 },
{ name: 'Sweden', value: 9382.297 },
{ name: 'Swaziland', value: 1193.148 },
{ name: 'Syria', value: 7830.534 },
{ name: 'Chad', value: 11720.781 },
{ name: 'Togo', value: 6306.014 },
{ name: 'Thailand', value: 66402.316 },
{ name: 'Tajikistan', value: 7627.326 },
{ name: 'Turkmenistan', value: 5041.995 },
{ name: 'East Timor', value: 10016.797 },
{ name: 'Trinidad and Tobago', value: 1328.095 },
{ name: 'Tunisia', value: 10631.83 },
{ name: 'Turkey', value: 72137.546 },
{ name: 'United Republic of Tanzania', value: 44973.33 },
{ name: 'Uganda', value: 33987.213 },
{ name: 'Ukraine', value: 46050.22 },
{ name: 'Uruguay', value: 3371.982 },
{ name: 'United States of America', value: 312247.116 },
{ name: 'Uzbekistan', value: 27769.27 },
{ name: 'Venezuela', value: 236.299 },
{ name: 'Vietnam', value: 89047.397 },
{ name: 'Vanuatu', value: 236.299 },
{ name: 'West Bank', value: 13.565 },
{ name: 'Yemen', value: 22763.008 },
{ name: 'South Africa', value: 51452.352 },
{ name: 'Zambia', value: 13216.985 },
{ name: 'Zimbabwe', value: 13076.978 }
]
}
]
};
this.worldMap.setOption(this.worldoption);
window.onresize = () => {
this.worldMap.resize()
}
window.addEventListener('resize', () => {
this.worldMap.resize()
})
this.chgMap();
},
// 切换地图(注:onclick事件里的地图属性可以根据自己所需设置)
chgMap() {
var worldBox = document.querySelector(".miniWorldMap");
var flag = true;
var mid = this.chinaoption;
var _this = this;
worldBox.onclick = function () {
if (flag) {
_this.chinaMap.clear();//清空图表
_this.chinaMap.setOption(_this.worldoption);//把中国地图切换为世界地图
_this.worldMap.clear();//清空图表
//把中国地图上的文字隐藏
_this.chinaoption.geo.label.normal.show = false;
_this.chinaoption.geo.label.emphasis.show = false;
_this.chinaoption.tooltip.show = false;//隐藏提示悬浮框
_this.worldMap.setOption(_this.chinaoption);//把世界地图切换为中国地图
_this.worldMap.off('click');
flag = false;
} else {
_this.chinaMap.clear();
_this.chinaMap.setOption(_this.worldoption);
_this.chinaoption.geo.label.normal.show = true;//把中国地图上的文字重新展示
_this.chinaoption.geo.label.emphasis.show = true;
_this.chinaoption.tooltip.show = true;//隐藏提示悬浮框
_this.chinaMap.setOption(_this.chinaoption);//把中国地图切换为世界地图
_this.worldMap.clear();//清空图表
_this.worldMap.setOption(_this.worldoption);//把世界地图切换为中国地图
_this.worldMap.off('click');
flag = true;
}
}
},
}
具体效果可以直接复制代码到本地看哦~ 欢迎阅读点赞给个支持⭐️⭐️⭐️
如有更好的建议改造 欢迎各位评论互相学习,感谢阅读😊😜