zmc
2023-07-11 77a108bd860c737449b46809f6e8a4e1242e5e3b
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
 
<template>
  <div>
    <div style="margin: 5px;">
    <span style="color: #b1b3b8;" >实时数据</span>
  </div>
 
  <hr style="margin-top: 10px;"/>
  <div v-loading="loading">
    <el-row :gutter="20">
      <el-col :span="8" v-for="device in devices" :key="device.id">
        <div class="dashboard">
          <div class="dashboard-title">设备编号为:{{ device.mnCode }}</div>
          <div ref="charts" class="echarts"></div>
          <div>
            <div class="data">当前数值:{{ device.value }}</div>
            <div class="data">产生时间:{{ device.time }}</div>
            <div class="status" :class="{'exceed': device.value > 1}"> {{ device.value >= 1 ? '超标' : '' }}</div>
            <div class="status1" :class="{'exceed1': device.value < 1}"> {{ device.value < 1 ? '未超标' : '' }}</div>
        </div>
        </div>
        
        
      </el-col>
    </el-row>
    加载
  </div>
  </div>
</template>
 
<script>
 
import * as echarts from 'echarts'
import axios from 'axios'
 
export default {
  data() {
    return {
      devices: [],
      loading:false, //加载中
    }
  },
  methods: {
    fetchData() {
      const params = {
        params: {
          page: 1,
          per_page: 10
        }
      }
      axios.get('https://fyami.com.cn/device/min/value/real_time', params)
        .then(response => {
          this.devices = response.data.data
        })
        .catch(error => {
          console.error(error)
        })
    },
    updateCharts() {
      for (let i = 0; i < this.devices.length; i++) {
        const device = this.devices[i]
        const chart = echarts.init(this.$refs.charts[i])
        
        const option = {
        title: {
          // text: '实时数据'
        },
        tooltip: {
          formatter: '{a} <br/>{b} : {c}'
        },
        series: [
          {
            name: '实时数据',
            type: 'gauge',
            detail: { formatter: '{value}' },
            data: [{ 
              value: device.value, 
              name: '油烟浓度' ,
              itemStyle: {
                color:device.value >= 1?'red' :'green'
              }
            }],
            max:2
          }
        ]
      }
        
        chart.setOption(option)
      }
      console.log('调用了!');
    }
  },
  mounted() {
    this.fetchData()
    this.loading = true
    setInterval(() => {
      this.updateCharts()
    }, 30000)    //每分钟(30秒)调用一次
    this.loading = false
  },
  created() {
    // 监听窗口大小变化,动态调整仪表盘布局
    window.addEventListener('resize', this.updateCharts)
  },
  beforeUnmount() {
    // 移除窗口大小变化的监听
    window.removeEventListener('resize', this.updateCharts)
  }
}
</script>
 
<style scoped>
.dashboard {
  margin-bottom: 20px;
}
 
.dashboard-title {
  font-size: 20px;
  text-align: center;
  padding: 10px 0;
  background-color: #f5f7fa;
}
 
.echarts {
  height: 300px;
}
 
/* 超标 文字效果*/
.status {
    font-size: 18px;
    color: #FF4D4F;
    font-weight: bold;
  }
  
  .exceed {
    color: #FF4D4F;
  }
  
/* 未超标 */
  .status1 {
    font-size: 18px;
    color: #1de01d;
    font-weight: bold;
  }
  
  .exceed1 {
    color:  #1de01d;
  }
</style>