Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding visible elements on click D3.js

I am using D3.js to dynamically create svg elements. When I click a circle, I run this function:

       .on("click", function(d) { 
            d3.select(this).select("rect").transition().duration(900).style("visibility", "visible");
            d3.select(this).selectAll("tspan").transition().duration(900).style("visibility", "visible");
        })

Aside from the fact that my transitions aren't working, clicking on the circle shows that circles children rectangle and tspan, all is well. However if I click another circle, the new rectangle and tspan show but I need the current one to hide. Wondering what the best/most efficient way to do this is with D3

like image 544
JordanBarber Avatar asked Mar 06 '16 15:03

JordanBarber


1 Answers

If your circles have a class, say ".circle", you can do something like this:

.on("click", function(d) {
    var clickedCircle = this;
    d3.selectAll(".circle").each(function() {
        var currCircle = this;
        d3.select(this).select("rect").transition().duration(900).style("visibility", function() {
            return (currCircle === clickedCircle) ? "visible" : "hidden";
        });
    });
});

And obviously do the same for your tspan element. This will hide all but your currently clicked circle.

like image 173
James Avatar answered Nov 18 '22 00:11

James