-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
375 lines (300 loc) · 10.3 KB
/
index.html
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<style>
body { font: 12px Arial;}
path {
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
h1 {
font-family: Arial, Helvetica, sans-serif;
}
p {
font-family: Arial, Helvetica, sans-serif;
width: 500px;
line-height: 150%;
text-align: justify;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js" charset="utf=8"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<h1> CSE 512 - A3: Interactive World Ocean Atlas </h2>
<p>This visualization shows temperature and salinity data from the <a href="https://www.nodc.noaa.gov/OC5/woa13/woa13data.html">World Ocean Atlas 2013 dataset.</a> All of the data shown are average values from 1955-2012. The background maps are generated using surface values binned at 1° resolution. Clickable locations are at 5° increments.
</p>
<!-- Add button div -->
<div id="option">
<input name="updateButton"
type="button"
value="Toggle Temperature/Salinity Map"
onclick="updateImage()" />
</div>
<script>
var margin = {top:80, right:20, bottom:40, left:80},
width = {map: 4374/5 , profile: ((4374/2)/5) - margin.left - margin.right},
height = {map:3316/5, profile: (3316/5) - margin.top - margin.bottom};
var x1 = d3.scale.linear()
.range([0, width.profile]);
var x2 = d3.scale.linear()
.range([0, width.profile]);
var y = d3.scale.linear()
.range([0,height.profile]);
var xAxis1 = d3.svg.axis()
.scale(x1)
.orient("top");
var xAxis2 = d3.svg.axis()
.scale(x2)
.orient("bottom")
.ticks(6);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line1 = d3.svg.line()
.x(function(d) {return x1(d.temperature); })
.y(function(d) {return y(d.depth); })
.defined(function(d) {return d.temperature != null; }) //line break if measure is null
.interpolate("basis");
var line2 = d3.svg.line()
.x(function(d) {return x2(d.salinity); })
.y(function(d) {return y(d.depth); })
.defined(function(d) {return d.salinity != null; }) //line break if measure is null
.interpolate("basis");
var svg1 = d3.select("body")
.append("svg")
.attr("id", "profile")
.attr("width", width.profile + margin.left + margin.right)
.attr("height", height.profile + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var svg2 = d3.select("body")
.append("svg")
.attr("id", "map")
.attr("width", width.map)
.attr("height", height.map);
var img_sal = 1; // variable: is salinity being shown in bg map? 1 -> yes
var projection = d3.geo.mercator().translate([(width.map/2)-40, (height.map/2)]).scale(127);
var xyorigin = [0,0];
svg2.selectAll("image")
.data([xyorigin]).enter()
.append("image")
.attr("xlink:href","woa_sal2.png")
.attr("width", width.map)
.attr("height", height.map)
.attr("id","bgmap")
.style("opacity", 1);
// Is this old? Do we need it?
// svg2.append("text")
// .text("WOA 2013 Salinity")
// .attr("x",200)
// .attr("y",800)
// .style("fill","blue");
function drawAxes(data, graph) {
var allTemp = []
for (var i in data.objects.temp_woa.geometries) {
for (var j in data.objects.temp_woa.geometries[i].properties) {
if (data.objects.temp_woa.geometries[i].properties[j] == -999.999) { allTemp.push(null) }
else { allTemp.push(data.objects.temp_woa.geometries[i].properties[j]) };
}
}
x1.domain(d3.extent(allTemp));
x2.domain(d3.extent(graph.map(function(d) { return d.salinity; } )) );
y.domain(d3.extent(graph.map(function(d) { return d.depth; } )) );
svg1.append("g")
.attr("class", "x axis")
.call(xAxis1)
.attr("transform", "translate(0,"+ (margin.top*(-0.5)) +")");
svg1.append("g")
.attr("class", "x axis")
.attr("id", "salinity")
.call(xAxis2)
.attr("transform", "translate(0,"+ (margin.top*(-0.5)) +")");
svg1.append("g")
.attr("class", "y axis")
.call(yAxis);
svg1.append("text")
.attr("class", "x label")
.attr("text-anchor", "middle")
.attr("font-size", "14px")
.attr("x", width.profile/2)
.attr("y", (margin.top*(-0.8)))
.style("fill", "red")
.text("Temperature (\xB0C)");
svg1.append("text")
.attr("class", "x label")
.attr("text-anchor", "middle")
.attr("font-size", "14px")
.attr("x", width.profile/2)
.attr("y", (margin.top*(-0.05)))
.style("fill", "blue")
.text("Salinity (\u2030)");
svg1.append("text")
.attr("class", "y label")
.attr("text-anchor", "middle")
.attr("font-size", "14px")
.attr("transform", "translate("+(-margin.left/1.5)+","+(height.profile/2)+")rotate(-90)")
.text("Depth (m)");
svg1.append("path")
.datum(graph)
.attr("class", "line1")
.attr("d", line1)
.style("stroke", "white");
svg1.append("path")
.datum(graph)
.attr("class", "line2")
.attr("d", line2)
.style("stroke", "white");
};
function update(data, idx) {
var temperature = data.objects.temp_woa.geometries[idx].properties;
var salinity = data.objects.salin_woa.geometries[idx].properties;
graph = [];
// push all variables into the relevant components of the graph variable
for (var i in temperature) {
graph.push({"depth" : i, "temperature" : temperature[i], "salinity": salinity[i]});
}
graph.forEach(function(d) {
// if temp value is -999.999 we assign it a value of null
if (d.temperature == -999.999) { d.temperature = null}
else d.temperature = d.temperature;
// if sal vaue is -999.999 we assign it a value of null
if (d.salinity == -999.999) { d.salinity = null }
else d.salinity = d.salinity;
// parse depth to obtain numerical values
if (d.depth == "SURFACE") { d.depth = 0 }
else {d.depth = +d.depth.split("d")[1].split("M")[0]};
});
var x2_floor = Math.floor(d3.min(graph.map(function(d) {return d.salinity; } )));
var x2_ceil = Math.ceil(d3.max(graph.map(function(d) {return d.salinity; } )));
x2.domain([x2_floor, x2_ceil]);
var axisSelect = d3.select("#profile").selectAll("g#salinity.x.axis").data([graph]);
axisSelect.attr("class", "x axis").attr("transform", "translate(0,"+ (margin.top*(-0.5)) +")");
axisSelect.call(xAxis2);
var tempSelect = d3.select("#profile").selectAll(".line1").data([graph]);
tempSelect.attr("class", "line1");
tempSelect.transition().duration(800).attr("d",line1).style("stroke", "red");
var salinSelect = d3.select("#profile").selectAll(".line2").data([graph]);
salinSelect.attr("class", "line2");
salinSelect.transition().duration(800).attr("d",line2).style("stroke", "blue");
};
d3.json("temp_salin.json", function(error, data){
if (error) return console.error(error);
update(data, 0);
drawAxes(data, graph);
features = topojson.feature(data, data.objects.temp_woa).features
var rect = svg2.selectAll("rect")
.data(features).enter()
.append("rect")
.attr("x", function(d) {
return projection(d.geometry.coordinates)[0];
})
.attr("y", function(d) {
return projection(d.geometry.coordinates)[1];
})
.attr("width", 15)
.attr("height", 15)
.attr("rx",3) // rounded corners
.attr("ry",3) // rounded corners
.style("stroke", "none")
.style("fill", "black")
.style("fill-opacity",0);
var lat = svg1.append("text")
.attr("id", "lat")
.attr("text-anchor", "left")
.attr("font-size", "14px")
.attr("font-weight", "bold")
.attr("x", width.profile/12)
.attr("y", height.profile+margin.bottom*0.5)
.text(" ");
var lon = svg1.append("text")
.attr("id", "lon")
.attr("text-anchor", "left")
.attr("font-size", "14px")
.attr("font-weight", "bold")
.attr("x", width.profile/12)
.attr("y", height.profile+margin.bottom*0.9)
.text(" ");
var lat_hover = svg2.append("text")
.attr("id", "hover1")
.attr("text-anchor", "left")
.attr("font-size", "12px")
.attr("x", width.map/2)
.attr("y", height.map/2)
.text(" ");
var lat_hover = svg2.append("text")
.attr("id", "hover1")
.attr("text-anchor", "left")
.attr("font-size", "12px")
.attr("x", width.map/2)
.attr("y", height.map/2)
.text(" ");
var lon_hover = svg2.append("text")
.attr("id", "hover2")
.attr("text-anchor", "left")
.attr("font-size", "12px")
.attr("x", width.map/2)
.attr("y", height.map/2)
.text(" ");
var active = null; // variable to record which rectangle has been clicked
rect.on({
"mouseover" : function(d, i) {
d3.select(this).style("fill", "black").style("fill-opacity",0.2);
var hoverLat = Math.round(features[i].geometry.coordinates[1]*10)/10
if (hoverLat < 0) { var NS = "S"; } else { var NS = "N"; }
var hoverLon = Math.round(features[i].geometry.coordinates[0]*10)/10
if (hoverLon < 0) { var EW = "W"; } else { var EW = "E"; }
d3.select("#map").selectAll("text#hover1")
.text(Math.abs(hoverLat) + "\xB0 " + NS )
.attr("x", projection(features[i].geometry.coordinates)[0]+15 )
.attr("y", projection(features[i].geometry.coordinates)[1]-20 );
d3.select("#map").selectAll("text#hover2")
.text(Math.abs(hoverLon) + "\xB0 " + EW )
.attr("x", projection(features[i].geometry.coordinates)[0]+15 )
.attr("y", projection(features[i].geometry.coordinates)[1]-5 );
},
"mouseout" : function(d) {
if (this != active) {
d3.select(this).style("fill-opacity", 0);
}
d3.select("#map").selectAll("text#hover1").text(" ");
d3.select("#map").selectAll("text#hover2").text(" ");
},
"click" : function(d,i) {
var coordIdx = i;
update(data, coordIdx);
d3.select(active).style("stroke", "none"); //de-select previous
d3.select(active).style("fill", "black");
d3.select(active).style("fill-opacity", 0);
active = this;
d3.select(active).style("stroke", "white");
d3.select(active).style("stroke-width", 2);
var clickLat = Math.round(features[i].geometry.coordinates[1]*10)/10
if (clickLat < 0) { var NS = "S"; } else { var NS = "N"; }
var clickLon = Math.round(features[i].geometry.coordinates[0]*10)/10
if (clickLon < 0) { var EW = "W"; } else { var EW = "E"; }
d3.select("#profile").selectAll("text#lat").text("Latitude: " + Math.abs(clickLat) + "\xB0 " + NS );
d3.select("#profile").selectAll("text#lon").text("Longitude: " + Math.abs(clickLon) + "\xB0 " + EW );
}
});
});
// Update background map based on button click (Temp/Salinity)
function updateImage() {
if (img_sal == 1) {
d3.select("#bgmap").attr("xlink:href","woa_temp2.png");
img_sal = 0;
}
else {
d3.select("#bgmap").attr("xlink:href","woa_sal2.png");
img_sal = 1;
}
}
</script>
</body>
</html>