D3的第三个参数.attr()是一个对象数组而不是索引

时间:2018-03-29 08:50:11

标签: angularjs d3.js

我需要当前选择父母的索引。我已经读过我可以使用.attr()函数的第三个参数,但我得到一个对象数组而不是整数索引。

我使用D3版本3。

目前代码是:

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';
        })
}

日志给了我:

enter image description here

使用IndexAIndexB它有效,但不是我想要它。如何获得j变量的正确值?

0 个答案:

没有答案