Last active
March 1, 2020 14:21
-
-
Save raidan00/ea0d0e608c4d40f95296688a024887aa to your computer and use it in GitHub Desktop.
d3 v4 pie animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Привет, напиши в телегу @tipichniir есть предложения по работе, на постоянку или проектно, как понравится.