-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.js
161 lines (131 loc) · 4.99 KB
/
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
$(document).ready(function() {
function zoomed() {
that_svg.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
var width = $("#ex1").width();
var height = $("#ex1").height();
var zoom = d3.behavior.zoom()
.scaleExtent([1, 10])
.on("zoom", zoomed);
var that_svg = d3.select("#ex1").append("svg")
.attr("width", width)
.attr("height", height)
.attr("fill", "white")
.attr("transform", "translate(" + 10 + "," + 10 + ")");
var svg = that_svg.append("g")
var force = d3.layout.force()
.gravity(.05)
.distance(300)
.charge(-100)
.size([width, height]);
function reset_name_list(){
var json_file = "graphFile.0.833.json";
var path = "results/"+$('input[name=domain]:checked').val()+"/";
var keep = [];
$("input:checkbox:checked").each(function(index){
keep.push(this.value);
});
d3.json((path+json_file), function(_json){
var json = jQuery.extend(true, {}, _json);
var n_sorted = json.nodes.sort(function(a, b) {
if(a.name < b.name) return -1;
if(a.name > b.name) return 1;
return 0;
});
$("#button_div").empty();
n_sorted.forEach(element => {
var name = element.name;
if (keep.indexOf(name) != -1){
var r = $('<input type="checkbox" value="'+name+'" checked>'+name+'</input></br>');
}else{
var r = $('<input type="checkbox" value="'+name+'">'+name+'</input></br>');
}
r.click(function() {
filter(_json);
});
$("#button_div").append(r);
});
filter(_json);
});
}
reset_name_list();
$('input[name=domain]').click(function(){
reset_name_list();
});
function filter(_json){
var json = jQuery.extend(true, {}, _json);
var json_file = "graphFile.0.833.json";
var path = "results/"+$('input[name=domain]:checked').val()+"/";
var jsonEdit = {
"nodes": [],
"links": []
};
var remove_names = [];
var remove_positions = [];
$("input:checkbox:not(:checked)").each(function(index){
remove_names.push(this.value);
});
json.nodes.forEach(function (n, i) {
if(remove_names.indexOf(n.name) != -1){
remove_positions.push(i);
}else{
jsonEdit.nodes.push(n);
}
});
json.links.forEach(function (l, i) {
var s_nr = l.source;
var t_nr = l.target;
var s_name = json.nodes[s_nr].name;
var t_name = json.nodes[t_nr].name;
if(remove_positions.includes(s_nr) || remove_positions.includes(t_nr)){
}else{
jsonEdit.nodes.forEach(function (n, i) {
if(s_name == n.name){
l.source = i;
}else if(t_name == n.name){
l.target = i;
}
});
jsonEdit.links.push(l);
}
});
createCompleteGraph(jsonEdit);
}
function createCompleteGraph(json){
$("g").remove();
var svg = that_svg.append("g")
force
.nodes(json.nodes)
.links(json.links)
.start();
var link = svg.selectAll(".link")
.data(json.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.weight) })
.style("stroke", "#aaa");
var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.style("fill", function(d) { return randomColor({luminosity: 'light', seed: d.name}); })
.attr("r", function(d) {
return Math.sqrt(d.count)});
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name })
.style("font-family", "Helvetica, sans-serif")
.style("fill", "#000000")
.style("font-size", "large");
force.on("tick", function() {
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("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
}
});