maps.vue 8.72 KB
Newer Older
乐宝呗666's avatar
乐宝呗666 committed
1
<template>
xulili's avatar
xulili committed
2 3 4 5 6 7
  <div class="map-box">
    <span id="back" v-if="parentInfo.length !== 1" @click="handleBack()"
      >返回</span
    >
    <div id="mapDiv"></div>
  </div>
乐宝呗666's avatar
乐宝呗666 committed
8 9 10
</template>

<script>
xulili's avatar
xulili committed
11 12 13
// import "echarts/map/js/china";
import axios from "axios";
import echarts from "echarts";
xulili's avatar
xulili committed
14 15 16 17 18
export default {
  name: "maps",
  data() {
    return {
      dataList: [],
xulili's avatar
xulili committed
19 20 21 22 23 24 25
      show: "province",
      parentInfo: [
        {
          cityName: "全国",
          code: 100000,
        },
      ],
xulili's avatar
xulili committed
26 27 28
    };
  },
  mounted() {
xulili's avatar
xulili committed
29 30
    // this.getList();
    this.initData(100000);
xulili's avatar
xulili committed
31 32
  },
  methods: {
xulili's avatar
xulili committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    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,
          })
          .then((res) => {
            if (res.status == 200) {
              if (res.data.resultCode == 200) {
                dataList = res.data.data;
              } else {
                dataList = [];
              }
xulili's avatar
xulili committed
56
            } else {
xulili's avatar
xulili committed
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
              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;
乐宝呗666's avatar
乐宝呗666 committed
82
            }
xulili's avatar
xulili committed
83
          }
xulili's avatar
xulili committed
84 85 86
        }
        this.getMapData({ features: features });
      });
xulili's avatar
xulili committed
87
    },
xulili's avatar
xulili committed
88 89
    init(mapData) {
      let _this = this;
xulili's avatar
xulili committed
90 91
      let option = {
        tooltip: {
xulili's avatar
xulili committed
92
          // triggerOn: "click",
xulili's avatar
xulili committed
93 94 95 96
          formatter: function (e, t, n) {
            return 0.5 == e.value
              ? e.name + ":播放量"
              : e.seriesName + "<br />" + e.name + ":" + e.value;
乐宝呗666's avatar
乐宝呗666 committed
97
          },
xulili's avatar
xulili committed
98 99 100
        },
        visualMap: [
          {
乐宝呗666's avatar
乐宝呗666 committed
101
            dimension: 0,
xulili's avatar
xulili committed
102 103
            left: "8%",
            bottom: 20,
乐宝呗666's avatar
乐宝呗666 committed
104
            itemWidth: 16,
xulili's avatar
xulili committed
105 106 107 108
            orient: "horizontal",
            text: ["播放量", "由低到高"],
            backgroundColor: "rgba(0,19,45,0.4)",
            padding: 16,
乐宝呗666's avatar
乐宝呗666 committed
109
            textStyle: {
xulili's avatar
xulili committed
110
              color: "gba(255,255,255,1)",
乐宝呗666's avatar
乐宝呗666 committed
111 112
            },
            inRange: {
xulili's avatar
xulili committed
113 114 115 116 117 118
              color: [
                "rgba(161,255,255,1)",
                "rgba(91,214,255,1)",
                "rgba(38,157,255,1)",
                "rgba(0,72,149,1)",
              ],
乐宝呗666's avatar
乐宝呗666 committed
119
            },
xulili's avatar
xulili committed
120 121 122
          },
        ],
        geo: {
xulili's avatar
xulili committed
123
          map: _this.parentInfo.length === 1 ? "china" : "map",
xulili's avatar
xulili committed
124
          roam: true, //是否开启平游或缩放
xulili's avatar
xulili committed
125 126 127 128 129 130
          // scaleLimit: {
          //   //滚轮缩放的极限控制
          //   min: 1,
          //   // max: 2,
          // },
          zoom: 1.1,
xulili's avatar
xulili committed
131 132 133 134 135
          top: 51,
          label: {
            normal: {
              show: !0,
              fontSize: "12",
xulili's avatar
xulili committed
136
              color:'#968f8f',
xulili's avatar
xulili committed
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 163 164 165 166
              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;
              },
            },
            emphasis: {
              // color: "#ffffff",
              show: true,
乐宝呗666's avatar
乐宝呗666 committed
167
            },
xulili's avatar
xulili committed
168 169 170 171 172 173
          },
          itemStyle: {
            normal: {
              borderColor: "rgba(0, 0, 0, 0.2)",
            },
            emphasis: {
xulili's avatar
xulili committed
174 175
              // areaColor: "#f2d5ad",
              areaColor: "#8dd7fc",
xulili's avatar
xulili committed
176 177 178
              shadowOffsetX: 0,
              shadowOffsetY: 0,
              borderWidth: 0,
乐宝呗666's avatar
乐宝呗666 committed
179 180
            },
          },
xulili's avatar
xulili committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
          regions: [
            {
              name: "南海诸岛",
              itemStyle: {
                // 隐藏地图
                normal: {
                  opacity: 0, // 为 0 时不绘制该图形
                },
              },
              label: {
                show: false, // 隐藏文字
              },
            },
          ],
        },
        series: [
          {
乐宝呗666's avatar
乐宝呗666 committed
198 199 200
            name: "播放量",
            type: "map",
            geoIndex: 0,
xulili's avatar
xulili committed
201
            data: mapData,
xulili's avatar
xulili committed
202 203 204 205
          },
        ],
      };
      let echartsDiv = this.$echarts.init(document.getElementById("mapDiv"));
xulili's avatar
xulili committed
206
      echartsDiv.setOption(option,true);
xulili's avatar
xulili committed
207 208 209 210 211 212 213 214 215 216 217 218 219
      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;
      }
xulili's avatar
xulili committed
220
      debugger
xulili's avatar
xulili committed
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
      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, childAdcode = "") {
    //   return new Promise((resolve, reject) => {
    //     function insideFun(adcode, childAdcode) {
    //       AMapUI.loadUI(["geo/DistrictExplorer"], (DistrictExplorer) => {
    //         var districtExplorer = new DistrictExplorer();
    //         districtExplorer.loadAreaNode(adcode, function (error, areaNode) {
    //           if (error) {
    //             console.error(error);
    //             reject(error);
    //             return;
    //           }
    //           let Json = areaNode.getSubFeatures();
    //           if (Json.length === 0) {
    //             let parent = areaNode._data.geoData.parent.properties.acroutes;
    //             insideFun(parent[parent.length - 1], adcode);
    //             return;
    //           }

    //           if (childAdcode) {
    //             Json = Json.filter((item) => {
    //               return item.properties.adcode == childAdcode;
    //             });
    //           }
    //           let mapJson = {
    //             features: Json,
    //           };
    //           resolve(mapJson);
    //         });
    //       });
    //     }
    //     insideFun(adcode, childAdcode);
    //   });
    // },
    getGeoJson(adcode) {
      return new Promise((resolve, reject) => {
        axios({
          method: "get",
          url: `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
      this.initEcharts(mapData, mapJson);
    },
    initEcharts(mapData, mapJson) {
      echarts.registerMap(
        this.parentInfo.length === 1 ? "china" : "map",
        mapJson
      );
      this.init(mapData);
xulili's avatar
xulili committed
301 302 303
    },
  },
};
乐宝呗666's avatar
乐宝呗666 committed
304 305
</script>

xulili's avatar
xulili committed
306 307
<style lang="scss">
.map-box {
xulili's avatar
xulili committed
308 309
  width: 100%;
  height: 100%;
xulili's avatar
xulili committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  #back {
    position: absolute;
    font-size: 14px;
    color: rgba(255, 255, 255, 0.8);
    left: 60px;
    cursor: pointer;
    z-index: 99;
    &:hover {
      color: #4efffd;
    }
  }
  #mapDiv {
    width: 100%;
    height: 100%;
  }
xulili's avatar
xulili committed
325
}
乐宝呗666's avatar
乐宝呗666 committed
326
</style>