先来看最终实现的效果图:
想要实现各省市的行政区域划分图首先肯定需要有这个省市的数据,那么很多小伙伴可能就会问了:我没有啊,去哪里找? 这就需要阿里云的数据平台了:datav.aliyun.com/portal/scho… ps:echarts实现的话记得选择“其他类型”,然后下载对应省市的json数据就好啦。
以下为代码实现(其实很多平台都有,这里不过是加了点特效)
//盒子
<div ref="echartsBoxMap" id="ArmamentariumMap" class="ArmamentariumClassMap"></div>
//js
import * as echarts from "echarts";
import "echarts-gl";
import elementResizeDetectorMaker from "element-resize-detector";//这个不需要的话可以不引用
import shandong from "../json/shandong.json";//山东
initMap() {
var myChart = echarts.init(document.getElementById("ArmamentariumMap"));
echarts.registerMap("山东", getData());
let data = getData().features.map((item) => {
return {
name: item.properties.name,
adcode: item.properties.adcode,
};
});
const points = [
[117.805132, 34.527667],
[117.395792, 35.066443],
[116.229504, 39.764735],
[115.883434, 39.899721]
]
var option = {
// backgroundColor: "#030528",
geo: [
{
map: "山东",
aspectScale: 1,
zoom: 0.55,
layoutCenter: ["50%", "50%"],
layoutSize: "180%",
show: true,
roam: false,
label: {
emphasis: {
show: false,
},
},
itemStyle: {
normal: {
borderColor: "#c0f3fb",
borderWidth: 1,
shadowColor: "#8cd3ef",
shadowOffsetY: 10,
shadowBlur: 120,
areaColor: "transparent",
},
}
},
// 重影
{
type: "map",
map: "山东",
zlevel: -1,
aspectScale: 1,
zoom: 0.55,
layoutCenter: ["50%", "51%"],
layoutSize: "180%",
roam: false,
silent: true,
itemStyle: {
normal: {
borderWidth: 1,
// borderColor:"rgba(17, 149, 216,0.6)",
borderColor: "rgba(58,149,253,0.8)",
shadowColor: "rgba(172, 122, 255,0.5)",
shadowOffsetY: 5,
shadowBlur: 15,
areaColor: "rgba(5,21,35,0.1)",
},
},
},
{
type: "map",
map: "山东",
zlevel: -2,
aspectScale: 1,
zoom: 0.55,
layoutCenter: ["50%", "52%"],
layoutSize: "180%",
roam: false,
silent: true,
itemStyle: {
normal: {
borderWidth: 1,
// borderColor: "rgba(57, 132, 188,0.4)",
borderColor: "rgba(58,149,253,0.6)",
shadowColor: "rgba(65, 214, 255,0.6)",
shadowOffsetY: 5,
shadowBlur: 15,
areaColor: "rgba(5,21,35,0.1)",
},
},
},
{
type: "map",
map: "山东",
zlevel: -3,
aspectScale: 1,
zoom: 0.55,
layoutCenter: ["50%", "53%"],
layoutSize: "180%",
roam: false,
silent: true,
itemStyle: {
normal: {
borderWidth: 1,
// borderColor: "rgba(11, 43, 97,0.8)",
borderColor: "rgba(58,149,253,0.4)",
shadowColor: "rgba(29, 111, 165,1)",
shadowOffsetY: 15,
shadowBlur: 10,
areaColor: "rgba(5,21,35,0.1)",
},
},
},
{
type: "map",
map: "山东",
zlevel: -4,
aspectScale: 1,
zoom: 0.55,
layoutCenter: ["50%", "54%"],
layoutSize: "180%",
roam: false,
silent: true,
itemStyle: {
normal: {
borderWidth: 5,
// borderColor: "rgba(11, 43, 97,0.8)",
borderColor: "rgba(5,9,57,0.8)",
shadowColor: "rgba(29, 111, 165,0.8)",
shadowOffsetY: 15,
shadowBlur: 10,
areaColor: "rgba(5,21,35,0.1)",
},
},
},
],
series: [
{
name: "山东市数据",
type: "map",
map: "山东", // 自定义扩展图表类型
aspectScale: 1,
zoom: 0.55, // 缩放
showLegendSymbol: true,
label: {
normal: {
show: true,
textStyle: { color: "#fff", fontSize: "120%" },
},
emphasis: {
show: false,
},
},
itemStyle: {
normal: {
areaColor: {
type: "linear",
x: 1200,
y: 0,
x2: 0,
y2: 0,
colorStops: [
{
offset: 0,
color: "rgba(3,27,78,0.75)", // 0% 处的颜色
},
{
offset: 1,
color: "rgba(58,149,253,0.75)", // 50% 处的颜色
},
],
global: true, // 缺省为 false
},
borderColor: "#fff",
borderWidth: 0.2,
},
emphasis: {
show: false,
color: "#fff",
areaColor: "rgba(0,254,233,0.6)",
},
},
layoutCenter: ["50%", "50%"],
layoutSize: "180%",
markPoint: {
symbol: "none",
},
data: data,
},
{ // 动效散点公共配置项
silent: true, // 暂不支持鼠标交互
type: 'effectScatter',
coordinateSystem: 'geo',
rippleEffect: {
//涟漪特效
period: 4, //动画时间,值越小速度越快
// brushType: 'stroke', //波纹绘制方式 stroke, fill
scale: 5 //波纹圆环最大限制,值越大波纹越大
},
label: {
normal: {
show: true,
position: 'right', //显示位置
offset: [5, 0], //偏移设置
formatter: function (params) {
console.log(params)
//圆环显示文字
return params.data.name
},
fontSize: 13,
color: 'white'
},
emphasis: {
show: true
}
},
symbol: 'circle',
symbolSize: 10,
itemStyle: {
normal: {
show: false,
borderWidth: 1,
color: '#f5e74f'
}
},
data: [//打点位置数据
{
name: "养护A",
value: [
...points[0],
323
]
},
{
name: "养护B",
value: [
...points[1],
323
]
},
{
name: "养护C",
value: [
...points[2],
323
]
},
{
name: "养护D",
value: [
...points[3],
323
]
}
],
}
],
};
function getData() {
// let res = sdData;
return shandong;//这里是引入的省市区json数据,因为我这里市要根据选择变化所以单独拿出来了
}
option && myChart.setOption(option);
myChart.on('click', function (params) {
console.log(params)
})
},
大小调整zoom值,打点颜色什么的都可以在echarts官网找到对应配置项。 记得定义盒子的高度和宽度,不然无法显示。 大家再见。