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>

实现效果:

在这里插入图片描述

  • 13
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3是一种流行的JavaScript框架,而Echarts5是一种强大的数据可视化库。结合Vue3和Echarts5,可以很容易地实现世界地图的可视化效果。 首先,你需要在Vue项目中安装Echarts5。可以通过npm或yarn来安装echarts: ``` npm install echarts ``` 或者 ``` yarn add echarts ``` 安装完成后,你可以在Vue组件中引入Echarts,并使用它来绘制世界地图。 在Vue组件中,你可以使用`<template>`标签来定义HTML模板,使用`<script>`标签来编写JavaScript代码,使用`<style>`标签来定义CSS样式。 下面是一个简单的示例代码,展示了如何在Vue3中使用Echarts5绘制世界地图: ```vue <template> <div id="world-map" style="width: 100%; height: 400px;"></div> </template> <script> import * as echarts from 'echarts'; export default { mounted() { this.drawWorldMap(); }, methods: { drawWorldMap() { const chartDom = document.getElementById('world-map'); const myChart = echarts.init(chartDom); // 定义地图数据 const mapData = [ { name: 'China', value: 100 }, { name: 'United States', value: 50 }, // 其他国家... ]; // 配置地图选项 const option = { tooltip: { trigger: 'item', formatter: '{b}: {c}', }, visualMap: { min: 0, max: 100, left: 'left', top: 'bottom', text: ['High', 'Low'], seriesIndex: [1], inRange: { color: ['#e0ffff', '#006edd'], }, }, series: [ { type: 'map', mapType: 'world', roam: true, label: { show: true, }, data: mapData, }, ], }; // 使用配置项绘制地图 myChart.setOption(option); }, }, }; </script> <style> #world-map { width: 100%; height: 400px; } </style> ``` 在上面的代码中,我们首先引入了Echarts库,然后在`mounted`生命周期钩子函数中调用`drawWorldMap`方法来绘制地图。`drawWorldMap`方法中,我们使用`echarts.init`方法初始化一个Echarts实例,并通过配置项`option`来定义地图的样式和数据。最后,使用`myChart.setOption(option)`方法将配置项应用到地图上。 这样,你就可以在Vue项目中使用Vue3和Echarts5来实现世界地图的可视化效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值