Accepted answer

this is in part a repeat of lars' equations from the comments, but i thought it was worth recapping all at once because the trig identities for converting from angles to x/y coordinates won't match your trig text book.

most text books assume that angles start at the right horizontal axis and increase counter-clockwise, and that the vertical axis has larger values higher up on the page.

in svg, larger y values are lower on the page, and the angles created by the pie chart layout (and the example code the op is using for the sunburst layout) draw an angle of zero as vertical line to the top of the circle, with angles increasing clockwise.

with that information, you can convert to x and y values with the following trig equations:

  g.append("circle") //circles inherit pie chart data from the <g>
      .attr("r", 5) 
      .attr("cx", function(d) { 
          return math.sin((d.startangle + d.endangle)/2) *radius;
      .attr("cy", function(d) { 
          return -math.cos((d.startangle + d.endangle)/2) *radius;

live example:

again, this simple example uses a pie chart layout, so the data has startangle and endangle values, and the radius is constant. for a sunburst diagram made with the partition layout, you would replace (d.startangle + d.endangle)/2 with d.x + d.dx/2, and you would replace radius with a function based on d.depth.


as an alternative to trigonometry, you could use transformations to position the circles. if the first step in a transformation is a rotation, and then you apply a translation afterwards, the translation will be applied in the rotated coordinate system.

a little extra complication, though, is that the d3 pie chart gives angles in radians (since that's what the trigonometry functions use), but the rotation needs angles in degrees.

  var degreesperradian = 180/math.pi;
  g.append("circle") //circles inherit pie chart data from the <g>
      .attr("r", 5)
      .attr("transform", function(d) { 
          return "rotate(" +  degreesperradian*((d.startangle + d.endangle)/2) 
          + ")" +
              //rotate by the average of the start and end angle
              //note that d3 specifies angles in radians, but the rotate
              //function needs them in degrees
              "translate(0," + -radius + ")";
              //then translate "up" the distance of the radius;
              //"up" is interpretted according to the rotated coordinates,
              //but for zero rotation it will position the dot at the top
              //of the circle, which is the zero angle for d3

live example:

(based on this simple pie chart code )

Related Query