Search code examples
javascriptd3.jssunburst-diagram

Sunburst incomplete Segments


I've been trying to understand the arc function used within d3 v5 sunburst, and I can't quite wrap my head around it. I am able to draw the sunburst, but once it goes beyond depth of 1 the arcs are incomplete. They appear to be roughly half the size of the proceeding segment. Could someone explain the piece I am missing here?

let data = {
  "name": "DEMO",
  "children": [{
      "Build": "1",
      "value": "90",
      "children": [{
        "Build": "1",
        "value": "70",
        "children": [{
          "Build": "1",
          "value": "60",
        }]
      }]
    },
    {
      "Build": "2",
      "value": "95",
      "children": [{
        "Build": "2",
        "value": "85",
        "children": [{
          "Build": "2",
          "value": "65",
        }]
      }]
    }
  ]
}

function createSunBurst(data) {

  let container = document.getElementById("container")
  let width = 200
  let height = 200
  let radius = (Math.min(width, height) / 2) - 5
  let format = d3.format(",d")

  let partition = data => d3.partition()
    .size([2 * Math.PI, radius])(d3.hierarchy(data)
      .sum(d => d.value)
      .sort((a, b) => b.value - a.value))

  let root = partition(data)

  d3.select("#container")
    .append("svg")

  let svg = d3.select("svg")
    .style("width", "100%")
    .style("height", "100%")
    .attr("id", "canvas")
    .append("g")
    .attr("class", "center-group")

  let arc = d3.arc()
    .startAngle(d => d.x0)
    .endAngle(d => d.x1)
    .innerRadius(d => d.y0)
    .outerRadius(d => d.y1 - 0)

  svg.selectAll("path")
    .data(root.descendants().filter(d => d.depth))
    .enter()
    .append("path")
    .attr("stroke", "white")
    .attr("fill", "green")
    .style("fill-rule", "evenodd")
    .attr("d", arc)

}

createSunBurst(data)
#container {
  height: 100vh;
}

.center-group {
  transform: scale(1, 1) translate(50%, 50%);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

<div id="container">
</div>


Solution

  • Since posting this I have continued to research. I have solved my issue. The arc isn't the issue. Rather the issue is with the way I initially calculated the sum of the partitioned data.

    I resolved my issue after reading this answer.