/
weather.ts
177 lines (147 loc) · 5.52 KB
/
weather.ts
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
import {Component, EventEmitter, AfterViewChecked} from 'angular2/core';
import {NgFor} from "angular2/common";
import {Router} from 'angular2/router';
import {HTTP_PROVIDERS, Http, Response, Headers, RequestOptions} from 'angular2/http';
import {ShadedProgressBars} from './shadedProgressBars';
import {InputEffects} from './inputEffects'
@Component({
selector: 'weather',
providers: [HTTP_PROVIDERS],
directives: [ShadedProgressBars, InputEffects],
template: `
<div *ngIf="weatherData">
<div class="row">
<dir class="col-xs-3">
<h3> 今日天气</h3>
</dir>
<div class="col-xs-8 col-xs-offset-1">
<div class="row">
<div class="col-xs-4">
当前温度(摄氏度):
</div>
<div class="col-xs-8">
{{weatherData.now.tmp}}
</div>
<div class="col-xs-4">
湿度(%):
</div>
<div class="col-xs-8">
{{weatherData.now.hum}}
</div>
<div class="col-xs-4">
天气状况:
</div>
<div class="col-xs-8">
{{weatherData.now.cond.txt}}
</div>
<div class="col-xs-4">
降雨量(mm):
</div>
<div class="col-xs-8">
{{weatherData.now.pcpn}}
</div>
</div>
</div>
</div>
<dir class="col-xs-3">
<h3> 今日预报</h3>
</dir>
<svg class="col-xs-8" id="visualisation_today" width="1000" style="padding-left: 53px;" height="500"></svg>
</div>
<shadedProgressBars width="600px" value="35" [options]="options"></shadedProgressBars>
<inputEffects height="180%" width="300px" id="coolInput" [title]="_title" [inputStyle]="_inputStyle"></inputEffects>
`
})
export class Weather implements AfterViewChecked {
firstHeaders: Headers;
weatherData;
opts: RequestOptions;
hourly_time: number[] = [];
hourly_tmp: number[] = [];
hourly_pres: string[] = [];
hourly_hum: string[] = [];
hourly_svg: Object[] = [];
options = { color: "dark", move: false, rotate: false, ruler: "ruler-2" };
_title = "Password";
_inputStyle="shoko";
constructor(http: Http, _route: Router) {
this.firstHeaders = new Headers();
this.firstHeaders.append('apikey', '1b3e35e5bdb4cced72fae8c2244668a0');
this.opts = new RequestOptions();
this.opts.headers = this.firstHeaders;
// http.get('http://apis.baidu.com/heweather/weather/free?city=beijing', this.opts).subscribe(res=> {
// this.weatherData = res.json();
// this.weatherData = this.weatherData['HeWeather data service 3.0'][0];
// // console.log(this.weatherData);
// for (let v of this.weatherData.hourly_forecast) {
// let time = v.date.split(" ")[1];
// time = Number(time.split(":")[0]);
// this.hourly_time.push(time);
// let obj = new Object();
// obj = { time: parseInt(time), tmp: parseInt(v.tmp) }
// this.hourly_svg.push(obj);
// this.hourly_tmp.push(parseInt(v.tmp));
// }
// // console.log(this.hourly_time, this.hourly_tmp);
// })
}
ngAfterViewChecked() {
this.InitChart();
}
InitChart() {
// var data = [{
// "sale": "202",
// "year": "2000"
// }, {
// "sale": "215",
// "year": "2001"
// }, {
// "sale": "179",
// "year": "2002"
// }, {
// "sale": "199",
// "year": "2003"
// }, {
// "sale": "134",
// "year": "2003"
// }, {
// "sale": "176",
// "year": "2010"
// }];
var vis = d3.select("#visualisation_today"),
WIDTH = 1000,
HEIGHT = 500,
MARGINS = {
top: 20,
right: 80,
bottom: 20,
left: 50
},
xScale = d3.scale.linear().range([MARGINS.left, WIDTH - MARGINS.right]).domain([Math.min(...this.hourly_time), Math.max(...this.hourly_time)]),
yScale = d3.scale.linear().range([HEIGHT - MARGINS.top, MARGINS.bottom]).domain([Math.min(...this.hourly_tmp), Math.max(...this.hourly_tmp)]),
xAxis = d3.svg.axis()
.scale(xScale),
yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
vis.append("svg:g")
.attr("transform", "translate(0," + (HEIGHT - MARGINS.bottom) + ")")
.call(xAxis);
console.log();
vis.append("svg:g")
.call(yAxis);
var lineGen = d3.svg.line()
.x(function(d) {
return xScale(d['time']);
})
.y(function(d) {
return yScale(d['tmp']);
})
.interpolate("basis");
vis.append('svg:path')
.attr('d', lineGen(this.hourly_svg))
.attr('stroke', 'green')
.attr('stroke-width', 2)
.attr('fill', 'none');
}
}