-
Notifications
You must be signed in to change notification settings - Fork 0
/
msp_graph.js
244 lines (169 loc) · 5.22 KB
/
msp_graph.js
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
var node=[]
var link=[]
var graph_active
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function shuffle(array) {
let counter = array.length;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
let index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
let temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
function build_graph(data,names)
{
console.log("Building graph..")
w=1200
h=600
radius=6
dd = document.getElementById('graph')
if(dd!=null)
dd.innerHTML = "";
var div = document.createElement("div");
div.style.background = "lightgrey";
document.getElementById("graph").appendChild(div);
svg = d3.select("#graph").append("svg")
.attr("width", w)
.attr("height", h)
.append("g")
//build the data structures for the graph
var names2=[]
for(j=0;j<names.length;j++)
{
names2[j]=names[j].value
}
console.log("n2")
console.log(names2)
var nodes=[]
var nodes2=[]
var links=[]
if(names2.length==0)
{
for(i=0;i<data.length;i++)
{
nodes2.push({"id":data[i].name})
links.push({"source":data[i].name, "target":data[i].msp1, "value":3})
links.push({"source":data[i].name, "target":data[i].msp2, "value":2})
links.push({"source":data[i].name, "target":data[i].msp3, "value":1})
}
}
else
{
for(i=0;i<names2.length;i++)
{
data2 = data.filter(function(d){return d.name == names2[i];})
if(nodes.indexOf(data2[0].name)==-1)
nodes.push(data2[0].name)
if(nodes.indexOf(data2[0].msp1)==-1)
nodes.push(data2[0].msp1)
if(nodes.indexOf(data2[0].msp2)==-1)
nodes.push(data2[0].msp2)
if(nodes.indexOf(data2[0].msp3)==-1)
nodes.push(data2[0].msp3)
links.push({"source":data2[0].name, "target":data2[0].msp1, "value":3})
links.push({"source":data2[0].name, "target":data2[0].msp2, "value":3})
links.push({"source":data2[0].name, "target":data2[0].msp3, "value":3})
}
for(j=0;j<nodes.length;j++)
nodes2.push({"id":nodes[j]})
}
console.log("Nodi:")
console.log(nodes2)
console.log("Link:")
console.log(links)
nodes = shuffle(nodes2)
///----------------------------------------------------------------------------------------------------
var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation()
//.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("link", d3.forceLink().id(function(d) { return d.id; }).distance(20))
.force("charge", d3.forceManyBody().strength(-4))
.force("center", d3.forceCenter(w / 2, h / 2))
//.force('collision', d3.forceCollide().radius(5))
link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(links)
.enter().append("line")
.attr("stroke-width", function(d) { return Math.sqrt(d.value); });
node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(nodes2)
.enter().append("circle")
.attr("r", radius)
.attr("id",function(d){return (d.id).replace(" ","-")})
.attr("fill", function(d) { return color(getRandomColor()) })
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended))
.on("click", function(){
y =document.getElementById('anno').value
current_year = y
setURL(this.id.replace("-"," "))
set_tags(y)
update()
})
.on("mouseover", function(){d3.select("#"+this.id).transition().duration(300).attr("r",10)})
.on("mouseout", function(){d3.select("#"+this.id).transition().duration(300).attr("r",6)})
node.append("title")
.text(function(d) { return d.id; });
simulation
.nodes(nodes)
.on("tick", ticked);
simulation.force("link")
.links(links);
function ticked() {
link
.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; });
node
.attr("cx", function(d) { return d.x = Math.max(radius, Math.min(w - radius, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(radius, Math.min(h - radius, d.y)); });
}
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.1);
d.fx = null;
d.fy = null;
}
}
var prev_node=""
function highlight(name)
{
//x = node.selectAll("circle")
//ripristinate previous node str
if(prev_node!="")
d3.select("#"+prev_node).transition().duration(300).attr("r", "6");
//update the current node, if any
if(name!="")
{
prev_node = name.replace(" ","-")
d3.select("#"+prev_node).transition().duration(300).attr("r", "10");
}
}