Skip to content

Instantly share code, notes, and snippets.

@cspanring
Created August 27, 2012 20:24
Show Gist options
  • Select an option

  • Save cspanring/3491969 to your computer and use it in GitHub Desktop.

Select an option

Save cspanring/3491969 to your computer and use it in GitHub Desktop.
t4ma
{"editor_editor":{"coffee":false,"vim":false,"emacs":false,"width":677,"height":447,"hide":false},"endpoint":"tributary"}
// From http://mkweb.bcgsc.ca/circos/guide/tables/
var chord = d3.layout.chord()
.padding(.05)
.sortSubgroups(d3.descending)
.matrix([
[0,0,0,40,0],
[0,0,30,30,0],
[0,30,0,0,0],
[40,30,0,0,0],
[0,0,0,0,0]
]);
var width = 600,
height = 600,
innerRadius = Math.min(width, height) * .41,
outerRadius = innerRadius * 1.1;
var fill = d3.scale.ordinal()
.domain(d3.range(4))
.range(["#0D2157", "#0868C2", "#8FB8EC", "#BEBDBD"]);
var svg = d3.select("svg")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
svg.append("g")
.selectAll("path")
.data(chord.groups)
.enter().append("path")
.style("fill", function(d) { return fill(d.index); })
.style("stroke", "#000")
.style("stroke-width", .5)
.attr("d", d3.svg.arc().innerRadius(innerRadius).outerRadius(outerRadius))
.on("mouseover", fade(.1))
.on("mouseout", fade(1))
.append("title")
.text("my label");
var ticks = svg.append("g")
.selectAll("g")
.data(chord.groups)
.enter().append("g")
.selectAll("g")
.data(groupTicks)
.enter().append("g")
.attr("transform", function(d) {
return "rotate(" + (d.angle * 180 / Math.PI - 90) + ")"
+ "translate(" + outerRadius + ",0)";
});
ticks.append("line")
.attr("x1", 1)
.attr("y1", 0)
.attr("x2", 5)
.attr("y2", 0)
.style("stroke", "#000000");
ticks.append("text")
.attr("x", 8)
.attr("dy", ".35em")
.attr("text-anchor", function(d) {
return d.angle > Math.PI ? "end" : null;
})
.attr("transform", function(d) {
return d.angle > Math.PI ? "rotate(180)translate(-16)" : null;
})
.text(function(d) { return d.label; });
svg.append("g")
.attr("class", "chord")
.selectAll("path")
.data(chord.chords)
.enter().append("path")
.style("fill", function(d) { return fill(d.source.index); })
.style("stroke", "#000000")
.style("stroke-width", .5)
.attr("d", d3.svg.chord().radius(innerRadius))
.style("opacity", 1)
.append("svg:title")
.text("my label");
/** Returns an array of tick angles and labels, given a group. */
function groupTicks(d) {
var k = (d.endAngle - d.startAngle) / d.value;
return d3.range(0, d.value, 10).map(function(v, i) {
return {
angle: v * k + d.startAngle,
label: i % 5 ? null : v / 10
};
});
}
/** Returns an event handler for fading a given chord group. */
function fade(opacity) {
return function(g, i) {
svg.selectAll("g.chord path")
.filter(function(d) {
return d.source.index != i && d.target.index != i;
})
.transition()
.style("opacity", opacity);
};
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment