私は単純な棒グラフを作成し、ユーザーのクリックに反応させようとしています。クリックされたバーは消えています。最初のバーをクリックすると、最後のバーが消えてしまいます。私はこれがなぜそうであるのか完全に迷っており、本当に助けに感謝します。d3js:exitメソッドを使用して最初のバーが消えないのはなぜですか?
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