Skip to content

Instantly share code, notes, and snippets.

@ctiml
Forked from mbostock/.block
Last active November 11, 2015 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctiml/5463e6917ad06fcf235f to your computer and use it in GitHub Desktop.
Save ctiml/5463e6917ad06fcf235f to your computer and use it in GitHub Desktop.
12-Pie Padding
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.label {
font-size: 18px;
fill: white;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var data = [1, 1, 2, 3, 5, 8, 13, 21];
var width = 960,
height = 500,
radius = height / 2 - 10;
var arc = d3.svg.arc()
.innerRadius(radius - 50)
.outerRadius(radius);
var pie = d3.layout.pie()
.padAngle(.02);
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.selectAll("path")
.data(pie(data))
.enter().append("path")
.style("fill", function(d, i) { return color(i); })
.attr("d", arc);
var text = svg.selectAll("text")
.data(pie(data));
text.enter().append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("class", "label")
.attr("x", function(d) { return -2 - (d.data.toString().length * 3); })
.attr("y", 6)
.text(function(d) { return d.data; });
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment