forked from miromarchi/rbv_view_graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forcedirected.js
92 lines (77 loc) · 2.56 KB
/
forcedirected.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
/**
* @file
* D3 Force Directed library
*/
(function($) {
Drupal.d3.forcedirected = function (select, settings) {
var width = (settings.config.width || 300),
height = (settings.config.height || 300),
nodes = settings.nodes,
links = settings.links,
z = d3.scale.ordinal().range(["blue", "red", "orange", "green"]),
k = Math.sqrt(nodes.length / (width * height)),
color = d3.scale.category20();
var force = d3.layout.force()
.size([width, height]);
// additional settings for the advanced force directed graphs
if (settings.gravity) {
force.gravity(settings.gravity)
}
if (settings.friction) {
force.friction(settings.friction)
}
if (settings.theta) {
force.theta(settings.theta)
}
if (settings.charge) {
force.charge(settings.charge)
}
if (settings.linkDistance) {
force.linkDistance(settings.linkDistance)
}
force.charge(-1400);
force.distance(140);
force.gravity(.5);
var svg = d3.select('#' + settings.id).append("svg")
.attr("width", width)
.attr("height", height);
var graph = svg.append("g")
.attr("class", "data");
force
.nodes(nodes)
.links(links)
.start();
var link = graph.selectAll("line.link")
.data(links)
.enter().append("line")
.attr("class", "link")
.style("stroke", function(d) { return d3.hsl("#378722"); })
.style("stroke-width", 1.5);
var node = graph.selectAll("g.node")
.data(nodes)
.enter().append("svg:g")
.attr("class", "node")
.call(force.drag);
node.append("svg:circle")
.attr("class", "node")
.attr("r", function(d) { return (d.data.d) ? d.data.d : 9; })
.style("fill", d3.hsl('#ABDC0A'))
.style("stroke", function(d) { return d3.hsl("#FFF"); })
.style("stroke-width", 3);
node.append("svg:text")
.attr("class", "nodetext")
.attr("dx", 10)
.attr("dy", ".35em")
.attr('font-size', '12')
.text(function(d) { return d.name });
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("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.attr("cy", function(d) { return d.y; });
});
}
})(jQuery);