2013-02-09 174 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)); }); 
计算位置