Skip to content

Instantly share code, notes, and snippets.

Last active January 12, 2019 05:40
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mbostock/4341574 to your computer and use it in GitHub Desktop.
The Amazing Pie
license: gpl-3.0

So fun, right? But think twice before you use gratuitous enter animations; they can be annoying! The motion is distracting if you’re reading text elsewhere on the page. And, if you’re looking at the visualization, the animation forces you to wait. Instead, use animation to facilitate by making it easier to follow data across views, or as a form of visual explanation. For more details, read Object Constancy.

<!DOCTYPE html>
<meta charset="utf-8">
<title>Pie Chart</title>
<script src="//"></script>
var width = 960,
height = 500,
radius = Math.min(width, height) / 2 - 10;
var data = d3.range(10).map(Math.random).sort(d3.descending);
var color = d3.scale.category20();
var arc = d3.svg.arc()
var pie = d3.layout.pie();
var svg ="body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var arcs = svg.selectAll("g.arc")
.attr("class", "arc");
.attr("fill", function(d, i) { return color(i); })
.attrTween("d", tweenPie)
.delay(function(d, i) { return 2000 + i * 50; })
.attrTween("d", tweenDonut);
function tweenPie(b) {
b.innerRadius = 0;
var i = d3.interpolate({startAngle: 0, endAngle: 0}, b);
return function(t) { return arc(i(t)); };
function tweenDonut(b) {
b.innerRadius = radius * .6;
var i = d3.interpolate({innerRadius: 0}, b);
return function(t) { return arc(i(t)); };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment