Skip to content

Instantly share code, notes, and snippets.

@chabb
Last active August 29, 2015 14:05
Show Gist options
  • Save chabb/a173e96b0e128cfb2a74 to your computer and use it in GitHub Desktop.
Save chabb/a173e96b0e128cfb2a74 to your computer and use it in GitHub Desktop.
Custom interpolator for path

This example use a custom path interpolator, as opposed to the other examples.

The interpolator interpolates the number of elements of the array and inserts missing elements

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.frame {
fill: none;
stroke: #000;
}
.axis text {
font: 10px sans-serif;
}
.axis line,
.axis circle {
fill: none;
stroke: #777;
stroke-dasharray: 1,4;
}
.axis :last-of-type circle {
stroke: #333;
stroke-dasharray: none;
}
.line {
fill: none;
stroke: red;
stroke-width: 1.5px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
d3.interpolators.push(function(a,b){
if (Array.isArray(a[0]) && Array.isArray(b[0])) {
var x = [],
c = [],
na = a.length,
nb = b.length,
n0 = Math.min(a.length, b.length),
i,
delta = b.length - a.length,
newSize;
console.log('No',na,nb);
for (i=0; i < na; ++i)
{
c[i]=b[i];
}
newSize;
return function(t) {
newSize = a.length + delta*t;
//console.log(newSize,c.length,t,delta);
if (newSize>c.length){
for (i=c.length;i<newSize;i++) c.push(b[i]);
}
return c;
}
}
});
function d3_interpolateArray(a, b) {
//we suppose a is smaller
var x = [],
c = [],
na = a.length,
nb = b.length,
n0 = Math.min(a.length, b.length),
i,
delta = b.length - a.length,
newSize;
for (i = 0; i < n0; ++i) x.push(d3_interpolate([0,0], b[i]));
for (i=0; i < na; ++i) c[i] = a[i];
return function(t) {
newSize = a.length + delta*t;
if (newSize>c.length) c.push(b[newSize]);
for (i = 0; i < c.length; ++i) c[i] = x[i](t);
return c;
};
}
var data = d3.range(0, 2*Math.PI, .01).map(function(t) {
return [t, Math.sin(4*t)];
});
var empty = d3.range(0, 2*Math.PI, .01).map(function(t) {
return [0,0];
});
var margin = 30;
// radius is the minimal dimension, minus the margin
var width = 960,
height = 500,
radius = Math.min(width, height) / 2 - margin;
var r = d3.scale.linear()
.domain([0, 1])
.range([0, radius]);
//default accessor [[x1,y1]] => radian and angle
var line = d3.svg.line.radial()
.radius(function(d){; return (r(d[1])); }) // will change between -1 and 1
.angle(function(d) { ;return d[0];});
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
// radius axis
// cheat with CSS
var gr = svg.append("g")
.attr("class", "r axis")
.selectAll("g")
.data(r.ticks(10).slice(0))
.enter().append("g");
gr.append("circle")
.attr("r", function(d,i){console.log("=>",d,i,r(d));return r(d)});
gr.append("text")
.attr("y", function(d) { return -r(d) - 4; })
.attr("transform", "rotate(50)")
.style("text-anchor", "middle")
.text(function(d) { return d; });
var ga = svg.append("g")
.attr("class", "a axis")
.selectAll("g")
.data(d3.range(0, 360, 15))
.enter().append("g")
.attr("transform", function(d) { return "rotate(" + (d-90) + ")"; });
ga.append("line")
.attr("x2", radius);
ga.append("text")
.attr("x", radius + 6)
.attr("dy", ".35em")
.style("text-anchor", function(d) { return d < 270 && d > 90 ? "end" : null; })
.attr("transform", function(d) { return d < 270 && d > 90 ? "rotate(180 " + (radius + 6) + ",0)" : null; })
.text(function(d) { return d + "°"; });
// a bit cheating
var path =
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
var totalLength = path.node().getTotalLength();
path.transition().duration(1000).attrTween("d",function(d){
console.log(empty,d);
var interpolate = d3.interpolate( [[0,0]],d);
return function(t) { return line(interpolate(t))};
})
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment