vue3 + echarts5.4.3 实现3D省份地图【动态icon】
vue3项目 + echarts5.4.3
实现效果:
1. 下载echarts插件
npm i echarts
2. 按需引入echarts插件
<script setup>
import * as echarts from 'echarts';
<script>
3. 复制 省份json文件
1. 查找省份/全国的json文件的地址:
直接复制,在浏览器中打开,全选复制即可 ,在vue项目中新建.json文件,将复制的json,粘贴在此!
2. 当前页面中引入刚才复制的json文件
<script setup>
import * as echarts from 'echarts';
import china from './china.json'
<script>
4. 准备好demo结构:
<template>
<div id="main">
</div>
</template>
<style scoped>
#main{
width: 100%;
height: 100%;
}
</style>
5. 地图初始化:
<script setup>
import { onMounted } from 'vue'
import * as echarts from 'echarts';
import china from './china.json'
const init = ()=>{
// 基于准备好的dom,初始化echarts实例
let myChart = echarts.init(document.querySelector('#main'))
// 注册地图的json,小写的china
echarts,registerMap('china',china)
let option = { 地图数据...}
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option,true)
}
onMounted(()=>{
init()
})
<script>
6. 3D地图数据: option
let option = {
geo: [
{
map: "china",
aspectScale: 1.2,
zoom: 16,
layoutCenter: ["30%", "68%"],
layoutSize: "100%",
show: true,
roam: false,
center:[114.03, 36.2],
label: {
emphasis: {
show: false,
},
},
itemStyle: {
normal: {
borderColor: "rgba(255,255,255,0.5)",//rgba(255,255,255,0.8)
borderWidth: 5,
shadowColor: "rgba(255,255,255,0.8)", //#8cd3ef rgba(45,35,154,1)
shadowOffsetY: 3,//10
shadowBlur: 8,//120
areaColor: "transparent",
},
}
},
// 重影
{
type: "map",
map: "china",
zlevel: -1,
aspectScale: 1.2,
zoom: 16,
layoutCenter: ["30%", "71%"],
layoutSize: "100%",
roam: false,
center:[114.03, 36.2],
silent: true,
itemStyle: {
normal: {
borderWidth: 1,
// borderColor:"rgba(17, 149, 216,0.6)",
borderColor: "rgba(66,51,191,0.8)",
shadowColor: "rgba(66,51,191,0.5)",//172, 122, 255,
shadowOffsetY: 5,
shadowBlur: 15,
areaColor: "rgba(98,57,201,0.1)",//rgba(5,21,35,0.1)
},
},
},
{
type: "map",
map: "china",
zlevel: -2,
aspectScale: 1.2,
zoom: 16,
layoutCenter: ["30%", "73%"],
layoutSize: "100%",
roam: false,
center:[114.03, 36.2],
silent: true,
itemStyle: {
normal: {
borderWidth: 1,
// borderColor:"rgba(17, 149, 216,0.6)",
borderColor: "rgba(66,51,191,0.8)",
shadowColor: "rgba(66,51,191,0.5)",//172, 122, 255,
shadowOffsetY: 5,
shadowBlur: 15,
areaColor: "rgba(98,57,201,0.1)",//rgba(5,21,35,0.1)
},
},
},
{
type: "map",
map: "china",
zlevel: -3,
aspectScale: 1.2,
zoom: 16,
layoutCenter: ["30%", "75%"],
layoutSize: "100%",
center:[114.03, 36.2],
roam: false,
silent: true,
itemStyle: {
normal: {
borderWidth: 1,
// borderColor:"rgba(17, 149, 216,0.6)",
borderColor: "rgba(66,51,191,0.8)",
shadowColor: "rgba(66,51,191,0.5)",//172, 122, 255,
shadowOffsetY: 10,
shadowBlur: 15,
areaColor: "rgba(98,57,201,0.1)",//rgba(5,21,35,0.1)
},
},
},
{
type: "map",
map: "china",
zlevel: -4,
aspectScale: 1.2,
zoom: 16,
layoutCenter: ["30%", "78%"],
layoutSize: "100%",
center:[114.03, 36.2],
roam: false,
silent: true,
itemStyle: {
normal: {
borderWidth: 8,
// borderColor:"rgba(17, 149, 216,0.6)",
borderColor: "rgba(66,51,191,0.8)",
shadowColor: "rgba(66,51,191,0.5)",//172, 122, 255,
shadowOffsetY: 15,
shadowBlur: 8,
areaColor: "rgba(98,57,201,0.4)",//rgba(5,21,35,0.1)
},
},
},
],
// 最上层地图数据!
series: [
{
name: "城市",
type: "map",
map: "china", // 自定义扩展图表类型
aspectScale: 1.2,
zoom: 16, // 缩放
showLegendSymbol: true,
label: {
normal: {
show: true,
textStyle: {
color: "rgba(94,106,218,1)",
fontSize: "9px"
},
},
// hover区域文字大小
emphasis: {
show: true,
textStyle: {
color: "#fff",
fontSize: "9px"
},
},
},
itemStyle: {
normal: {
areaColor: {
type: "linear",
x: 200,
y: 0,
x2: 0,
y2: 0,
colorStops: [
{
offset: 0.8,
color: "rgba(54,47,178,1)", // 0% 处的颜色
},
{
offset: 0.8,
color: "rgba(39,52,178,0.9) ", // 50% 处的颜色rgba(39,52,180,1)
},
],
global: true, // 缺省为 false
},
borderColor: "rgba(76,94,220,1)",// #CACDFC rgba(76,94,220,1)
borderWidth: 1,
},
// hover 区域时的颜色
emphasis: {
show: true,
color: "#fff",
areaColor: {
type: "linear",
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{
offset: 0,
color: "rgba(86,146,251,1)", // 0% 处的颜色
},
{
offset: 1,
color: "rgba(133,68,199,1) ", // 50% 处的颜色rgba(39,52,180,1)
},
],
global: false, // 缺省为 false
},
// hover区域边框大小
borderColor: "rgba(255,255,255,1)",// #CACDFC rgba(76,94,220,1)
borderWidth: 1,
},
},
layoutCenter: ["30%", "68%"],
layoutSize: "100%",
center:[114.03, 36.2],
markPoint: {
symbol: "none",
},
// data: data,
},
// 定点区域气泡
{
name: 'Top1气泡',
type: 'scatter',
coordinateSystem: 'geo',
opacity: 2, // 设置透明度
// ui给的图片,base64 遍历数据添加所需图片
symbol:function(points,params) {
for(var i = 0; i < points.length; i++){
if(params.name === '武安市'){
return `image://data:image/png;base64,...`
}
else if(params.name === '肥乡县'){
return `image://data:image/png;base64,...`
}else if(params.name === '临漳县'){
return `image://data:image/png;base64,...`
}
}
},
symbolSize: [60, 30],
// 定点区域文本
label: {
normal: {
show: true,
textStyle: {
color: '#fff',
fontSize: 10,
},
formatter: function(params) {
const areaName = params.data.title
return areaName
}
},
},
itemStyle: {
normal: {
opacity:0.9, //标志的图标透明度
}
},
zlevel: 6,
data:points,
},
],
};
完整代码:
<template>
<div id="main">
</div>
</template>
<script setup>
import { onMounted } from 'vue'
import * as echarts from 'echarts';
import china from './china.json'
const init = () => {
// 基于准备好的dom,初始化echarts实例
let myChart = echarts.init(document.querySelector('#main'))
// 注册默认全国地图的json,小写的china
echarts.registerMap('china', china)
// 指定图表的配置项和数据
// 气泡定点坐标
const points = [
{
name:'武安市',
value: [114.00153,36.66281],
title:'第一名'
},
{
name:'肥乡县',
value: [114.93998,36.67807],
title:'第二名'
},
{
name:'临漳县',
value: [114.5595,36.30861],
title:'第三名'
},
]
let option = {
geo: [
{
map: "china",
aspectScale: 1.2,
zoom: 16,
layoutCenter: ["30%", "68%"],
layoutSize: "100%",
show: true,
roam: false,
center:[114.03, 36.2],
label: {
emphasis: {
show: false,
},
},
itemStyle: {
normal: {
borderColor: "rgba(255,255,255,0.5)",//rgba(255,255,255,0.8)
borderWidth: 5,
shadowColor: "rgba(255,255,255,0.8)", //#8cd3ef rgba(45,35,154,1)
shadowOffsetY: 3,//10
shadowBlur: 8,//120
areaColor: "transparent",
},
}
},
// 重影
{
type: "map",
map: "china",
zlevel: -1,
aspectScale: 1.2,
zoom: 16,
layoutCenter: ["30%", "71%"],
layoutSize: "100%",
roam: false,
center:[114.03, 36.2],
silent: true,
itemStyle: {
normal: {
borderWidth: 1,
// borderColor:"rgba(17, 149, 216,0.6)",
borderColor: "rgba(66,51,191,0.8)",
shadowColor: "rgba(66,51,191,0.5)",//172, 122, 255,
shadowOffsetY: 5,
shadowBlur: 15,
areaColor: "rgba(98,57,201,0.1)",//rgba(5,21,35,0.1)
},
},
},
{
type: "map",
map: "china",
zlevel: -2,
aspectScale: 1.2,
zoom: 16,
layoutCenter: ["30%", "73%"],
layoutSize: "100%",
roam: false,
center:[114.03, 36.2],
silent: true,
itemStyle: {
normal: {
borderWidth: 1,
// borderColor:"rgba(17, 149, 216,0.6)",
borderColor: "rgba(66,51,191,0.8)",
shadowColor: "rgba(66,51,191,0.5)",//172, 122, 255,
shadowOffsetY: 5,
shadowBlur: 15,
areaColor: "rgba(98,57,201,0.1)",//rgba(5,21,35,0.1)
},
},
},
{
type: "map",
map: "china",
zlevel: -3,
aspectScale: 1.2,
zoom: 16,
layoutCenter: ["30%", "75%"],
layoutSize: "100%",
center:[114.03, 36.2],
roam: false,
silent: true,
itemStyle: {
normal: {
borderWidth: 1,
// borderColor:"rgba(17, 149, 216,0.6)",
borderColor: "rgba(66,51,191,0.8)",
shadowColor: "rgba(66,51,191,0.5)",//172, 122, 255,
shadowOffsetY: 10,
shadowBlur: 15,
areaColor: "rgba(98,57,201,0.1)",//rgba(5,21,35,0.1)
},
},
},
{
type: "map",
map: "china",
zlevel: -4,
aspectScale: 1.2,
zoom: 16,
layoutCenter: ["30%", "78%"],
layoutSize: "100%",
center:[114.03, 36.2],
roam: false,
silent: true,
itemStyle: {
normal: {
borderWidth: 8,
// borderColor:"rgba(17, 149, 216,0.6)",
borderColor: "rgba(66,51,191,0.8)",
shadowColor: "rgba(66,51,191,0.5)",//172, 122, 255,
shadowOffsetY: 15,
shadowBlur: 8,
areaColor: "rgba(98,57,201,0.4)",//rgba(5,21,35,0.1)
},
},
},
],
series: [
{
name: "邯郸",
type: "map",
map: "china", // 自定义扩展图表类型
aspectScale: 1.2,
zoom: 16, // 缩放
showLegendSymbol: true,
label: {
normal: {
show: true,
textStyle: {
color: "rgba(94,106,218,1)",
fontSize: "9px"
},
},
// hover区域文字大小
emphasis: {
show: true,
textStyle: {
color: "#fff",
fontSize: "9px"
},
},
},
itemStyle: {
normal: {
areaColor: {
type: "linear",
x: 200,
y: 0,
x2: 0,
y2: 0,
colorStops: [
{
offset: 0.8,
color: "rgba(54,47,178,1)", // 0% 处的颜色
},
{
offset: 0.8,
color: "rgba(39,52,178,0.9) ", // 50% 处的颜色rgba(39,52,180,1)
},
],
global: true, // 缺省为 false
},
borderColor: "rgba(76,94,220,1)",// #CACDFC rgba(76,94,220,1)
borderWidth: 1,
},
// hover 区域时的颜色
emphasis: {
show: true,
color: "#fff",
areaColor: {
type: "linear",
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{
offset: 0,
color: "rgba(86,146,251,1)", // 0% 处的颜色
},
{
offset: 1,
color: "rgba(133,68,199,1) ", // 50% 处的颜色rgba(39,52,180,1)
},
],
global: false, // 缺省为 false
},
// hover区域边框大小
borderColor: "rgba(255,255,255,1)",// #CACDFC rgba(76,94,220,1)
borderWidth: 1,
},
},
layoutCenter: ["30%", "68%"],
layoutSize: "100%",
center:[114.03, 36.2],
markPoint: {
symbol: "none",
},
// data: data,
},
// 定点区域气泡
{
name: 'Top1气泡',
type: 'scatter',
coordinateSystem: 'geo',
opacity: 2, // 设置透明度
// ui给的图片,base64 遍历数据添加所需图片
symbol:function(points,params) {
for(var i = 0; i < points.length; i++){
if(params.name === '武安市'){
return `image://data:image/png;base64,...`
}
else if(params.name === '肥乡县'){
return `image://data:image/png;base64,...`
}else if(params.name === '临漳县'){
return `image://data:image/png;base64,...`
}
}
},
symbolSize: [60, 30],
// 定点区域文本
label: {
normal: {
show: true,
textStyle: {
color: '#fff',
fontSize: 10,
},
formatter: function(params) {
const areaName = params.data.title
return areaName
}
},
},
itemStyle: {
normal: {
opacity:0.9, //标志的图标透明度
}
},
zlevel: 6,
data:points,
},
],
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option,true)
}
onMounted(()=>{
init()
})
</script>
<style scoped>
#main{
width: 100%;
height: 100%;
}
</style>