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
| <template>
| <div class="air_trend">
| <div class="air_top header_air">
| <img class="air_img" :src="require('@/assets/images/regionalOverview/histogram.png')" alt="">
| <span class="air_text">最近24小时空气变化趋势</span>
| </div>
| <div v-if="this.dataAxis.length > 0" class="air_histogram">
| <div id="myChart" :style="{width: '100%', height: '200px'}" />
| </div>
| <div v-else style="width: 100%">
| <el-empty style="width: 100%" :image-size="60" description="无空气变化数据" />
| </div>
| </div>
| </template>
|
| <script>
| import * as echarts from 'echarts'
| export default {
| name: 'AirChangeTrend',
| data() {
| return {
| dataAxis: [],
| dataValue: [],
| timer: null
| }
| },
| created() {
| },
| mounted() {
| // console.log(this.timer)
| if (this.timer !== null) {
| clearInterval(this.timer)
| this.timer = null
| } else {
| this.query24HoursAQI()
| setTimeout(() => {
| this.initChats()
| }, 800)
| }
| this.timer = setInterval(() => {
| this.query24HoursAQI()
| setTimeout(() => {
| this.initChats()
| }, 1000)
| }, 1000 * 1800)
| },
| beforeDestroy() {
| if (this.timer !== null) {
| clearInterval(this.timer)
| this.timer = null
| }
| },
| methods: {
| // 获取一个城市最近24小时AQI
| query24HoursAQI() {
| this.$request({
| url: '/aqi/query24HoursAQI',
| method: 'get',
| params: {
| regionCode: this.$store.state.regionCode
| }
| }).then(res => {
| if (res.code === 0) {
| const arr = []
| const arr1 = []
| this.dataValue = []
| for (const key in res.data) {
| arr.push(key)
| arr1.push(res.data[key])
| }
| this.dataAxis = arr
| this.dataValue = arr1
| }
| })
| },
| initChats() {
| const chartDom = document.getElementById('myChart')
| const myChart = echarts.init(chartDom)
| // 'aqi': [50, 100, 150, 200, 300]
| let option
| const dataObj = this.dataValue.map(value => {
| const obj = {}
| obj.value = value
| const itemStyle = {
| color: value < 50 ? '#00e400' : value < 100 ? '#ffff00' : value < 150 ? '#ff7e00' : value < 200 ? '#ff0000' : value < 300 ? '#99004c' : '#7e0023'
| }
| obj.itemStyle = itemStyle
| return obj
| })
| option = {
| tooltip: {
| trigger: 'axis',
| backgroundColor: 'rgba(255,255,255,.8)', // 通过设置rgba调节背景颜色与透明度
| color: 'gray',
| borderWidth: '1',
| borderColor: '#d9d9d9',
| textStyle: {
| color: '#808080'
| },
| axisPointer: {
| type: 'none',
| lineStyle: {
| color: 'rgba(128, 128, 128, .6)'
| }
| }
| },
| grid: {
| left: '3%',
| right: '4%',
| bottom: '3%',
| containLabel: true
| },
| xAxis: [
| {
| type: 'category',
| data: this.dataAxis,
| axisTick: {
| alignWithLabel: true
| },
| axisLabel: {
| inside: false,
| color: '#999',
| interval: (num, value) => {
| if (num < 3) {
| return false
| } else {
| if ((num - 3) % 6 === 0) {
| return true
| } else {
| return false
| }
| }
| },
| formatter: function(value) {
| return echarts.format.formatTime('hh:mm', new Date(value))
| }
| },
| axisLine: {
| show: false,
| lineStyle: {
| color: 'rgba(153, 153, 153, 0.4)'
| }
| }
| }
| ],
| yAxis: {
| // 定义网格线条的颜色
| splitLine: {
| show: true,
| lineStyle: {
| color: 'rgba(153, 153, 153, 0.1)',
| width: 1,
| type: 'solid'
| }
| },
| axisLine: {
| show: false
| },
| axisTick: {
| show: false
| },
| axisLabel: {
| color: '#999'
| }
| },
| series: [
| {
| name: 'AQI',
| type: 'bar',
| barWidth: '60%',
| data: dataObj
| }
| ]
| }
| myChart.setOption(option)
| }
| }
| }
| </script>
|
| <style lang="less">
| .air_trend {
| width: 100%;
| margin-top: 6px;
| background-color: #fff;
| //box-shadow: 0 0 5px 1px #999;
| padding: 0;
| border: solid 1px #d9d9d9;
| border-radius: 3px;
| box-sizing: border-box;
| }
| .air_trend > .air_top {
| display: inline-block;
| outline: none;
| width: 100%;
| float: left;
| }
| .header_air {
| position: relative;
| height: 40px;
| background-color: #F8F7F7;
| .air_img {
| position: absolute;
| top: 10px;
| left: 16px;
| width: 20px;
| }
| .air_text {
| position: absolute;
| height: 40px;
| line-height: 40px;
| left: 40px;
| font-size: 15px;
| color: #808080;
| }
| }
| .air_histogram {
| width: 100%;
| height: 200px;
| margin-bottom: 10px;
| }
| </style>
|
|