D3.js d3可缩放的日光暴击,工具提示在转换时调整百分比?

D3.js d3可缩放的日光暴击,工具提示在转换时调整百分比?,d3.js,D3.js,我创建了一个可缩放的太阳暴流图。这是我到目前为止的最新进展。我现在要做的是更新工具提示%值,以反映单击图表时的新值-例如,当单击“交通”时,在转换后,新交通环应显示一个说明“100%”的工具提示,“交通”的子项将相应调整其%值。当点击“卡车”时,它应该调整为“100%”。。。等等,请帮忙 var path = g.append("path") .attr("d", arc) .style("fill", function(d) { return d.color; }) .

我创建了一个可缩放的太阳暴流图。这是我到目前为止的最新进展。我现在要做的是更新工具提示%值,以反映单击图表时的新值-例如,当单击“交通”时,在转换后,新交通环应显示一个说明“100%”的工具提示,“交通”的子项将相应调整其%值。当点击“卡车”时,它应该调整为“100%”。。。等等,请帮忙

var path = g.append("path")
  .attr("d", arc)
  .style("fill", function(d) {
    return d.color;
  })
  .style("stroke", "white")
  .style("stroke-width", "1px")
  .on("click", click)
  .on("mouseover", function(d, i) {
    var totalSize = path.node().__data__.value;
    var percentage = Math.round((100 * d.value / totalSize) * 10) / 10;
    var percentageString = percentage + "%";
    tooltip.text(d.name + " " + percentageString)
      .style("opacity", 0.8)
      .style("left", (d3.event.pageX) + 0 + "px")
      .style("top", (d3.event.pageY) - 0 + "px");
  })
  .on("mouseout", function(d) {
    tooltip.style("opacity", 0);
  });

function click(d) {
  text.transition().attr("opacity", 0);
  path.transition()
    .duration(750)
    .attrTween("d", arcTween(d))
    .each("end", function(e, i) {
      if (e.x >= d.x && e.x < (d.x + d.dx)) {
        var arcText = d3.select(this.parentNode).select("text");
        arcText.transition().duration(750)
          .attr("opacity", 1)
          .attr("transform", function() {
            return "rotate(" + computeTextRotation(e) + ")"
          })
          .attr("x", function(d) {
            return y(d.y);
          });
       }
    })
  }
var path=g.append(“路径”)
.attr(“d”,弧)
.样式(“填充”,功能(d){
返回d.color;
})
.style(“笔划”、“白色”)
.style(“笔划宽度”、“1px”)
.on(“单击”,单击)
.on(“鼠标悬停”,功能(d,i){
var totalSize=path.node();
风险值百分比=数学四舍五入((100*d.value/totalSize)*10)/10;
var percentageString=百分比+“%”;
工具提示.text(d.name+“”+percentageString)
.样式(“不透明度”,0.8)
.style(“左”,“d3.event.pageX)+0+“px”)
.style(“top”,(d3.event.pageY)-0+“px”);
})
.开启(“鼠标出”,功能(d){
工具提示。样式(“不透明度”,0);
});
功能点击(d){
text.transition().attr(“不透明度”,0);
path.transition()
.持续时间(750)
.attrTween(“d”,arcTween(d))
.每个(“结束”,功能(e,i){
如果(e.x>=d.x&&e.x<(d.x+d.dx)){
var arcText=d3.select(this.parentNode).select(“text”);
arcText.transition()持续时间(750)
.attr(“不透明度”,1)
.attr(“转换”,函数(){
返回“旋转(“+ComputeExtraotation(e)+”)”
})
.attr(“x”,函数(d){
返回y(d.y);
});
}
})
}

我的解决方案是创建一个变量:

var percentBase = 100;
并且,在函数
内单击
,根据单击路径的属性
百分比
更改该变量:

percentBase = parseFloat(d.percent.split("%")[0]);
由于中心路径(“源”)不具有此属性,因此这也是必要的:

if(d.name == "Sources") percentBase = 100;
然后,在
mouseover
中,我们使用这个
percentBase
来计算百分比

这是你的小提琴:


数学上有一些必要的调整,但这取决于你。

事实上,你知道为什么“发电”、“道路灰尘”和“其他”不说“100%”吗?(分别是“101%,“99%,”101%。)因为四舍五入。正如我告诉过你的,这只是一般的想法,现在你必须对数学进行微调。