0

I need the index of the current selection's parent. I have read that I can use the third parameter of the .attr() function but I get an array of objects instead of an integer index.

I use D3 version 3.

At the moment the code is:

function positiveBars(x, y, n, svgElement, colors) {
    IndexA = -1; //<--
    IndexB = -1; //<--

    svgElement.selectAll(".positiveBar")
        .data(function(d){ return d; })
        .enter()
        .append("rect")
        .attr("x", function(d, i, j){ 
            if(i == 0) {IndexA++;} //<--
            console.log(j); //<--
            return x(d.time) + x.bandwidth() / n *IndexA; 
        })
        .attr("width", x.bandwidth() / n)
        .attr("y", function(d) { return y(d.y); })
        .attr("height", function(d){ return y(0)- y(d.y); })
        .attr("class","positiveBar")
        .attr("fill",function(d, i, j) {
            if(i == 0) {IndexB++;} //<--
            return colors ? colors[IndexB] : '#111';
        })
}

The log gives me:

enter image description here

Using IndexA and IndexB it works but not like I want it. How can I get the right value of the j variable?

altocumulus
  • 21,179
  • 13
  • 61
  • 84
nat1993
  • 1
  • 1
  • 2
    Are you sure, you are using **v3**? Getting the current group's nodes as the third argument clearly indicates the use of **v4**... Can you do a `console.log(d3.version);` somewhere in your code? – altocumulus Mar 29 '18 at 09:47
  • If you are indeed using v4, then this is a duplicate of [*"d3.js v4: How to access parent group's datum index?"*](/q/38233003). – altocumulus Mar 29 '18 at 09:55

0 Answers0