/** * 环境风险统计图 */ // 请求接口数据 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} */ 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: '
' }) }).addTo(riskLayerGroup) this.animalService.L.marker([coordinates[1], coordinates[0]], { icon: this.animalService.L.divIcon({ className: '', iconAnchor: [30, 60], iconSize: [70, 70], html: '
' }) }).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 '
' + '
' + qyjc + '
' + '
一级风险:' + oneLevel + '个
' + '
二级风险:' + twoLevel + '个
' + '
三级风险:' + threeLevel + '个
' + '
' } }, 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) } }