Javascript D3根据需要隐藏节点

Javascript D3根据需要隐藏节点,javascript,d3.js,Javascript,D3.js,我正在寻找一种根据节点集中的变量显示和隐藏节点的简单方法 我有3个复选框FTSE 100 FTSE 250 FTSE ALL Share,我的节点有一个可变索引,即FTSE 100、250、allshare,因此每当用户选择FTSE 100时,我只想显示具有FTSE索引的节点,以此类推,并显示其余复选框选项 数据示例: var dataset = {"nodes":[{"fullName":"Ultra Electronics Holdings plc","index":"FTSE 250"},

我正在寻找一种根据节点集中的变量显示和隐藏节点的简单方法

我有3个复选框FTSE 100 FTSE 250 FTSE ALL Share,我的节点有一个可变索引,即FTSE 100、250、allshare,因此每当用户选择FTSE 100时,我只想显示具有FTSE索引的节点,以此类推,并显示其余复选框选项

数据示例:

var dataset = {"nodes":[{"fullName":"Ultra Electronics Holdings plc","index":"FTSE 250"},{"fullName":"Unilever PLC","index":"FTSE 100"},{"fullName":"United Utilities Group PLC","index":"FTSE 100"},{"fullName":"Victrex plc","index":"FTSE 250"},{"fullName":"Vectura Group plc","index":"FTSE 250"},{"fullName":"Vp plc","index":"FTSE All-Share"},{"fullName":"VPC Specialty Lending Investments Plc","index":"FTSE All-Share"},{"fullName":"Wincanton plc","index":"FTSE All-Share"},{"fullName":"Workspace Group plc","index":"FTSE 250"},{"fullName":"William Hill plc","index":"FTSE 250"},{"fullName":"Witan Pacific Investment Trust PLC","index":"FTSE All-Share"},{"fullName":"Worldpay Group plc","index":"FTSE 100"},{"fullName":"Witan Investment Trust plc","index":"FTSE 250"},{"fullName":"Whitbread PLC","index":"FTSE 100"},{"fullName":"Worldwide Healthcare Trust PLC","index":"FTSE 250"},{"fullName":"Xaar plc","index":"FTSE All-Share"},{"fullName":"Zoopla Property Group Plc","index":"FTSE 250"}],"edges":[{"source":0,"target":68,"officers":["PARKER, Thomas, Sir"]},{"source":0,"target":147,"officers":["STEVENS, Anne"]},{"source":0,"target":286,"officers":["PARKER, Thomas, Sir"]},{"source":0,"target":341,"officers":["GROTE, Byron"]},{"source":0,"target":363,"officers":["GROTE, Byron"]},{"source":1,"target":2,"officers":["ABERDEEN ASSET MANAGEMENT PLC","YOUNG, Hugh"]},{"source":1,"target":7,"officers":["GILBERT, Martin","YOUNG, Hugh"]},{"source":1,"target":23,"officers":["ABERDEEN ASSET MANAGEMENT PLC"]},{"source":1,"target":67,"officers":["YEA, Philip"]},{"source":1,"target":98,"officers":["ABERDEEN ASSET MANAGEMENT PLC"]},{"source":1,"target":102,"officers":["ABERDEEN ASSET MANAGEMENT PLC"]},{"source":1,"target":150,"officers":["YEA, Philip"]},{"source":1,"target":254,"officers":["ABERDEEN ASSET MANAGEMENT PLC"]},{"source":1,"target":255,"officers":["ABERDEEN ASSET MANAGEMENT PLC"]}]};
d3代码呢

var w = 1200;
var h = 800;

var viewBoxX = 0, viewBoxY = 0;

var force = d3.layout.force()
             .nodes(dataset.nodes)
             .links(dataset.edges)
             .size([w, h])
             .linkDistance([100])
             .charge([-240])
             .gravity(0.07)
             .start();

var colors = d3.scale.category10();

var svg = d3.select("#networkGraph")
            .append("svg")
            .attr("width", w)
            .attr("height", h)
            .attr("viewBox", "0 0 "+w+" "+h);

svg.append('rect')
              .classed('bg', true)
              .attr('stroke', 'transparent')
              .attr('fill', 'transparent')
              .attr("x", 0)
              .attr("y", 0)
              .attr('width', w)
              .attr('height', h)
              .call(zoom);

var container = svg.append("g")
                .classed("node-area", true)
                .attr("transform", "translate(0,0)");

var edge = container.selectAll(".edge")
            .data(dataset.edges)
            .enter()
            .append("g")
            .attr("class", "edge")
            .append("line")
            .attr("class", "link-edge")
            .style("stroke", function(d){return "rgb(0,"+(168-(officersScale(d.officers.length)*20))+","+(168-(officersScale(d.officers.length)*20))+")";})
                .style("stroke-width", function(d){return officersScale(d.officers.length);});
    var edgeText = container.selectAll(".edge")
                .append("text")
                .attr("dy", ".35em")
                .each(function(d){
var officers = ''; 
                                for (index = 0; index < d.officers.length; ++index) {
                                    d3.select(this).append("tspan")
                                        .text(d.officers[index])
                                        .attr("dy", index ? "1.2em" : 0)
                                        .attr("text-anchor", "middle")
                                        .attr("x", 0);
                                }
                            });

var node = container.selectAll(".node")
    .data(dataset.nodes)
    .enter()
    .append("g")
    .attr("class", "node")
    .call(force.drag);

node.append("circle")
    .attr("r", 15)
    .style("fill", function(d,i){ 
    return colors(i)
        })
    .call(force.drag);

node.append("text")
    .attr("dx", 0)
    .attr("dy", "0.35em")
    .attr("font-weight", 600)
    .attr("text-anchor", "middle") 
    .style("pointer-events", "none") 
    .text(function(d){ return d.name});
HTML位:

<div id="networkGraph">
        <div id="menuLeft">
            <button onclick="zoomFit(0.95, 500)">Center</button>
        </div>
        <div id="menuRight">
            <fieldset>
                <div>
                    <input type="checkbox" id="FTSE100"> 
                    <label for="FTSE100">FTSE 100</label>
                </div>
                <div>
                    <input type="checkbox" id="FTSE250">
                    <label for="FTSE250">FTSE 250</label>
                </div>
                <div>
                    <input type="checkbox" id="FTSEA-S">
                    <label for="FTSEA-S">FTSE All-Share</label>
                </div>
            </fieldset>
        </div>
    </div>

居中
富时100指数
富时250指数
富时全股
以及该程序实际外观的图片:

<div id="networkGraph">
        <div id="menuLeft">
            <button onclick="zoomFit(0.95, 500)">Center</button>
        </div>
        <div id="menuRight">
            <fieldset>
                <div>
                    <input type="checkbox" id="FTSE100"> 
                    <label for="FTSE100">FTSE 100</label>
                </div>
                <div>
                    <input type="checkbox" id="FTSE250">
                    <label for="FTSE250">FTSE 250</label>
                </div>
                <div>
                    <input type="checkbox" id="FTSEA-S">
                    <label for="FTSEA-S">FTSE All-Share</label>
                </div>
            </fieldset>
        </div>
    </div>