Programatically disable series on nvd3 Horizontal Multi-Bar Chart

邮差的信 提交于 2020-01-02 10:18:05

问题


The issue is that I have a lot of series on my chart and, on load, I would like only 3 selected.

Is there a way to 'disable' ( hide ) a series on nvd3 Horizontal Multi-Bar Chart? ( I'm looking for the click series behavior but programatically )

I have tried to send JSON with 'visible': false, but it not works.

var data=[{"key": "Series 1",
           "visible": false, 
           "values": [{"value": 10000.0, "label": ...

Following @shabeer90 instructions tested with:

           "disabled": true,

In this case series is disabled but don't allow to switch to enable clicking series circle.


回答1:


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.



来源:https://stackoverflow.com/questions/28938221/programatically-disable-series-on-nvd3-horizontal-multi-bar-chart

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