whyczyk
2021-10-14 36d078db470bf939ec47b6018ea40fa0b875e86e
src/components/charts/chinaMap.vue
@@ -1,7 +1,16 @@
<template>
   <div class="echarts-wrapper">
   <div class="echarts-wrapper" @dblclick="dblclick">
      <div class="echarts-content" ref="chart" id="cityChart">
      </div>
      <div class="infoPanel" v-show="isShowInfoPanel" :style="{ left: mapInfoX + 'px', top: mapInfoY + 'px' }">
         <h4 class="infoPanel-Title">
            {{ mapInfoTitle }}
            <div class="closeBtn" @click.self="isShowInfoPanel=false"></div>
         </h4>
         <div class="infoPanel-center">
            <info-panel :infos="panelInfo"></info-panel>
         </div>
      </div>
   </div>
</template>
@@ -12,6 +21,8 @@
   searchMap,
   searchMapHomeState,
   getAllMapOutlineAction,
   searchByDevId,
   searchHomeNum,
} from "../../assets/js/api";
import HomeNormal from '@/assets/images/home.png'
@@ -31,45 +42,96 @@
homeWarnImage.src = HomeWarnImage;
// 放电图标
import HomeDischargeImage from '@/assets/images/home-discharge.png';
import InfoPanel from '../indexPanel/InfoPanel.vue';
let homeDischargeImage = new Image();
homeDischargeImage.src = HomeDischargeImage;
const behindOverlay = {
   animate: "",
   img: ""
};
const warnOverlay = {
   animate: "",
   img: ""
};
const timeoutOverlay = {
   animate: "",
   img: ""
};
const normalOverlay = {
   animate: "",
   img: ""
};
let addHomeData = [];
let chartData = []; //chart数据
let abnormalArr = []; //异常数组
let tempData = []; //站点数组
let chart, chartLng, chartLat;
export default {
   components: { InfoPanel },
   name: "chinaMap",
   chart: "",
   chartData: "",
   data() {
      return {
         mapName: 'zhongguo'
         parentsStyle: {},
         isAllScreen: false,
         timers: null,
         mapName: 'zhongguo',
         mapInfoX: null,
         mapInfoY: null,
         mapInfoTitle: "",
         isShowInfoPanel: false,
         panelInfo: {
            num: 369,
            StationId: "42070471",
            StationName: "贵州省-贵阳市-观山湖区-观山湖区护理学院-设备1",
            StationName3: "观山湖区护理学院",
            Address: "湖北省武汉市武昌区",
            longitude: 114.37285909,
            latitude: 30.56442241,
            information: "",
            FBSDeviceId: 910000111,
            nums: {
               code: 0,
               sum: 0,
               newsum: 0
            }
         },
      }
   },
   methods: {
      findParents(node, select) {
         var parent = node.parentNode;
         if (parent === null || parent.className.indexOf(select) != -1) {
            return parent;
         } else {
            return this.findParents(parent, select);
         }
      },
      dblclick(e) {
         this.isAllScreen = !this.isAllScreen
         let parents = this.findParents(e.currentTarget, 'vdr')
         if (this.isAllScreen) {
            this.parentsStyle = JSON.parse(JSON.stringify(parents.style));
            parents.style.transform = 'none';
            parents.style.width = '100%';
            parents.style.height = '100%';
            parents.style.position = 'fixed';
            parents.style.left = 0;
            parents.style.right = 0;
            parents.style.bottom = 0;
            parents.style.top = 0;
            parents.style.zIndex = 99999;
         } else {
            parents.style.transform = this.parentsStyle.transform;
            parents.style.width = this.parentsStyle.width;
            parents.style.height = this.parentsStyle.height;
            parents.style.position = this.parentsStyle.position;
            parents.style.left = 'initial';
            parents.style.right = 'initial';
            parents.style.bottom = 'initial';
            parents.style.top = 'initial';
            parents.style.zIndex = 'auto';
         }
         this.$options.chart.resize();
      },
      setOption(opt) {
         this.$options.chart.setOption(opt);
      },
      setData(sendData) {
         this.$options.chartData = sendData;
         if (sendData) {
            this.$options.chartData = sendData;
            this.organizeData(sendData)
         } else {
            // 初始化页面
            this.getAllMapOutlineAction();
         }
      },
      organizeData(data) {
         let geoJson = require(`../../../public/mapJson/${this.mapName}.json`);
         echarts.registerMap('map', geoJson);
         // 整体配置项
@@ -149,7 +211,7 @@
                  }
               }
            },
            series: this.getSeries(sendData)
            series: this.getSeries(data)
         };
         // 设置配置项
         this.setOption(option);
@@ -178,7 +240,7 @@
            let rs = JSON.parse(res.data.result);
            if (rs.code == 1) {
               let data = rs.data;
               data.map((item, index) => {
               data.map((item) => {
                  if (item.status == 1) {
                     this.mapName = item.name;
                  }
@@ -192,47 +254,122 @@
               console.log(err);
            });
      },
      changeChartPanelStatus(homeData) {
         //panel面板状态改变
         this.isShowInfoPanel = true;
         let poit = this.convertMain(homeData.longitude, homeData.latitude);
         this.mapInfoX = poit[0] - 120;
         this.mapInfoY = poit[1] - 190;
         this.mapInfoTitle = homeData.StationName;
         this.$nextTick(() => {
            this.panelInfo = homeData
            // this.$refs.infoPanel.setInfo(homeData); //传值子组件
         });
      },
      convertMain(Lng, lat) {
         //根据经纬度计算定位
         // let getModel = chart.getModel().getSeries()[1];
         let seriesModel = chart.getModel().getSeriesByIndex(0);
         let coordSys = seriesModel.coordinateSystem;
         let point = coordSys.dataToPoint([Lng, lat]);
         return point;
      },
      startSearchMapHomeState() {
         this.timers = setInterval(() => {
            this.searchChartHomeState()
         }, 60 * 1000)
      },
      initPage() {
         // 初始化地图
         this.initCityChart();
         chart = echarts.init(document.getElementById('cityChart'));
         chart.off('click'); //防止chart点击触发多次
         chart.on("click", params => { //点击跳转实时数据
         chart.on("georoam", (params) => {
            clearInterval(this.timers)
            if (!this.isShowInfoPanel) {
               return;
            }
            this.isShowInfoPanel = false;
            this.timers = setTimeout(() => {
               this.isShowInfoPanel = true;
               let poit = this.convertMain(chartLng, chartLat);
               this.mapInfoX = poit[0] - 120;
               this.mapInfoY = poit[1] - 190;
               var option = chart.getOption(); //获得option对象
               if (params.zoom != null && params.zoom != undefined) {
                  //捕捉到缩放时
                  option.series[0].zoom = option.geo[0].zoom; //下层geo的缩放等级跟着上层的geo一起改变
                  option.series[0].center = option.geo[0].center; //下层的geo的中心位置随着上层geo一起改变
               } else {
                  //捕捉到拖曳时
                  option.series[0].center = option.geo[0].center; //下层的geo的中心位置着上层geo一起改变
               }
               chart.setOption(option); //设置option
               // this.startSearchMapHomeState();
            }, 300);
         });
         chart.off("click"); //防止chart点击触发多次
         chart.on("click", (params) => {
            //点击跳转实时数据
            if (params.seriesType == "scatter") {
               this.$router.push({
                  path: '/functionList'
               })
               chartLng = params.data.longitude;
               chartLat = params.data.latitude;
               this.showChartPanel(
                  params.data.StationId,
                  params.data.FBSDeviceId,
                  params.data
               );
            }
         });
      },
      // 查询告警落后的信息
      showChartPanel(sId, dev_id, homeData) {
         // 检测是否存在3D机房
         searchByDevId({
            deviceId: dev_id,
         })
            .then((res) => {
               let rs = JSON.parse(res.data.result);
               if (rs.data.length != 0) {
                  this.areas = rs.data;
                  //this.hdwDialog = true;
                  this.showThreeHomeDialog(this.areas);
               } else {
                  searchHomeNum({
                     // 查询告警落后的信息
                     StationId: sId,
                     FBSDeviceId: dev_id,
                  })
                     .then((res) => {
                        let rs = JSON.parse(res.data.result);
                        homeData.nums = rs;
                        this.changeChartPanelStatus(homeData);
                     })
                     .catch((error) => {
                        console.log(error);
                     });
               }
            })
            .catch((error) => {
               console.log(error);
            });
      },
      initCityChart() {
         searchMap().then((res) => {
            let rs = JSON.parse(res.data.result);
            let list = this.mergeMapInfos(rs);
            if (list != undefined && list.length > 0) {
               list.map((item, index) => {
               list.map((item) => {
                  item.name = item.StationName;
                  item.value = [];
                  item.value.push(item.longitude);
                  item.value.push(item.latitude);
                  tempData.push(item)
               })
               this.searchChartHomeState();
               let mapDotList = list.map(data => {
                  return {
                     lng: data.longitude,
                     lat: data.latitude,
                     title: data.StationName,
                     data: data,
                     name: data.StationName,
                     geometry: {
                        type: 'Point',
                        coordinates: [data.longitude, data.latitude]
                     }
                  }
               });
               addHomeData = mapDotList;
               this.searchChartHomeState()
               this.startSearchMapHomeState();
            } else {
               this.initChart([], []);
            }
@@ -247,7 +384,7 @@
            abnormalArr = [];
            if (rsState.code === 1) {
               let data = rsState.data;
               data.map((item, i) => {
               data.map((item) => {
                  let infos = tempData.filter((jtem) => {
                     return jtem.StationId == item.StationId
                  })
@@ -333,11 +470,11 @@
                  },
                  itemStyle: {
                     normal: {
                        color: function (value, params) {
                        color: function (value) {
                           return value.data.color
                        },
                        shadowBlur: 8,
                        shadowColor: function (value, params) {
                        shadowColor: function (value) {
                           return value.data.color
                        },
                     }
@@ -399,20 +536,100 @@
         }
         return rs;
      },
      showThreeHomeDialog(areas) {
         window.parent.parent.postMessage(
            {
               cmd: "showDialog",
               params: {
                  dialog: "hdwDialog",
                  pageInfo: areas,
               },
            },
            "*"
         );
      },
      outClear() {
         window.removeEventListener('resize', this.resize);
         clearInterval(this.timers)
         this.timers = null
      }
   },
   mounted() {
      // 初始化页面
      this.getAllMapOutlineAction();
      // 基于准备好的dom,初始化echarts实例
      this.$options.chart = echarts.init(this.$refs.chart);
      // 监听chartMap的面板
      window.addEventListener("resize", () => {
         if (!this.isShowInfoPanel || !this.mapName) {
            return;
         }
         this.isShowInfoPanel = false;
         setTimeout(() => {
            this.isShowInfoPanel = true;
            let poit = this.convertMain(chartLng, chartLat);
            this.mapInfoX = poit[0] - 120;
            this.mapInfoY = poit[1] - 190;
         }, 300);
      });
   },
   destroyed() {
      window.removeEventListener('resize', this.resize);
   }
      clearInterval(this.timers)
   },
}
</script>
<style scoped>
.infoPanel {
   position: absolute;
   background-color: #ffffff;
   color: rgb(0, 0, 0);
   border: 1px solid #999;
   z-index: 99;
}
.infoPanel .infoPanel-Title {
   border-bottom: 1px solid #ccc;
   height: 31px;
   line-height: 30px;
   background-color: #f9f9f9;
   overflow: hidden;
   height: 30px;
   padding: 0 5px;
   font-size: 12px;
   position: relative;
}
.infoPanel .infoPanel-Title .closeBtn {
   background: url("../../assets/images/iw_close1d3.gif") 0 0 no-repeat;
   background-size: 100% 100%;
   position: absolute;
   top: 9px;
   right: 12px;
   width: 10px;
   height: 10px;
   user-select: none;
   overflow: hidden;
   cursor: pointer;
   line-height: 9999px;
   z-index: 10000;
}
.infoPanel .infoPanel-center {
   padding: 3px 5px;
   overflow-x: auto;
   overflow-y: hidden;
}
.infoPanel::after {
   content: "";
   display: block;
   z-index: 5;
   position: absolute;
   bottom: -31px;
   width: 58px;
   height: 31px;
   left: 125px;
   background: url("../../assets/images/iw_tail.png") 0 0 no-repeat;
   background-size: 100% 100%;
}
</style>