【问题标题】:show/hide elements in d3在 d3 中显示/隐藏元素
【发布时间】:2021-05-16 22:53:28
【问题描述】:

我是D3的新手,我正在尝试通过示例学习,我修改了一个示例代码,但我无法做到这一点:在旭日形图中只显示两个环,这是代码:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

path {
  stroke: #fff;
  fill-rule: evenodd;
}

text {
  font-family: Arial, sans-serif;
  font-size: 16px;
}

</style>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>

// Static dimensions
var width = 960,
    height = 700,
    radius = Math.min(width, height) / 2;

// Dynamic dimensions
    width = window.innerWidth;
    height = window.innerHeight;
    radius = Math.min(width, height) / 2;

const formatNumber = d3.format(',d');

var x = d3.scale.linear()
    .range([0, 2 * Math.PI]);

var y = d3.scale.pow()
    .exponent(0.4)
    .range([0, radius]);

var color = d3.scale.category20c();

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(" + width / 2 + "," + (height / 2 + 10) + ")");

var partition = d3.layout.partition()
    .value(function(d) { return d.size; });

var arc = d3.svg.arc()
    .startAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x))); })
    .endAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx))); })
    .innerRadius(function(d) { return Math.max(0, y(d.y)); })
    .outerRadius(function(d) { return Math.max(0, y(d.y + d.dy)); });

d3.json("structure.json", function(error, root) {
  var g = svg.selectAll("g")
      .data(partition.nodes(root)
//        .filter(function(d) {
  //        return (d.depth < 2);
    //    })
      )
    .enter().append("g");

  var path = g.append("path")
    .attr("d", arc)
    .style("fill", function(d) { return color((d.children ? d : d.parent).name); })
    .on("click", click)

  var text = g.append("text")
    .attr("transform", function(d) { return "rotate(" + computeTextRotation(d) + ")"; })
    .attr("x", function(d) { return y(d.y); })
    .attr("dx", "6") // margin
    .attr("dy", ".35em") // vertical-align
    .attr('display',function(d) { 

            const CHAR_SPACE = 10;

            const deltaAngle = x(d.x1) - x(d.x0);
            const r = Math.max(0, (y(d.y0) + y(d.y1)) / 2);
            const perimeter = r * deltaAngle;

            return d.name.length * CHAR_SPACE < perimeter;
        }
      )
    .text(function(d) { 
        if (d.name.includes("chip")) {
          label = '';
        }
        else {
          label = d.name;
        }

        if (!isNaN(d.value)) {
          label = '\n' + label + formatNumber(d.value);
        }
        return label;
        //return d.name; 
      }

    )

  function click(d) {
    // fade out all text elements
    text.transition().attr("opacity", 0);

    path.transition()
      .duration(750)
      .attrTween("d", arcTween(d))
      .each("end", function(e, i) {
          // check if the animated element's data e lies within the visible angle span given in d
          if (e.x >= d.x && e.x < (d.x + d.dx)) {

            // get a selection of the associated text element
            var arcText = d3.select(this.parentNode).select("text");

            // fade in the text element and recalculate positions
            arcText.transition().duration(750)
              .attr("opacity", 1)
              .attr("transform", function() { return "rotate(" + computeTextRotation(e) + ")" })
              .attr("x", function(d) { return y(d.y); });
          }
      });
  }
});

d3.select(self.frameElement).style("height", height + "px");

// Interpolate the scales!
function arcTween(d) {
  var xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),
      yd = d3.interpolate(y.domain(), [d.y, 1]),
      yr = d3.interpolate(y.range(), [d.y ? 20 : 0, radius]);
  return function(d, i) {
    return i
        ? function(t) { return arc(d); }
        : function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); return arc(d); };
  };
}

function computeTextRotation(d) {
  return (x(d.x + d.dx / 2) - Math.PI / 2) / Math.PI * 180;
}

</script>

如您所见,我添加了一行 .filter 并且在加载 json 时有效,但是一旦单击一个环,它就不会动态显示下一个环。一个怎么可能在任何时候都只显示两个“儿童戒指”?

【问题讨论】:

    标签: d3.js sunburst-diagram


    【解决方案1】:

    我能够解决我的问题的唯一方法是将类属性添加到 d3 元素,并使用 jquery 禁用/启用环,它实际上是这样工作的,不确定是否有更直接的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      相关资源
      最近更新 更多