【问题标题】:d3.js - circle pack layout with nested g nodesd3.js - 带有嵌套 g 节点的圆形包布局
【发布时间】:2013-01-27 22:01:36
【问题描述】:

我正在尝试实现圆形包装示例:http://bl.ocks.org/4063530 - 但我希望它使用嵌套的“g”节点,以便更轻松地设置样式和控制每个圆形子节点的可见性 - 但在此示例中,所有节点在dom中的深度相同。我如何嵌套它们?或者,如果不是嵌套,我如何只选择一个圆圈的直接子级(不是所有圆圈的所有子级)。我目前已修改示例以添加具有对象深度的类,如下所示:

d3.json("skills.json", function(error, root) {
  var node = svg.datum(root).selectAll(".node")
    .data(pack.nodes)
    .enter().append("g")
      .attr("class", function(d) { return "node node"+ d.depth; })

现在想这样做:

d3.selectAll(".node1").on("mouseover",function(){
    d3.select(this).classed("active",true).//SELECT ALL CHILDREN OF THE HOVERED NODE HERE

有人有什么想法吗?

【问题讨论】:

  • 好问题,我认为您应该对答案感到满意。

标签: javascript svg d3.js hierarchy circle-pack


【解决方案1】:

我想不出在pack数据之后嵌套 g 元素的方法,所以这是一个不太优雅的解决方案:

  function checkParent(d, w) {
      if(!d.parent) return false;

      if(d.parent == w) {
        return true;
      } else {
        return checkParent(d.parent, w);
      }
  }

  node.on("mouseover",function(d){
      d3.select(this).classed("active",true);
      d3.selectAll(".node")
          .filter(function(w){ return checkParent(w, d); })
          .classed("active",true);
  });

  node.on("mouseout",function(d){
      d3.select(this).classed("active",false);
      d3.selectAll(".node")
          .filter(function(w){ return checkParent(w, d); })
          .classed("active",false);
  });

http://bl.ocks.org/4771875

【讨论】:

  • 谢谢 - "w" 变量代表什么?
  • "w" 为数据节点对象;有点像外部上下文中的“d”,但它们需要采用不同的名称。
猜你喜欢
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多