-
Notifications
You must be signed in to change notification settings - Fork 4
/
d3.dependencyedgebundling.js
364 lines (317 loc) · 10.1 KB
/
d3.dependencyedgebundling.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
d3.chart = d3.chart || {};
/**
* Dependency edge bundling chart for d3.js
*
* Usage:
* var chart = d3.chart.dependencyedgebundling();
* d3.select('#chart_placeholder')
* .datum(data)
* .call(chart);
*/
d3.chart.dependencyedgebundling = function(options) {
var _radius;
var _diameter = 900;
var _textRadius = 100;
var _innerRadius;
var _nodeTextHyperLink;
var _txtLinkGap = 5;
var _minTextWidth = 5.5;
var _radialTextHeight = 12;
var _mouseOvered, _mouseOuted;
var _mouseOverArc, _mouseOutArc;
function findStartAngle(children) {
var min;
children.forEach(function(d) {
if (!d.children && !d.hasHiddenChildren) {
if (!min || d.x < min) min = d.x;
}
});
if (min) return min - 1;
else return 0;
}
function findEndAngle(children) {
var max;
children.forEach(function(d) {
if (!d.children && !d.hasHiddenChildren) {
if (!max || d.x > max) max = d.x;
}
});
if (max) return max + 1;
else return 0;
}
function resetDimension() {
_radius = _diameter / 2;
_innerRadius = _radius - _textRadius;
}
function autoDimension(data) {
// automatically resize the dimension based on total number of nodes
var item=0, maxLength=0, length=0, maxItem;
for (item in data){
length = data[item].name.length;
if (maxLength < length)
{
maxLength = length;
maxItem = data[item].name;
}
}
var minTextRadius = Math.ceil(maxLength * _minTextWidth);
if (_textRadius < minTextRadius) {
_textRadius = minTextRadius;
}
var minInnerRadius = Math.ceil((_radialTextHeight * data.length)/2/Math.PI);
if (minInnerRadius < 140)
{
minInnerRadius = 140;
}
var minDiameter = 2 * (_textRadius + minInnerRadius + _txtLinkGap + 2);
if (_diameter < minDiameter) {
_diameter = minDiameter;
}
}
// Lazily construct the package hierarchy
var _packageHierarchy = function (classes) {
var map = {};
function setparent(name, data) {
var node = map[name];
if (!node) {
node = map[name] = data || {name: name, children: []};
if (name.length) {
node.parent = map[""];
node.parent.children.push(node);
node.key = name;
}
}
}
setparent("", null)
classes.forEach(function(d) {
setparent(d.name, d);
});
return map[""];
}
// Return a list of depends for the given array of nodes.
var packageDepends = function (nodes) {
var map = {},
depends = [],
dependents =[];
// Compute a map from name to node.
nodes.forEach(function(d) {
map[d.name] = d;
});
// For each dependency, construct a link from the source to target node.
nodes.forEach(function(d) {
if (d.depends) d.depends.forEach(function(i) {
depends.push({source: map[d.name], target: map[i]});
});
if (d.dependents) d.dependents.forEach(function(i) {
dependents.push({source: map[i], target: map[d.name]});
});
});
var result = $.merge(depends,dependents)
return result;
}
function chart(selection) {
selection.each(function(data) {
// logic to set the size of the svg graph based on input
autoDimension(data);
resetDimension();
var root = data;
// create the layout
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(.95)
.radius(function(d) { return d.y; })
.angle(function(d) { return d.x / 180 * Math.PI; });
var svg = selection.insert("svg")
.attr("width", _diameter)
.attr("height", _diameter)
.append("svg:g")
.attr("transform", "translate(" + 720 + "," + 620 + ")");
// get all the link and node
var link = svg.append("g").selectAll(".link"),
node = svg.append("g").selectAll(".node");
var pkgNodes = _packageHierarchy(root);
var nodes = cluster.nodes(pkgNodes),
links = packageDepends(nodes);
var groupData = nodes.filter(function(d) {
if (!d.children) { return d.children; }
var groupNodes = [];
d.children.forEach(function(d) {
if (!d.children && !d.hasHiddenChildren) {
groupNodes.push(d);
}
});
return groupNodes;
});
var groupArc = d3.svg.arc()
.innerRadius(_innerRadius + 25)
.outerRadius(_innerRadius + 5)
.startAngle(function(d) {
return findStartAngle(d.children) * Math.PI / 180;
})
.endAngle(function(d) {
return findEndAngle(d.children) * Math.PI / 180;
});
svg.selectAll("g.arc")
.data(groupData)
.enter().append("svg:path")
.attr("d", groupArc)
.attr("class", "groupArc")
.on("mouseover", mouseoverarc)
.on("mouseout", mouseoutarc);
link = link
.data(bundle(links))
.enter().append("path")
.each(function(d) { d.source = d[0], d.target = d[d.length - 1]; })
.attr("class", "link")
.attr("d", line);
node = node
.data(nodes.filter(function(n) { return !n.children && !n.hasHiddenChildren; }))
.enter();
if (_nodeTextHyperLink) {
node = node.append("a")
.attr("xlink:href", _nodeTextHyperLink)
.attr("target", "_blank")
.style("text-decoration", "none")
.insert("text");
}
else {
node = node.append("text");
}
node = node.attr("class", "node")
.attr("dy", ".31em")
.attr("dx", function(d) { return d.x < 180 ? 25 : -25; })
.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + (d.y + _txtLinkGap) + ",0)" + (d.x < 180 ? "" : "rotate(180)"); })
.style("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
.text(function(d) { return d.name; })
.on("mouseover", mouseovered)
.on("mouseout", mouseouted);
//.on("click", _onNodeClick);
link.forEach(function(link) {
link.source = node[link.source] ||
(node[link.source] = {name: link.source});
link.target = node[link.target] ||
(node[link.target] = {name: link.target});
});
function mouseovered(d) {
// the following two variables are used to find and class paths that are dependant
// and depend upon the mouseovered package.
//
// targetNames keeps the opposite end of the paths value and index
// duplicateIndexes holds the index again when the opposite path is found
var targetNames = {};
var duplicateIndexes=[]
node
.each(function(n) { n.target = n.source = false; });
var links = link.filter(function(l) { return l.target === d || l.source === d; })
.classed(palette+"-link--target link--target", function(l,i) {
if (l.target === d) {
targetNames[l.source.name] = i;
return l.source.source = true;
}
})
.classed(palette+"-link--source link--source", function(l,i) {
if(l.target.name in targetNames) {
duplicateIndexes.push(targetNames[l.target.name])
d3.select(this).classed('link--target', true);
}
if (l.source === d) {
return l.target.target = true;
}
});
duplicateIndexes.forEach(function(d) {
d3.select(links[0][d]).classed('link--source', true);
links[0][d]= links[0][d];
});
links.each(function() {this.parentNode.appendChild(this); });
node
.classed(palette+"-node--target node--target", function(n) { return n.target; })
.classed(palette+"-node--source node--source", function(n) { return n.source; });
if (_mouseOvered) {
_mouseOvered(d);
}
}
function mouseouted(d) {
link
.classed(palette+"-link--target link--target", false)
.classed(palette+"-link--source link--source", false);
node
.classed(palette+"-node--target node--target", false)
.classed(palette+"-node--source node--source", false);
if (_mouseOuted) {
_mouseOuted(d);
}
}
function mouseoverarc(d) {
if (_mouseOverArc) {
_mouseOverArc(d);
}
}
function mouseoutarc(d) {
if (_mouseOutArc) {
_mouseOutArc(d);
}
}
});
}
chart.nodeTextHyperLink = function(n) {
if (!arguments.length) return n;
_nodeTextHyperLink = n;
return chart;
};
chart.packageHierarchy = function (p) {
if (!arguments.length) return p;
_packageHierarchy = p;
return chart;
};
chart.diameter = function (d) {
if (!arguments.length) return d;
_diameter = d;
return chart;
};
chart.textRadius = function (t) {
if (!arguments.length) return t;
_textRadius = t;
return chart;
};
chart.txtLinkGap = function (t) {
if (!arguments.length) return t;
_txtLinkGap = t;
return chart;
};
chart.txtWidth = function (t) {
if (!arguments.length) return t;
_minTextWidth = t;
return chart;
};
chart.nodeWidth = function (n) {
if (!arguments.length) return n;
_radialTextHeight = n;
return chart;
};
chart.mouseOvered = function (d) {
if (!arguments.length) return d;
_mouseOvered = d;
return chart;
};
chart.mouseOuted = function (d) {
if (!arguments.length) return d;
_mouseOuted = d;
return chart;
};
chart.mouseOverArc = function (d) {
if (!arguments.length) return d;
_mouseOverArc = d;
return chart;
};
chart.mouseOutArc = function (d) {
if (!arguments.length) return d;
_mouseOutArc = d;
return chart;
};
return chart;
};