Skip to content

Instantly share code, notes, and snippets.

@uttamg911
Created November 7, 2012 12:36
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 uttamg911/4031371 to your computer and use it in GitHub Desktop.
Save uttamg911/4031371 to your computer and use it in GitHub Desktop.
Artoo Donuts
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
svg {
padding: 10px 0 0 10px;
}
.arc {
stroke: #fff;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var radius = 74,
padding = 10;
var color = d3.scale.ordinal()
.range([ "#FFD700","#BEBADA","#8DD3C7" ]);
var arc = d3.svg.arc()
.outerRadius(radius)
.innerRadius(radius - 30);
var dummy = [0,0,0]
d3.json("projects.json", function(error, data) {
color.domain(d3.keys(data.project[0]).splice(1,4));
data.project.forEach(function(d) {
d.types = color.domain().map(function(name) {
return {name: name, population: +d[name]};
});
});
var legend = d3.select("body").append("svg")
.attr("class", "legend")
.attr("width", radius * 2)
.attr("height", radius * 2)
.selectAll("g")
.data(color.domain().slice().reverse())
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", 24)
.attr("y", 9)
.attr("dy", ".35em")
.text(function(d) { return d; });
var svg = d3.select("body").selectAll(".pie")
.data(data.project)
.enter().append("svg")
.attr("class", "pie")
.attr("width", radius * 2)
.attr("height", radius * 2)
.append("g")
.attr("transform", "translate(" + radius + "," + radius + ")");
svg.selectAll(".arc")
.data(pie(dummy))
.enter().append("path")
.attr("class", "arc")
.attr("d", arc)
.style("fill", function(d) { return color(d.data.name); })
.each(function(d){this._current = d;});
svg.append("text")
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d,i) { return d.name; });
});
setTimeOut(function(){
update();
}, 500);
function update() {
svg.selectAll(".arc").data(function(d, i) { return pie(d.types); })
.transition()
.duration(500)
.attrTween("d", arcTween);
}
function arcTween(a){
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
return arc(i(t));
};
}
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d.population; });
</script>
</body>
</html>
{
"project": [
{
"name": "Village1",
"enrolled": 608,
"screened": 250,
"redeemed": 100
},
{
"name": "Village2",
"enrolled": 254,
"screened": 230,
"redeemed": 10
},
{
"name": "Village3",
"enrolled": 237,
"screened": 100,
"redeemed": 90
},
{
"name": "Village4",
"enrolled": 278,
"screened": 50,
"redeemed": 30
},
{
"name": "Village5",
"enrolled": 827,
"screened": 450,
"redeemed": 300
},
{
"name": "Village6",
"enrolled": 698,
"screened": 650,
"redeemed": 600
},
{
"name": "Village7",
"enrolled": 620,
"screened": 413,
"redeemed": 240
},
{
"name": "Village8",
"enrolled": 116,
"screened": 110,
"redeemed": 92
},
{
"name": "Village9",
"enrolled": 480,
"screened": 350,
"redeemed": 281
},
{
"name": "Village10",
"enrolled": 667,
"screened": 476,
"redeemed": 331
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment