我正在创建 d3 条形图。我有一个 json 值数组
bars = this.svg.selectAll("g")
.data(data)
.enter()
.append('g')
.attr('transform',function(d, i) {
return "translate(0," + i * 30 + ")";
});
bars.append('rect')
.attr('height',20)
.attr('fill',this.color)
.attr('width',0)
.attr("x",100)
.transition()
.attr('width',function(d){
console.log('rect',d);
return widthScale(d.value)
})
.duration(1000)
一段时间后,我得到一个新的值列表,因此我更新数组并传递它。
this.svg.selectAll("g")
.data(data)
.attr('asd',function(d){
console.log("My Data",d);
return 1;
});
在我的控制台日志中,我得到的数据非常好,但是当我尝试从矩形访问时
bars.selectAll('rect')
.transition()
.attr('fill',this.color)
.attr('width',function(d){
console.log(d.value);
return widthScale(d.value)
})
.duration(1000)
我正在获取旧的值(value)观。我做错了什么?
最佳答案
您将数据绑定(bind)到 g
元素,然后选择 rect
元素。如果要传播数据,请使用 .select("rect")
:
this.svg.selectAll("g").data(data).select("rect")
关于javascript - 更新d3数据到子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29734852/