Newer
Older
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
font: 300 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
fill: #bbb;
stroke: steelblue;
stroke-opacity: 0.4;
fill: none;
pointer-events: none;
}
.node:hover,
.node--source,
.node--target {
}
.link--source,
.link--target {
}
</style>
<body style="text-align: center;">
<script>@D3_SCRIPT@</script>
<script>
///////////////////////////// Parse input
var tension = @TENSION@;
var diameter = @WIDTH@,
radius = diameter / 2,
innerRadius = radius - @PADDING@;
var root = packageHierarchy(JSON.parse('@JSON_STRING@'))
cluster(root);
var imports = packageImports(root.leaves());
///////////////////////////// Create canvas
var link, node; // Required by callbacks.
var svg = d3.select("body").append("svg")
.call(d3.zoom().scaleExtent([0.1, 10]).on("zoom", function () {
svg.attr("transform",
"translate(" +
(d3.event.transform.applyX(radius)) + "," +
(d3.event.transform.applyY(radius)) + ") " +
"scale(" + d3.event.transform.k + ")")
}))
.attr("width", diameter)
.attr("height", diameter)
.append("g")
.attr("transform", "translate(" + radius + "," + radius + ")");
update();
///////////////////////////// Add user interaction
document.onkeydown = function(e) {
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
switch (e.keyCode) {
case 48: // 0
tension = 0.0
update();
break;
case 49: // 1
tension = 0.1
update();
break;
case 50: // 2
tension = 0.2
update();
break;
case 51: // 3
tension = 0.3
update();
break;
case 52: // 4
tension = 0.4
update();
break;
case 53: // 5
tension = 0.5
update();
break;
case 54: // 6
tension = 0.6
update();
break;
case 55: // 7
tension = 0.7
update();
break;
case 56: // 8
tension = 0.8
update();
break;
case 57: // 9
tension = 0.9
update();
break;
case 173: // -
tension = 1.0
update();
break;
case 87: // w
tension = Math.min(tension + 0.01, 1);
update();
break;
case 83: // s
tension = Math.max(tension - 0.01, 0);
update();
break;
}
};
///////////////////////////// Callbacks
function update() {
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
d3.selectAll("g > *").remove();
svg.append("g")
var line = d3.radialLine()
.curve(d3.curveBundle.beta(tension))
.radius(function(d) { return d.y; })
.angle(function(d) { return d.x / 180 * Math.PI; });
link = svg.append("g").selectAll(".link"),
node = svg.append("g").selectAll(".node");
link = link
.data(imports)
.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(root.leaves())
.enter().append("text")
.attr("class", "node")
.attr("dy", "0.31em")
.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + (d.y + 8) + ",0)" + (d.x < 180 ? "" : "rotate(180)"); })
.attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
.text(function(d) { return d.data.key; })
.on("mouseover", mouseovered)
.on("mouseout", mouseouted);
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; })
.raise();
node
.classed("node--target", function(n) { return n.target; })
.classed("node--source", function(n) { return n.source; });
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 d3.hierarchy(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.imports) d.data.imports.forEach(function(i) {
if (map[i]) {
imports.push(map[d.data.name].path(map[i]));
}
});
});
return imports;