zhaowei
昨天 54d11f9a2f395e021486e6a31912616274b3feda
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<template>
  <div>
    <div id="workshop-equipment-status"></div>
    <div class="circle-status-container">
      <div v-for="(value,key) in dataSource">
        <span :style="{backgroundColor:getStatusColorAndLabel(key,'color')}">{{dataSource[key]}}</span>
        <span :style="{color:getStatusColorAndLabel(key,'color')}">{{getStatusColorAndLabel(key,'label')}}</span>
      </div>
    </div>
  </div>
</template>
 
<script>
  import signageApi from '@/api/signage'
 
  export default {
    name: 'WorkshopEquipmentStatusChart',
    components: {},
    props: {
      deviceStatusList: {
        type: Array
      }
    },
    data() {
      return {
        chartContainer: null,
        dataSource: {}
      }
    },
    mounted() {
      window.addEventListener('resize', this.handleWindowSizeChange)
    },
    methods: {
      // 调用接口获取全厂设备状态
      getWorkshopEquipmentStatusByApi() {
        const that = this
        signageApi.getWorkshopEquipmentStatusApi()
          .then(res => {
            if (res.success) {
              let total = 0
              for (let key in res.result) {
                total += res.result[key]
              }
              that.dataSource = res.result
              that.initChart(total)
            }
          })
      },
 
      /**
       * 初始化图表
       * @param total 设备数量总和
       */
      initChart(total) {
        this.chartContainer = this.$echarts.init(document.getElementById('workshop-equipment-status'))
        const option = {
          tooltip: {
            trigger: 'item',
            show: true,
            fontSize: '100%',
            hoverAnimation: false,
            itemStyle: {
              label: {
                show: true,
                position: 'center',
                formatter: '20'
              }
            }
          },
          label: {
            show: true,
            hoverAnimation: false,
            position: 'center',
            formatter: '20'
          },
          legend: {
            bottom: '3%',
            left: 'center',
            data: [
              {
                name: '运行',
                textStyle: {
                  color: '#eee',
                  fontSize: this.fontSize(0.12),
                  fontWeight: 'bold'
                }
              },
              {
                name: '待机',
                textStyle: {
                  color: '#eee',
                  fontSize: this.fontSize(0.12),
                  fontWeight: 'bold'
                }
              },
              {
                name: '报警',
                textStyle: {
                  color: '#eee',
                  fontSize: this.fontSize(0.12),
                  fontWeight: 'bold'
                }
              }
              ,
              {
                name: '关机',
                textStyle: {
                  color: '#eee',
                  fontSize: this.fontSize(0.12),
                  fontWeight: 'bold'
                }
              }
            ]
          },
          color: ['#00FF19', '#FFF701', '#FC0001', '#7B7B7B'],
          // 设置环形中间的数据
          graphic: [{
            type: 'text',
            z: 10,
            left: '46%',
            top: '42%',
            style: {
              fill: '#eee',
              //环中间的数据
              text: total,
              fontSize: this.fontSize(0.20),
              fontWeight: 'bold'
            }
 
          }],
          series: [
            {
              name: '当前设备状态',
              type: 'pie',
              radius: ['40%', '70%'],
              center: ['50%', '45%'],
              // avoidLabelOverlap:false,
              hoverAnimation: false,
              itemStyle: {
                borderColor: '#eee',
                borderWidth: 2
              },
              label: {
                show: false
              },
              labelLine: {
                show: false
              },
              data: [
                {
                  name: '运行',
                  value: this.dataSource.runCount,
                  textStyle: {
                    color: '#2278ff'
                  },
                  //设置环上的数据
                  label: {
                    show: true,
                    hoverAnimation: false,
                    position: 'inner',
                    //环上的数据
                    formatter: function(value) {
                      if (value.value !== 0) return value.value
                      else ''
                    },
                    fontSize: this.fontSize(0.16),
                    fontWeight: 'bold'
                  }
                },
                {
                  name: '待机',
                  value: this.dataSource.standbyCount,
                  textStyle: {
                    color: '#ff9e3c'
                  },
                  //设置环上的数据
                  label: {
                    show: true,
                    hoverAnimation: false,
                    position: 'inner',
                    //环上的数据
                    formatter: function(value) {
                      if (value.value !== 0) return value.value
                      else ''
                    },
                    fontSize: this.fontSize(0.16),
                    fontWeight: 'bold'
                  }
                },
                {
                  name: '报警',
                  value: this.dataSource.alarmCount,
                  textStyle: {
                    color: '#ff9e3c'
                  },
                  //设置环上的数据
                  label: {
                    show: true,
                    hoverAnimation: false,
                    position: 'inner',
                    //环上的数据
                    formatter: function(value) {
                      if (value.value !== 0) return value.value
                      else ''
                    },
                    fontSize: this.fontSize(0.16),
                    fontWeight: 'bold'
                  }
                },
                {
                  name: '关机',
                  value: this.dataSource.offCount,
                  textStyle: {
                    color: '#ff9e3c'
                  },
                  //设置环上的数据
                  label: {
                    show: true,
                    hoverAnimation: false,
                    position: 'inner',
                    //环上的数据
                    formatter: function(value) {
                      if (value.value !== 0) return value.value
                      else ''
                    },
                    fontSize: this.fontSize(0.16),
                    fontWeight: 'bold'
                  }
                }
              ]
            }
          ]
        }
        this.chartContainer.setOption(option, true)
      },
 
      //自适应字体方法封装
      fontSize(res) {
        var docEl = document.documentElement,
          clientWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
        if (!clientWidth) return
        var fontSize = 100 * (clientWidth / 1920)
        return res * fontSize
      },
 
      /**
       * 获取状态颜色和标题
       * @param dataSourceKey 接口数据属性key
       * @param statusKey 状态属性label/color
       * @returns {string} 颜色码或标题
       */
      getStatusColorAndLabel(dataSourceKey, statusKey) {
        const { deviceStatusList } = this
        switch (dataSourceKey) {
          case 'runCount':
            return deviceStatusList.find(item => item.label === '运行')[statusKey]
          case 'standbyCount':
            return deviceStatusList.find(item => item.label === '待机')[statusKey]
          case 'alarmCount':
            return deviceStatusList.find(item => item.label === '报警')[statusKey]
          case 'offCount':
            return deviceStatusList.find(item => item.label === '关机')[statusKey]
        }
      },
 
      handleWindowSizeChange() {
        if (this.chartContainer) this.chartContainer.resize()
      }
    }
  }
</script>
 
<style scoped lang="less">
  #workshop-equipment-status {
    width: 75%;
    height: 100%
  }
 
  .circle-status-container {
    width: 25%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
 
    span {
      display: inline-block;
      font-size: 0.8vw;
    }
 
    span:first-child {
      text-align: center;
      width: 2vw;
      height: 2vw;
      font-size: 1vw;
      line-height: 2vw;
      border-radius: 50%;
      margin-right: 8%;
      color: #000;
      font-weight: bold;
    }
  }
</style>