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?
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)); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With