javascript - How to create elements depending on data in D3? -


looking @ sample:

d3.select("body").selectall("div")     .data([4, 8, 15, 16, 23, 42])   .enter().append("div")     .text(function(d) { return d; }); 

cant wonder how make append sensitive provided data - do append , fill text predicate if d == 8 othervise not append?

the simplest way filter array before calling .data( ... ):

d3.select("body").selectall("p")     .data([4, 8, 15, 16, 23, 42].filter(function(d){ return d < 10; }))   .enter().append("div")     .text(function(d) { return d; }); 

will create div 4 , 8.

alternatively, can filter selection after binding array elements on page conditionally create children elements:

d3.select("body").selectall("div")     .data([4, 8, 15, 16, 23, 42])   .enter().append("div")     .text(function(d) { return d; })  .filter(function(d){ return d == 8; }).append("span")     .text("equal 8") 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -