xufenglei
2018-07-13 1f5010e1328314b9f90a2f12dd2a7a1fb8924f5f
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
import {environment} from '../../../environments/environment';
import {HttpClient} from '@angular/common/http';
import {Component, OnInit} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {zip} from 'rxjs/observable/zip';
import * as echarts from 'echarts';
import * as moment from 'moment';
 
@Component({
  selector: 'app-forecasting-warning',
  templateUrl: './forecasting-warning.component.html',
})
export class ForecastingWarningComponent implements OnInit {
 
  constructor(
    private _http: _HttpClient,
    public http: HttpClient
  ) {
 
  }
  [x: string]: any;
 
  public option = {
    title: {
      text: '',
      left: 'center'
    },
    xAxis: {
      type: 'category',
      data: []
    },
    yAxis: {
      type: 'value',
      name: '单位:'
    },
    series: [{
      data: [],
      type: 'line',
      smooth: true
    }]
  };
  PM2_5 = []; PM10 = []; CO = []; NO2 = []; O3 = []; SO2 = [];
  title = '空气质量因子';
 
  ngOnInit() {
    this.initWarning();
  }
 
  changeType(event) {
    if (event === 'warning') {
      this.initWarning();
    } else {
      this.initForecasting();
    }
  }
 
  initForecasting() {
    //    this.http.get('http://sapi.7drlb.com/api/mj?cityID=1102&apiKey=forecast15days').subscribe((res: any) => {
    //      console.info(res);
    //    });
    zip(
      this.http.get('http://sapi.7drlb.com/api/mj?cityID=1102&apiKey=forecast15days'),
      this.http.get('http://sapi.7drlb.com/api/mj?cityID=1102&apiKey=forecast24hours')
    ).subscribe(
      ([daysRes, hoursRes]) => {
        console.info(daysRes);
        console.info(hoursRes);
      });
  }
 
  initWarning() {
    this.PM2_5 = []; this.PM10 = []; this.CO = []; this.NO2 = []; this.O3 = []; this.SO2 = [];
    this.option.xAxis.data = [];
    this.http.get(environment.SERVER_BASH_URL + 'demo/list').subscribe((res: any) => {
      res.forEach(data => {
        const json = JSON.parse(data.aqi_json);
        this.PM2_5.push(json.PM2_5);
        this.PM10.push(json.PM10);
        this.CO.push(json.CO);
        this.NO2.push(json.NO2);
        this.O3.push(json.O3);
        this.SO2.push(json.SO2);
        this.option.xAxis.data.push(moment(data.time).format('DD日HH时'));
      });
      const warning_PM25Chart = echarts.init(document.getElementById('warning_PM25'));
      this.option.series[0].data = this.PM2_5;
      this.option.yAxis.name = '单位:ug/m³';
      this.option.title.text = this.title + '(PM2.5)';
      warning_PM25Chart.setOption(this.option, false);
      window.onresize = warning_PM25Chart.resize;
 
      const warning_PM10Chart = echarts.init(document.getElementById('warning_PM10'));
      this.option.series[0].data = this.PM10;
      this.option.yAxis.name = '单位:ug/m³';
      this.option.title.text = this.title + '(PM10)';
      warning_PM10Chart.setOption(this.option, false);
      window.onresize = warning_PM10Chart.resize;
 
      const warning_COChart = echarts.init(document.getElementById('warning_CO'));
      this.option.series[0].data = this.CO;
      this.option.yAxis.name = '单位:mg/m³';
      this.option.title.text = this.title + '(CO)';
      warning_COChart.setOption(this.option, false);
      window.onresize = warning_COChart.resize;
 
      const warning_NO2Chart = echarts.init(document.getElementById('warning_NO2'));
      this.option.series[0].data = this.NO2;
      this.option.yAxis.name = '单位:ug/m³';
      this.option.title.text = this.title + '(NO2)';
      warning_NO2Chart.setOption(this.option, false);
      window.onresize = warning_NO2Chart.resize;
 
      const warning_O3Chart = echarts.init(document.getElementById('warning_O3'));
      this.option.series[0].data = this.O3;
      this.option.yAxis.name = '单位:ug/m³';
      this.option.title.text = this.title + '(O3)';
      warning_O3Chart.setOption(this.option, false);
      window.onresize = warning_O3Chart.resize;
 
      const warning_SO2Chart = echarts.init(document.getElementById('warning_SO2'));
      this.option.series[0].data = this.SO2;
      this.option.yAxis.name = '单位:ug/m³';
      this.option.title.text = this.title + '(SO2)';
      warning_SO2Chart.setOption(this.option, false);
      window.onresize = warning_SO2Chart.resize;
    });
  }
}