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

前端 未结 8 725
萌比男神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:09

    I ended up defining an external variable "j" and then increment it whenever "i" is 0

    example V3 snippet below.

    rowcols.enter().append("rect")
     .attr("x", function (d, i, j) { return CalcXPos(d, j); })
     .attr("fill", function (d, i, j) { return GetColor(d, j); })
    

    and in V4, code converted as below.

    var j = -1;
    rowcols.enter().append("rect") 
     .attr("x", function (d, i) { if (i == 0) { j++ }; return CalcXPos(d, j); })
     .attr("fill", function (d, i) { return GetColor(d, j); })
    

提交回复
热议问题