Skip to content

Instantly share code, notes, and snippets.

@micahstubbs
Last active March 25, 2016 19:35
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 micahstubbs/ce9602e5b308f3bd53f5 to your computer and use it in GitHub Desktop.
Save micahstubbs/ce9602e5b308f3bd53f5 to your computer and use it in GitHub Desktop.
ring chart clockwise transition
license: CC0-1.0

a ring chart (or a donut chart if you are hungry) that animates into existence.

this answer http://stackoverflow.com/questions/20501067/animating-d3-donut-chart-on-load had just the tween and transition code examples needed to animate each slice in sequence.


Original README.md:

This is the code for Chapter 5, Figure 6 from D3.js in Action demonstrating how to make a donut chart using d3.layout.pie().

forked from emeeks's block: Ch. 5, Fig. 6 - D3.js in Action

forked from emeeks's block: Pie Transition Ch. 5 - D3.js in Action

<html>
<head>
<title>ring chart clockwise transition</title>
<meta charset="utf-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.16/d3.min.js" type="text/JavaScript"></script>
</head>
<style>
svg {
height: 500px;
width: 960px;
border: 0px solid gray;
}
</style>
<body>
<div id="viz">
<svg>
</svg>
</div>
</body>
<footer>
<script>
d3.json("tweets.json",function(error,data) {dataViz(data.tweets)});
function dataViz(incData) {
var background = d3.select('svg')
.append('rect')
.classed('background', true)
.attr({
'x': 0,
'y': 0,
'height': 500,
'width': 960
})
.style('fill', 'white');
nestedTweets = d3.nest()
.key(function (el) {return el.user})
.entries(incData);
var sunlight14 = [
'#193556',
'#42A5B3',
'#E3BA22',
'#F2DA57',
'#BD8F22',
'#E6842A',
'#F6B656',
'#BA5F06',
'#137B80',
'#42A5B3',
'#005D6E',
'#8E6C8A',
'#B396AD',
'#684664'
]
var colorScale = d3.scale.ordinal()
.range(sunlight14);
nestedTweets.forEach(function (el) {
el.numTweets = el.values.length
el.numFavorites = d3.sum(el.values, function (d) {return d.favorites.length});
el.numRetweets = d3.sum(el.values, function (d) {return d.retweets.length});
})
var pieChart = d3.layout.pie().sort(null);
pieChart.value(function(d) {return d.numRetweets});
newArc = d3.svg.arc();
newArc.outerRadius(200).innerRadius(170);
pieChartFavs = d3.layout.pie().sort(null);
pieChartFavs.value(function(d) {return d.numFavorites});
console.log('pieChartFavs', pieChartFavs);
d3.select("svg")
.append("g")
.attr("transform","translate(480,250)")
.selectAll("path")
.data(pieChartFavs(nestedTweets), function(d) {return d.data.key})
.enter()
.append("path")
.attr("d", newArc)
.style("fill", function(d, i) {return colorScale(i)})
.style("opacity", 0)
.style("stroke", "none")
.style("stroke-width", "2px")
.each(function(d) { this._current = d; });
function animate() {
d3.selectAll("path")
.transition()
.delay(function(d, i) { return i * 500; })
.duration(500)
.attrTween('d', function(d) {
var i = d3.interpolate(d.startAngle+0.1, d.endAngle);
return function(t) {
d.endAngle = i(t);
return newArc(d);
}
})
.style('opacity', 1)
}
animate();
d3.select('rect.background').on('click', click);
function click() {
d3.selectAll('path')
.transition()
.duration(0)
.style('opacity', 0)
.call(null, animate())
}
function arcTween(a) {
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
return newArc(i(t));
};
}
}
</script>
</footer>
</html>
{
"tweets": [
{"user": "Al", "content": "I really love seafood.", "timestamp": " Mon Dec 23 2013 21:30 GMT-0800 (PST)", "retweets": ["Raj","Pris","Roy"], "favorites": ["Sam"]},
{"user": "Al", "content": "I take that back, this doesn't taste so good.", "timestamp": "Mon Dec 23 2013 21:55 GMT-0800 (PST)", "retweets": ["Roy"], "favorites": []},
{"user": "Al", "content": "From now on, I'm only eating cheese sandwiches.", "timestamp": "Mon Dec 23 2013 22:22 GMT-0800 (PST)", "retweets": [], "favorites": ["Roy","Sam"]},
{"user": "Roy", "content": "Great workout!", "timestamp": " Mon Dec 23 2013 7:20 GMT-0800 (PST)", "retweets": [], "favorites": []},
{"user": "Roy", "content": "Spectacular oatmeal!", "timestamp": " Mon Dec 23 2013 7:23 GMT-0800 (PST)", "retweets": [], "favorites": []},
{"user": "Roy", "content": "Amazing traffic!", "timestamp": " Mon Dec 23 2013 7:47 GMT-0800 (PST)", "retweets": [], "favorites": []},
{"user": "Roy", "content": "Just got a ticket for texting and driving!", "timestamp": " Mon Dec 23 2013 8:05 GMT-0800 (PST)", "retweets": [], "favorites": ["Sam", "Sally", "Pris"]},
{"user": "Pris", "content": "Going to have some boiled eggs.", "timestamp": " Mon Dec 23 2013 18:23 GMT-0800 (PST)", "retweets": [], "favorites": ["Sally"]},
{"user": "Pris", "content": "Maybe practice some gymnastics.", "timestamp": " Mon Dec 23 2013 19:47 GMT-0800 (PST)", "retweets": [], "favorites": ["Sally"]},
{"user": "Sam", "content": "@Roy Let's get lunch", "timestamp": " Mon Dec 23 2013 11:05 GMT-0800 (PST)", "retweets": ["Pris"], "favorites": ["Sally", "Pris"]}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment