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
Post a Comment