派生自 wuyushui/SewerAndRainNetwork

徐旺旺
2021-05-30 a312e0dd96d8f7e96fb3341f1a55561b12394405
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
 * 环境风险统计图
 */
// 请求接口数据
const mapApi = require('../../../api/mapApi').default
// 使用封装方法
const AnimalService = require('../service/AnimalService').default
// 引入echarts基本组件
const echarts = require('echarts/lib/echarts')
 
module.exports = function () {
  // 环形统计图数组
  let riskLayerGroup = null
 
  /**
   * 初始化图层数组
   * @param L leaflet对象
   */
  this.init = async (layer, L) => {
    this.animalService = new AnimalService({
      L: L,
      layer: layer
    })
  }
 
  /**
   * 加载图层
   * @returns {Promise<void>}
   */
  this.start = async () => {
    if (riskLayerGroup) { // 图层全选时,清理图层数组并重新加载
      riskLayerGroup.remove()
      riskLayerGroup = null
    }
    riskLayerGroup = this.animalService.L.featureGroup().addTo(this.animalService.layer)
    const result = await mapApi.getEnvironmentRisk()
    const features = result.features
    for (let i = 0; i < features.length; i++) {
      const feature = features[i]
      const geometry = feature.geometry
      const properties = feature.properties
      const qyId = properties.QY_ID
      const distract = properties.DISTRACT
      const coordinates = geometry.coordinates
      if (distract !== '长江沿线') {
        continue
      }
      this.animalService.L.marker([coordinates[1], coordinates[0]], {
        icon: this.animalService.L.divIcon({
          className: '',
          iconAnchor: [15, 45],
          iconSize: [40, 40],
          html: '<div style="width: 40px; height: 40px; background-color: rgba(255,255,255,1); position: relative; border-radius: 50%;"></div>'
        })
      }).addTo(riskLayerGroup)
      this.animalService.L.marker([coordinates[1], coordinates[0]], {
        icon: this.animalService.L.divIcon({
          className: '',
          iconAnchor: [30, 60],
          iconSize: [70, 70],
          html: '<div id="qy_id_' + qyId + '" style="width: 70px; height: 70px; background-color: transparent; position: relative; border-radius: 50%;"></div>'
        })
      }).addTo(riskLayerGroup)
      chartRender(properties)
    }
  }
 
  /**
   * 清除图层
   */
  this.destory = () => {
    if (riskLayerGroup) {
      riskLayerGroup.remove()
      riskLayerGroup = null
    }
  }
 
  /**
   * Echarts环形饼图
   * @param properties
   */
  function chartRender (properties) {
    const qyId = properties.QY_ID
    const qyjc = properties.QY_JC
    const num = properties.QY_NUM
    const oneLevel = properties.QY_ONELEVEL
    const twoLevel = properties.QY_TWOLEVEL
    const threeLevel = properties.QY_THREELEVEL
    const o = echarts.init(document.getElementById('qy_id_' + qyId))
    const option = {
      tooltip: {
        trigger: 'item',
        formatter: function (e) {
          return '<div style="background-color: #0c5460; padding: 5px;">' +
              '<div style="color: #63EEF5; font-size: 18px; font-weight: 500; text-align: center">' + qyjc + '</div>' +
              '<div>一级风险:<span style="color: red">' + oneLevel + '个</span></div>' +
              '<div>二级风险:<span style="color: yellow">' + twoLevel + '个</span></div>' +
              '<div>三级风险:<span style="color: lawngreen">' + threeLevel + '个</span></div>' +
              '</div>'
        }
      },
      color: ['red', 'yellow', 'lawngreen'], // 设置饼图各块颜色
      graphic: [ // 为环形图中间添加文字
        {
          type: 'text',
          left: 'center',
          top: '43%', // 设置环形文字的top位置
          style: {
            text: num,
            textAlign: 'center',
            fill: '#000',
            fontSize: 12
          }
        }],
      series: [{
        name: qyjc,
        type: 'pie',
        radius: ['35%', '65%'],
        label: {
          normal: {
            show: false,
            position: 'center',
            formatter: function (t) {
              return num
            },
            textStyle: {
              fontSize: 12,
              color: '#000000'
            }
          }
        },
        data: [{
          value: oneLevel,
          name: '一级风险'
        }, {
          value: twoLevel,
          name: '二级风险'
        }, {
          value: threeLevel,
          name: '三级风险'
        }]
      }]
    }
    o.setOption(option)
  }
}