Skip to content

Instantly share code, notes, and snippets.

@dwtkns
Created February 21, 2013 13:14
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 dwtkns/5004662 to your computer and use it in GitHub Desktop.
Save dwtkns/5004662 to your computer and use it in GitHub Desktop.
{
"libraries": [
"d3"
],
"mode": "javascript",
"layout": "fullscreen mode (vertical)",
"resolution": "reset"
}
body {
font: 10px sans-serif;
}
.arc path {
stroke: #fff;
}
/*.arc path:hover {
stroke: black;
stroke-width: 2px;
}*/
var raw_data = [
{ age: '<5', population: 2704659 },
{ age: '5-13', population: 4499890 },
{ age: '14-17', population: 2159981 },
{ age: '18-24', population: 3853788 },
{ age: '25-44', population: 14106543 },
{ age: '45-64', population: 8819342 },
{ age: '≥65', population: 61246 }
]
var width = 550,
height = 500,
radius = Math.min(width, height) / 2;
var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#D0753C", "#ff8c00"]);
var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(radius - 70);
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d.population; });
var svg = d3.select("svg")
.attr("width", width).attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var pie_data = pie(raw_data);
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);
});
g.append("text")
.attr("transform", function(d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d) {
return d.data.age;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment