d3.data跳过第一行数据 [英] d3.data skipping the first row of data
问题描述
我有以下代码工作得很好,除非当我遍历我的数据集,第一行(0索引)被跳过。
I have the following code that works great, except when I iterate through my data set, the first row (the 0 index) is getting skipped.
svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x",function(d){
console.log(data);
console.log(d);
return xScale(d.year-1980);
})
注意 console.log(data)
返回我的完整数据集,包括第一行,以便数据在那里!
Note the console.log(data)
returns my full data set, including the first row so the data is there!
但 console.log(d)
显示第二行数据后面的所有行 - 它删除第一行。
But console.log(d)
shows all rows after and including my second row of data - it drops the first row.
欢迎任何建议。
推荐答案
并使用类似的代码,并根据 Lars Kothoff 的评论修正此问题。
I had the same issue with similar code, and fixed it based on Lars Kothoff's comment.
在我的例子中,改变 selectAll
来处理ag元素是有意义的,更像如下:
In my case it made sense to change the selectAll
to work on a g element, more like so:
svg.selectAll("g")
.data(data);
.enter()
.append("g")
.append("rect")
.attr("x",function(d) { return xScale(d.year-1980); });
您还可以使用类来区分rect:
You could also differentiate the rects with a class:
svg.selectAll("rect.year")
.data(data)
.enter()
.append("rect")
.attr("x",function(d){ return xScale(d.year-1980); })
.classed("year");
这篇关于d3.data跳过第一行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!