forked from hannahkfriedrich/tfdd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript3.js
More file actions
296 lines (259 loc) · 8.82 KB
/
script3.js
File metadata and controls
296 lines (259 loc) · 8.82 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
/////////////////////////////////////////////////////////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("events.csv").then(function (data) {
var columns = ['Basin','Country 1','Country 2', 'Description','Scale'];
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 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);
}
var i,
transitionTime = 1500,
spacing = 11,
margin = 20,
nodeY = 380,
nodes = events.nodes,
links = events.links,
colors = d3.scaleOrdinal(d3.schemePaired);
τ = 2 * Math.PI; // http://tauday.com/tau-manifesto
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
function mapRange(value, inMin, inMax, outMin, outMax){
var inVal = Math.min(Math.max(value, inMin), inMax);
return outMin + (outMax-outMin)*((inVal - inMin)/(inMax-inMin));
}
// Set each node's value to the sum of all incoming and outgoing link values
var nodeValMin = 100000000,
nodeValMax = 0;
for(i=0; i<nodes.length; i++){
nodes[i].value = 0;
nodes[i].displayOrder = i;
}
for(i=0; i<links.length; i++){
var link = links[i];
value = link.value;
nodes[link.source].value += link.value;
nodes[link.target].value += link.value;
}
for(i=0; i<nodes.length; i++){
nodeValMin = Math.min(nodeValMin, nodes[i].value);
nodeValMax = Math.max(nodeValMax, nodes[i].value);
}
var arcBuilder = d3.arc()
.startAngle(-τ/4)
.endAngle(τ/4);
arcBuilder.setRadii = function(d){
var arcHeight = 0.5 * Math.abs(d.x2-d.x1);
this
.innerRadius(arcHeight - d.thickness/2)
.outerRadius(arcHeight + d.thickness/2);
};
function arcTranslation(d){
return "translate(" + (d.x1 + d.x2)/2 + "," + nodeY + ")";
}
function nodeDisplayX(node){
return node.displayOrder * spacing + margin;
}
var path;
function update(){
// DATA JOIN
path = svg.selectAll("path")
.data(links);
// UPDATE
path.transition()
.duration(transitionTime)
.call(pathTween, null);
// ENTER
path.enter()
.append("path")
.attr("transform", function(d,i){
d.x1 = nodeDisplayX(nodes[d.target]);
d.x2 = nodeDisplayX(nodes[d.source]);
return arcTranslation(d);
})
.attr("fill", function(d,i) { if (d.value <= "8") {return d3.rgb(255, 0, 0)} else {return d3.rgb(0, 0, 255)}})
.attr("d", function(d,i){
d.thickness = 1 + d.value/8;
arcBuilder.setRadii(d);
return arcBuilder();
});
// DATA JOIN
var circle = svg.selectAll("circle")
.data(nodes);
// UPDATE
circle.transition()
.duration(transitionTime)
.attr("cx", function(d,i) {return nodeDisplayX(d);});
// ENTER
circle.enter()
.append("circle")
.attr("cy", nodeY)
.attr("cx", function(d,i) {return nodeDisplayX(d);})
.attr("r", function(d,i) {return Math.sqrt(d.treaties);})
.attr("fill", function(d,i) {return colors(d.group);})
.attr("stroke", function(d,i) {return d3.rgb(colors(d.group)).darker(1);})
.on('click', connectedNodes);
circle.append('title')
.text(function (d) { return d.nodeName + ': ' + d.treaties+ ' events' })
function textTransform(node){
return ("rotate(55 " + (nodeDisplayX(node) - 15) + " " + (nodeY + 12) + ")");
}
// DATA JOIN
var text = svg.selectAll("text")
.data(nodes);
// UPDATE
text.transition()
.duration(transitionTime)
.attr("x", function(d,i) {return nodeDisplayX(d) - 5;})
.attr("transform", function(d,i) { return textTransform(d); });
// ENTER
text.enter()
.append("text")
.attr("y", nodeY + 12)
.attr("x", function(d,i) {return nodeDisplayX(d) - 5;})
.attr("transform", function(d,i) { return textTransform(d); })
.attr("font-size", "10px")
.text(function(d,i) {return d.nodeName;});
/////////////////////HIGHLIGHT FUNCTION/////////////////
//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 < nodes.length; i++) {
linkedByIndex[i + "," + i] = 1;
};
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__;
circle.style("opacity", function (o) {
return neighboring(d, o) | neighboring(o, d) ? 1 : 0.1;
});
path.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
circle.style("opacity", 1);
path.style("opacity", 1);
toggle = 0;
}
}
///////////////////////////////END HIGHLIGHT FUNCTION//////////////////////////////
}
doSort(0);
update();
function pathTween(transition, dummy){
transition.attrTween("d", function(d){
var interpolateX1 = d3.interpolate(d.x1, nodeDisplayX(nodes[d.target]));
var interpolateX2 = d3.interpolate(d.x2, nodeDisplayX(nodes[d.source]));
return function(t){
d.x1 = interpolateX1(t);
d.x2 = interpolateX2(t);
arcBuilder.setRadii(d);
return arcBuilder();
};
});
transition.attrTween("transform", function(d){
var interpolateX1 = d3.interpolate(d.x1, nodeDisplayX(nodes[d.target]));
var interpolateX2 = d3.interpolate(d.x2, nodeDisplayX(nodes[d.source]));
return function(t){
d.x1 = interpolateX1(t);
d.x2 = interpolateX2(t);
return arcTranslation(d);
};
});
}
d3.select("#selectSort").on("change", function() {
doSort(this.selectedIndex);
update();
});
function doSort(sortMethod){
var nodeMap = [],
sortFunciton;
for(i=0; i<nodes.length; i++){
var node = $.extend({index:i}, nodes[i]); // Shallow copy
nodeMap.push(node);
}
if (sortMethod == 0){
// GROUP
sortFunction = function(a, b){
return b.group - a.group;
};
}
else if (sortMethod == 1){
// FREQUENCY
sortFunction = function(a, b){
return b.value - a.value;
};
}
else if(sortMethod == 2){
// ALPHABETICAL
sortFunction = function(a, b){
return a.nodeName.localeCompare(b.nodeName)
};
}
nodeMap.sort(sortFunction);
for(i=0; i<nodeMap.length; i++){
nodes[nodeMap[i].index].displayOrder = i;
}
}