mapForce.vue 11.1 KB
Newer Older
!阳仔's avatar
!阳仔 已提交
1 2 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
<template>
  <div class="csdn-map">
    <div id="china_map" :style="`width:${mapStyle.width}px;height:${mapStyle.height}px;`" ref="myEchart1"></div>
  </div>
</template>
<script>
// 引入echarts
import * as echarts from "echarts";
// 引入中国,城市地图
import { chinaJson } from "../../../public/echarts/city.js"
import Bus from "@/common/bus"
export default {
  data() {
    return {
      flag: true,
      initOptions: {
        renderer: 'canvas'
      },
      pointArray: [],
      GLOptions: null,
      normalDistributionNumber: 150, // 正态分布扩散数量
      pointSize: 2, // 地图点的大小
      pointColor: '#E95307', // 地图点的颜色
      dataArray: [],
      dataList:[],
      geoGPS: {},
      oldMapData: {},
      isLoadFlights: true, // 是否设置echrts配置项
    };
  },
  // props:['optionList','title','mapStyle'],
  props:{
    type: {
      type: String,
      default: () => {
        return ''
      }
    },
    title: {
      type: String,
      default: () => {
        return ''
      }
    },
    optionList: {
      type: Array,
      default: () => {
        return []
      }
    },
    mapStyle: {
      type: Object,
      default: () => {
        return {
          width:'1000',
          height:'900',
          zoom:1,
          tooltip:true
        }
      }
    },
  },
  watch:{
     optionList () {
      this.renderMap()
    },
    mapStyle () {
      this.renderMap()
    }
  },
  methods: {
    randomData() {
      return Math.round(Math.random() * 1000);
    },
    renderMap() {
      this.handleData(this.optionList)
      this.setEchartOption()
    },
    handleData(res) {
      for (let i = 0; i < res.length; i++) {
        let dataObj = {
          name: '',
          value: ''
        }
        let resObj = res[i]
        dataObj.name = resObj.city
        if (this.title !== '铁粉数'){
          dataObj.value = resObj.score /3000
        }else{
!阳仔's avatar
!阳仔 已提交
90
          dataObj.value = resObj.score/300
!阳仔's avatar
!阳仔 已提交
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        }
        dataObj.score = resObj.score
        dataObj.list = resObj.list
        this.dataArray.push(dataObj)
        this.geoGPS[resObj.city] = resObj.gps
      }
      this.dataArray = this.dataArray.sort(function (a, b) {
              return b.value - a.value;
            })
      this.dataList = this.dataArray
    },
    convertData(data) {
      var res = [];
      for (var i = 0; i < data.length; i++) {
        var geoCoord = this.geoGPS[data[i].name];

        if (geoCoord) {
          res.push({
            name: data[i].name,
            value: geoCoord.concat(data[i].value)
          })
        }
      }
      return res;
    },
    setEchartOption() {
      this.myEchart = echarts.init(document.getElementById("china_map"));
      echarts.registerMap('chinas', chinaJson);
      var that = this
      var option = {
        geo: {
          map: 'chinas',
          zoom: this.mapStyle.zoom, // 地图缩放率
          roam: true,
          center: this.type === 'wap'?[104.40529, 24.904987]:[120.40529, 35.904987], // 地图中心点
          silent: 'move', // 拖拽以及缩放
          itemStyle: {
            normal: {
              areaColor: '#1E2F56', // 地图颜色
              borderColor: '#33547E', // 地图边界颜色
              borderWidth: .5
            }
          },
          scaleLimit:{                       //所属组件的z分层,z值小的图形会被z值大的图形覆盖
            min:0.8,                          //最小的缩放值
!阳仔's avatar
!阳仔 已提交
136
            max:35,                            //最大的缩放值
!阳仔's avatar
!阳仔 已提交
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 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 203 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 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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
          }
        },
        tooltip: {
          trigger: "item",
          borderColor: 'transparent',
          show: this.mapStyle.tooltip,
          transitionDuration: 0,
          showContent: true,
          enterable: true,
          backgroundColor: 'transparent', // 提示框浮层的背景颜色。
          axisPointer: { // 坐标轴指示器配置项。
            type: 'line', // 'line' 直线指示器  'shadow' 阴影指示器  'none' 无指示器  'cross' 十字准星指示器。
            axis: 'auto', // 指示器的坐标轴。 
            snap: true, // 坐标轴指示器是否自动吸附到点上
          },
          textStyle: { // 提示框浮层的文本样式。
            color: '#41feff',
            fontStyle: 'normal',
            fontWeight: 'normal',
            fontFamily: 'sans-serif',
            fontSize: 14,
          },
          padding: 0, // 提示框浮层内边距, 
          formatter: function (params) {
            var str = ''
            if (params) {
              var demo = ''
              if (that.dataArray[params.dataIndex].list.length>0){
                that.dataArray[params.dataIndex].list.forEach((item,index) => {
                  if (index<3){
                    demo += `<a href="${`https://blog.csdn.net/`+item.username}" target="_blank" class="exhibition active"><p>${index+1}</p> <p class="img"> <img src="${item.avatar}" alt=""> <span>${item.nickname || item.username}</span></p> <p class="num">${item.score}</p></a>`
                  }
                });
              }else{
                demo = ` <div class="popover-empty">
                        <span>暂无上榜</span>
                        <a href="" target="_blank">前往查看热榜更新规则</a>
                      </div>`
              }
              str = `<div class="popover">
                    <div class="popover-top">
                      <p class="popover-title">上榜用户</p>
                      <p class="popover-city"><i class="force"></i><span class="city">${params.name}</span> <span>${that.title}</span> <span class="num">${that.dataArray[params.dataIndex].score}</span></p>
                      <div class="exhibition"><p>排名</p> <p class="img">用户</p> <p>${that.title}</p></div>
                      ${demo}
                    </div>
                  </div>`
            } 
            return str

          },
        },
        series: [
          {
            name: 'Top 20',
            type: 'effectScatter',
            coordinateSystem: 'geo',
            data: this.convertData(this.dataArray).slice(0, 10),
            symbolSize: function (val) {
              if (that.type === 'wap') {
                if (val[2] / 40 <= 6) {
                  return 6;
                } else {
                  return val[2] / 40;
                }
              }
              if (val[2] / 40 <= 8) {
                return 8;
              } else {
                return val[2] / 40;
              }
            },
            encode: {
              value: 10
            },
            showEffectOn: 'render',
            hoverAnimation: true,
            label: {
              formatter: '{b}',
              position: 'left',
              show: true,
                  textStyle: { //图例文字的样式
                            color: '#fff',
                            fontSize: 13
                        },
            },
            itemStyle: {
              color: '#549AD7',
              shadowBlur: 10,
              shadowColor: '#333'
            },
            zlevel: 10
          },
        ]
      };
      this.myEchart.setOption(option);
      window.addEventListener("resize", () => {
        if (this.myEchart) {
          this.myEchart.resize();
        }
      });
    },
 
  },
  beforeDestroy(){
    this.myEchart && this.myEchart.clear();
  },
  mounted() {
    this.renderMap()
    this.myEchart.on('click', (chinaParam) => {
        if (chinaParam.seriesName === "Top 20"){
          this.$emit('setrankList',{list:this.dataArray[chinaParam.dataIndex].list,city:chinaParam.name})
          Bus.$emit('popupObj',{obj:this.dataArray[chinaParam.dataIndex],title:'上榜用户',navname:"铁粉数"})
        }
      })
      this.myEchart.on('georoam',(params)=> {
      // 逻辑代码
        let _option = this.myEchart.getOption()
        let _zoom = _option.geo[0].zoom; 
        if ( _zoom<=1.5){
          this.dataArray = this.dataList.slice(0,10)
        }
        if (1.5<_zoom&& _zoom<=3){
          this.dataArray = this.dataList.slice(0,20)
        }
        if (3<_zoom&& _zoom<=4){
          this.dataArray = this.dataList.slice(0,50)
        }
         if (4<_zoom&& _zoom<=6){
          this.dataArray = this.dataList.slice(0,90)
        }
        if (6<_zoom&& _zoom<=9){
          this.dataArray = this.dataList.slice(0,280)
        }
        if (9<_zoom&& _zoom<=10){
          this.dataArray = this.dataList
        }
        _option.series[0].data = this.convertData(this.dataArray);
        this.myEchart.setOption(_option);
      })

  },
};
</script>
<style lang="scss">
html,
body {
  width: 100%;
  height: 100%;
}
.map-city {
  width: 360px;
  height: 258px;
}
// #china_map {
//   width: 1200px;
//   height: 900px;
//   z-index: 1;
// }
.csdn-map {
  overflow: hidden;
  .popover {
    width: 300px;
    height: 258px;
    background-image: url("@/assets/img/popover.png");
    background-size: cover;
    padding-top: 10px;
    .popover-title {
      font-size: 16px;
      font-weight: 500;
      color: #77c0ff;
      height: 36px;
      line-height: 36px;
      padding: 0 16px;
    }
    .popover-empty {
      height: 120px;
      text-align: center;
      span {
        font-size: 18px;
        font-weight: 500;
      }
      a {
        font-size: 14px;
        font-weight: 500;
        color: #77c0ff;
      }
    }
    .popover-city {
      height: 44px;
      line-height: 44px;
      padding: 0 16px;
      display: flex;
      align-items: center;
      i.force {
        display: inline-block;
        width: 16px;
        height: 16px;
!阳仔's avatar
!阳仔 已提交
335
        background-image: url("@/assets/img/place-pc.png");
!阳仔's avatar
!阳仔 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
        background-size: cover;
      }
      span {
        font-size: 14px;
        font-weight: 500;
        color: #77c0ff;
        margin-left: 4px;
        &.city {
          margin-right: 48px;
        }
        &.num {
          color: rgba(41, 255, 163, 0.8);
        }
      }
    }
    .exhibition {
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      text-decoration:none;
      padding: 0 16px;
      margin: 0 2px;
      p {
        font-size: 14px;
        font-weight: 500;
        color: rgba(119, 192, 255, 0.7);
        text-align: start;
        display: flex;
        &:nth-child(1){
          width: 30px;
          text-align: center;
        }
        &:nth-child(3){
          width: 80px;
          text-align: center;
          margin-left:10px ;
        }
        &.img{
          width: 120px;
          margin-left: 24px;
          display: flex;
          align-items: center;
          span{
!阳仔's avatar
!阳仔 已提交
380 381
            display: inline-block;
            width: 88px;
!阳仔's avatar
!阳仔 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
            overflow: hidden;
            text-overflow: ellipsis;
            -webkit-line-clamp: 1;
            -webkit-box-orient: vertical;
            word-break: break-all;
          }
        }
      }
      img{
        width: 24px;
        height: 24px;
        border-radius: 50%;
        margin-right: 8px;
      }
      &.active {
        p {
          color: #77c0ff;
        }
        .num {
          color: rgba(41, 255, 163, 0.8);
        }
        &:hover {
          background: #141f38;
          cursor: pointer;
        }
      }
    }
  }
}
/* .wrapper{
  background: #0f0;
} */
</style>