forked from miromarchi/rbv_view_graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_dependencies.js
95 lines (80 loc) · 2.89 KB
/
module_dependencies.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
/**
* @file
* D3 Module Depedencies library
*/
(function($) {
Drupal.d3.module_dependencies = 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();
// Add an attribute to each node that is a source node so that we can
// use that attribute to style them differently.
links.map(function(d) { nodes[d.target].is_source = true; });
var force = d3.layout.force()
.size([width, height])
.charge(-100)
.distance(100)
.gravity(.05);
// 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)
}
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('#257924'); })
.style("stroke-width", 2);
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.is_source) ? 9 : 9; })
.style("fill", function (d) { return (d.is_source) ? d3.hsl('#378722') : d3.hsl('#ABDC0A'); })
.style("stroke", function(d) { return (d.is_source) ? d3.hsl('#fff') : d3.hsl('#fff'); })
.style("stroke-width", function(d) { return (d.is_source) ? 3 : 3; });
node.append("svg:text")
.attr("class", "nodetext")
.attr("dx", 10)
.attr("dy", "1")
.attr('font-size', '13')
.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);