-
Notifications
You must be signed in to change notification settings - Fork 4
/
package_dep_graph.php
591 lines (516 loc) · 17.7 KB
/
package_dep_graph.php
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<!DOCTYPE html>
<html>
<title>VistA Package Dependency Force-Directed Graph</title>
<head>
<?php
include_once "vivian_common.php";
?>
<script src="https://d3js.org/d3-queue.v3.min.js"></script>
<!-- JQuery Buttons -->
<script>
$(function() {
fileName = window.location.href.substring(window.location.href.lastIndexOf('/')+1)
$('a[href="'+fileName+'"]').parents("#navigation_buttons li").each(function (i) {
$(this).removeClass().addClass("active");
});
});
</script>
</head>
<body>
<div class="grid" style="padding: 10px 10px 10px 10px;">
<div style="width: 20%; float: left;">
<div class="row" style="padding-left: 10px; padding-bottom: 10px;">
<div><label for ="search" title="Select Package">Search for a package:</label></div>
<div><input id="search" style="width: 300px;"></div>
</div>
<label class="btn-primary btn-sm">
<input type="checkbox" id="colorMode"> Colorblind Mode </input>
</label>
<div class="row" style="padding-left: 10px; padding-bottom: 10px; padding-top: 10px;">
<button id="selectAll">Select All</button>
<button id="reset">Clear</button>
</div>
<div class="panel panel-default">
<div class="panel-heading">Groups</div>
<div id="checkBoxDiv" class="panel-body" style="overflow:auto; overflow-x:hidden; height:400px; padding: 5px;"></div>
</div>
</div>
<div id="chart_placeholder" style="width: 80%; height: 600; float: left;"></div>
</div>
<script type="text/javascript">
///////////////////////////////////////////////////////////////////////////////
var svg = d3.select("#chart_placeholder")
.append("svg");
//Set-up the force layout
var force = d3.layout.force()
.charge(-100)
.linkDistance(400);
resize();
d3.select(window).on("resize", resize);
var vivianDataPath = FILES_URL
// Read input files
d3.queue()
.defer(d3.json, vivianDataPath + 'pkgdep.json')
.defer(d3.json, 'PackageCategories.json')
.await(plot);
function resize() {
// TODO: Where do these multipliers come from?
width = window.innerWidth * .80 - 28, height = window.innerHeight*.9 ;
svg.attr("width", width).attr("height", height);
force.size([width, height]).resume();
}
function plot(error, packages, categories) {
// Set-up the color scales
var color1 = d3.scale.category20();
var color2 = d3.scale.category20b();
function getNodeColor(d) {
if (d.group_index < 20) return color1(d.group_index);
else return color2(d.group_index - 20);
}
// Organize input data into the expected format
// First, get a mapping of package to group
var groups = [""], // First entry reserved for packages without a group
group_map = {}; // package name --> group index
var parent;
function getChildren(group) {
if (group.children) {
parent = group.name;
group.children.forEach(getChildren);
}
else {
if (groups.indexOf(parent) < 0) groups.push(parent);
var group_index = groups.indexOf(parent);
var package_name = group.name;
group_map[package_name] = group_index;
}
}
getChildren(categories);
// Get a list of groups
var sortedGroups = groups.slice(1).sort(); // Skip first, empty element
d3.select("#checkBoxDiv").selectAll("input")
.data(sortedGroups)
.enter().append("div")
.append('label')
.attr("id", function(d,i) { return 'group_label'; })
.append("input")
.attr("class", function(d,i) { return 'group_checkbox'; })
.attr("value", function(d) { return d; })
.attr("type", "checkbox");
d3.selectAll(".group_checkbox").on("change", filterGroups);
d3.selectAll("#group_label")
.data(sortedGroups)
.append("text")
.text(function(d) { return d; })
.style("color", function(d) {
// TODO: Copy + paste
var group_index = groups.indexOf(d);
if (group_index < 20) return color1(group_index);
else return color2(group_index - 20);
});
// Then, find the nodes (packages)
var nodes = [],
package_names = [],
sorted_package_names = []; // For autocomplete
packages.forEach(function(pkg, num) {
package_names.push(pkg.name);
var group_index = group_map[pkg.name];
if (group_index === undefined) { group_index = 0; }
var group_name = groups[group_index];
var node = {
name: pkg.name,
package_num: package_names.indexOf(pkg.name),
group_index: group_index,
group: group_name,
dependencies: pkg.depends,
dependents: pkg.dependents,
};
nodes.push(node);
});
// Finally, set-up the links between nodes
var links = [];
packages.forEach(function(pkg, num) {
if (pkg.depends) {
pkg.depends.forEach(function(depend_name) {
var source_index = package_names.indexOf(pkg.name);
var target_index = package_names.indexOf(depend_name);
var link = {
target: target_index,
source: source_index,
link_index: links.length
};
links.push(link);
});
}
});
// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker")
.data(links)
.enter().append("marker")
// Create a unique id for each marker
.attr("id", function(d) { return "marker" + d.link_index; })
.attr("viewBox", "0 -5 10 10")
.attr("refX", 10)
.attr("refY", 0)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("d", "M0,-5L10,0L0,5");
// Create the graph data structure out of the json data
force.nodes(nodes)
.links(links)
.start();
var link = svg.selectAll(".link");
var node = svg.selectAll(".node");
d3.selectAll(".group_checkbox").each(function(d, i) {
if (i === 0) {
cb = d3.select(this);
cb.property("checked", true);
}
});
filterGroups();
function getDependencyLinkColor() {
if (palette == "rg") return "#d62728"; // red
else return "#ff9933"; // orange
}
function getDependentLinkColor() {
if (palette == "rg") return "#2ca02c"; // green
else return "#5233FF"; // blue
}
function getBothLinkColor() { return "#A02CA0"; } // purple
function getUnselectedLinkColor() { return "#999"; } // gray
force.on("tick", function() {
// Don't allow nodes to fall off the screen
d3.selectAll("circle")
.attr("cx", function(d) {
return d.x = Math.max(d.radius, Math.min(width - d.radius, d.x));
})
.attr("cy", function(d) {
return d.y = Math.max(d.radius, Math.min(height - d.radius, d.y));
});
d3.selectAll("text")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; });
// Draw links/arrows to the edge of node
link.attr("d", function(d) {
// Total difference in x and y from source to target
diffX = d.target.x - d.source.x;
diffY = d.target.y - d.source.y;
// Length of path from center of source node to center of target node
pathLength = Math.sqrt((diffX * diffX) + (diffY * diffY));
// x and y distances from center to outside edge of target node
offsetX = (diffX * d.target.radius) / pathLength;
offsetY = (diffY * d.target.radius) / pathLength;
return "M" + d.source.x + "," + d.source.y + "L" + (d.target.x - offsetX) + "," + (d.target.y - offsetY);
});
// Make sure nodes don't overlap
node.each(collide(0.5));
});
var choices;
function filterGroups() {
// Get selected groups
choices = [];
d3.selectAll(".group_checkbox").each(function(d) {
cb = d3.select(this);
if (cb.property("checked")) {
choices.push(cb.property("value"));
}
});
newData = nodes.filter(function(d) { return choices.includes(d.group);});
newLinks = links.filter(function(d) {
return choices.includes(d.target.group) && choices.includes(d.source.group);
});
force.nodes(newData);
force.links(newLinks);
force.start();
// Create an array logging what is connected to what
linkedByIndex = {};
for (i = 0; i < newData.length; i++) {
linkedByIndex[i + "," + i] = 1;
};
newLinks.forEach(function(d) {
linkedByIndex[d.source.index + "," + d.target.index] = 1;
});
// Remove all links
link = link.data([]);
link.exit().remove();
// Add selected links
link = link.data(newLinks);
link = link.enter().append("path")
.attr("class", "link")
// Assign a unique marker to each link object
.attr("marker-end", function(d) {
return "url(#marker" + d.link_index + ")";
});
// Remove all of the nodes
node = node.data([]);
node.exit().remove();
// Add selected nodes
node = node.data(newData);
node.enter().append("g");
node.attr("class", "node")
.call(force.drag)
.on('click', toggleConnectedNodes);
// Node radius is determined by weight (number of links)
var minWeight = newData.length * 2,
maxWeight = 0;
node.each(function(d) {
minWeight = Math.min(minWeight, d.weight);
maxWeight = Math.max(maxWeight, d.weight);
});
var minRadius = 8,
maxRadius = 24;
var scale = d3.scale.linear()
.domain([minWeight, maxWeight])
.range([minRadius,maxRadius]);
node.append("circle")
.attr("r", function(d) {
d.radius = scale(d.weight);
return d.radius;
})
.style("fill", function(d) { return getNodeColor(d) });
// Set-up labels
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.attr("class", function(d) { return 'node-label'; })
.text(function(d) { return d.name })
.style("stroke", "black")
.style("opacity", 0);
var selected_packages = packages.filter(function(d) {
var group_index = group_map[d.name];
if (group_index === undefined) { group_index = 0; }
var group_name = groups[group_index];
return choices.includes(group_name);
});
var selected_package_names = [];
sorted_package_names.length = 0; // clear array
selected_packages.forEach(function(pkg) {
selected_package_names.push(pkg.name);
sorted_package_names.push(pkg.name);
});
sorted_package_names.sort();
var package_map = {};
selected_packages.forEach(function(pkg, num) {
//if (pkg.name === d.name) {
var depends = [];
if (pkg.depends) {
depends = pkg.depends.filter(function(d) {
return selected_package_names.includes(d);
});
}
var dependents = [];
if (pkg.dependents) {
dependents = pkg.dependents.filter(function(d){
return selected_package_names.includes(d);
});
}
var both = []; // Get a list of links that are dependencies AND dependents
if (depends && dependents) {
both = depends.filter(function(n) {
return dependents.indexOf(n) != -1
});
}
var both_count = both.length,
dependencies_count = 0,
dependents_count = 0;
if (depends) dependencies_count = depends.length - both_count;
if (dependents) dependents_count = dependents.length - both_count;
var p = {
both_count: both_count,
dependencies_count: dependencies_count,
dependents_count: dependents_count
};
package_map[pkg.name] = p;
});
// Set-up tooltips
node.append("title")
.text(function(d) {
var text = "Name: " + d.name;
if (d.group) {
text += "\nGroup: " + d.group;
}
if (package_map[d.name].dependencies_count > 0) {
text += "\nDependencies: " + package_map[d.name].dependencies_count;
}
if (package_map[d.name].both_count> 0) {
text += "\nBoth: " + package_map[d.name].both_count;
}
if (package_map[d.name].dependents_count> 0) {
text += "\nDependents: " + package_map[d.name].dependents_count;
}
return text;
});
clearSearch();
}
var padding = 1; // separation between circles;
function collide(alpha) {
var quadtree = d3.geom.quadtree(nodes);
return function(d) {
var rb = 2*d.radius + padding,
nx1 = d.x - rb,
nx2 = d.x + rb,
ny1 = d.y - rb,
ny2 = d.y + rb;
quadtree.visit(function(quad, x1, y1, x2, y2) {
if (quad.point && (quad.point !== d)) {
var x = d.x - quad.point.x,
y = d.y - quad.point.y,
l = Math.sqrt(x * x + y * y);
if (l < rb) {
l = (l - rb) / l * alpha;
d.x -= x *= l;
d.y -= y *= l;
quad.point.x += x;
quad.point.y += y;
}
}
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
});
};
}
var linkedByIndex = {};
function neighboring(a, b) {
return linkedByIndex[a.index + "," + b.index];
}
function toggleConnectedNodes() {
if (d3.event.defaultPrevented) return; // ignore drag
d = d3.select(this).node().__data__;
toggleNode(d);
}
var selected_node;
function toggleNode(d) {
if (selected_node != d) {
// Clear search box
document.getElementById("search").value = '';
// Update selected node
selected_node = d;
updateDisplay();
} else {
clearSearch();
}
}
function updateDisplay() {
// Update node and link opacity and colors
d3.selectAll("circle")
// Reduce the opacity of all but the neighboring nodes
.style("opacity", function(d) {
return neighboring(selected_node, d) | neighboring(d, selected_node) ? 1 : 0.1;
})
// Toggle the color of the selected node
.style("fill", function(d) {
if (selected_node === d) return "#fff";
else return getNodeColor(d);
})
.style("stroke", function(d) {
if (selected_node === d) return getNodeColor(d)
else return "#fff"; // white
});
// Show label for selected node
d3.selectAll(".node-label")
.style("opacity", function(d) {
return selected_node === d ? 1 : 0;
});
link
.style("opacity", function(d) {
return isADependency(d) | isADependent(d) ? 1 : 0.1;
})
.style("stroke", function(d) {
var is_a_dependency = isADependency(d);
var is_a_dependent = isADependent(d);
if (is_a_dependency) {
var dependents = nodes[selected_node.package_num].dependents;
is_a_dependent = (dependents && dependents.indexOf(d.target.name) >= 0);
} else if (is_a_dependent) {
var dependencies = nodes[selected_node.package_num].dependencies;
is_a_dependency = (dependencies && dependencies.indexOf(d.source.name) >= 0);
}
// Update link and arrow (marker) color
if (is_a_dependency && is_a_dependent) {
d3.selectAll("#marker" + d.link_index +" path")
.style("fill", getBothLinkColor());
return getBothLinkColor();
} else if (is_a_dependency) {
d3.selectAll("#marker" + d.link_index +" path")
.style("fill", getDependencyLinkColor())
return getDependencyLinkColor();
} else if (is_a_dependent) {
d3.selectAll("#marker" + d.link_index +" path")
.style("fill", getDependentLinkColor());
return getDependentLinkColor();
} else { // Not connected to selected node, link is gray
d3.selectAll("#marker" + d.link_index +" path")
.style("fill", getUnselectedLinkColor())
return getUnselectedLinkColor();
}
})
.style("stroke-width", function(d) {
return isADependency(d) | isADependent(d) ? 2 : 1;
});
}
// Return true if node is a dependency of the selected node
// i.e. [selected node] --> [node]
function isADependency(node){
return selected_node.index == node.source.index;
}
// Return true if node is a dependent of the selected node
// i.e. [selected node] <-- [node]
function isADependent(node) {
return selected_node.index == node.target.index;
}
function searchNode(eve, ui) {
var selected_value = ui.item.value;
var selected_node;
node.each(function (d, i) {
// Assumes nodes have unique names
if (d.name == selected_value) selected_node = d;
});
toggleNode(selected_node);
}
function clearSearch() {
// Clear search box
document.getElementById("search").value = '';
// Update selected node
selected_node = null;
// Return nodes and links to original opacity and colors
d3.selectAll("circle")
.style("opacity", 1)
.style("fill", function(d) { return getNodeColor(d); })
.style("stroke", function(d) { return "#fff"; });
d3.selectAll(".node-label")
.style("opacity", 0);
link
.style("opacity", 0.6)
.style("stroke", function(d) {
d3.selectAll("#marker" + d.link_index +" path")
.style("fill", getUnselectedLinkColor())
return getUnselectedLinkColor(); })
.style("stroke-width", 1);
}
function selectAll() {
d3.selectAll(".group_checkbox").property('checked', true);
filterGroups();
}
function reset(){
d3.selectAll(".group_checkbox").property('checked', false);
filterGroups();
}
var palette = "rg";
function swapColorScheme() {
palette = (palette == "rg") ? "cb" : "rg";
if (selected_node) updateDisplay();
}
$(function () {
$("#search").autocomplete({
source: sorted_package_names,
select: searchNode
});
});
document.getElementById("search").onfocus = function(){ clearSearch(); };
document.getElementById("colorMode").onclick = function(){ swapColorScheme(); };
document.getElementById("selectAll").onclick = function() { selectAll(); };
document.getElementById("reset").onclick = function() { reset(); };
}
</script>
</body>
</html>