userMod2 userMod2 - 3 months ago 22
Javascript Question

D3 Sunburst - Possible to show certain layers

I looking at Sunburst charts - namely from this example:

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

I want to ask is it possible in D3 - to control the number of rings of that are shown. So say I only wanted to show up the the 2nd ring?

I've noticed this section of code

// 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
});


I've tried to append to this something along the lines of
d.depth = 2
however doesn't work:

// 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
}
});


Any help would be appreciated.

Thanks.

Answer

You were close, filters need to return for every element. Try adding your depth check via a logical &&:

// 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
});
Comments