d3.jsselectall

d3 selectAll: count results


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)


Solution

  • 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.