-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
283 lines (222 loc) · 9.39 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Aflamagn löndunarhafna</title>
<script src="d3.v4.min.js" charset="utf-8"></script>
<script src="topojson.v2.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<!-- Page elements and content go here. -->
<script>
// Width and Height of the whole visualization
var width = document.body.clientWidth, //window.innerWidth,
height = window.innerHeight-6,
centered, text, circles, ports, completeDataset, subDataset, tegundir, legends, infoText, infoText2;
var legend = { 'width': Math.max(300, Math.min(500, width/6)), 'height': Math.min(height*.8,180+(5*2*60)+40), 'scaleMax': (width > 1400) ? 60: 40, 'scaleMin': 10, "transition": 500, 'margin': 20 }
// Global control variables
var selectedAr = 0; // 0 == 2016
var selectedTegund = "Alls";
// Create SVG
var svg = d3.select("body")
.append("svg:svg")
.attr("xmlns","http://www.w3.org/2000/svg")
.attr("width", width)
.attr("height", height);
// Append empty placeholder g element to the SVG
// g will contain geometry elements
var g = svg.append("g");
// Left legends
var leftLegend = svg.append("g");
leftLegend.append("rect")
.attr("x", legend.margin)
.attr("y", legend.margin)
.attr("width", legend.width)
.attr("height", legend.height)
.attr("style", "fill: white; stroke-width:2; stroke:rgb(0,0,0); fill-opacity: 0.4;");
var infoBox = leftLegend.append("svg:foreignObject")
.attr("width", legend.width - legend.margin)
.attr("height", 180)
.attr("x", legend.margin)
.attr("y", legend.margin)
.append("xhtml:div");
var formContainer = infoBox.append("xhtml:form");
function showText(d, circle) {
text
.attr("y", circle.cy.baseVal.value + 8 + "px")
.attr("x", circle.cx.baseVal.value + 18 + "px")
.text(d.properties.nafn + " (" + addDot(d.properties.afli) + " t.)")
var bBox = text.node().getBoundingClientRect();
if(bBox.left + bBox.width > width) { // If text exits screen to right
text.attr("x", circle.cx.baseVal.value - 18 - bBox.width + "px")
}
text.style("visibility", "visible");
}
// Width and Height of the whole visualization
// Set Projection Parameters
var projection = d3.geoConicConformal()
.center([-2.5, 64.8])
.parallels([63, 68])
.rotate([18, 0])
.translate( [width/2,height/2] );
// Create GeoPath function that uses built-in D3 functionality to turn
// lat/lon coordinates into screen coordinates
var geoPath = d3.geoPath()
.projection(projection);
d3.json("island-simple-1000000-topo.json", drawIceland);
// Function that draws the legend circles in the legend box for reference
// Called by renderCircles
function doLegend(dataLength) {
var data = [];
var parts = (height < 1000) ? Math.min(3, dataLength): Math.min(5, dataLength);
var incr = tegundir[selectedTegund][selectedAr].max/parts;
var increment = Math.floor(incr/Math.pow(10,Math.ceil(Math.log(incr + 1) / Math.LN10)-1))*Math.pow(10,Math.ceil(Math.log(incr + 1) / Math.LN10)-1);
for(var i = 0; i < parts; i++)
data[i] = (i+1)*increment;
// Radius times 2 = circumference
var legendUnit = legend.scaleMax*2;
legends = leftLegend.selectAll(".legends")
.data(data, function(d,i) { return parts + "_" + i; });
var exit = legends.exit();
exit
.transition()
.duration(legend.transition)
.select("circle")
.attr("r", 0);
exit
.remove();
legends
.transition()
.duration(legend.transition)
.select("circle").attr("r", (d) => (tegundir[selectedTegund][selectedAr].scale(d)));
legends.select("text").text((d) => addDot(d)+' t.');
var enter = legends.enter();
var gl = enter.append("svg:g").classed("legends", true);
gl.append("circle")
.attr("r", (d) => (tegundir[selectedTegund][selectedAr].scale(d)))
.attr("cx", legend.margin*2 + legendUnit/2)
.attr("cy", (d,i) => ((i+1) * legendUnit) + infoBox.node().getBoundingClientRect().height)
gl.append("text")
.attr("x", legendUnit)
.attr("y", (d,i) => ((i+1) * legendUnit) + infoBox.node().getBoundingClientRect().height)
.attr("dx", legendUnit/2)
.attr("dy", (height/128))
.attr("font-family", "arial")
.attr("font-size", Math.max(18, (height/64)) + "px")
.text((d) => addDot(d)+' t.');
infoText.text(" lönduðu íslensk veiðiskip " + addDot(tegundir[selectedTegund][selectedAr].sum) + " tonnum af ");
if(tegundir[selectedTegund][selectedAr].innanlands/tegundir[selectedTegund][selectedAr].sum < 1) {
infoText2.text(", þar af var " + addDot(tegundir[selectedTegund][selectedAr].erlendis) + " tonnum landað erlendis.");
}
else {
infoText2.text("");
}
}
function processData(data) {
if(data) { // Argument only provided on inital page load
ports = svg.append("svg:g");
completeDataset = data.objects.hafnir.geometries;
tegundir = data.objects.tegundir;
var div1 = formContainer.append("xhtml:div").classed("col-auto my-1", true);
div1.append("span").text("Árið ")
var select = div1
.append("xhtml:select")
.classed("custom-select my-1 mr-sm-2", true)
.attr("style", "width: 90px;")
.on("change", function(s) { selectedAr = this.selectedIndex; processData(); });
infoText = div1.append("span")
.classed("text", true)
.text("lönduðu íslensk veiðiskip " + addDot(tegundir[selectedTegund][selectedAr].sum) + " tonnum af ");
var select2 = div1.append("xhtml:select")
.classed("custom-select mr-sm-2", true)
.attr("style", "max-width: 160px;")
.on("change", function(s) { selectedTegund = this.options[this.selectedIndex].value; processData(); });
infoText2 = div1.append("span")
.classed("text", true)
.text("");
for(var i = 0; i < 35; i++) {
select.append("option").text(2016-i)
}
var array = [];
for(var key in data.objects.tegundir) {
var c = data.objects.tegundir[key];
c.forEach(function(y){ y.scale = d3.scaleLinear().domain([y.min, y.max]).range([legend.scaleMin, legend.scaleMax]);})
array.push(key)
}
array.sort().forEach(function(k) { select2.append("option").text(k);});
if(!text)
text = svg.append("text").attr("font-size", "16pt");
}
subDataset = completeDataset.filter((d) => (d.properties.tegundir[selectedTegund])).map((e) => ({type:e.type, properties: {id: e.properties.id, hofn: e.properties.hofn, innanlands: e.properties.innanlands, nafn: e.properties.nafn, afli : e.properties.tegundir[selectedTegund][selectedAr]}, geometry: e.geometry}));
renderCircles(subDataset.filter((d) => (d.properties.innanlands === 1 && d.properties.nafn != 'Ýmsir staðir' && d.properties.afli > 0)).sort((a,b) => (b.properties.afli - a.properties.afli))); // Sorted in order for biggest circles to be drawn first and smaller on top
}
function renderCircles(data) {
doLegend(data.length); // Redo legend info
// Start D3 General Update Pattern for fishery data
circles = ports.selectAll("circle").data(data, function(d) { return d.properties.id;});
circles.exit()
.transition()
.duration(1000)
.attr("fill", "red")
.attr("r", 1)
.remove();
circles
.transition()
.duration(1000)
.attr("r", function(d) { return (d.properties.afli) ? tegundir[selectedTegund][selectedAr].scale(d.properties.afli): 0;});
circles.enter()
.append("circle")
//.on("click", clicked)
.on("mouseover", function(d) { showText(d, this); })
.on("mouseout", function(d) { text.style("visibility", "hidden");})
.attr("opacity", "0.4")
.attr("r", 1)
.attr("cx", (d) => (projection(d.geometry.coordinates)[0]))
.attr("cy", (d) => (projection(d.geometry.coordinates)[1]))
.transition()
.duration(1000)
.attr("r", function(d) { return (d.properties.afli) ? tegundir[selectedTegund][selectedAr].scale(d.properties.afli): 0;})
.attr("fill", "black")
.attr("stroke", "blue")
.attr("stroke-width", "1px")
.attr("class", "place");
}
// Helper function that returns an integer with dots between thousands
function addDot(n) {
return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}
function drawIceland(data) {
if(width >= 1366) {
var featureCollection = topojson.feature(data, data.objects["island-geo"]);
// Compute the feature bounds and centroid
// http://bl.ocks.org/pnavarrc/62047b5638d624cfa9cb
var bounds = d3.geoBounds(featureCollection);//,
//center = d3.geoCentroid(featureCollection);
var distance = d3.geoDistance(bounds[0], bounds[1]);
//scale = height / distance;
// console.log(distance)
// Update the projection scale and centroid
projection.scale(height / distance*1.4);
g.selectAll( "path" )
.data( featureCollection.features )
//.data( data.features )
.enter()
.append( "path" )
.attr( "fill", "mediumaquamarine")
.attr( "stroke", "burlywood")
.attr( "d", geoPath )
d3.json("data4.json", processData);
}
else {
alert("Þú ert með of lága upplausn á tölvuskjánum.")
}
}
function prettyPercent(p, n) {
return Math.round(parseFloat(p) * Math.pow(10, n+2))/Math.pow(10,n);
}
</script>
</body>
</html>