-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdisplaygraph.js
More file actions
131 lines (112 loc) · 4.43 KB
/
Copy pathdisplaygraph.js
File metadata and controls
131 lines (112 loc) · 4.43 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
/* FILE: displaygraph.js
* AUTHORS: Marina Elmore, Jennifer Hu
* AHPCRC Summmer Institute 2014
-------------------------------------------------
* This Javascript function displays a force-directed graph of the dataset
* selected by the client. It also creates a textbox that details the statitics
* for each graph, given a perspective to the efficacy of the Louvain Algorithm
*/
function graphData(input, textfile) {
//Getting window size and width
var window_width = $(window).innerWidth(),
window_height = $(window).height()
//Colors
var color = d3.scale.category20();
//Setting force
var force = d3.layout.force()
.charge(-120)
.linkDistance(200)
.size([window_width, window_height]);
//Resize Function (for recentering the graph when the window is changed)
$(window).resize(function () {
width = $(window).innerWidth();
height = $(window).height();
force.size([width, height]);
force.start();
});
var svg = d3.select("body").append("svg")
.attr("width", $(window).innerWidth())
.attr("height", $(window).height());
//Created the graph from the JSON file passed into the function
d3.json(input, function(error, graph) {
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) {
return 1;
});
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", function(d) {
if(Math.sqrt(d.weight) >= 3){
return Math.sqrt(d.weight);
}else{
return 5;
}
})
.style("fill", function(d) {
return color(d.group);
})
.call(force.drag);
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("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
});
});
//Creating the textbox with statistics about each graph iteration
var g = svg.append('g').attr("transform" ,"scale(0)").attr('id', "textbox_g");
var rect = g.append('rect')
.attr('id', 'textbox')
.attr('width', 210)
.attr('height', 270)
.attr('x', 20)
.attr('y', 100)
.style('fill', 'white')
.style("fill-opacity", 0.6)
.attr('stroke', 'black')
var text = g.append('foreignObject')
.attr('id','text')
.attr('x', 45)
.attr('y', 102)
.attr('width', 225)
.attr('height', 270)
.style("background", "transparent");
g.transition().duration(500).attr("transform" ,"scale(1)");
//Ajax call to get statistics from various textfiles
$.ajax({
type: 'GET',
url: "getStatistics?me=" + textfile,
success: function() {
$.get('stats.txt', function(data){
var text_old = document.getElementById("text");
text.attr('style', 'width:200px');
text.html(data);
});
}
})
});
}
//Calling the graph data function
graphData(input, textfile);