Map.vue 5.54 KB
<!--
 * @Description: 
 * @Author: huyue
 * @Date: 2023-07-20 10:19:06
 * @LastEditors: huyue
 * @LastEditTime: 2023-08-23 16:15:25
-->
<template>
  <div>
    <div id="map" class="map"></div>
  </div>
</template>
<script>
import utils from '../../utils/util'
import * as echarts from 'echarts'
import mapBg from '../../assets/images/map-bg.jpg'
export default {
  name: 'Map',
  props: {
    storeDistrictList: Array
  },
  data() {
    return {
      myChart: ''
    }
  },
  mounted() {
    this.myChart = echarts.init(document.getElementById('map'))
    this.chartBindEvent()
  },
  methods: {
    getOptions(data) {
      var option = {
        tooltip: {
          show: true,
          borderWidth: 0,
          padding: 0,
          backgroundColor: '#051526',
          formatter: function(params) {
            var _html = `<div style="backdrop-filter: blur(4px);background-color: #051526;border: 2px solid;border-image: linear-gradient(90deg, #074989, #30DCFF, #074989) 1;padding: 10px;">
              <div style="color: #fff;">${params.name}</div>
              <div style="display:flex;"><div style="color: #c9c9c9">客单价:</div><div style="color: #fff;">¥${params.data ? utils.numAddSymbol(params.data.perCusTrans) : 0}</div></div>
              <div style="display:flex;"><div style="color: #c9c9c9">转化率:</div><div style="color: #fff;">${params.data ? utils.numAddSymbol(params.data.conversionRate) : 0}%</div></div>
              <div style="display:flex;"><div style="color: #c9c9c9">占比:</div><div style="color: #fff;">${params.data ? utils.numAddSymbol(params.data.rate) : 0}%</div></div>
          </div>`
            return _html
          }
        },
        series: [{
          type: 'map',
          map: 'china',
          data: data,
          label: {
            normal: {
              color: '#ffffff'
            },
            emphasis: {
              color: '#ffffff'
            }
          },
          itemStyle: {
            color: '#ffffff',
            // areaColor: '#061422',
            areaColor: {
              image: mapBg,
              repeat: 'repeat'
            },
            borderColor: '#65C6F8',
            shadowBlur: 10,
            shadowColor: 'rgba(61, 87, 255, 0.41)',
            emphasis: {
              areaColor: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [{
                  offset: 0, color: '#079FF1' // 0% 处的颜色
                }, {
                  offset: 1, color: '#6578FF' // 100% 处的颜色
                }],
                global: false // 缺省为 false
              }
            }
          },
          select: {
            itemStyle: {
              // color: this.clickcolor, // 设置地图点击后的颜色
              color: '#ffffff', // 设置地图点击后的颜色
              areaColor: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [{
                  offset: 0, color: '#079FF1' // 0% 处的颜色
                }, {
                  offset: 1, color: '#6578FF' // 100% 处的颜色
                }],
                global: false // 缺省为 false
              }
            }
          },
        }]
      }
      return option
    },
    drawMap(data) {
      this.mapOption = this.getOptions(data)
      this.myChart.setOption(this.mapOption)
      this.selectEvent()
      this.myChart.dispatchAction({
        type: 'showTip',
        seriesIndex: 0,
        dataIndex: 0
      })
      // 取消高亮指定的数据图形
      this.myChart.dispatchAction({
        type: 'downplay',
        seriesIndex: 0,
        dataIndex: 0
      })
      this.myChart.dispatchAction({
        type: 'highlight',
        seriesIndex: 0,
        dataIndex: 0
      })
    },
    selectEvent() {
      const myChart = this.myChart
      this.myChart.on('click', function(e) {
        myChart.dispatchAction({
          type: 'downplay',
          seriesIndex: 0
        })
        myChart.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: e.dataIndex
        })
        myChart.dispatchAction({
          type: 'showTip',
          seriesIndex: 1,
          dataIndex: e.dataIndex
        })
      })
      this.myChart.on('mousemove', function(e) {
        myChart.dispatchAction({
          type: 'downplay',
          seriesIndex: 0
        })
        myChart.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: e.dataIndex
        })
        myChart.dispatchAction({
          type: 'showTip',
          seriesIndex: 1,
          dataIndex: e.dataIndex
        })
      })
      this.myChart.on('mouseout', function (e) {
        myChart.dispatchAction({
          type: 'downplay',
          seriesIndex: 0
        })
        myChart.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: e.dataIndex
        })
        myChart.dispatchAction({
          type: 'showTip',
          seriesIndex: 1,
          dataIndex: e.dataIndex
        })
      })
    },
    chartBindEvent() {
      // resize
      window.addEventListener('resize', () => {
        this.myChart.resize()
      })
    }
  }
}
</script>

<style lang="scss">
  .map {
    width: 40.58vw;
    height: 45.89vh;
  }
</style>