echarts地图下钻(高德+vue+echarts)右键返回

1.首先在index.html里添加

<script src='https://webapi.amap.com/mapsv=1.3&key=自己的key&plugin=AMap.DistrictSearch'>
 </script>
    <script src="https://webapi.amap.com/ui/1.0/main.js"></script>

2.在项目里npm下载并引入echarts

import echarts from "echarts";

3.实例化地图

1.html部分

<div class="echarts">
    <div class="top"></div>
    <div class="bot">
      <div class="left">
            //清除浏览器默认右键事件
        <div class="sctterMap" ref="sctterMap" @contextmenu.prevent></div>
        <div class="mapChoose">
          <span v-for="(item,index) in parentInfo" :key="item.code">
            <span
              class="title"
              @click="chooseArea(item,index)"
            >{{item.cityName=='江西省'?'江西省':item.cityName}}</span>
            <span class="icon" v-show="index+1!=parentInfo.length">></span>
          </span>
        </div>
        
      </div>
    
    </div>
  </div>

2.css部分

.echarts {
  width: 100%;
  height: 100%;
  background: url("../assets/切图/1.png") no-repeat;
//背景 替换自己的图片
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;
  color: #fff;
  font-size: 13px;
  .bot {
    height: 100%;
    width: 100%;
    display: flex;
    .left {
      width: 35%;
      height: 100%;
      position: relative;
      .sctterMap {
        width: 100%;
        height: 75%;
        background: url("../assets/切图/圆.png") no-repeat center;
//背景 替换自己的图片
        background-size: 90% 60%;
        position: relative;
      }
      .mapChoose {
        position: absolute;
        left: 20px;
        top: 5px;
        color: #eee;

        .title {
          padding: 5px;
          border-top: 1px solid rgba(147, 235, 248, 0.8);
          border-bottom: 1px solid rgba(147, 235, 248, 0.8);
          cursor: pointer;
        }

        .icon {
          font-family: "simsun";
          font-size: 25px;
          margin: 0 11px;
        }
      }
      
    }

  }

}

3.js部分

data() {
    return {
      myCharts: null,
      geoJson: {
        features: []
      },
      parentInfo: [
            //全国的编码为100000,这里从江西下钻
        {
          cityName: "江西省",
          code: 360000
        }
      ],
      index: 0,
      duration: 3000,
      // 初始值
      startVal: 0,
      // 最终值
      endVal: 112018
    };
  },
  mounted() {
    //全国的编码为100000,这里从江西下钻
    this.getGeoJson(360000);
    this.initMychart();
  },
  methods: {
    getGeoJson(adcode) {
      let that = this;
      AMapUI.loadUI(["geo/DistrictExplorer"], DistrictExplorer => {
        var districtExplorer = new DistrictExplorer();
        districtExplorer.loadAreaNode(adcode, function(error, areaNode) {
          if (error) {
            console.error(error);
            return;
          }
          let Json = areaNode.getSubFeatures();
          if (Json.length > 0) {
            that.geoJson.features = Json;
          } else if (Json.length === 0) {
            that.geoJson.features = that.geoJson.features.filter(
              item => item.properties.adcode == adcode
            );
            if (that.geoJson.features.length === 0) return;
          }
          that.getMapData();
        });
      });
    },
    //获取数据
    getMapData() {
      let mapData = this.geoJson.features.map(item => {
        return {
          name: item.properties.name,
          value: Math.random() * 1000,
          cityCode: item.properties.adcode
        };
      });
      mapData = mapData.sort(function(a, b) {
        return b.value - a.value;
      });
      this.initEcharts(mapData);
    },
    initEcharts(mapData) {
      console.log(mapData, "地图数据");
      var min = mapData[mapData.length - 1].value;
      var max = mapData[0].value;
      if (mapData.length === 1) {
        min = 0;
      }
      this.myChart = echarts.init(this.$refs.sctterMap);
      echarts.registerMap("Map", this.geoJson); //注册
      this.myChart.setOption(
        {
          geo: [
            {
              // 这个是重点 —— 这里设置为地图的阴影
              map: "Map",
              top: "11%",
              roam: false, // 禁止拖拽
              silent: true, // 禁止hover效果
              zoom: 1.1, // 地图比例
              // center: [105.194115019531, 36.582111640625], // 地图中心位置, 此处的纬度与下面的center相差1度是形成阴影的距离,可自己随意调整
              itemStyle: {
                normal: {
                  borderColor: "#fff",
                  areaColor: "transparent"
                }
              }
            },
            {
              // 这个是重点 —— 这里设置为地图的阴影
              map: "Map",
              top: "12%",
              roam: false, // 禁止拖拽
              silent: true, // 禁止hover效果
              zoom: 1.1, // 地图比例
              // center: [105.194115019531, 36.582111640625], // 地图中心位置, 此处的纬度与下面的center相差1度是形成阴影的距离,可自己随意调整
              itemStyle: {
                normal: {
                  borderColor: "#fff",
                  areaColor: "transparent"
                }
              }
            }
          ],
          tooltip: {
            trigger: "item",
            formatter: p => {
              let val = p.value;
              if (!val) {
                val = 0;
              }
              let txtCon = p.name + ":" + val.toFixed(2);
              return txtCon;
            }
          },
          visualMap: {
            min: min,
            max: max,
            left: "3%",
            bottom: "5%",
            calculable: false,
            seriesIndex: [0],
            inRange: {
              color: ["#0f6153", "#29957b", "#01b053", "#94cf52"]
            },
            textStyle: {
              color: "#24CFF4"
            }
          },
          series: [
            {
              // geoIndex: 1 ,
              name: "地图",
              type: "map",
              map: "Map",
              roam: false, //是否可缩放
              zoom: 1.1, //缩放比例
              data: mapData,
              label: {
                normal: {
                  show: true,
                  color: "rgb(249, 249, 249)", //省份标签字体颜色
                  formatter: p => {
                    switch (p.name) {
                      case "内蒙古自治区":
                        p.name = "内蒙古";
                        break;
                      case "西藏自治区":
                        p.name = "西藏";
                        break;
                      case "新疆维吾尔自治区":
                        p.name = "新疆";
                        break;
                      case "宁夏回族自治区":
                        p.name = "宁夏";
                        break;
                      case "广西壮族自治区":
                        p.name = "广西";
                        break;
                      case "香港特别行政区":
                        p.name = "香港";
                        break;
                      case "澳门特别行政区":
                        p.name = "澳门";
                        break;
                      default:
                        break;
                    }
                    return p.name;
                  }
                },
                emphasis: {
                  show: true,
                  color: "#f75a00"
                }
              },
              itemStyle: {
                normal: {
                  areaColor: "#0a363b",
                  borderColor: "#2affde",
                  borderWidth: 1.5,
                  shadowBlur: 15,
                  shadowColor: "#2affde",
                  shadowOffsetX: 5,
                  shadowOffsetY: 3
                },
                emphasis: {
                  areaColor: "#8dd7fc",
                  borderWidth: 1.6,
                  shadowBlur: 25
                }
              }
            }
          ]
        },
        true
      );
      window.addEventListener("resize", () => {
        this.myChart.resize();
      });
      let that = this;

      this.myChart.off("click");
      // 右键返回
      this.myChart.on("contextmenu", params => {
        if (that.parentInfo.length - 1 > 0) {
          that.parentInfo.splice(-1);
          that.getGeoJson(that.parentInfo[that.parentInfo.length - 1].code);
          params.event.preventDefault();
        }
      });
      // 左键下钻
      this.myChart.on("click", params => {
        console.log(params, "数据");
        if (
          that.parentInfo[that.parentInfo.length - 1].code ==
          params.data.cityCode
        ) {
          return;
        }
        this.index += 1;
        let data = params.data;
        that.parentInfo.push({
          cityName: data.name,
          code: data.cityCode
        });
        that.getGeoJson(data.cityCode);
      });
    },
    //选择切换市县
    chooseArea(val, index) {
      if (this.parentInfo.length === index + 1) {
        return;
      }
      this.parentInfo.splice(index + 1);
      this.getGeoJson(this.parentInfo[this.parentInfo.length - 1].code);
    },
    
  }

4.效果

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ECharts 是一款非常强大的可视化图表库,它支持多种类型的图表展示,并提供了丰富的交互方式和动画效果。在 Vue 中使用 ECharts 非常方便,只需要引入 echartsvue-echarts 即可。 动态更新 ECharts 数据的方法有多种,下面介绍两种比较常见的方式。 方法一:使用 ECharts 的 setOption 方法 ECharts 提供了 setOption 方法,可以用来更新图表数据。我们可以通过在 Vue 中监听数据变化,然后调用 setOption 方法更新图表数据。 示例代码如下: ``` <template> <div id="chart"></div> </template> <script> import echarts from 'echarts' import { VueECharts } from 'vue-echarts' export default { components: { VueECharts }, data() { return { chartData: { // 初始数据 xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [{ data: [820, 932, 901, 934, 1290, 1330, 1320], type: 'line' }] } } }, mounted() { this.$nextTick(() => { this.initChart() }) }, methods: { initChart() { this.chart = echarts.init(document.getElementById('chart')) this.chart.setOption(this.chartData) }, updateChart() { // 更新数据 this.chartData.series[0].data = [randomData(), randomData(), randomData(), randomData(), randomData(), randomData(), randomData()] // 调用 setOption 方法更新图表数据 this.chart.setOption(this.chartData) } } } function randomData() { return Math.floor(Math.random() * 1000) } </script> ``` 在上面的代码中,我们首先在 data 中定义了初始的图表数据,然后在 mounted 钩子函数中调用 initChart 方法初始化图表。 在 updateChart 方法中,我们通过修改数据,然后调用 setOption 方法更新图表数据。 方法二:使用 ECharts 的动态数据接口 ECharts 还提供了动态数据接口,在数据变化时自动更新图表数据。我们只需要通过调用 setOption 方法传入一个动态数据接口,就可以实现图表数据的动态更新。 示例代码如下: ``` <template> <div id="chart"></div> </template> <script> import echarts from 'echarts' import { VueECharts } from 'vue-echarts' export default { components: { VueECharts }, data() { return { option: { xAxis: { type: 'category', data: [] }, yAxis: { type: 'value' }, series: [{ data: [], type: 'line' }] }, dataUrl: 'data.json', timer: null } }, mounted() { this.$nextTick(() => { this.initChart() }) }, methods: { initChart() { this.chart = echarts.init(document.getElementById('chart')) this.chart.setOption(this.option) this.getData() // 每隔一段时间更新数据 this.timer = setInterval(() => { this.getData() }, 5000) }, getData() { // 通过 axios 获取数据 axios.get(this.dataUrl).then(res => { const data = res.data // 更新 option 中的数据 this.option.xAxis.data = data.map(item => item.time) this.option.series[0].data = data.map(item => item.value) // 调用 setOption 方法更新图表数据 this.chart.setOption(this.option, true) }) } }, destroyed() { clearInterval(this.timer) } } </script> ``` 在上面的代码中,我们首先在 data 中定义了 option、dataUrl 和 timer 三个变量。option 用于存储图表的配置项和数据,dataUrl 用于存储获取数据的接口地址,timer 用于存储定时器,用于定时更新数据。 在 initChart 方法中,我们首先初始化图表,然后调用 getData 方法获取初始数据,并启动定时器定时更新数据。 在 getData 方法中,我们通过 axios 获取数据,并更新 option 中的数据,然后调用 setOption 方法更新图表数据。同时,我们还传入了 true 参数,表示采用动态数据接口的方式更新数据。 最后,在 destroyed 钩子函数中,我们清除定时器,防止内存泄漏。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值