whyczyk
2021-10-12 672c9c35b818c03090dbdb13425b04308855a963
src/components/charts/imgPieChart.vue
@@ -12,9 +12,7 @@
import {
   chartFontsize
} from '@/assets/js/chartFontsize'
import {
   rectifier
} from '@/assets/js/api'
import { WebSocketClass } from '@/assets/js/socket'
const pieImg = require('../../assets/images/rectifier-img.png');
export default {
   name: "imgPieChart",
@@ -29,7 +27,7 @@
   },
   data() {
      return {
         websock: null
      }
   },
   methods: {
@@ -182,34 +180,48 @@
            this.organizeData(sendData)
         } else {
            this.postData()
            setInterval(() => {
               this.postData()
            }, 3000)
         }
      },
      postData() {
         let userId = localStorage.getItem('userId');
         let params = {
            userId: userId
         }
         rectifier(params).then((res) => {
            if (res.data.code == 1) {
               let optionData = {
                  data: []
               }
               let resData = res.data.data;
               for (let key in resData) {
                  let obj = {};
                  obj.name = key;
                  obj.value = resData[key];
                  optionData.data.push(obj)
               }
               this.$options.chartData = optionData;
               this.organizeData(optionData)
         this.websock = new WebSocketClass(`/screen/powerAlarm/rectifier/${userId}`, this.wsMessage)
      },
      wsMessage(res) {
         if (res.code == 1) {
            let optionData = {
               data: [{
                  value: 0,
                  name: '整流器交流异常'
               },
               {
                  value: 0,
                  name: '整流器过流'
               },
               {
                  value: 0,
                  name: '整流器总故障'
               },
               {
                  value: 0,
                  name: '整流器欠压'
               },
               {
                  value: 0,
                  name: '整流器过压'
               },
               ]
            }
         }).catch((err) => {
            console.log(err)
         });
            let resData = res.data;
            for (let key in resData) {
               optionData.data.map(item => {
                  if (item.name == key) {
                     item.value = resData[key]
                  }
               })
            }
            this.$options.chartData = optionData;
            this.organizeData(optionData)
         }
      },
      resize() {
         setTimeout(() => {
@@ -218,6 +230,11 @@
               this.setData(this.$options.chartData);
            }
         }, 300)
      },
      outClear() {
         this.websock.closeMyself()
         this.websock = null
         window.removeEventListener('resize', this.resize);
      }
   },
   mounted() {
@@ -227,6 +244,7 @@
      window.addEventListener('resize', this.resize);
   },
   destroyed() {
      this.websock.closeMyself()
      window.removeEventListener('resize', this.resize);
   }
}