Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created December 26, 2014 05:07
Show Gist options
  • Save miguelmota/1941ca3ed6a8dbf6dc83 to your computer and use it in GitHub Desktop.
Save miguelmota/1941ca3ed6a8dbf6dc83 to your computer and use it in GitHub Desktop.
D3.js arc transition
var width = 400,
height = 400,
endAngle = 2 * Math.PI,
colors = d3.scale.category20();
var svg = d3.select("body").append("svg")
.attr("class", "pie")
.attr("height", height)
.attr("width", width);
function render(innerRadius) {
var data = [
{startAngle: 0, endAngle: 0.1 * endAngle},
{startAngle: 0.1 * endAngle, endAngle: 0.2 * endAngle},
{startAngle: 0.2 * endAngle, endAngle: 0.4 * endAngle},
{startAngle: 0.4 * endAngle, endAngle: 0.6 * endAngle},
{startAngle: 0.6 * endAngle, endAngle: 0.7 * endAngle},
{startAngle: 0.7 * endAngle, endAngle: 0.9 * endAngle},
{startAngle: 0.9 * endAngle, endAngle: endAngle}
];
var arc = d3.svg.arc().outerRadius(200).innerRadius(innerRadius);
svg.select("g").remove();
svg.append("g")
.attr("transform", "translate(200,200)")
.selectAll("path.arc")
.data(data)
.enter()
.append("path")
.attr("class", "arc")
.attr("fill", function (d, i) {
return colors(i);
})
.transition().duration(1000)
.attrTween("d", function (d) {
var start = {startAngle: 0, endAngle: 0};
var interpolate = d3.interpolate(start, d);
return function (t) {
return arc(interpolate(t));
};
});
}
render(100);
@AbraaoAbe
Copy link

It doesn't work fine for me here, when i put the.
d3.interpolate(start, d);
But, i saw in others sites and found one way to do that:

.attrTween('d', (d) => {
            return (t) => {
                const angle = d3.interpolate(d.endAngle, newAngle)(t);
                return currentArc(null)
            }
        } ); 

Where currentArc is the old arc that i want substitute

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