我需要当前选择父母的索引。我已经读过我可以使用.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';
})
}
日志给了我:
使用IndexA
和IndexB
它有效,但不是我想要它。如何获得j
变量的正确值?