最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - d3.js Plot elements using polar coordinates - Stack Overflow

programmeradmin0浏览0评论

I am new to d3.js and not sure which d3 functionality to use.

I need to place a set of elements concentrically about an origin (in a circle).

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

So instead of doing something tedious like the above code, what is the d3 short way of doing this?

I am new to d3.js and not sure which d3 functionality to use.

I need to place a set of elements concentrically about an origin (in a circle).

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

So instead of doing something tedious like the above code, what is the d3 short way of doing this?

Share Improve this question asked Feb 9, 2013 at 19:33 Blake RegaliaBlake Regalia 2,7662 gold badges21 silver badges29 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

The d3 way to do this would be to pass in the data and calculate the positions based on the index of the datum, i.e. something like

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)); });
发布评论

评论列表(0)

  1. 暂无评论