白话大前端-echarts渲染一个自定义地图

440 阅读1分钟

直接上例子

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>echart 自定义地图</title>
  <style>
    #app {
      width: 100vw;
      height: 100vh;
    }
  </style>
  <script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
</head>

<body>
  <div id="app"></div>
  <script>
    const jsonUrl = 'https://geo.datav.aliyun.com/areas_v3/bound/320000_full.json';
    fetch(jsonUrl).then((res) => {
      return res.json();
    }).then(result => {
      init(result)
    })

    var echartInstance = null;
    function init(json) {
      echartInstance = echarts.init(document.querySelector('#app'));
      // 注册可用的地图,只在 [geo] 组件或者 [map]图表类型中使用。
      echarts.registerMap("jiangsu", json);

      const options = {
        backgroundColor: "rgba(255, 255 ,255, 0)",
        geo: {
          map: "jiangsu",
          show: true,
          zoom: 1.2,
          label: {
            show: true,
            color: "#fff",
            fontWeight: "bold",
            fontSize: 22,
          },
          itemStyle: {
            areaColor: "#083465",
            borderColor: "#3879cc",
            borderWidth: 1,
          },
          emphasis: {
            label: {
              color: "#fff",
            },
            itemStyle: {
              areaColor: "rgba(11,37,82,1)",
            },
          },
        },
        series: [],
      };
      echartInstance.setOption(options);
    }

    window.addEventListener('resize', () => {
      echartInstance && echartInstance.resize()
    })
  </script>
</body>

</html>

地图json 下载地址 datav.aliyun.com/portal/scho…

echarts有很多配置项,可以去官网不断尝试 echarts官网 echarts.apache.org/zh/index.ht… 现在echarts荣誉apache的孵化项目,所以打开这个地址最好采用科学上网