d3 selectAll: count results

Vituel picture Vituel · Jul 3, 2015 · Viewed 22.2k times · Source

How do I count how many nodes were matched by a selectAll? (without joined data)

Or if there's data, how to count the data from the selection? (suppose I've set it with "data(function...)" so I don't know the length in advance)

Answer

Gabriel picture Gabriel · Jul 4, 2015

Just use d3.selectAll(data).size().Hope this example help you:

 var matrix = [
   [11975,  5871, 8916, 2868],
   [ 1951, 10048, 2060, 6171],
   [ 8010, 16145, 8090, 8045],
   [ 1013,   990,  940, 6907]
 ];

 var tr = d3.select("body").append("table").selectAll("tr")
            .data(matrix)
            .enter().append("tr");

 var td = tr.selectAll("td")
          .data(function(d) { return d; })
          .enter().append("td")
          .text(function(d) { return d; });
 var tdSize=tr.selectAll("td").size();

Complete jsfiddle here.