echarts 二级下钻地图

echarts 二级下钻地图

<template>
  <div class="map-box">
    <div
      class="echats"
      :id="id"
      :style="`width:${width};height:${height}`"
      v-loading="loading"
    ></div>

    <div class="tools-box">
      <el-button type="" class="back-btn" @click="handleBack">返回</el-button>
    </div>
  </div>
</template>

<script>
import echarts from "echarts";
import fullJson from "./full.json";
import { getProvenceData, getWordOrChinaData } from "@/api/map";
import province2pinyin from "@/utils/province2pinyin";
// 引入china.json
import china from "./full.json";

export default {
  props: {
    value: {
      type: Number,
      default: 0,
    },
    width: {
      type: String,
      default: "800px",
    },
    height: {
      type: String,
      default: "800px",
    },
    id: {
      type: String,
      default: "map111",
    },
  },
  data() {
    return {
      loading: true,
      chart: null,
      fullJson: fullJson,
      mapData: {},
    };
  },
  created() {},
  mounted() {
    this.initChart();
  },
  watch: {
    value() {
      // this.showChart();
    },
  },
  methods: {
    // 返回
    handleBack() {
      this.getChinaData();
    },
    // 获取中国数据
    async getChinaData() {
      let china = await getWordOrChinaData("china");
      echarts.registerMap("china", china);
      let option = {
        series: {
          name: "11",
          type: "map",
          map: "china",
          // data: [
          //   {
          //     name: "河南",
          //     value: "1222",
          //     itemStyle: {
          //       areaColor: "#008cff", // 单个地区的颜色
          //     },
          //   },
          // ],
        },
        // 提示信息
        tooltip: {
          formatter(data) {
            return data;
          },
        },
        itemStyle: {
          normal: {
            borderColor: "rgba(19,198,249,0.45)",
            borderWidth: 1,
            color: "rgba(0,0,0,0)",
            areaColor: {
              type: "radial",
              x: 0.5,
              y: 0.5,
              r: 0.8,
              colorStops: [
                {
                  offset: 0,
                  color: "rgba(0,0,0,0)",
                },
                {
                  offset: 1,
                  color: "rgba(19,198,249,0.15)",
                },
              ],
              global: false,
            },
            shadowColor: "rgba(19,198,249,1)",
            shadowOffsetX: 0,
            shadowOffsetY: 0,
            shadowBlur: 10,
          },
        },
      };
      if (this.chart) {
        this.chart.clear();
        this.chart.setOption(option, true);
      } else {
        this.initChart();
      }
      this.loading = false;
    },

    // 获取省份数据
    async getProvenceData(cname) {
      let provinceName = cname;
      let provinceData = await getProvenceData(cname);
      echarts.registerMap(provinceName, provinceData);
      let option = {
        series: {
          name: provinceName,
          type: "map",
          map: provinceName,
        },
        // 提示信息
        tooltip: {
          formatter(data) {
            return data;
          },
        },
      };
      if (this.chart) {
        this.chart.clear();
        this.chart.setOption(option, true);
      } else {
        this.initChart();
      }
      this.loading = false;
    },
    // 展示图表
    showChart() {
      // 注册地图
      this.getChinaData();
    },
    initChart() {
      let self = this;
      this.chart = echarts.init(document.getElementById(this.id));
      this.showChart();
      this.chart.on("click", function (params) {
        let provinceName = province2pinyin(params.name).toLowerCase();
        self.getProvenceData(provinceName);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.map-box {
  position: relative;
  width: 100%;
  height: 100%;
}
.echats {
  padding: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.back-btn {
  position: absolute;
  top: 10px;
  left: 5%;
  background-color: transparent;
  border: none;
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值