D3 update pie data with zero values

后端 未结 1 1492
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 23:04

I am updating a pie chart with a dynamic JSON feed. My update function is below

function updateChart(data) {
arcs.data(pie(data)); // recompute angles, rebin         


        
相关标签:
1条回答
  • 2020-12-11 00:06

    You've actually hit a bug in D3 there -- if everything is zero, the pie layout returns angles of NaN, which cause errors when drawing the paths. As a workaround, you can check whether everything is zero and handle this case separately. I've modified your change function as follows.

    if(data.filter(function(d) { return d.totalCrimes > 0; }).length > 0) {
      path = svg.selectAll("path").data(pie(data));
      path.enter().append("path")
          .attr("fill", function(d, i) { return color(d.data.crimeType); })
          .attr("d", arc)
          .each(function(d) { this._current = d; });
      path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs
    } else {
      path.remove();
    }
    

    Complete jsbin here.

    0 讨论(0)
提交回复
热议问题