2014-01-14 21 views
2

我是d3.js的新手。我想了解下面的代码:d3.interpolate的显示值

.tween("text", function(d) { 
     var i = d3.interpolate(this.textContent, d), 
      prec = (d + "").split("."), 
      round = (prec.length > 1) ? Math.pow(10, prec[1].length) : 1; 

     console.log(i); 

     return function(t) { 
      this.textContent = Math.round(i(t) * round)/round; 
     }; 
    });​ 

我想看到的var i值,所以如果我做console.log(i),我得到了一些公式返回。我怎样才能看到插值?

回答

7

d3.interpolate方法接收过渡的开始和结束值,并返回一个插补函数。内插函数接收0和1之间的值,并返回内插值。例如:

// Create an interpolator function to compute intermediate colors between blue and red 
var i = d3.interpolate('blue', 'red'); 

// Evaluate the interpolator 
console.log(i(0.0)); // #0000ff - blue 
console.log(i(0.4)); // #cc0033 - purple 
console.log(i(1.0)); // #ff0000 - red