Skip to content

Instantly share code, notes, and snippets.

@wayneminton
Forked from mbostock/.block
Last active August 29, 2015 14:07
Show Gist options
  • Save wayneminton/a12b563819b04a3555aa to your computer and use it in GitHub Desktop.
Save wayneminton/a12b563819b04a3555aa to your computer and use it in GitHub Desktop.
Pie Chart with Legend
age population
<5 2704659
5-13 4499890
14-17 2159981
18-24 3853788
25-44 14106543
45-64 8819342
≥65 612463
<!DOCTYPE html>
<meta charset="utf-8">
<style>
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
var arc = d3.svg.arc()
.outerRadius(Math.min(width, height) / 2 - 10)
.innerRadius(Math.min(width, height) / 2 - 70);
var pie = d3.layout.pie()
.value(function(d) { return d.population; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
d3.csv("data.csv", function(error, data) {
data.forEach(function(d) {
d.population = +d.population;
});
var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function(d) { return color(d.data.age); });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment