Javascript 将当前元素索引传递给D3中的函数
我使用“源代码”创建一组独立的行作为Javascript 将当前元素索引传递给D3中的函数,javascript,d3.js,Javascript,D3.js,我使用“源代码”创建一组独立的行作为path元素: svgContainer.append("path") .attr("d", lineFunction(lineData)) .attr("stroke", "blue") .attr("stroke-width", 2) .attr("fill", "none"); 因为我在lineData数组中有所有的行数据,所以在每次迭代中,我只想从中传递一
path
元素:
svgContainer.append("path")
.attr("d", lineFunction(lineData))
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none");
因为我在lineData数组中有所有的行数据,所以在每次迭代中,我只想从中传递一个项目,比如
lineFunction(lineData[I])
。我的问题是如何获得当前索引,就像我通常使用D3中的someFunction(d,I){}
一样?您需要首先以典型的D3方式绑定到lineData
。因此:
svgContainer.selectAll('path').data(lineData).enter()
.append("path")
.attr("d", lineFunction)
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none");
在线
.attr("d", lineFunction)
lineFunction
以d
和i
作为参数调用,其中d
是数组中的元素(如果我理解的话,它是一个点数组)。这和做同样的事
.attr("d", function(d,i) { return lineFunction(d); }
这和
.attr("d", function(d,i) { return lineFunction( lineData[i]); }
您不需要对此进行迭代,请参见例如。