d3.js v4: How to access parent group's datum index?

前端 未结 8 724
萌比男神i
萌比男神i 2020-11-28 10:33

The description of the selection.data function includes an example with multiple groups (link) where a two-dimensional array is turned into an HTML table.

8条回答
  •  我在风中等你
    2020-11-28 11:04

    My workaround is somewhat similar to Dinesh Rajan's, assuming the parent index is needed for attribute someAttr of g.nestedElt:

    v3:

    svg.selectAll(".someClass")
        .data(nestedData)
      .enter()
      .append("g")
        .attr("class", "someClass")
      .selectAll(".nestedElt")
        .data(Object)
      .enter()
      .append("g")
        .attr("class", "nestedElt")
        .attr("someAttr", function(d, i, j) {
    
        });
    

    v4:

    svg.selectAll(".someClass")
        .data(nestedData)
      .enter()
      .append("g")
        .attr("class", "someClass")
        .attr("data-index", function(d, i) { return i; }) // make parent index available from DOM
      .selectAll(".nestedElt")
        .data(Object)
      .enter()
      .append("g")
        .attr("class", "nestedElt")
        .attr("someAttr", function(d, i) {
          var j = +this.parentNode.getAttribute("data-index");
        });
    

提交回复
热议问题