Appending multiple non-nested elements for each data member with D3.js

后端 未结 6 1860
无人及你
无人及你 2020-12-04 19:14

I would like to create multiple non-nested elements using d3 to create a structure like this:

    

from data[0] &l

6条回答
  •  無奈伤痛
    2020-12-04 19:31

    Similar to the above but in a different idiom. This is truer to the nested selections approach and eliminates the need for sorting or insertion:

    var divs = d3.select('#parent');
    
    var ps = divs.selectAll('#parent > div')
        .data(d3.range(3)).enter().append('div');
    
    ps.append('p').html(function(d,i) { return 'from data[' + i + ']'; });
    ps.append('p').html(function(d,i) { return 'from data[' + i + ']'; });
    

    More elegantly (and probably faster):

    var divs = d3.select('#parent');
    
      var ps = divs.selectAll('#parent > div')
        .data(d3.range(3)).enter().append('div');
    
        ps.append('p').html(function(d,i) { return 'from data[' + i + ']'; })
        .select(function() { return this.parentNode.appendChild(this.cloneNode(true)); });
    

提交回复
热议问题