D3.js d3.data跳过第一行数据

D3.js d3.data跳过第一行数据,d3.js,D3.js,下面的代码非常有用,除了在遍历数据集时,第一行(0索引)被跳过 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)返回我的完整数据集,包括第一行,因此数据就在那里 但是c

下面的代码非常有用,除了在遍历数据集时,第一行(0索引)被跳过

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)
返回我的完整数据集,包括第一行,因此数据就在那里

但是
console.log(d)
显示第二行数据之后的所有行,包括第二行数据-它会删除第一行


欢迎提供任何建议。

我在类似代码中遇到了相同的问题,并根据的评论修复了该问题

在我的例子中,更改
selectAll
以处理g元素是有意义的,更像这样:

svg.selectAll("g")
    .data(data);
    .enter()
    .append("g")
    .append("rect")
    .attr("x",function(d) { return xScale(d.year-1980); });
您还可以使用类来区分矩形:

svg.selectAll("rect.year")
    .data(data)
    .enter()
    .append("rect")
    .attr("x",function(d){ return xScale(d.year-1980); })
    .classed("year");

是的,如果已经有一个元素,它就会被.enter()跳过


D3试验
var theData=[“詹姆斯”、“苏”、“本”]
变量p=d3。选择(“主体”)。选择全部(“p”)
.数据(theData)
.输入()
.append('p')
.文本(功能(d,i){
返回“i=“+i+”数据=“+d”;
});
产生

i=0data=James

i=1数据=Sue

i=2data=Ben

但是如果在其中添加一个p元素,它将跳过它

...[previous code]

<body>
<p>Here is the first p tag that "James gets matched too"</p>

</body>

...[previous code]
…[以前的代码]
这是第一个p标签“James也被匹配”

…[以前的代码]
我猜在运行该代码时,您已经有了
rect
元素。此元素正在与第一个数据行匹配,因此
.enter()
选择缺少它。D3非常强大,但太大。它也没有被广泛使用或出版。它完全利用了javascript的强大功能。希望我们在x函数(d)之外有更多关于D3Move console.log(数据)的教程。每个d都会打印出来。也许,你错过了。然后,发布一些您的输出。谢谢@LarsKotthoff,您至少节省了我几个小时的时间!我发现这也是一个救命稻草。搜索结果出来花了一些时间
d3 selectall。跳过第一个元素的数据并没有切断它。底部方法是最简单的解决方案。美好的只需将
selectAll(“rect”)
更改为
selectAll(“rect.year”)
...[previous code]

<body>
<p>Here is the first p tag that "James gets matched too"</p>

</body>

...[previous code]