-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshamy.graph.js
169 lines (131 loc) · 5.56 KB
/
shamy.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
162
163
164
165
166
/**
* shamy.graph
*
* Displays RACI relationships in a circle graph.
* It makes use of D3 and Mike Bostock's code
* for the Hierarchical Edge Bundling example
*
* @author Rafael C Jimenez
* @version 0.9.0
*/
var shamy = shamy || {};
shamy.graph = shamy.graph || {};
shamy.graph.display = function(shamyData,targetHtmlId,size){
var diameter = size.diameter, // size.diameter: 960, 2000
radius = diameter / 2,
innerRadius = radius - size.labelRadius; // size.labelRadius: 120, 400
var cluster = d3.layout.cluster()
.size([360, innerRadius])
.sort(null)
.value(function(d) { return d.size; });
var bundle = d3.layout.bundle();
var line = d3.svg.line.radial()
.interpolate("bundle")
.tension(.85)
.radius(function(d) { return d.y; })
.angle(function(d) { return d.x / 180 * Math.PI; });
var svg = d3.select(targetHtmlId).append("svg")
.attr("width", diameter)
.attr("height", diameter)
.append("g")
.attr("transform", "translate(" + radius + "," + radius + ")");
var link = svg.append("g").selectAll(".link");
var node = svg.append("g").selectAll(".node");
d3.select(self.frameElement).style("height", diameter + "px");
var classes = shamyData;
var nodes = cluster.nodes(packageHierarchy(classes));
var links = packageImports(nodes);
var links2rep = {};
links.forEach(function(l) {
links2rep[l.source.name + "-" + l.target.name] = l.relationship;
});
var bundleData = bundle(links);
link = link
.data(bundleData)
.enter().append("path")
.each(function(d) {
d.source = d[0];
d.target = d[d.length - 1];
d.relationship = links2rep[d.source.name + "-" + d.target.name];
})
.attr("class", function(d) {
var classText = "link";
if(d.relationship != ""){ classText += " rel-" + d.relationship}
return classText;
})
.attr("d", line);
node = node
.data(nodes.filter(function(n) { return !n.children; }))
.enter().append("text")
.attr("class", "node")
.attr("dy", ".31em")
.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + (d.y + 8) + ",0)" + (d.x < 180 ? "" : "rotate(180)"); })
.style("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
.text(function(d) { return d.key; })
.on("mouseover", mouseovered)
.on("mouseout", mouseouted);
function mouseovered(d) {
node
.each(function(n) { n.target = n.source = false; });
link
.classed("link--target", function(l) { if (l.target === d) return l.source.source = true;})
.classed("link--source", function(l) { if (l.source === d) return l.target.target = true; })
.filter(function(l) { return l.target === d || l.source === d; })
.each(function() { this.parentNode.appendChild(this); });
node
.classed("node--target", function(n) { return n.target; })
.classed("node--source", function(n) { return n.source; });
}
function mouseouted(d) {
link
.classed("link--target", false)
.classed("link--source", false);
node
.classed("node--target", false)
.classed("node--source", false);
}
// Lazily construct the package hierarchy from class names.
function packageHierarchy(classes) {
var map = {};
function find(name, data) {
var node = map[name], i;
if (!node) {
node = map[name] = data || {name: name, children: []};
if (name.length) {
node.parent = find(name.substring(0, i = name.lastIndexOf(".")));
node.parent.children.push(node);
node.key = name.substring(i + 1);
}
}
return node;
}
classes.forEach(function(d) {
find(d.name, d);
});
return map[""];
}
// Return a list of imports for the given array of nodes.
function packageImports(nodes) {
var map = {},
imports = [];
// Compute a map from name to node.
nodes.forEach(function(d) {
map[d.name] = d;
});
// For each import, construct a link from the source to target node.
nodes.forEach(function(d) {
if (d.imports) d.imports.forEach(function(i) {
var rel = "";
var node = "";
if (typeof i === 'object'){
var rel = Object.keys(i)[0];
var node = i[rel];
} else {
node = i;
}
imports.push({source: map[d.name], target: map[node], relationship: rel});
});
});
return imports;
}
}