-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARCHESgraph2.js
More file actions
253 lines (221 loc) · 6.58 KB
/
ARCHESgraph2.js
File metadata and controls
253 lines (221 loc) · 6.58 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
// data stores
var graph;
var store;
// pulls JSON file containing nodes and links from local directory
var graphFile = "ARCHES_connections.json";
// sizing
var width = getWidth() - 40,
height = getHeight() - 30,
radius = 10;
// creates svg container in the viz svg element to draw menu and network visualization elements
var svg = d3.select("#viz").attr("width", width).attr("height", height);
var container = svg.append("g");
// uses mouse scroll wheel as zoom function to scale the svg container
svg.call(
d3.zoom()
.scaleExtent([.1, 4])
.on("zoom", function() { container.attr("transform", d3.event.transform); })
);
var link = container.append("g").selectAll(".link"),
node = container.append("g").selectAll(".node");
// force simulation initialization
var simulation = d3.forceSimulation()
.force("charge", d3.forceManyBody().strength(-250))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("link", d3.forceLink().id(function(d) {return d.id; }).distance(50).strength(0.2))
.on("tick", ticked);
// filtered types
typeFilterList = [];
// filter button event handlers
$(".filter-btn").on("click", function() {
var id = $(this).attr("value");
if (typeFilterList.includes(id)) {
typeFilterList.splice(typeFilterList.indexOf(id), 1)
} else {
typeFilterList.push(id);
}
var filterLabel = $(this).text();
if (filterLabel.includes("Hide")) {
$(this).text(filterLabel.replace("Hide","Show"));
}
else if (filterLabel.includes("Show")) {
$(this).text(filterLabel.replace("Show","Hide"));
}
filter();
console.log(id);
update(graph);
});
// data read and store
d3.json(graphFile)
.then(function(g) {
var nodeByID = {};
g.nodes.forEach(function(n) {
nodeByID[n.id] = n;
});
g.links.forEach(function(l) {
l.sourceGroup = nodeByID[l.source].affiliation.toString();
l.targetGroup = nodeByID[l.target].affiliation.toString();
});
graph = g;
store = $.extend(true, {}, g);
update();
}).catch(function(error){
throw error;
});
// general update pattern for updating the graph
function update() {
// UPDATE
node = node.data(graph.nodes, function(d) { return d.id;});
// EXIT
node.exit().remove();
// ENTER
var newNode = node.enter().append("circle")
.attr("class", "node")
.attr("r", radius)
.attr("fill", function(d) {
if (d.fundingLogScaled == "-1") {
return "#696969";
}
else {
return d3.color(d3.interpolatePlasma(d.fundingLogScaled)).formatHex();
}
}
)
.attr("stroke", function(d) {
if (d.affiliation == "OSF") {
return "#70362a";
}
else if (d.affiliation == "UICOMP") {
return "#001E62";
}
else if (d.affiliation == "UIUC") {
return "#E84A27";
}
else if (d.affiliation == "Other") {
return "#FFCE30";
}
else {
return "#fff";
};
})
.attr("stroke-width", "2px")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
newNode.append("title")
.text(function(d) { return "group: " + d.affiliation + "\n" + "id: " + d.id; });
// ENTER + UPDATE
node = node.merge(newNode);
// UPDATE
link = link.data(graph.links, function(d) { return d.id;});
// EXIT
link.exit().remove();
// ENTER
newLink = link.enter().append("line")
.attr("class", "link")
.attr("stroke", "#bbb")
// link width is function of project funding, scaled by 1/$20000
.attr("stroke-width", function(d) {
return d.amount/20000 + 2;
}
);
newLink.append("title")
.text(function(d) { return "source: " + d.source + "\n" + "target: " + d.target; });
// ENTER + UPDATE
link = link.merge(newLink);
// update simulation nodes, links, and alpha
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
simulation.alpha(1).alphaTarget(0).restart();
}
// drag event handlers
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
// tick event handler
function ticked() {
node.call(updateNode);
link.call(updateLink);
}
function fixna(x) {
if (isFinite(x)) return x;
return 0;
}
// redraws nodes per tick
function updateNode(node) {
node.attr("transform", function(d) {
return "translate(" + fixna(d.x) + "," + fixna(d.y) + ")";
});
}
// redraws link endpoints per tick
function updateLink(link) {
link.attr("x1", function(d) { return fixna(d.source.x); })
.attr("y1", function(d) { return fixna(d.source.y); })
.attr("x2", function(d) { return fixna(d.target.x); })
.attr("y2", function(d) { return fixna(d.target.y); });
}
// filter function
function filter() {
// add and remove nodes from data based on type filters
store.nodes.forEach(function(n) {
if (!typeFilterList.includes(n.affiliation) && n.filtered) {
n.filtered = false;
graph.nodes.push($.extend(true, {}, n));
} else if (typeFilterList.includes(n.affiliation) && !n.filtered) {
n.filtered = true;
graph.nodes.forEach(function(d, i) {
if (n.id === d.id) {
graph.nodes.splice(i, 1);
}
});
}
});
// add and remove links from data based on availability of nodes
store.links.forEach(function(l) {
if (!(typeFilterList.includes(l.sourceGroup) || typeFilterList.includes(l.targetGroup)) && l.filtered) {
l.filtered = false;
graph.links.push($.extend(true, {}, l));
} else if ((typeFilterList.includes(l.sourceGroup) || typeFilterList.includes(l.targetGroup)) && !l.filtered) {
l.filtered = true;
graph.links.forEach(function(d, i) {
if (l.id === d.id) {
graph.links.splice(i, 1);
}
});
}
});
}
function getWidth() {
return Math.max(
document.body.scrollWidth,
document.documentElement.scrollWidth,
document.body.offsetWidth,
document.documentElement.offsetWidth,
document.documentElement.clientWidth
);
}
function getHeight() {
return Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight,
document.body.offsetHeight,
document.documentElement.offsetHeight,
document.documentElement.clientHeight
);
}