我正在创建一个简单的条形图并试图使其响应用户点击。被点击的栏应该消失。所有似乎都在工作,除了点击第一个酒吧,最终酒吧消失。我完全不知道为什么会出现这种情况,非常感谢任何帮助。d3js:为什么不先用退出方法消失吧?
上Plunkr完整代码:
https://plnkr.co/edit/H8K0ISdhGrb5HirrX2MG?p=preview
我所说的更新功能,当用户点击一个栏上。我创建了一个removefromarray
函数来返回数据对象减去绑定到单击栏的数据。 :
d3.tsv("CantTouchThis.tsv",function(d,i){
d.FieldGoals = +d.FieldGoals;
return d;
}, function(error,data){
if (error) throw error;
y.domain([0,d3.max(data, function(d){return d.FieldGoals})]);
x.domain(data.map(function(d){return d.Player}));data used as the x attribute
function update(indx){
var selection = g.selectAll(".bars")
.data(data.removefromarray(indx), function(d){console.log('d');console.log(d); return d}) //printing d shows the previous bars and new bars are being returned, I suspect this may be causing the problem, but not sure
selection.enter().append("rect")
.attr("class","bars")
.attr("width",function(d){return x.bandwidth()})
.attr("x",function(d){return x(d.Player)})
.attr("height",function(d){return height - y(d.FieldGoals)})
.attr("y",function(d){return y(d.FieldGoals)})
.on("click",function(d,i){update(i);});
console.log(selection.enter())
console.log(selection.exit())
selection.exit().remove()
}
你想要点击的栏被删除或不可见? “消失”似乎非常大 – Dummy