前端 javascript vue.js Vue Echarts 中国地图自定义大区 - 湖北"
安装Eacharts
npm i echarts
引入
import * as echarts from "echarts"; 拼多多帮助力入口
import chinaJSON from "../../assets/china.json"; //地图数据
新版本的echarts已经没有ChinaJson ,需要自行下载:gitup地址
自定义大区数据
var params = {
names: [
//把各个大区的省份用二维数组分开
["北京", "天津", "宁夏", "山东", "山西", "新疆", "河北", "河南", "甘肃", "陕西"],
["安徽", "江苏", "江西", "浙江", "湖北", "湖南", "福建"],
["四川", "重庆"],
["云南", "广东", "广西", "海南", "贵州"],
["内蒙古", "黑龙江", "吉林", "辽宁", "江西", "上海", "台湾", "香港", "澳门", "西藏", "青海"]
],
properties: {
//自定义大区的名字,要和上面的大区省份一一对应
name: ["北方", "华东", "川渝", "西南", "其他"],
cp: [
//经纬度可以自己随意定义
[90.92364, 40.5855],
[116.85973, 33.2126],
[101.64516, 30.80417],
[108.83674, 23.90636],
[87.54753, 32.05666]
]
}
};
合并函数
function mergeProvinces(chinaJson, names, properties) {
// console.log(chinaJson);
//合并大区里省份的coordinates
var features = chinaJson.features;
var polygons = [];
var polygons2 = [];
for (var i = 0; i < names.length; i++) {
var polygonsCoordinates = [];
var polygonsEncodeOffsets = [];
for (var z = 0; z < names[i].length; z++) {
for (var j = 0; j < features.length; j++) {
if (features[j].properties.name == names[i][z]) {
if (features[j].geometry.coordinates[0].constructor == String) {
//合并coordinates
for (var l = 0; l < features[j].geometry.coordinates.length; l++) {
polygonsCoordinates.push(features[j].geometry.coordinates[l]);
}
} else if (features[j].geometry.coordinates[0].constructor == Array) {
for (var k = 0; k < features[j].geometry.coordinates.length; k++) {
for (var d = 0; d < features[j].geometry.coordinates[k].length; d++) {
polygonsCoordinates.push(features[j].geometry.coordinates[k][d]);
}
}
}
if (features[j].geometry.encodeOffsets[0].constructor == String) {
//合并encodeOffsets
polygonsEncodeOffsets.push(features[j].geometry.encodeOffsets);
} else if (features[j].geometry.encodeOffsets[0].constructor == Array) {
for (let k = 0; k < features[j].geometry.encodeOffsets.length; k++) {
if (features[j].geometry.encodeOffsets[k][0].constructor == Array) {
for (var e = 0; e < features[j].geometry.encodeOffsets[k].length; e++) {
polygonsEncodeOffsets.push(features[j].geometry.encodeOffsets[k][e]);
}
} else {
polygonsEncodeOffsets.push(features[j].geometry.encodeOffsets[k]);
}
}
}
break;
}
}
}
polygons.push(polygonsCoordinates);
polygons2.push(polygonsEncodeOffsets);
}
// 构建新的合并区域
var featuress = [];
for (var a = 0; a < names.length; a++) {
var feature = {
id: features.length.toString(),
type: "FeatureCollection",
geometry: {
type: "Polygon",
coordinates: polygons[a],
encodeOffsets: polygons2[a]
},
properties: {
name: properties.name[a] || "",
childNum: polygons[a].length
}
};
if (properties.cp[a]) {
feature.properties.cp = properties.cp[a];
}
// 将新的合并区域添加到地图中
featuress.push(feature);
}
chinaJson.features = featuress;
// console.log(chinaJson.features);
return chinaJson.features;
}
注册地图
function drawChina() {
mergeProvinces(chinaJSON, params.value.names, params.value.properties);
var myChart = echarts.init(chinaMap.value);
echarts.registerMap("china", chinaJSON); //注册可用的地图
var option = {
title: {
text: "全国展点分布",
left: "center",
textStyle: {
color: "#ddb926"
}
},
grid: {
//echarts地图距离容器位置
left: 20,
top: 20,
containLabel: false
},
toolbox: {
show: true,
right: "80px",
feature: {
//自定义toolbox,必须以my开头
myFullScreen: {
show: true,
title: "全屏显示",
//icon从阿里图标复制对应的svg格式代码
icon: "path://M368.896 192H224a32 32 0 0 0-32 32v137.888a32 32 0 0 0 64 0V256h112.896a32 32 0 0 0 0-64zM784.864 192H640a32 32 0 1 0 0 64h112.864v105.888a32 32 0 1 0 64 0V224a32 32 0 0 0-32-32zM368.896 777.92H256V672a32 32 0 1 0-64 0v137.92a32 32 0 0 0 32 32h144.896a32 32 0 1 0 0-64zM784.864 640a32 32 0 0 0-32 32v105.92H640a32 32 0 1 0 0 64h144.864a32 32 0 0 0 32-32V672a32 32 0 0 0-32-32z",
onclick: function (e) {
//isFullScreen定义在data中,初始化为false
// console.log(e);
var opts = e.getOption();
dialogTableVisible.value = true;
nextTick(() => {
var myChart = echarts.init(fullscreen.value);
myChart.setOption(opts);
});
}
}
}
},
renderMode: "richText",
geo: {
map: "china",
//中心点(经纬度)
// center: [118.27958, 31.117566],
center: [105.03827 , 36.56377],
//缩放比例
zoom: 1,
zoom: 0.5, //初始化大小
//缩放大小限制
scaleLimit: {
min: 0.5, //最小
max: 2 //最大
},
label: {
emphasis: {
show: false
}
},
//是否允许拖拽
roam: true,
itemStyle: {
normal: {
areaColor: "#f6efa6", //默认区块颜色
borderColor: "#03b8c0", //区块描边颜色
borderWidth: 2 //区块描边颜色
}
},
regions: regions.value
},
tooltip: {
//鼠标放上去提示
trigger: "item",
formatter: function (params) {
console.log(params);
return "";
}
},
emphasis: {
label: {
formatter: function (params) {
// console.log(params);
return "";
}
}
}
};
});
myChart.setOption(option);
}
相关链接,前端