2013-02-09 182 views
4

我是d3.js的新手,不確定使用哪個d3功能。d3.js使用極座標的繪圖元素

我需要將一組元素放置在一個原點(圓圈)上。

svg.selectAll('circle').each(function() { 
    d3.select(this) 
     .attr('cx', r * Math.cos(theta)) 
     .attr('cy', r * Math.sin(theta)); 
    theta += thetaInc; 
}); 

因此,不要像上面的代碼那樣做一些乏味的事情,那麼做到這一點的d3簡短方法是什麼?

回答

7

D3的方式做,這將是在數據傳遞和基於基準的指數,即像

var theta = 2 * Math.PI/array.length; 
svg.selectAll('circle').data(array).enter() 
    .append("circle") 
    .attr('cx', function(d, i) { return(r * Math.cos(i * theta)); }) 
    .attr('cy', function(d, i) { return(r * Math.sin(i * theta)); }); 
計算位置