简体   繁体   English

在D3分区布局示例中选择叶节点并应用不同的填充颜色的正确方法是什么?

[英]What is the correct way to select leaf nodes in D3 partition layout example and apply different fill color?

I am using the Zoomable Icicle layout in D3 to visualise a folder hierarchy found here: http://bl.ocks.org/mbostock/1005873 . 我正在D3中使用Zoomable Icicle布局来可视化此处找到的文件夹层次结构: http ://bl.ocks.org/mbostock/1005873。

At the moment, to fill leaf nodes with a different color I edit the fill of rect: 此刻,为了用不同的颜色填充叶节点,我编辑rect的填充:

rect.data(data)
        .enter()
        .append("rect")
        .attr("x", function (d) { return x(d.x); })
        .attr("y", function (d) { return y(d.y); })
        .attr("width", function (d) { return x(d.dx); })
        .attr("height", function (d) { return y(d.dy); })
        .attr("fill", function (d) { return d.children ? /* parent color */ : /* leaf color */; })
.on("click", clicked);

Is this the most correct way to do it? 这是最正确的方法吗?

How can I use a selector to select leaf nodes and apply a different fill color to rect based on the example code? 如何使用选择器选择叶节点并根据示例代码将其他填充颜色应用于rect?

Thanks! 谢谢!

Yes, this is the correct way to do it -- the only thing that differentiates leaf from non-leaf nodes is whether the children property is null or not. 是的,这是正确的方法-区分叶子节点和非叶子节点的唯一一件事是children属性是否为null

To select leaf nodes, you can use .selectAll() in conjunction with .filter() : 要选择叶节点,可以将.selectAll().filter()结合使用:

var leaves = d3.selectAll("rect").filter(function(d) {
  return d.children === null;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM