javascript - 向 d3 sunburst 添加标签

标签 javascript d3.js sunburst-diagram

我正在尝试修改此处的 D3 sunburst 示例 ( http://bl.ocks.org/mbostock/4063423 ) 以向弧添加标签。

我添加了代码来计算 Angular 并生成文本(取自此示例: http://jsfiddle.net/4PS53/6/ ),但它不起作用。文本没有显示,当我尝试使用检查器检查 元素时,它们似乎不在 DOM 中。

非常感谢任何帮助! JS代码如下(根据原D3图库示例修改),JSON数据相同。我正在展示我修改过的代码部分;其余的都是一样的。

d3.json("data.json", function(error, root) {
  console.log(root);
  var path = svg.datum(root).selectAll("path")
      .data(partition.nodes)
      .enter().append("path")
      .attr("display", function(d) { return d.depth ? null : "none"; }) // hide inner ring
      .attr("d", arc)
      .style("stroke", "#fff")
      .style("fill", function(d) { return color((d.children ? d : d.parent).name); })
      .style("fill-rule", "evenodd")
      .each(stash);

  path.append("text")
        .text(function(d) { return d.name})
        .classed("label", true)
        .attr("x", function(d) { return d.x; })
        .attr("text-anchor", "middle")
        // translate to the desired point and set the rotation
        .attr("transform", function(d) {
            if (d.depth > 0) {
                return "translate(" + arc.centroid(d) + ")" +
                       "rotate(" + getAngle(d) + ")";
            }  else {
                return null;
            }
        })
        .attr("dx", "6") // margin
        .attr("dy", ".35em") // vertical-align
        .attr("pointer-events", "none");

});

function getAngle(d) {
    // Offset the angle by 90 deg since the '0' degree axis for arc is Y axis, while
    // for text it is the X axis.
    var thetaDeg = (180 / Math.PI * (arc.startAngle()(d) + arc.endAngle()(d)) / 2 - 90);
    // If we are rotating the text by more than 90 deg, then "flip" it.
    // This is why "text-anchor", "middle" is important, otherwise, this "flip" would
    // a little harder.
    return (thetaDeg > 90) ? thetaDeg - 180 : thetaDeg;
}

最佳答案

您需要 g 组元素来添加文本,因此您只需更改:

var path = svg.datum(root).selectAll("path")
  .data(partition.nodes)
  .enter().append("path")
  .attr("display", function(d) { return d.depth ? null : "none"; }) // hide inner ring
  .attr("d", arc)
  .style("stroke", "#fff")
  .style("fill", function(d) { return color((d.children ? d : d.parent).name); })
  .style("fill-rule", "evenodd")
  .each(stash);

var path = svg.datum(root).selectAll("path")
  .data(partition.nodes)
  .enter().append("g")

path.append("path")
  .attr("display", function(d) { return d.depth ? null : "none"; }) // hide inner ring
  .attr("d", arc)
  .style("stroke", "#fff")
  .style("fill", function(d) { return color((d.children ? d : d.parent).name); })
  .style("fill-rule", "evenodd")
  .each(stash);

检查这个 jsFiddle: http://jsfiddle.net/5d0zd2s7/

关于javascript - 向 d3 sunburst 添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29884995/

相关文章:

javascript - 似乎无法为选择选项设置默认选项;使用 Angular

javascript - 如何访问 stenciljs 中的 native HTML 属性?以及如何让它出现在文档中?

javascript - 具有重置功能的 D3js 缩放

javascript - 通过绑定(bind)新数据来操作元素

python - 有没有办法改变plotly.express.sunburst图中叶子的不透明度?

javascript - Window.open 不适用于 jquery

javascript - 使用jquery从JSP响应中解析Json

javascript - d3js 支持的事件类型列表

d3.js - 如何使用 d3.js 获取旭日示例中每个弧的 startAngle 和 endAngle?

javascript - 可缩放旭日与标签问题