Skip to content

Instantly share code, notes, and snippets.

@raidan00
Last active March 1, 2020 14:21
Show Gist options
  • Save raidan00/ea0d0e608c4d40f95296688a024887aa to your computer and use it in GitHub Desktop.
Save raidan00/ea0d0e608c4d40f95296688a024887aa to your computer and use it in GitHub Desktop.
d3 v4 pie animation
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
function getRandomData (){
var data = [ [10, 20, 100], [100, 30, 10, 5], [500, 600, 700, 900, 100], [1, 2] ];
return data[Math.floor(Math.random()*data.length)];
}
var width = 960,
height = 500,
radius = Math.min(width, height) / 2;
var colors = d3.scaleOrdinal().range(d3.schemeCategory20);
var arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(170);
var pie = d3.pie().sort(null).value(function(d) { return d; });
var g = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
reDraw();
function reDraw (){
g.selectAll('path').remove();
g.selectAll("path")
.data(pie(getRandomData()))
.enter()
.append("path")
.attr("d", arc)
.style("fill", function(d) { return colors(d.data); })
.transition()
.duration(1000)
.attrTween("d", unfold)
.transition()
.delay(3000)
.duration(1000)
.attrTween("d", fold)
function unfold(d) {
var i = d3.interpolate({startAngle: 0, endAngle: 0}, d);
return function(t) {
return arc(i(t))
}
}
function fold(d) {
var i = d3.interpolate(d, {startAngle: 0, endAngle: 0});
return function(t) {
return arc(i(t))
}
}
}
d3.interval(reDraw, 5000)
</script>
</body>
@ad-m-in
Copy link

ad-m-in commented Mar 1, 2020

Привет, напиши в телегу @tipichniir есть предложения по работе, на постоянку или проектно, как понравится.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment