2014-01-14 23 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