Skip to content

Commit c7199a6

Browse files
committed
Added some comments.
1 parent 35d41fb commit c7199a6

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

layouts/force-directed-layout.js

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Implements a force-directed layout, the algorithm is based on Fruchterman and Reingold and
55
the JUNG implementation.
66
7-
Needs the following graph data structure:
7+
Needs the graph data structure Graph.js:
88
https://github.com/davidpiegza/Graph-Visualization/blob/master/Graph.js
99
1010
Parameters:
@@ -20,12 +20,12 @@
2020
positionUpdated: <function>, called when the position of the node has been updated
2121
}
2222
23-
Examples
23+
Examples:
2424
25-
create:
25+
create:
2626
layout = new Layout.ForceDirected(graph, {width: 2000, height: 2000, iterations: 1000, layout: "3d"});
2727
28-
call init when graph is loaded (and for reset or when new nodes has been added to graph):
28+
call init when graph is loaded (and for reset or when new nodes has been added to the graph):
2929
layout.init();
3030
3131
call generate in a render method, returns true if it's still calculating and false if it's finished
@@ -65,6 +65,9 @@ Layout.ForceDirected = function(graph, options) {
6565
// performance test
6666
var mean_time = 0;
6767

68+
/**
69+
* Initialize parameters used by the algorithm.
70+
*/
6871
this.init = function() {
6972
this.finished = false;
7073
temperature = this.width / 10.0;
@@ -75,8 +78,13 @@ Layout.ForceDirected = function(graph, options) {
7578
repulsion_constant = this.repulsion_multiplier * forceConstant;
7679
};
7780

81+
/**
82+
* Generates the force-directed layout.
83+
*
84+
* It finishes when the number of max_iterations has been reached or when
85+
* the temperature is nearly zero.
86+
*/
7887
this.generate = function() {
79-
// TODO: stop if total force reached 0
8088
if(layout_iterations < this.max_iterations && temperature > 0.000001) {
8189
var start = new Date().getTime();
8290

@@ -101,7 +109,6 @@ Layout.ForceDirected = function(graph, options) {
101109

102110
for(var j=i+1; j < nodes_length; j++) {
103111
var node_u = graph.nodes[j];
104-
// if(node_v.id != node_u.id) {
105112
if(i != j) {
106113
node_u.layout = node_u.layout || {};
107114
node_u.layout.tmp_pos_x = node_u.layout.tmp_pos_x || node_u.position.x;
@@ -150,7 +157,7 @@ Layout.ForceDirected = function(graph, options) {
150157
}
151158
}
152159

153-
// calc attraction
160+
// calculate attraction
154161
for(var i=0; i < edges_length; i++) {
155162
var edge = graph.edges[i];
156163
var delta_x = edge.source.layout.tmp_pos_x - edge.target.layout.tmp_pos_x;
@@ -184,14 +191,13 @@ Layout.ForceDirected = function(graph, options) {
184191
}
185192
}
186193

187-
// calc positions
194+
// calculate positions
188195
for(var i=0; i < nodes_length; i++) {
189196
var node = graph.nodes[i];
190197
var delta_length = Math.max(EPSILON, Math.sqrt(node.layout.offset_x * node.layout.offset_x + node.layout.offset_y * node.layout.offset_y));
191198
if(this.layout === "3d") {
192199
var delta_length_z = Math.max(EPSILON, Math.sqrt(node.layout.offset_z * node.layout.offset_z + node.layout.offset_y * node.layout.offset_y));
193200
}
194-
// alert(delta_length_z + " " + this.layout);
195201

196202
node.layout.tmp_pos_x += (node.layout.offset_x / delta_length) * Math.min(delta_length, temperature);
197203
node.layout.tmp_pos_y += (node.layout.offset_y / delta_length) * Math.min(delta_length, temperature);
@@ -206,36 +212,17 @@ Layout.ForceDirected = function(graph, options) {
206212
if(this.layout === "3d") {
207213
node.position.z -= (node.position.z-node.layout.tmp_pos_z)/10;
208214
}
209-
210-
// var c = 200;
211-
// var updated = false;
212-
// if(node.position.x < (node.layout.tmp_pos_x - c) || node.position.x > (node.layout.tmp_pos_x + c)) {
213-
// node.position.x -= (node.position.x-node.layout.tmp_pos_x)/10;
214-
// updated = true;
215-
// }
216-
// if(node.position.y < (node.layout.tmp_pos_y - c) || node.position.y > (node.layout.tmp_pos_y + c)) {
217-
// node.position.y -= (node.position.y-node.layout.tmp_pos_y)/10;
218-
// updated = true;
219-
// }
220-
// if(this.layout === "3d") {
221-
// if(node.position.z < (node.layout.tmp_pos_z - c) || node.position.z > (node.layout.tmp_pos_z + c)) {
222-
// node.position.z -= (node.position.z-node.layout.tmp_pos_z)/10;
223-
// updated = true;
224-
// }
225-
// }
226-
215+
216+
// execute callback function if positions has been updated
227217
if(updated && typeof callback_positionUpdated === 'function') {
228218
callback_positionUpdated(node);
229219
}
230220
}
231-
var end = new Date().getTime();
232-
mean_time += end - start;
233-
// info.innerHTML = "node_force: " + parseInt(node_force) + "<br>edge_force: " + edge_force + "<br>div: " + (node_force-edge_force);
234-
235221
temperature *= (1 - (layout_iterations / this.max_iterations));
236-
// temperature -= 1/100;
237-
// console.log(temperature);
238222
layout_iterations++;
223+
224+
var end = new Date().getTime();
225+
mean_time += end - start;
239226
} else {
240227
if(!this.finished) {
241228
console.log("Average time: " + (mean_time/layout_iterations) + " ms");
@@ -245,7 +232,10 @@ Layout.ForceDirected = function(graph, options) {
245232
}
246233
return true;
247234
};
248-
235+
236+
/**
237+
* Stops the calculation by setting the current_iterations to max_iterations.
238+
*/
249239
this.stop_calculating = function() {
250240
layout_iterations = this.max_iterations;
251241
}

0 commit comments

Comments
 (0)