1
+ <template >
2
+ <div class =" chart-all-kA6iA2" >
3
+ <div class =" chart-top-kA6iA2" >
4
+ <div style ="
5
+ text-align :center ;
6
+ color : #fff ;
7
+ width : 100% ;
8
+ white-space : nowrap ;
9
+ overflow : hidden ;
10
+ text-overflow : ellipsis ;
11
+ " >二氧化碳浓度当日最高值</div >
12
+ <!-- <div style="color: #5b92ff">Noise concentration</div> -->
13
+ </div >
14
+ <div class =" chart-body-kA6iA2" :id =" 'chart_' + id" ></div >
15
+ </div >
16
+ </template >
17
+ <script >
18
+ // import * as echarts from "echarts";
19
+ export default {
20
+ props: {
21
+ id: {
22
+ type: Number ,
23
+ default: 0 ,
24
+ },
25
+ loading: {
26
+ type: Boolean ,
27
+ default: true ,
28
+ },
29
+ apiData: {
30
+ type: Object ,
31
+ },
32
+ legend: {
33
+ type: Boolean ,
34
+ default: true ,
35
+ },
36
+ },
37
+ data () {
38
+ return {
39
+ latest: {},
40
+ fields: [],
41
+ chart: null ,
42
+ systime: ' ' ,
43
+ theTime: ' ' ,
44
+ hValue: 0
45
+ };
46
+ },
47
+ watch: {
48
+ apiData: {
49
+ // document.getElementById('ElementId').removeAttribute("style")
50
+ // style="width: 100%; height: 100%"
51
+ // apiData的数据样例,fields是传感器一段时间内的数据,latest是传感器最新数据
52
+ // {
53
+ // "device_id": "f1ab2c47-951f-10b8-60c0-c6b33440f189",
54
+ // "fields": [
55
+ // {
56
+ // "hum": 24,
57
+ // "systime": "2022-01-18 18:59:11",
58
+ // "temp": 26
59
+ // },
60
+ // {
61
+ // "hum": 24,
62
+ // "systime": "2022-01-18 18:59:17",
63
+ // "temp": 26
64
+ // },
65
+ // {
66
+ // "hum": 24,
67
+ // "systime": "2022-01-18 18:59:41",
68
+ // "temp": 26
69
+ // }
70
+ // ],
71
+ // "latest": {
72
+ // "hum": 24,
73
+ // "systime": "2022-01-18 18:59:41",
74
+ // "temp": 26
75
+ // }
76
+ // }
77
+ // deep: true,
78
+ immediate: true ,
79
+ handler (val , oldVal ) {
80
+ console .log (" 01-kA6iA2-图表接收到数据" );
81
+ console .log (" 02-kA6iA2-图表id:" + this .id );
82
+ if (val[" fields" ]) {
83
+ console .log (" 03-kA6iA2-fields有值" );
84
+ console .log (" 04-kA6iA2-device_id:" + val[" device_id" ]);
85
+ this .latest = val[" latest" ];
86
+ this .fields = val[" fields" ];
87
+ this .getData ();
88
+ } else {
89
+ console .log (" 05-kA6iA2-fields没有值" );
90
+ }
91
+ },
92
+ },
93
+ colorStart () { },
94
+ colorEnd () { },
95
+ legend (val , oldVal ) {
96
+ this .chart .setOption ({
97
+ legend: {
98
+ show: val,
99
+ },
100
+ });
101
+ },
102
+ },
103
+ mounted () {
104
+ this .initChart ();
105
+ const resizeObserver = new ResizeObserver ((entries ) => {
106
+ // 回调,重置图表大小
107
+ this .chart && this .chart .resize ();
108
+ });
109
+ resizeObserver .observe (document .getElementById (" chart_" + this .id ));
110
+ },
111
+ methods: {
112
+ getData () {
113
+ if (this .latest .systime .substr (0 , 10 ) > this .theTime ) {
114
+ this .theTime = this .latest .systime .substr (0 , 10 )
115
+ console .log (this .theTime )
116
+ }
117
+ for (var i = 0 ; i < this .fields .length ; i++ ) {
118
+ if (this .theTime == this .fields [i].systime .substr (0 , 10 )) {
119
+ if (this .hValue < this .fields [i].carbon ) {
120
+ this .hValue = this .fields [i].carbon
121
+ }
122
+ }
123
+ }
124
+ this .initChart ();
125
+ // setTimeout(() => {
126
+ // this.initChart();
127
+ // }, 1000);
128
+ },
129
+ initChart () {
130
+ console .log (" 05-kA6iA2-初始化图表开始" );
131
+ this .chart = echarts .init (document .getElementById (" chart_" + this .id ));
132
+ var option = {
133
+ title: {
134
+ text: ' '
135
+ },
136
+ legend: {
137
+ data: []
138
+ },
139
+ tooltip: {
140
+ trigger: ' axis' ,
141
+ axisPointer: {
142
+ type: ' shadow'
143
+ }
144
+ },
145
+ grid: {
146
+ containLabel: true ,
147
+ top: ' 100px' ,
148
+ left: 0 ,
149
+ bottom: ' 10px' ,
150
+ },
151
+ yAxis: {
152
+ data: [],
153
+ inverse: true ,
154
+ axisLine: { show: false },
155
+ axisTick: { show: false },
156
+ axisLabel: {
157
+ margin: 0 ,
158
+ fontSize: 14 ,
159
+ },
160
+ axisPointer: {
161
+ label: {
162
+ show: true ,
163
+ margin: 0
164
+ }
165
+ }
166
+ },
167
+ xAxis: {
168
+ splitLine: { show: false },
169
+ axisLabel: { show: false },
170
+ axisTick: { show: false },
171
+ axisLine: { show: false }
172
+ },
173
+ series: [{
174
+ itemStyle: {
175
+ color: ' #5E94FC' ,
176
+ },
177
+ name: ' 二氧化碳' ,
178
+ type: ' pictorialBar' ,
179
+ label: {
180
+ normal: {
181
+ formatter: ' {c}{title| ppm}' ,
182
+ rich: {
183
+ title: {
184
+ color: ' #5B92FF' ,
185
+ fontSize: ' 16px' ,
186
+ align: ' center'
187
+ },
188
+ },
189
+ show: true ,
190
+ position: ' left,top' ,
191
+ offset: [10 , - 30 ],
192
+ textStyle: {
193
+ fontSize: 50
194
+ },
195
+ color: ' #fff'
196
+ }
197
+ },
198
+ symbolRepeat: true ,
199
+ symbolSize: [' 7%' , ' 50%' ],
200
+ barCategoryGap: ' 0%' ,
201
+ data: [{
202
+ value: this .hValue ,
203
+ symbol: ' roundRect' ,
204
+
205
+ // symbol: 'media/bg/chart-img.png',
206
+ }]
207
+ }]
208
+ };
209
+ // this.chart.clear();
210
+ this .chart .setOption (option);
211
+ // window.addEventListener("resize", () => {
212
+ // this.chart.resize();
213
+ // });
214
+ console .log (" 06-kA6iA2-初始化图表完成" );
215
+ },
216
+ /**
217
+ * 重置图表大小
218
+ */
219
+ resizeChart () {
220
+ /* eslint-disable no-unused-expressions */
221
+ this .chart && this .chart .resize ();
222
+ },
223
+ },
224
+ };
225
+ </script >
226
+ <style scoped>
227
+ .chart-all-kA6iA2 {
228
+ width : 100% ;
229
+ height : 100% ;
230
+ /* position: absolute;
231
+ top: 50%;
232
+ left: 50%;
233
+ transform: translate(-50%, -50%); */
234
+ /* border: 1px solid rgb(41, 189, 139); */
235
+ }
236
+
237
+ .chart-top-kA6iA2 {
238
+ padding-left : 0px ;
239
+ left : 0px ;
240
+ top : 0px ;
241
+ width : 100% ;
242
+ height : 20px ;
243
+ box-sizing : border-box ;
244
+ /* border: 2px solid rgb(24, 222, 50); */
245
+ }
246
+
247
+ .chart-body-kA6iA2 {
248
+ width : 100% ;
249
+ height : calc (100% - 50px );
250
+ /* border: 2px solid rgb(201, 26, 26); */
251
+ }
252
+ </style >
0 commit comments