In D3 3.5, you can generate rounded arcs with d3.svg.arc cornerRadius and separate adjacent arcs with d3.layout.pie padAngle. The schematic representation above shows the circles used to round the arcs’ corners.
Last active
July 9, 2016 09:38
-
-
Save mbostock/aff9e559c5c9968b7ac6 to your computer and use it in GitHub Desktop.
Arc Corners
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var data = [1, 1, 2, 3, 5, 8, 13]; | |
var width = 960, | |
height = 500, | |
outerRadius = height / 2 - 10, | |
innerRadius = outerRadius - 60, | |
cornerRadius = 12, | |
padAngle = .03; | |
var pie = d3.layout.pie() | |
.padAngle(padAngle); | |
var arcs = pie(data); | |
var arc = d3.svg.arc() | |
.innerRadius(innerRadius) | |
.outerRadius(outerRadius) | |
.cornerRadius(cornerRadius); | |
var color = d3.scale.category10(); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.append("g") | |
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); | |
svg.append("circle") | |
.style("fill", "none") | |
.style("stroke", "#555") | |
.attr("r", innerRadius); | |
svg.append("circle") | |
.style("fill", "none") | |
.style("stroke", "#555") | |
.attr("r", outerRadius); | |
svg.append("g") | |
.style("stroke", "#555") | |
.style("fill", "none") | |
.attr("class", "corner") | |
.selectAll("circle") | |
.data(d3.merge(arcs.map(function(d) { | |
return [ | |
{angle: d.startAngle + padAngle / 2, radius: outerRadius - cornerRadius, start: +1}, | |
{angle: d.endAngle - padAngle / 2, radius: outerRadius - cornerRadius, start: -1}, | |
{angle: d.endAngle - padAngle / 2 * outerRadius / innerRadius, radius: innerRadius + cornerRadius, start: -1}, | |
{angle: d.startAngle + padAngle / 2 * outerRadius / innerRadius, radius: innerRadius + cornerRadius, start: +1} | |
]; | |
}))) | |
.enter().append("circle") | |
.attr("cx", function(d) { return d.start * cornerRadius * Math.cos(d.angle) + Math.sqrt(d.radius * d.radius - cornerRadius * cornerRadius) * Math.sin(d.angle); }) | |
.attr("cy", function(d) { return d.start * cornerRadius * Math.sin(d.angle) - Math.sqrt(d.radius * d.radius - cornerRadius * cornerRadius) * Math.cos(d.angle); }) | |
.attr("r", cornerRadius); | |
svg.append("g").selectAll("path") | |
.data(arcs) | |
.enter().append("path") | |
.style("fill", function(d, i) { return color(i); }) | |
.style("fill-opacity", .25) | |
.style("stroke", "#000") | |
.style("stroke-width", "1.5px") | |
.attr("d", arc); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment