Blame view

src/components/Echart/Map/Chart.vue 7.31 KB
任超 committed
1
<template>
任超 committed
2
  <!-- 地图 -->
任超 committed
3
  <Echart id="centreLeft2Chart" class="centreLeft2Chart" ref="centreLeft2ChartRef" width="100%" height="53vh"
任超 committed
4
    :options="options"></Echart>
任超 committed
5 6 7 8 9
</template>

<script>
import Echart from '@/common/echart';
export default {
任超 committed
10
  data () {
任超 committed
11 12
    return {
      options: {},
xiaomiao committed
13 14
      max: "100", //最大value值
      min: "1", // 最小value值
任超 committed
15 16 17 18 19 20 21 22 23 24 25 26 27
    };
  },
  components: {
    Echart,
  },
  props: {
    cdata: {
      type: Array,
      default: () => [],
    },
  },
  watch: {
    cdata: {
任超 committed
28 29
      handler (newData) {
        let _this = this;
任超 committed
30 31
        // 设置点的位置(经纬度)
        const geoCoordMap = {
任超 committed
32 33 34 35 36 37 38 39 40 41 42
          汉台区: [107.03187, 33.06774, 20],
          南郑区: [106.94024, 33.00299, 20],
          城固县: [107.33367, 33.15661, 20],
          洋县: [107.545837, 33.222739, 20],
          西乡县: [107.76867, 32.98411, 20],
          镇巴县: [107.89648, 32.53487, 20],
          勉县: [106.673221, 33.153553, 20],
          留坝县: [106.92233, 33.61606, 20],
          佛坪县: [107.98974, 33.52496, 20],
          宁强县: [106.25958, 32.82881, 20],
          略阳县: [106.15399, 33.33009, 20],
任超 committed
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
        };
        let convertData = function (data) {
          let scatterData = [];
          for (var i = 0; i < data.length; i++) {
            var geoCoord = geoCoordMap[data[i].name];
            if (geoCoord) {
              scatterData.push({
                name: data[i].name,
                value: geoCoord.concat(data[i].value),
              });
            }
          }
          return scatterData;
        };
        this.options = {
          showLegendSymbol: true,
          tooltip: {
            trigger: 'item',
            textStyle: {
              fontSize: 14,
              lineHeight: 22,
            },
            position: point => {
              // 固定在顶部
              return [point[0] + 50, point[1] - 20];
            },
            // 如果需要自定义 tooltip样式,需要使用formatter
任超 committed
70
            formatter: params => {
任超 committed
71
              return `<div style="">${params.name}:${params.value + "个"}</div>`
任超 committed
72
            }
任超 committed
73 74 75
          },
          visualMap: {
            min: 0,
任超 committed
76
            max: _this.max,
任超 committed
77
            bottom: '13%',
任超 committed
78
            left: 50,
任超 committed
79 80 81 82 83
            splitNumber: 6,
            seriesIndex: [0],
            itemWidth: 20, // 每个图元的宽度
            itemGap: 2,    // 每两个图元之间的间隔距离,单位为px
            pieces: [      // 自定义每一段的范围,以及每一段的文字
xiaomiao committed
84 85 86 87
              { gte: 100, label: '100以上', color: '#035cf5' }, // 不指定 max,表示 max 为无限大(Infinity)。
              { gte: 50, lte: 6000, label: '20-100', color: '#3375e4' },
              { gte: 20, lte: 2000, label: '1-200', color: '#6797ef' },
              { gte: 1, lte: 1000, label: '1-20', color: '#96b5ef' },
任超 committed
88 89 90 91
            ],
            textStyle: {
              color: '#737373'
            }
任超 committed
92
          },
任超 committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
          geo: {
            aspectScale: 1, //长宽比
            zoom: 1.1,
            mapType: '汉中市', // 自定义扩展图表类型
            top: '15%',
            left: '10%',
            map: '汉中市',
            itemStyle: {
              normal: {//阴影
                areaColor: '#02D9FD ',
                shadowColor: '#01271F',
                borderWidth: 0,
                shadowOffsetX: 2,
                shadowOffsetY: 25
              }
            }
          },
任超 committed
110 111 112
          series: [
            {
              type: 'map',
任超 committed
113 114 115
              aspectScale: 1, //长宽比
              zoom: 1.1,
              mapType: '汉中市', // 自定义扩展图表类型
任超 committed
116
              top: '15%',
任超 committed
117
              left: '10%',
任超 committed
118 119
              itemStyle: {
                normal: {
xiaomiao committed
120 121 122
                  areaColor: 'rgba(19,54,162,.5)',
                  borderColor: 'rgba(0,242,252,.5)',
                  borderWidth: 2,
任超 committed
123
                  shadowBlur: 7,
任超 committed
124
                  borderColor: 'rgba(19,54,162,.1)',
xiaomiao committed
125
                  shadowColor: '#44f2fc',
任超 committed
126 127 128
                },
                emphasis: {
                  areaColor: '#4f7fff',
xiaomiao committed
129
                  borderColor: 'rgba(0,242,252,.5)',
任超 committed
130 131 132 133 134 135
                  borderWidth: 2,
                  shadowBlur: 10,
                  shadowColor: '#00f2fc',
                },
              },
              label: {
任超 committed
136
                formatter: params => {
xiaomiao committed
137
               return `${params.name}\n${params.value+"个"}`;
xiaomiao committed
138
            },
任超 committed
139 140 141 142 143 144 145 146 147
                show: true,
                position: 'insideRight',
                textStyle: {
                  fontSize: 14,
                  color: '#efefef',
                },
                emphasis: {
                  textStyle: {
                    color: '#fff',
任超 committed
148 149
                  }
                }
任超 committed
150 151
              },
              data: newData,
xiaomiao committed
152
            }
任超 committed
153 154 155 156 157 158 159 160 161 162 163
          ],
        };
        // 重新选择区域
        this.handleMapRandomSelect();
      },
      immediate: true,
      deep: true,
    },
  },
  methods: {
    // 开启定时器
任超 committed
164
    startInterval () {
任超 committed
165 166 167 168 169 170 171 172 173 174 175
      const _self = this;
      // 应通过接口获取配置时间,暂时写死5s
      const time = 2000;
      if (this.intervalId !== null) {
        clearInterval(this.intervalId);
      }
      this.intervalId = setInterval(() => {
        _self.reSelectMapRandomArea();
      }, time);
    },
    // 重新随机选中地图区域
任超 committed
176
    reSelectMapRandomArea () {
任超 committed
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
      const length = 9;
      this.$nextTick(() => {
        try {
          const map = this.$refs.centreLeft2ChartRef.chart;
          let index = Math.floor(Math.random() * length);
          while (index === this.preSelectMapIndex || index >= length) {
            index = Math.floor(Math.random() * length);
          }
          map.dispatchAction({
            type: 'mapUnSelect',
            seriesIndex: 0,
            dataIndex: this.preSelectMapIndex,
          });
          map.dispatchAction({
            type: 'showTip',
            seriesIndex: 0,
            dataIndex: index,
          });
          map.dispatchAction({
            type: 'mapSelect',
            seriesIndex: 0,
            dataIndex: index,
          });
          this.preSelectMapIndex = index;
        } catch (error) {
          console.log(error)
        }
      });
    },
任超 committed
206
    handleMapRandomSelect () {
任超 committed
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
      this.$nextTick(() => {
        try {
          const map = this.$refs.centreLeft2ChartRef.chart;
          const _self = this;
          setTimeout(() => {
            _self.reSelectMapRandomArea();
          }, 0);
          // 移入区域,清除定时器、取消之前选中并选中当前
          map.on('mouseover', function (params) {
            clearInterval(_self.intervalId);
            map.dispatchAction({
              type: 'mapUnSelect',
              seriesIndex: 0,
              dataIndex: _self.preSelectMapIndex,
            });
            map.dispatchAction({
              type: 'mapSelect',
              seriesIndex: 0,
              dataIndex: params.dataIndex,
            });
            _self.preSelectMapIndex = params.dataIndex;
          });
          // 移出区域重新随机选中地图区域,并开启定时器
          map.on('globalout', function () {
            _self.reSelectMapRandomArea();
            _self.startInterval();
          });
          this.startInterval();
        } catch (error) {
          console.log(error)
        }
      });
    },
  },
};
</script>
任超 committed
243 244 245
<style>

</style>