Skip to content

Instantly share code, notes, and snippets.

@aznmonkey
Last active October 30, 2016 00:43
Show Gist options
  • Save aznmonkey/06d1ee5a078e804ae213e6d0b0b6f9a5 to your computer and use it in GitHub Desktop.
Save aznmonkey/06d1ee5a078e804ae213e6d0b0b6f9a5 to your computer and use it in GitHub Desktop.
fresh block
license: mit
[
{"name":"AgglomerativeCluster","songs":[]},
{"name":"CommunityStructure","songs":["AgglomerativeCluster"]},
{"name":"test","songs":["AgglomerativeCluster"]},
{"name":"The bestest song", "songs":["test"]},
{"name":"The bomb song", "songs":["test"]}
]
<!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="//d3js.org/d3.v4.min.js"></script>
<script>
var diameter = 960,
radius = diameter / 2,
innerRadius = radius - 120;
var svg = d3.select("body").append("svg")
.attr("width", diameter)
.attr("height", diameter)
.append("g")
.attr("transform", "translate(" + radius + "," + radius + ")");
var cluster = d3.cluster()
.size([360, innerRadius])
var bundle = d3.curveBundle();
var line = d3.radialLine()
.curve(bundle)
.radius(function(d) { return d.y; })
.angle(function(d) { return d.x / 180 * Math.PI; });
var link = svg.append("g").selectAll(".link"),
node = svg.append("g").selectAll(".node");
d3.json("readme-flare-imports.json", function(error, classes) {
if (error) throw error;
var hierarchy = d3.hierarchy(packageHierarchy(classes))
var nodes = cluster(hierarchy).descendants()
var links = packageImports(nodes);
node = node
.data(nodes.filter(function(n) { return !n.data.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.data.key; })
});
// 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.data.name] = d;
});
// For each import, construct a link from the source to target node.
nodes.forEach(function(d) {
if (d.data.songs) d.data.songs.forEach(function(i) {
imports.push({source: map[d.data.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