Skip to content

Instantly share code, notes, and snippets.

@oscarandreu
Created February 15, 2016 09:53
Show Gist options
  • Select an option

  • Save oscarandreu/585f309aba630d883abc to your computer and use it in GitHub Desktop.

Select an option

Save oscarandreu/585f309aba630d883abc to your computer and use it in GitHub Desktop.
d3js example
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
font: 300 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
fill: #bbb;
}
.node:hover {
fill: #000;
}
.link {
stroke: steelblue;
stroke-opacity: .4;
fill: none;
pointer-events: none;
}
.node:hover,
.node--source,
.node--target {
font-weight: 700;
}
.node--source {
fill: #2ca02c;
}
.node--target {
fill: #d62728;
}
.link--source,
.link--target {
stroke-opacity: 1;
stroke-width: 2px;
}
.link--source {
stroke: #d62728;
}
.link--target {
stroke: #2ca02c;
}
</style>
<body>
<script src="htLOC6-://d3js.org/d3.v3.min.js"></script>
<script>
var classes = [
{ name: "10.10.108.185\SQLSDH,17000", size: 6000, imports: []},
{ name: "ALIAS_PDH", size: 6000, imports: []},
{ name: "AZ_POWERPUBLISH", size: 6000, imports: []},
{ name: "LOC2-CIT-OFC", size: 6000, imports: []},
{ name: "LOC2-MGM-S1", size: 6000, imports: []},
{ name: "LOC2-SQL-1\LIVE", size: 10000, imports: []},
{ name: "LOC2-SQL-2\LIVE1", size: 10000, imports: []},
{ name: "LOC2-SQL-3\LIVE", size: 10000, imports: []},
{ name: "LOC2-SQL-3\TEST1", size: 10000, imports: []},
{ name: "LOC2-SQL-3\TEST2", size: 10000, imports: []},
{ name: "LOC2-SQL-5\LIVE1", size: 10000, imports: []},
{ name: "LOC2-SQL-INTERNAL", size: 10000, imports: []},
{ name: "LOC2-TEST-SQL-1", size: 10000, imports: []},
{ name: "LOC2-TEST-SQL-2\TEST1", size: 10000, imports: []},
{ name: "LOC1-SQL-EXT-1", size: 10000, imports: ["LOC1-SQL-EXTERNAL","LOC1-SQL-INTERNAL","LOC1-TEST-SQL-1","PDN-LIVE-SQL-1\LIVE1"]},
{ name: "LOC1-SQL-EXTERNAL", size: 10000, imports: ["LOC2-CIT-OFC","LOC2-MGM-S1","LOC2-SQL-1\LIVE","LOC2-SQL-2\LIVE1","LOC2-SQL-5\LIVE1","LOC2-SQL-INTERNAL","LOC1-SQL-EXT-1","LOC1-SQL-INTERNAL","PDN-LIVE-SQL-1\LIVE1","PDN-TEST-SQL-2\TEST1","LOC4-SQL1","LOC4-SQL2","LOC5-SQL-INTERNAL"]},
{ name: "LOC1-SQL-INTERNAL", size: 10000, imports: ["LOC2-SQL-1\LIVE","LOC2-SQL-2\LIVE1","LOC2-SQL-3\LIVE","LOC2-SQL-3\TEST1","LOC2-SQL-3\TEST2","LOC2-SQL-5\LIVE1","LOC2-SQL-INTERNAL","LOC1-SQL-EXT-1","LOC1-SQL-EXTERNAL","PDN-LIVE-SQL-1\LIVE1","PDN-LIVE-SQL-2\LIVE1","PDN-TEST-SQL-1","PDN-TEST-SQL-1\TEST1","LOC5-SQL-INTERNAL"]},
{ name: "LOC1-TEST-SQL-1", size: 10000, imports: []},
{ name: "EUR-LIVE-SQL-1\PDH", size: 10000, imports: []},
{ name: "EUR-TEST-SQL-1\PDH", size: 10000, imports: []},
{ name: "LOC3-LIVE-SQL-1\LIVE1", size: 10000, imports: ["LOC1-SQL-EXTERNAL","LOC1-SQL-INTERNAL"]},
{ name: "LOC3-LIVE-SQL-2\LIVE1", size: 10000, imports: ["ALIAS_PDH","AZ_POWERPUBLISH","LOC2-SQL-2\LIVE1","LOC2-SQL-5\LIVE1","LOC2-TEST-SQL-1","LOC1-SQL-EXTERNAL","LOC1-SQL-INTERNAL","EUR-LIVE-SQL-1\PDH","LOC3-TEST-SQL-1\TEST1","LOC3-TEST-SQL-2\TEST1"]},
{ name: "LOC3-TEST-SQL-1", size: 10000, imports: ["10.10.108.185\SQLSDH,17000","ALIAS_PDH","LOC2-SQL-1\LIVE","LOC2-SQL-2\LIVE1","LOC2-SQL-5\LIVE1","LOC2-TEST-SQL-1","LOC2-TEST-SQL-2\TEST1","LOC1-SQL-EXTERNAL","LOC1-SQL-INTERNAL","LOC1-TEST-SQL-1","EUR-LIVE-SQL-1\PDH","LOC3-LIVE-SQL-2\LIVE1","LOC6-3-LIVE-SQL-1"]},
{ name: "LOC3-TEST-SQL-1\TEST1", size: 10000, imports: ["10.10.108.185\SQLSDH,17000","ALIAS_PDH","LOC2-SQL-1\LIVE","LOC2-SQL-2\LIVE1","LOC2-SQL-5\LIVE1","LOC2-TEST-SQL-1","LOC2-TEST-SQL-2\TEST1","LOC1-SQL-EXTERNAL","LOC1-SQL-INTERNAL","LOC1-TEST-SQL-1","EUR-LIVE-SQL-1\PDH","LOC3-LIVE-SQL-2\LIVE1","LOC6-3-LIVE-SQL-1"]},
{ name: "LOC3-TEST-SQL-2\TEST1", size: 10000, imports: ["LOC2-SQL-1\LIVE","LOC2-SQL-2\LIVE1","LOC2-SQL-5\LIVE1","LOC1-SQL-EXTERNAL","LOC1-SQL-INTERNAL","LOC3-SQL-1\LIVE1","LOC3-TEST-SQL-1\TEST1","LOC3-TEST-SQL-2\TEST1"]},
{ name: "LOC4-SQL1", size: 10000, imports: []},
{ name: "LOC4-SQL2", size: 10000, imports: []},
{ name: "LOC5-DEV-2K3TEST\SQL2008", size: 10000, imports: []},
{ name: "LOC5-SQL-INTERNAL", size: 10000, imports: []},
{ name: "LOC6-3-LIVE-SQL-1", size: 10000, imports: []},
]
var diameter = 960,
radius = diameter / 2,
innerRadius = radius - 120;
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("body").append("svg")
.attr("width", diameter)
.attr("height", diameter)
.append("g")
.attr("transform", "translate(" + radius + "," + radius + ")");
var link = svg.append("g").selectAll(".link"),
node = svg.append("g").selectAll(".node");
var nodes = cluster.nodes(packageHierarchy(classes)),
links = packageImports(nodes);
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; }))
.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);
}
d3.select(self.frameElement).style("height", diameter + "px");
// 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) {
imports.push({source: map[d.name], target: map[i]});
});
});
return imports;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment