whyczyk
2021-10-13 a355aafef5a635bd31a701127cea097338cee0a7
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
<template>
    <div class="chartCon">
        <div class="chartItem">
            <pross-pie-chart id="prossPieChart0" ref="prossPieChart0"></pross-pie-chart>
        </div>
        <div class="chartItem">
            <pross-pie-chart id="prossPieChart1" ref="prossPieChart1"></pross-pie-chart>
        </div>
        <div class="chartItem">
            <pross-pie-chart id="prossPieChart2" ref="prossPieChart2"></pross-pie-chart>
        </div>
        <div class="chartItem">
            <pross-pie-chart id="prossPieChart3" ref="prossPieChart3"></pross-pie-chart>
        </div>
    </div>
</template>
 
<script>
import prossPieChart from "./prossPieChart"
import { WebSocketClass } from '@/assets/js/socket'
 
export default {
    components: {
        prossPieChart
    },
    data() {
        return {
            websock: null
        }
    },
    mounted() {
 
    },
    methods: {
        setData(data) {
            this.$nextTick(() => {
                if (data) {
                    data.map((item, i) => {
                        let chart = this.$refs[`prossPieChart${i}`];
                        chart.setData(item);
                        chart.resize();
                    })
                } else {
                    this.postData()
                }
 
            })
        },
        postData() {
            let userId = localStorage.getItem('userId');
            this.websock = new WebSocketClass(`/screen/powerAlarm/status/${userId}`, this.wsMessage)
        },
        wsMessage(res) {
            if (res.code == 1) {
                let optionData = [{
                    title: '',
                    name: '',
                    data: 0,
                    unit: '',
                    color: '#37a9b3',
                }, {
                    title: '',
                    name: '',
                    data: 0,
                    unit: '',
                    color: '#f3535f'
                }, {
                    title: '',
                    name: '',
                    data: 0,
                    unit: '',
                    color: '#ff8b00'
                }, {
                    title: '',
                    name: '',
                    data: 0,
                    unit: '',
                    color: '#757ffb'
                },]
                let index = 0;
                let resData = res.data;
                for (let key in resData) {
                    optionData[index].title = key;
                    optionData[index].name = key;
                    if (typeof resData[key] == 'string') {
                        optionData[index].data = Number(resData[key].split('%')[0]);
                        optionData[index].unit = '%';
                    } else {
                        optionData[index].data = resData[key];
                    }
                    index++;
                }
                optionData.map((item, i) => {
                    let chart = this.$refs[`prossPieChart${i}`];
                    chart.setData(item);
                    chart.resize();
                })
            }
        },
        outClear() {
            this.websock.closeMyself()
            this.websock = null
            window.removeEventListener('resize', this.resize);
        },
        resize() {
            this.$refs.prossPieChart0.resize();
            this.$refs.prossPieChart1.resize();
            this.$refs.prossPieChart2.resize();
            this.$refs.prossPieChart3.resize();
        }
    }
}
</script>
 
<style scoped>
.chartCon {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
}
 
.chartCon .chartItem {
    width: 50%;
    height: 48%;
    float: left;
    margin-bottom: 2%;
}
</style>