-
Notifications
You must be signed in to change notification settings - Fork 1
/
weatherHourly.js
104 lines (87 loc) · 2.77 KB
/
weatherHourly.js
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
"use strict";
// STANDARD VARIABLES
var margin = {
top: 200,
right: 250,
bottom: 60,
left: 40
},
width = 2 * circleRadius + 300 - margin.left - margin.right,
height = 900 - margin.top - margin.bottom;
// initialize state
var cState = new state('SAN FRANCISCO',
"cloudCover",
{normalTemperature: [-10,105], heatIndex: [-10,105], windChill: [-10,105], cloudCover: [0,100], aveWindSpeed: [0,25]},
{width: width, height: height},
1);
if(window.location.hash.split("&").length != 0){
var windowState = window.location.hash.split("&");
for(var i = 0; i < windowState.length; i++){
var k = windowState[i].replace('#','').split('=');
if(k[0] == "city"){
cState.setCity(k[1].replace(/%20/g, ' '));
} else if (k[0] == "metric"){
cState.setMetric(k[1]);
} else if (k[0] == "colored"){
cState.setColor(k[1]);
}
}
}
var dataFile = 'dataMunging/' + cState.getCity() + '.csv';
// initialize data
var data = new dataObj();
// STANDARD SVG SETUP
var svg = d3.select('#weatherLines')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// initialize view
var viz = new view();
// when metric changes, update data and view
d3.select('#metric')
.on("change", function() {
cState.setMetric(this.value);
updateDataAndView();
})
d3.select('#colorSelector')
.on("change", function() {
console.log(this.value)
cState.setColor(this.value);
viz.updateColor(cState, data.getPathData(cState.getCity(), cState.getMetric()));
svg.selectAll(".monthLegend").classed("hidden", this.value != 1)
})
drawClock();
// -----------------------------
// READ IN DATA AND DRAW GRAPH
// -----------------------------
d3.csv(dataFile, function(error, inputData) {
if (error) return console.error(error);
data.updateData(inputData, cState);
cState.updateHash();
// draw lines
viz.setView(cState, data.getPathData(cState.getCity(), cState.getMetric()));
drawMonthLegend(cState);
setUpMap();
});
// ----- helper functions ----- //
// update selected city
function updateCity(city) {
cState.setCity(city);
dataFile = 'dataMunging/' + city + '.csv';
// todo: add if statement here
d3.csv(dataFile, function(error, inputData) {
if (error) return console.error(error);
// still can consolodate this
updateCities(city);
data.updateData(inputData, cState);
updateDataAndView();
}
)
}
// update data and view
function updateDataAndView() {
data.updateState(cState);
viz.updateView(cState, data.getPathData(cState.getCity(), cState.getMetric()));
}