Skip to content

Instantly share code, notes, and snippets.

@steve-kasica
Forked from mbostock/.block
Last active August 29, 2015 14:27
Show Gist options
  • Save steve-kasica/13847b8657326aab02d8 to your computer and use it in GitHub Desktop.
Save steve-kasica/13847b8657326aab02d8 to your computer and use it in GitHub Desktop.
The Pac-Man Pie Chart

The classic Pac-Man pie chart made with D3.js.

label amount Pac-Man
Does not resemble Pac-Man 1
Resembles Pac-Man 5
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.arc path {
stroke: #000;
stroke-width: 5;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var width = 960,
height = 500,
radius = Math.min(width, height) / 2;
var color = d3.scale.ordinal()
.range(["#973367", "#ffee00",]);
var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(0);
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d["amount Pac-Man"]; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ") rotate(60 0 0)");
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.label); });
g.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ") rotate(-60 0 0)"; })
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d) { return d.data.label; });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment