2011-06-25 21:41:38 +04:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
|
|
|
<title>Chord Diagram</title>
|
2012-10-06 05:46:05 +04:00
|
|
|
<script type="text/javascript" src="../../d3.js"></script>
|
2011-06-25 21:41:38 +04:00
|
|
|
<style type="text/css">
|
|
|
|
|
|
|
|
body {
|
|
|
|
font: 10px sans-serif;
|
|
|
|
}
|
|
|
|
|
|
|
|
path.chord {
|
|
|
|
fill-opacity: .67;
|
|
|
|
}
|
|
|
|
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
2012-04-06 08:00:55 +04:00
|
|
|
var outerRadius = 960 / 2,
|
|
|
|
innerRadius = outerRadius - 120;
|
2011-06-25 21:41:38 +04:00
|
|
|
|
|
|
|
var fill = d3.scale.category20c();
|
|
|
|
|
|
|
|
var chord = d3.layout.chord()
|
|
|
|
.padding(.04)
|
|
|
|
.sortSubgroups(d3.descending)
|
|
|
|
.sortChords(d3.descending);
|
|
|
|
|
|
|
|
var arc = d3.svg.arc()
|
2012-04-06 08:00:55 +04:00
|
|
|
.innerRadius(innerRadius)
|
|
|
|
.outerRadius(innerRadius + 20);
|
2011-06-25 21:41:38 +04:00
|
|
|
|
2011-11-24 02:46:37 +04:00
|
|
|
var svg = d3.select("body").append("svg")
|
2012-04-06 08:00:55 +04:00
|
|
|
.attr("width", outerRadius * 2)
|
|
|
|
.attr("height", outerRadius * 2)
|
2011-11-24 02:46:37 +04:00
|
|
|
.append("g")
|
2012-04-06 08:00:55 +04:00
|
|
|
.attr("transform", "translate(" + outerRadius + "," + outerRadius + ")");
|
2011-06-25 21:41:38 +04:00
|
|
|
|
2011-07-04 20:41:32 +04:00
|
|
|
d3.json("../data/flare-imports.json", function(imports) {
|
2011-06-25 21:41:38 +04:00
|
|
|
var indexByName = {},
|
|
|
|
nameByIndex = {},
|
|
|
|
matrix = [],
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
// Returns the Flare package name for the given class name.
|
|
|
|
function name(name) {
|
|
|
|
return name.substring(0, name.lastIndexOf(".")).substring(6);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute a unique index for each package name.
|
|
|
|
imports.forEach(function(d) {
|
|
|
|
d = name(d.name);
|
|
|
|
if (!(d in indexByName)) {
|
|
|
|
nameByIndex[n] = d;
|
|
|
|
indexByName[d] = n++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Construct a square matrix counting package imports.
|
|
|
|
imports.forEach(function(d) {
|
|
|
|
var source = indexByName[name(d.name)],
|
|
|
|
row = matrix[source];
|
|
|
|
if (!row) {
|
|
|
|
row = matrix[source] = [];
|
|
|
|
for (var i = -1; ++i < n;) row[i] = 0;
|
|
|
|
}
|
|
|
|
d.imports.forEach(function(d) { row[indexByName[name(d)]]++; });
|
|
|
|
});
|
|
|
|
|
|
|
|
chord.matrix(matrix);
|
|
|
|
|
|
|
|
var g = svg.selectAll("g.group")
|
|
|
|
.data(chord.groups)
|
2011-11-24 02:46:37 +04:00
|
|
|
.enter().append("g")
|
2011-06-25 21:41:38 +04:00
|
|
|
.attr("class", "group");
|
|
|
|
|
2011-11-24 02:46:37 +04:00
|
|
|
g.append("path")
|
2011-06-25 21:41:38 +04:00
|
|
|
.style("fill", function(d) { return fill(d.index); })
|
|
|
|
.style("stroke", function(d) { return fill(d.index); })
|
|
|
|
.attr("d", arc);
|
|
|
|
|
2011-11-24 02:46:37 +04:00
|
|
|
g.append("text")
|
2011-06-25 21:41:38 +04:00
|
|
|
.each(function(d) { d.angle = (d.startAngle + d.endAngle) / 2; })
|
|
|
|
.attr("dy", ".35em")
|
|
|
|
.attr("text-anchor", function(d) { return d.angle > Math.PI ? "end" : null; })
|
|
|
|
.attr("transform", function(d) {
|
|
|
|
return "rotate(" + (d.angle * 180 / Math.PI - 90) + ")"
|
2012-04-06 08:00:55 +04:00
|
|
|
+ "translate(" + (innerRadius + 26) + ")"
|
2011-06-25 21:41:38 +04:00
|
|
|
+ (d.angle > Math.PI ? "rotate(180)" : "");
|
|
|
|
})
|
|
|
|
.text(function(d) { return nameByIndex[d.index]; });
|
|
|
|
|
|
|
|
svg.selectAll("path.chord")
|
|
|
|
.data(chord.chords)
|
2011-11-24 02:46:37 +04:00
|
|
|
.enter().append("path")
|
2011-06-25 21:41:38 +04:00
|
|
|
.attr("class", "chord")
|
2011-06-26 00:26:32 +04:00
|
|
|
.style("stroke", function(d) { return d3.rgb(fill(d.source.index)).darker(); })
|
2011-06-25 21:48:31 +04:00
|
|
|
.style("fill", function(d) { return fill(d.source.index); })
|
2012-04-06 08:00:55 +04:00
|
|
|
.attr("d", d3.svg.chord().radius(innerRadius));
|
2011-06-25 21:41:38 +04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|