maps.vue 7.62 KB
Newer Older
Your Name's avatar
Your Name committed
1
<template>
2
  <div id="map-box">
Your Name's avatar
Your Name committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    <span id="back" v-if="parentInfo.length !== 1" @click="handleBack()"
      >返回</span
    >

    <div id="mapDiv"></div>
  </div>
</template>

<script>
import axios from "axios";
import echarts from "echarts";
export default {
  name: "maps",
  data() {
    return {
      dataList: [],
      show: "province",
      parentInfo: [
        {
          cityName: "全国",
          code: 100000,
        },
      ],
    };
  },
  mounted() {
    this.initData(100000);
  },
  methods: {
    getList(type, areaCode) {
      let _this = this;
      return new Promise((resolve, reject) => {
        let httpUrl = "";
        let dataList = [];
        if (type === "china") {
          httpUrl = "tBoardStatistic/getBoardProvincePlayTotalList";
        }
        if (type === "map") {
          httpUrl = `tBoardStatistic/getBoardCityPlayTotalList?areaCode=${areaCode}`;
        }
        _this
          .$https({
            method: "post",
            url: httpUrl,
            authType: _this.backToken,
          })
          .then((res) => {
            if (res.status == 200) {
              if (res.data.resultCode == 200) {
                dataList = res.data.data;
              } else {
                dataList = [];
              }
            } else {
              dataList = [];
            }
            resolve(dataList);
          })
          .catch((err) => {
            this.$message.error(err.message);
            resolve([]);
          });
      });
    },
    initData(adcode) {
      Promise.all([
        this.getGeoJson(adcode),
        this.getList(this.parentInfo.length === 1 ? "china" : "map", adcode),
      ]).then((res) => {
        let features = res[0].features;
        let json = res[1];
        let flen = features.length;
        let jlen = json.length;
        for (let i = 0; i < flen; i++) {
          for (let j = 0; j < jlen; j++) {
            let jName = json[j].name.substring(0, 2);
            if (features[i].properties.name.indexOf(jName) != -1) {
              features[i].properties.value = json[j].value;
              break;
            }
          }
        }
        this.getMapData({ features: features });
      });
    },
    init(mapData) {
      let option = {
        tooltip: {
          // triggerOn: "click",
          formatter: function (e, t, n) {
            return 0.5 == e.value
              ? e.name + ":播放量"
              : e.seriesName + "<br />" + e.name + ":" + e.value;
          },
        },
        visualMap: [
          {
            dimension: 0,
            right: 20,
            bottom: 20,
            itemWidth: 16,
            itemHeight: "200px",
            orient: "horizontal",
            text: ["由高到低", "播放量"],
            backgroundColor: "rgba(0,28,66,0.6)",
            padding: [15, 10],
            textStyle: {
              color: "rgba(255,255,255,1)",
            },
            inRange: {
              color: ["#9B1E23", "#E72128", "#FB8D1F", "#FFCF4E"],
            },
          },
        ],
        geo: {
          map: this.parentInfo.length === 1 ? "china" : "map",
          roam: !1,
          roam: true, //是否开启平游或缩放
121 122 123 124
          // scaleLimit: {
          //   //滚轮缩放的极限控制
          //   min: 1
          // },
Your Name's avatar
Your Name committed
125 126

          zoom: 1.1,
127 128 129
          top: 110,
          bottom:0,
          left: "24%",
Your Name's avatar
Your Name committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
          label: {
            normal: {
              show: !0,
              fontSize: "12",
              color: "#fff",
              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;
                }
                return p.name;
              },
            },
          },
163 164 165 166
          emphasis: {
            // color: "#ffffff",
            show: true,
          },
Your Name's avatar
Your Name committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
          itemStyle: {
            normal: {
              borderColor: "rgba(0, 0, 0, 0.2)",
            },
            emphasis: {
              areaColor: "#f2d5ad",
              shadowOffsetX: 0,
              shadowOffsetY: 0,
              borderWidth: 0,
            },
          },
          regions: [
            {
              name: "南海诸岛",
              itemStyle: {
                // 隐藏地图
                normal: {
                  opacity: 0, // 为 0 时不绘制该图形
                },
              },
              label: {
                show: false, // 隐藏文字
              },
            },
          ],
        },
        series: [
          {
            name: "播放量",
            type: "map",
            geoIndex: 0,
            data: mapData,
          },
        ],
      };
      let echartsDiv = this.$echarts.init(document.getElementById("mapDiv"));
203
      echartsDiv.setOption(option, true);
Your Name's avatar
Your Name committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
      echartsDiv.on("click", this.echartsMapClick);
    },
    handleBack() {
      if (this.parentInfo.length === 1) {
        return;
      }
      this.parentInfo.pop();
      this.initData(this.parentInfo[this.parentInfo.length - 1].code);
    },
    echartsMapClick(params) {
      if (!params.data) {
        return;
      }
      if (params.data.level === "district") {
        return false;
      }
      let cityCode = params.data.cityCode;
      this.parentInfo.push({
        cityName: params.data.name,
        code: params.data.cityCode,
      });
      this.initData(cityCode);
    },
    getGeoJson(adcode) {
      return new Promise((resolve, reject) => {
        axios({
          method: "get",
          url: `static/feature/${adcode}_full.json`,
        }).then((res) => {
          let mapJson = {
            features: res.data.features,
          };
          resolve(mapJson);
        });
      });
    },
    getMapData(geoJson) {
      // 获取后台接口数据
      let Json = geoJson.features;
      let mapData = Json.map((item) => {
        return {
          name: item.properties.name,
          value: item.properties.value,
          level: item.properties.level,
          cityCode: item.properties.adcode,
        };
      });
      let mapJson = {};
      //geoJson必须这种格式
      mapJson.features = Json;
      //去渲染echarts
      // console.log(mapData)
      this.initEcharts(mapData, mapJson);
    },
    initEcharts(mapData, mapJson) {
      echarts.registerMap(
        this.parentInfo.length === 1 ? "china" : "map",
        mapJson
      );
      this.init(mapData);
    },
  },
};
</script>

<style lang="less">
270
#map-box {
Your Name's avatar
Your Name committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  width: 100%;
  height: 100%;
  #back {
    position: absolute;
    font-size: 16px;
    color: #775858;
    left: 60px;
    top: 140px;
    cursor: pointer;
    z-index: 99;
    &:hover {
      color:#9B1E23;
    }
  }
  #mapDiv {
    width: 100%;
    height: 100%;
  }
}
</style>