score:8

Accepted answer

i would recommend manually drawing the arcs with svg's path.

first, here is a working example with the additions: https://jsfiddle.net/mztafs0w/


explanation:

svg path has commands such as m for move, a for arc, l for draw line to:

  • capital letters are absolute pixel movements
  • lowercase letters are relative pixel movements

to make a filled pie slice using svg path, you must perform these actions:

how to draw an arc image source


let's say your radius is 40 and you want a slice for the top-right quadrant. the entire command for this would be:

  • move x(0) y(-40) -- move to top
  • arc x-radius(40) y-radius(40) xrot(0) >180deg?(0) sweep(1) x(40) y(0) -- arc to right
  • line x(0) y(0) -- return to center

compressed into svg path format, this appears as:

m 0 -40 a 40 40 0 0 1 40 0 l 0 0 (minimally, m0,-40a40,40,0,0,1,40,0l0,0)

performing this 4 times to get all 4 quadrants is simple enough, and replacing radius with ${r} allows the size to be easily adjusted.

the final code added to your js fiddle:

var slices=[];
  slices[0] = node.append("path")
  .attr("d", function(d) {
    let r = d.type == "family" ? family_radius + 5 : 40;
    return `m 0 -${r} a ${r} ${r} 0 0 1 ${r} 0 l 0 0`; } )
  .attr("fill", "coral");
slices[1] = node.append("path")
    .attr("d", function(d) {
    let r = d.type == "family" ? family_radius + 5 : 40;
    return `m ${r} 0 a ${r} ${r} 0 0 1 0 ${r} l 0 0`; } )
  .attr("fill", "royalblue");
slices[2] = node.append("path")
  .attr("d", function(d) {
    let r = d.type == "family" ? family_radius + 5 : 40;
    return `m 0 ${r} a ${r} ${r} 0 0 1 -${r} 0 l 0 0`; } )
  .attr("fill", "olivedrab");
slices[3] = node.append("path")
  .attr("d", function(d) {
    let r = d.type == "family" ? family_radius + 5 : 40;
    return `m -${r} 0 a ${r} ${r} 0 0 1 0 -${r} l 0 0`; } )
  .attr("fill", "goldenrod");

i recommend you remove the non-working describearc section and make the code more dry. you may perform more calculations to have the circle slices break at places other than 0/90/180/270. let me know if you need help with any of those, or you may check the image source for more tips.

i also changed the family_radius to family_radius + 5 so you can see the arcs being drawn underneath the white fill of those smaller circles. if this is not desirable you may either remove the white fill on these circles (line 165 if(d.type == "family"){return "white"}) or simply not draw these slices at all for those circles.


Related Query

More Query from same tag