forked from hannahkfriedrich/tfdd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript4.js
More file actions
444 lines (354 loc) · 12.5 KB
/
script4.js
File metadata and controls
444 lines (354 loc) · 12.5 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/////////////////////////////////////////////////////////Table//////////////////////////////////////////////////////////////////
var tabulate = function (data,columns) {
$('.table').removeData()
var table = d3.select('.table').append('table');
var thead = table.append('thead');
var tbody = table.append('tbody');
thead.append('tr')
.selectAll('th')
.data(columns)
.enter()
.append('th')
.text(function (d) { return d });
var rows = tbody.selectAll('tr')
.data(data)
.enter()
.append('tr');
var cells = rows.selectAll('td')
.data(function(row) {
return columns.map(function (column) {
return { column: column, value: row[column] }
})
})
.enter()
.append('td')
.text(function (d) { return d.value })
return table;
};
var tTable = function(){
d3.csv("assets/treaties.csv").then(function (data) {
var columns = ['Basin','Country','Description', 'DateSigned','IssueArea'];
tabulate(data,columns)
})
};
/////////////////////////////////////////THE CALLS////////////////////////////////////////////////////////////////////
tTable();
////////////////////////////////////////LISTEN FOR CHANGES ON THE MAP//////////////////////////////////////////////////
$(document).ready(function () {
$("#myInput").on("keyup", function () {
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function () {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
window.onload = function() {
console.log("window loaded")
var a = document.getElementById('backbutton');
}
//////////////////////////////////////////ATLAS////////////////////////////////////////////////
//var colors = ['#afb6bc','#919ba3','#53626f','#374e60','#2f3942']
var positioning = 'map';
var width = $(window).width();
var height = $(window).height() *0.80 ;
$(window).on("resize",resize());
function resize(){
$("svg")
.attr("viewBox", "(0 , 0, 100, 100)")
.attr("width", width)
.attr("height", height);
}
/*window.onresize = function() {
var width = $(window).width();
var height = $(window).height();
$("svg").attr("viewBox", "(0 , 0, " + height + ", " + width + ")");
}*/
// var width = 1360
// var height = 800
var color = d3.scaleOrdinal(d3.schemePaired);
//var color = d3.scaleOrdinal(['#4F6168','#6E7E85','#2A9D8F','#7C9EB2','#2A9D8F','#476A6F']);
var projection = d3.geoBromley()
.scale([width * 0.15])
.translate([width / 2, height / 2]);
var path = d3.geoPath().projection(projection);
var linkForce = d3.forceLink()
.id(function (d) { return d.id })
.distance(40);
var simulation = d3.forceSimulation()
.force('link', linkForce)
.force('charge', d3.forceManyBody().strength(-90))
.force('center', d3.forceCenter(width/2 , height/2 ))
.stop();
var drag = d3.drag()
.on('start', dragStarted)
.on('drag', dragged)
.on('end', dragEnded);
var files = ["assets/data.json", "assets/world.json"];
var promises = [];
files.forEach(function(url) {
promises.push(d3.json(url))
});
Promise.all(promises).then(function(results) {
var graph = results[0];
var features = results[1].features;
simulation.nodes(graph.nodes)
.on('tick', ticked);
simulation.force('link').links(graph.links);
var svg = d3.select('#treaties')
.append('svg')
.attr('width', width)
.attr('height', height);
//add encompassing group for the zoom
var g = svg.append("g")
.attr("class", "everything");
var map = g.append('g')
.attr('class', 'map')
.selectAll('path')
.data(features)
.enter().append('path')
.attr('d', path)
var links = g.append('g')
.attr('class', 'links')
.selectAll('line')
.data(graph.links)
.enter().append('line')
.attr('stroke-width', function (d) { return d.count/6 })
var nodes = g.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(graph.nodes)
.enter()
.append("g")
.call(drag)
.on('click', connectedNodes)
var circles = nodes.append("circle")
.attr("r", function(d) { if (positioning === 'map') {return d.value/5;}} )
.attr("fill", function(d) { return color(d.group); })
.on('click', searchCountry)
//Added code ;
nodes.append("text")
.text(function(d) {if (d.group === 'Basin'){
return d.id;} else {}
}).style("font-size", 2.5)
.style("font-family", 'sans-serif')
.attr('x', 1)
.attr('y', 1)
/* .text(function(d) {
return d.id;
}).style("font-size", 2)
.style("font-family", 'sans-serif')
.attr('x', 1)
.attr('y', 1)
.style("fill", function(d) {if (d.group != 'Basin'){
return "red";} else {return "green"}
})
*/
nodes.append('title')
.text(function (d) { return d.id + ': ' + d.value + ' relations'})
links.append('title')
.text(function (d) { return d.count + ' relations'})
fixed(true)
d3.select('#toggle').on('click', toggle)
function fixed(immediate) {
graph.nodes.forEach(function (d) {
var pos = projection([d.lon, d.lat])
d.x = pos[0]
d.y = pos[1]
})
var t = d3.transition()
.duration(immediate ? 0 : 600)
.ease(d3.easeElastic.period(0.5))
update(links.transition(t), nodes.transition(t))
}
function ticked() {
update(links, nodes)
}
function update(links, nodes) {
links
.attr('x1', function (d) { return d.source.x })
.attr('y1', function (d) { return d.source.y })
.attr('x2', function (d) { return d.target.x })
.attr('y2', function (d) { return d.target.y })
nodes
.attr('cx', function (d) { return d.x })
.attr('cy', function (d) { return d.y })
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
})
}
function toggle() {
if (positioning === 'map') {
positioning = 'sim'
map.attr('opacity', 0)
simulation.alpha(1).restart()
links.attr('stroke-width', function (d) { return d.count/2 })
circles.attr("r", function(d) { return d.value/2; })
nodes.append("text")
.text(function(d) {
return d.id;
}).style("font-size", 12).style("font-family", 'sans-serif')
.attr('x', 12)
.attr('y', 3)
circles.style("stroke-width", 0.5 )
zoom_actions()
}
}
function searchCountry(e){
// var layer = e.target;
var value = e.id;
$("#myTable tr").filter(function () {
$(this).toggle($(this).text().indexOf(value) > -1)
});
}
//FUNCTION TO HIGHLIGHT NODES
//Toggle stores whether the highlighting is on
var toggle = 0;
//Create an array logging what is connected to what
var linkedByIndex = {};
for (i = 0; i < graph.nodes.length; i++) {
linkedByIndex[i + "," + i] = 1;
};
graph.links.forEach(function (d) {
linkedByIndex[d.source.index + "," + d.target.index] = 1;
});
//This function looks up whether a pair are neighbours
function neighboring(a, b) {
return linkedByIndex[a.index + "," + b.index];
}
function connectedNodes() {
if (toggle == 0) {
//Reduce the opacity of all but the neighbouring nodes
d = d3.select(this).node().__data__;
nodes.style("opacity", function (o) {
return neighboring(d, o) | neighboring(o, d) ? 1 : 0.1;
});
links.style("opacity", function (o) {
return d.index==o.source.index | d.index==o.target.index ? 1 : 0.1;
});
//Reduce the op
toggle = 1;
} else {
//Put them back to opacity=1
nodes.style("opacity", 1);
links.style("opacity", 1);
toggle = 0;
}
}
//add zoom capabilities
var zoom_handler = d3.zoom()
.on("zoom", zoom_actions);
zoom_handler(svg);
//Zoom functions
function zoom_actions(){
//if (positioning === 'map') { return }
g.attr("transform", d3.event.transform)
}
function tickActions() {
links
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
nodes
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
})
}
/* LEGEND
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end").style("font-family", 'sans-serif')
.text(function(d) { return d; });*/
/* Popup window
var tip;
nodes.on("dblclick", function(d){
if (tip) tip.remove();
tip = svg.append("g")
.attr("transform", "translate(" + d.x + "," + d.y + ")");
var rect = tip.append("rect")
.style("fill", "white")
.style("stroke", "steelblue");
tip.append("text")
.text("Name: " + d.id)
.attr("dy", "1em")
.attr("x", 5);
tip.append("text")
.text("Continent: " + d.group)
.attr("dy", "2em")
.attr("x", 5);
var con = graph.links
.filter(function(d1){
return d1.source.id === d.id;
})
.map(function(d1){
return d1.target.id + " with weight " + d1.count;
})
tip.append("text")
.text("Connected to: " + con.join(","))
.attr("dy", "3em")
.attr("x", 5);
var bbox = tip.node().getBBox();
rect.attr("width", bbox.width + 5)
.attr("height", bbox.height + 5)
});*/
//function initialize(results) {}
/* SEARCH functionality
var optArray = [];
for (var i = 0; i < graph.nodes.length - 1; i++) {
optArray.push(graph.nodes[i].id);
}
optArray = optArray.sort();
$(function () {
$("#search").autocomplete({
source: optArray
});
});
function searchNode() {
//find the node
var selectedVal = document.getElementById('search').value;
var node = svg.selectAll(".node");
if (selectedVal == "none") {
nodes.style("stroke", "white").style("stroke-width", "1");
} else {
var selected = node.filter(function (d, i) {
return d.name != selectedVal;
});
selected.style("opacity", "0");
var link = svg.selectAll(".link")
link.style("opacity", "0");
d3.selectAll(".node, .link").transition()
.duration(5000)
.style("opacity", 1);
}
}*/
});
function dragStarted(d) {
if (positioning === 'map') { return }
simulation.alphaTarget(0.3).restart()
d.fx = d.x
d.fy = d.y
}
function dragged(d) {
if (positioning === 'map') { return }
d.fx = d3.event.x
d.fy = d3.event.y
}
function dragEnded(d) {
if (positioning === 'map') { return }
simulation.alphaTarget(0)
d.fx = null
d.fy = null
}
// document.body.style.zoom="90%"