D3 Sunburst - 可以显示某些图层

D3 Sunburst - Possible to show certain layers

我在看 Sunburst 图表 - 即来自这个例子:

https://bl.ocks.org/kerryrodden/7090426

我想问一下在 D3 中是否可以控制显示的环数。这么说我只想出现第二环?

我注意到这段代码

// For efficiency, filter nodes to keep only those large enough to see.
var nodes = partition.nodes(json)
      .filter(function(d) {
          return (d.dx > 0.005); // 0.005 radians = 0.29 degrees
});

我尝试按照 d.depth = 2 的方式附加一些内容,但是没有用:

// For efficiency, filter nodes to keep only those large enough to see.
var nodes = partition.nodes(json)
          .filter(function(d) {
            if (d.depth = 2) {
              return (d.dx > 0.005); // 0.005 radians = 0.29 degrees
            }
});

如有任何帮助,我们将不胜感激。

谢谢。

你很接近,每个元素的过滤器都需要 return。尝试通过逻辑 &&:

添加深度检查
// For efficiency, filter nodes to keep only those large enough to see.   
var nodes = partition.nodes(json)
    .filter(function(d) {
        return (d.dx > 0.005 && d.depth < 3); // 0.005 radians = 0.29 degrees
});