我正在創建一個簡單的條形圖並試圖使其響應用戶點擊。被點擊的欄應該消失。所有似乎都在工作,除了點擊第一個酒吧,最終酒吧消失。我完全不知道爲什麼會出現這種情況,非常感謝任何幫助。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