Update selection not working

后端 未结 1 352
萌比男神i
萌比男神i 2021-01-22 10:00

I\'m new to D3 and I am trying to display a simple d3 bar chart that changes which data attribute it is visualizing based on a dropdown menu - the data remains the same and I am

1条回答
  •  春和景丽
    2021-01-22 10:10

    Actually, you don't have an update selection in your code.

    For having an update selection, break up your "enter" selection, like this:

    //this is the update selection
    var bars = g.selectAll(".bar")
        .data(data, function(d) {
            return d._id;
        });
    
    //and the remainder is the enter selection
    bars.enter().append("rect")
        .attr("class", "bar")
        .attr("height", 0)
        .attr("y", height);
    

    Also, it's worth mentioning that, since you don't have an update selection in your code, this...

    bars.exit().remove();
    

    ... is useless.

    0 讨论(0)
提交回复
热议问题