Reputation: 457
I am using the Zoomable Icicle layout in D3 to visualise a folder hierarchy found here: http://bl.ocks.org/mbostock/1005873.
At the moment, to fill leaf nodes with a different color I edit the fill of 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?
Thanks!
Upvotes: 0
Views: 915
Reputation: 109242
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.
To select leaf nodes, you can use .selectAll()
in conjunction with .filter()
:
var leaves = d3.selectAll("rect").filter(function(d) {
return d.children === null;
});
Upvotes: 3