score:8

Accepted answer

To the best of my knowledge, you cannot do this directly with NVD3 (at least not without modifying the source). However, you can trigger this programatically by simulating the click on the legend element:

d3.select("g.nv-legendWrap").selectAll("g.nv-series")
        .each(function(d) {
            this.dispatchEvent(new Event("click"));
        });

Assuming that your data has an attribute visible that determines whether the series should be shown or not, you can filter based on that:

d3.select("g.nv-legendWrap").selectAll("g.nv-series")
        .filter(function(d) { return d.visible == false; })
        .each(function(d) {
            this.dispatchEvent(new Event("click"));
        });

If you're using a transition to create the graph, remember to call this code when the transition has finished. Complete demo here.


Related Query