echarts中国地图开发 第三章 下钻与返回

上期回顾:上期文章讲解了如何在地图上打点与连线,本章将讲述如何实现双击下钻与tip标签返回上级。
本期将使用到的素材有 chinaProvince.js 省级JSON市级JSON 文件,JSON素材可以私聊我或者评论区百度云下载。
链接:https://pan.baidu.com/s/1EB3bv2JgvSlDDz2h5caDXw
提取码:9hj8
下钻其实就是JSON文件的替换与刷新,实现过程如下,完整代码在最底下,感谢支持!

第一步还是在data声明如下变量

  currentLevel: 0, //目前下钻层级
  endLevel: 2, //最深下钻层级
  nowDomainName: "china", //当前地图区域名
  history: [], //下钻历史记录

第二步为地图绑定双击事件

initMap(){
	  ······
      上述代码请见上章或者最底部
	  ······
	  const el = document.getElementById("china");
      this.chart = this.$echarts.init(el);
      // 注册双击事件
      this.chart.on("dblclick", (params) => {
        if (params.componentSubType == "map") {
          // 下钻事件进行下钻
          this.fnGoDown(params.name);
        }
      });
      this.chart.setOption(this.options);
}
    fnGoDown(name) {
      console.log("_fnGoDown_", name);
      //先判断可不可以下钻
      if (this.currentLevel >= this.endLevel) {
        return false;
      }
      const mapname = name == ("中国" || "china") ? "china" : name;
      this.nowDomainName = mapname;
      if (mapname == "china") {
        this.$echarts.registerMap(mapname, china);
        this.options.geo.map = mapname;
        this.options.series[0].map = mapname;
        this.chart.setOption(this.options);
      } else {
        // 下钻省
        if (this.currentLevel == 0) {
          // 寻找省id
          import("./mapJson/chinaProvince.json").then((provinceData) => {
            provinceData.features.forEach((element) => {
              if (element.properties.fullname === name) {
                // 获取省id
                const provinceId = element.properties.id;
                import(
                  "./mapJson/geometryProvince/" + provinceId + ".json"
                ).then((res) => {
                  // 清除打点与连线数据
                  this.options.series[1].data = [];
                  this.options.series[2].data = [];
                  //将options中数据替换掉,并刷新
                  const newMapJson = res.default;
                  this.options.geo.map = mapname;
                  this.options.series[0].map = mapname;
                  this.history.push(name);
                  this.currentLevel = 1;
                  this.nowGoDownData = res;
                  this.$echarts.registerMap(mapname, newMapJson);
                  this.chart.setOption(this.options);
                });
              }
            });
          });
        } else if (this.currentLevel == 1) {
          // 下钻市
          this.nowGoDownData.features.forEach((element) => {
            if (element.properties.name === mapname) {
              console.log("_element_", element, element.properties.id);
              import(
                "./mapJson/geometryCouties/" + element.properties.id + "00.json"
              ).then((res) => {
                // 清除打点与连线数据
                this.options.series[1].data = [];
                this.options.series[2].data = [];
                //将options中数据替换掉,并刷新
                const newMapJson = res.default;
                this.options.geo.map = mapname;
                this.options.series[0].map = mapname;
                this.history.push(name);
                this.currentLevel = 2;
                this.$echarts.registerMap(mapname, newMapJson);
                this.chart.setOption(this.options);
              });
            }
          });
        } else {
          console.log("__已经到最底层了!_");
        }
      }
    },

已经可以下钻到市了!
在这里插入图片描述

此时发现可以下钻但无法返回,所以需要在左上角增加一个下钻记录并点击能返回上级,同时为增加点样式,此处使用的是 less UI组件使用的是 iview

<template>
  <div class="mapContnet">
    <div class="tips">
      <Icon
        type="md-pin"
        color="#1FB9FF"
        size="18"
        style="padding-right: 4px;"
      />
      <div class="defaultTip" @click="fnMapTipReturn('china')">
        中华人民共和国
      </div>
      <div
        v-for="(item, index) in history"
        :key="`drill-${index}`"
        style="margin-left: 6px;"
        :class="index == history.length - 1 ? 'nowTip' : 'defaultTip'"
        @click="fnMapTipReturn(item)"
      >
        >
        {{ item }}
      </div>
    </div>
    <div id="china" style="width: 1300px;height: 650px;"></div>
  </div>
</template>
<style lang="less" scoped>
.mapContnet {
  overflow: hidden;
  width: 100%;
  height: 100%;
  background-color: #0f1733;
  overflow-y: auto;
  .tips {
    background: linear-gradient(to bottom right, #003c6f, #006aab);
    border: 1px solid #305a8c;
    border-radius: 4px;
    position: absolute;
    padding: 6px 10px;
    color: #7bb4e1;
    display: flex;
    align-items: center;
    left: 46px;
    font-size: 14px;
    z-index: 10;

    .nowTip {
      color: #c5ddec;
    }

    .defaultTip {
      cursor: pointer;
    }
  }
}
</style>

点击事件

fnMapTipReturn(name) {
  if (this.nowDomainName != name) {
    // 返回第二层
    this.currentLevel = 0;
    this.history = [];
    this.fnGoDown(name);
    if (name === "china") {
          // 返回获取大点数据,此处可以优化,把大点数据保存到状态机或者本地都可以
          this.fnGetUnitManagemeData();
        }
  }
},

此时就可以返回上层了!

完整代码如下

<template>
  <div class="mapContnet">
    <div class="tips">
      <Icon
        type="md-pin"
        color="#1FB9FF"
        size="18"
        style="padding-right: 4px;"
      />
      <div class="defaultTip" @click="fnMapTipReturn('china')">
        中华人民共和国
      </div>
      <div
        v-for="(item, index) in history"
        :key="`drill-${index}`"
        style="margin-left: 6px;"
        :class="index == history.length - 1 ? 'nowTip' : 'defaultTip'"
        @click="fnMapTipReturn(item)"
      >
        >
        {{ item }}
      </div>
    </div>
    <div id="china" style="width: 1300px;height: 650px;"></div>
  </div>
</template>

<script>
import china from "./mapJson/china.json";
import * as echarts from "echarts";
export default {
  data() {
    return {
      chart: null,
      options: null,
      unitManagementData: [], //大点数据
      mapLineDataArr: [], //连线数据
      currentLevel: 0, //目前下钻层级
      endLevel: 2, //最深下钻层级
      nowDomainName: "china", //当前地图区域名
      history: [], //下钻历史记录
      nowGoDownData: {}, //当前下钻信息
    };
  },
  methods: {
    initMap() {
      echarts.registerMap("china", china);
      // 加载纹理图片
      let mapTexture = document.createElement("img");
      mapTexture.src = require("./image/chinese_map_texture.png");
      this.options = {
        tooltip: {
          show: false,
        },
        //叠加阴影层
        geo: {
          map: "china",
          aspectScale: 0.8,
          layoutCenter: ["40%", "49.5%"],
          layoutSize: "100%",
          label: {
            emphasis: {
              show: false,
            },
          },
          itemStyle: {
            shadowColor: "#1253A0",
            shadowOffsetX: 0,
            shadowOffsetY: 15,
          },
          emphasis: {
            areaColor: "#101B3B",
          },
          regions: [
            {
              name: "南海诸岛",
              itemStyle: {
                areaColor: "#101B3B",
                borderColor: "#101B3B",
                opacity: 0,
                label: {
                  show: false,
                  color: "#009cc9",
                },
              },
              label: {
                show: false,
                color: "#FFFFFF",
                fontSize: 12,
              },
            },
          ],
        },
        series: [
          {
            type: "map",
            selectedMode: "single",
            map: "china",
            aspectScale: 0.8,
            layoutCenter: ["40%", "50%"], //地图位置
            layoutSize: "100%",
            zoom: 1, //当前视角的缩放比例
            label: {
              show: true,
              color: "#87B8DD",
              fontSize: 12,
            },

            itemStyle: {
              // 渲染背景图片
              areaColor: {
                image: mapTexture,
                repeat: "repeat",
              },
              borderColor: "#ADD0ED",
              borderWidth: 1.2,
              shadowColor: "rgba(255, 255, 255, 0.4)", // 设置阴影颜色,带有透明度
              shadowBlur: 15, // 设置阴影的模糊大小
              shadowOffsetX: 2, // 设置阴影在 X 轴方向上的偏移
              shadowOffsetY: 2, // 设置阴影在 Y 轴方向上的偏移
            },
            emphasis: {
              //区域
              areaColor: "#ffeb3b", // 高亮时的颜色
              itemStyle: {
                areaColor: "#1785BF",
              },
              label: {
                show: true,
                color: "#fff",
              },
            },
          },
          {
            name: "单位",
            type: "effectScatter",
            coordinateSystem: "geo",
            data: this.unitManagementData,
            symbolSize: function(val) {
              return 10;
            },
            showEffectOn: "render",
            rippleEffect: {
              brushType: "stroke",
            },
            label: {
              show: false,
              color: "#000",
              formatter(value) {
                return "";
              },
            },
            hoverAnimation: true,
            itemStyle: {
              normal: {
                color: "#4DEFFF",
                shadowBlur: 10,
                shadowColor: "#4DEFFF",
              },
            },
            zlevel: 1,
          },
          {
            name: "lines",
            type: "lines",
            coordinateSystem: "geo",
            zlevel: 2,
            large: true,
            effect: {
              show: true, // 开启动态线条效果
              constantSpeed: 30, // 线条速度
              symbol: "pin", // 标记的图形,支持图片和文字
              symbolSize: 10, // 标记的大小
              trailLength: 0, // 动态线条的长度
              loop: true, // 是否循环动画效果
            },
            lineStyle: {
              normal: {
                color: function(params) {
                  // 根据 status 属性判断连线颜色
                  return params.data.status === 0 ? "#CE373F" : "#4DEFFF";
                },
                width: 2,
                opacity: 0.4,
                curveness: 0.2, // 曲线程度
              },
              emphasis: {
                opacity: 0.8,
                width: 5,
              },
            },
            data: this.mapLineDataArr,
          },
        ],
      };

      const el = document.getElementById("china");
      this.chart = this.$echarts.init(el);
      // 注册点击事件
      this.chart.on("dblclick", (params) => {
        if (params.componentSubType == "map") {
          // 下钻事件进行下钻
          this.fnGoDown(params.name);
        }
      });
      this.chart.setOption(this.options);
    },
    fnGetUnitManagemeData() {
      import("./mapJson/china.json").then((econom) => {
        econom.features.forEach((element1) => {
          if (element1.properties.centroid) {
            this.unitManagementData.push({
              Name: element1.properties.name,
              value: element1.properties.centroid,
            });
            this.mapLineDataArr.push({
              name: `line`,
              fromName: `childPoint`,
              toName: `mainpoint`,
              coords: [element1.properties.centroid, [116.40717, 39.90469]],
              status: 1,
            });
          }
        });
        this.chart.setOption(this.options);
      });
    },
    fnGoDown(name) {
      console.log("_fnGoDown_", name);
      //先判断可不可以下钻
      if (this.currentLevel >= this.endLevel) {
        return false;
      }
      const mapname = name == ("中国" || "china") ? "china" : name;
      this.nowDomainName = mapname;
      if (mapname == "china") {
        this.$echarts.registerMap(mapname, china);
        this.options.geo.map = mapname;
        this.options.series[0].map = mapname;
        this.chart.setOption(this.options);
      } else {
        // 下钻省
        if (this.currentLevel == 0) {
          // 寻找省id
          import("./mapJson/chinaProvince.json").then((provinceData) => {
            provinceData.features.forEach((element) => {
              if (element.properties.fullname === name) {
                // 获取省id
                const provinceId = element.properties.id;
                import(
                  "./mapJson/geometryProvince/" + provinceId + ".json"
                ).then((res) => {
                  // 清除打点与连线数据
                  this.options.series[1].data = [];
                  this.options.series[2].data = [];
                  //将options中数据替换掉,并刷新
                  const newMapJson = res.default;
                  this.options.geo.map = mapname;
                  this.options.series[0].map = mapname;
                  this.history.push(name);
                  this.currentLevel = 1;
                  this.nowGoDownData = res;
                  this.$echarts.registerMap(mapname, newMapJson);
                  this.chart.setOption(this.options);
                });
              }
            });
          });
        } else if (this.currentLevel == 1) {
          // 下钻市
          this.nowGoDownData.features.forEach((element) => {
            if (element.properties.name === mapname) {
              console.log("_element_", element, element.properties.id);
              import(
                "./mapJson/geometryCouties/" + element.properties.id + "00.json"
              ).then((res) => {
                // 清除打点与连线数据
                this.options.series[1].data = [];
                this.options.series[2].data = [];
                //将options中数据替换掉,并刷新
                const newMapJson = res.default;
                this.options.geo.map = mapname;
                this.options.series[0].map = mapname;
                this.history.push(name);
                this.currentLevel = 2;
                this.$echarts.registerMap(mapname, newMapJson);
                this.chart.setOption(this.options);
              });
            }
          });
        } else {
          console.log("__已经到最底层了!_");
        }
      }
    },
    fnMapTipReturn(name) {
      if (this.nowDomainName != name) {
        // 返回第二层
        this.currentLevel = 0;
        this.history = [];
        this.fnGoDown(name);
        if (name === "china") {
          // 返回获取大点数据,此处可以优化,把大点数据保存到状态机或者本地都可以
          this.fnGetUnitManagemeData();
        }
      }
    },
  },
  mounted() {
    this.initMap();
    this.fnGetUnitManagemeData();
  },
};
</script>
<style lang="less" scoped>
.mapContnet {
  overflow: hidden;
  width: 100%;
  height: 100%;
  background-color: #0f1733;
  overflow-y: auto;
  .tips {
    background: linear-gradient(to bottom right, #003c6f, #006aab);
    border: 1px solid #305a8c;
    border-radius: 4px;
    position: absolute;
    padding: 6px 10px;
    color: #7bb4e1;
    display: flex;
    align-items: center;
    left: 46px;
    font-size: 14px;
    z-index: 10;

    .nowTip {
      color: #c5ddec;
    }

    .defaultTip {
      cursor: pointer;
    }
  }
}
</style>
  • 14
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ECharts是一款强大的数据可视化库,可以用于开发自动旋转的Map3D下和柱图地图。该功能使得地图展示更加生动、直观,帮助用户更好地理解数据分布和趋势。 首先,Map3D功能可以将地图呈现为三维效果,使得地理空间关系更加清晰。通过ECharts地图可视化组件,可以快速创建一个3D地图,并根据需求进行旋转操作。用户可以通过鼠标拖动或程序控制来实现地图的自动旋转,这使得数据展示更加活跃有趣,同时也方便用户从不同角度观察地图上的数据。 其次,ECharts还提供了柱图地图功能。柱图地图是一种直观的数据展示方式,可以清晰地表示不同地区的数据差异。ECharts的柱图地图可以根据不同的数据指标,给每个地区绘制不同高度的柱形图,使得数据的大小关系一目了然。在Map3D场景下,用户可以通过旋转地图和调整视角,更好地观察柱形图的高低变化、不同地区的数据对比等。 在使用ECharts开发自动旋转Map3D下和柱图地图时,只需按照ECharts的文档,引入相应的库和组件,并配置相关的图表参数即可。ECharts提供了丰富的API接口,可以用于自定义地图样式、交互行为、数据展示等需求。开发过程中,可以根据具体的业务需求,灵活运用ECharts的功能,实现地图的个性化展示和交互效果。 总结来说,ECharts的自动旋转Map3D下和柱图地图功能,能够提供生动直观的数据可视化效果,帮助用户更好地理解和分析数据。通过简单的配置和自定义,可以快速实现这些功能,为用户提供更好的数据展示和交互体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值