Programatically disable series on nvd3 Horizontal Multi-Bar Chart

回眸只為那壹抹淺笑 提交于 2019-12-06 06:02:15

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!