Javascript d3js强制布局未按预期工作

Javascript d3js强制布局未按预期工作,javascript,d3.js,force-layout,Javascript,D3.js,Force Layout,我一直在做一个D3JS force布局图,它并不像预期的那样工作 我已经将代码上传到JSFIDLE,可以在这里看到: 我认为它的重要部分是以下内容,即更新节点/链接的阈值: function threshold(thresh) { graph.links.splice(0, graph.links.length); for (var i = 0; i < graphRec.links.length; i++) { if (graphRec.links[

我一直在做一个D3JS force布局图,它并不像预期的那样工作

我已经将代码上传到JSFIDLE,可以在这里看到:

我认为它的重要部分是以下内容,即更新节点/链接的阈值:

function threshold(thresh) {

    graph.links.splice(0, graph.links.length);

    for (var i = 0; i < graphRec.links.length; i++) {
        if (graphRec.links[i].value > thresh) {
            graph.links.push(graphRec.links[i]);
        }
    }

    graph.nodes.splice(0, graph.nodes.length);
    for (var j = 0; j < graphRec.nodes.length; j++) {
        if (graphRec.nodes[j].value > thresh) {
            graph.nodes.push(graphRec.nodes[j]);
        }
    }

    restart();
}


//Restart the visualisation after any node and link changes

function restart() {
    node = node.data(graph.nodes);
    link = link.data(graph.links);
    link.exit().remove();
    node.exit().remove();
    node.enter().insert("circle", ".node").attr("class", "node").attr("r", function (d) {
        return d.value / 5
    }).style("fill", "steelblue").call(force.drag);
    link.enter().insert("line", ".node").attr("class", "link").call(force.drag);
    force.start();
}
功能阈值(阈值){
图链接拼接(0,图链接长度);
对于(var i=0;ithresh){
graph.links.push(graphRec.links[i]);
}
}
图.节点.拼接(0,图.节点.长度);
对于(var j=0;j阈值){
graph.nodes.push(graphRec.nodes[j]);
}
}
重启();
}
//在任何节点和链接更改后重新启动可视化
函数重新启动(){
node=node.data(graph.nodes);
link=link.data(graph.links);
link.exit().remove();
node.exit().remove();
node.enter().insert(“圆圈”,“节点”).attr(“类”,“节点”).attr(“r”,函数(d){
返回d.value/5
}).style(“填充”、“钢蓝”).call(强制拖动);
link.enter().insert(“line”,“node”).attr(“class”,“link”).call(force.drag);
force.start();
}
其思想是,随着阈值的增加,不满足该阈值的节点和链接将被删除。随后更新阈值时,应相应地添加/删除其他链接/节点

我使用了作为我试图做的事情的基础,但是不得不修改它以使节点消失,所以我的代码可能有问题

现在我发现,如果你慢慢地增加阈值,那么它工作得很好。但是,如果您只是将阈值单击到一个较高的值,然后再次更改它,则会出现一些奇怪的行为,导致图形中断。您可能会注意到节点聚集在左上角。在控制台中查看输出时显示以下错误消息:

未捕获类型错误:无法读取未定义的属性“权重”


但我无法用我的一生来解释为什么有时会发生这种情况,但不是每次都会发生。我是D3新手,所以任何帮助和建议都将不胜感激,我希望我已经提供了所有需要的信息,有人可以给我一些建议。谢谢

如果在出现错误情况时检查数据,则会丢失指向其中一个节点的链接中的
引用(未定义):

我不知道为什么,但我重写了阈值函数来处理
d3
链接和节点对象(而不是返回源json),它的行为与预期一致:

var masterNodes = graph.nodes;
var masterLinks = graph.links;
function threshold(thresh) {
    graph.nodes = [];
    masterNodes.forEach(function(d,i){
        if (d.value > thresh){
            graph.nodes.push(d);
        }
    });         
    graph.links = [];
    masterLinks.forEach(function(d,i){
        if (d.value > thresh){
            graph.links.push(d);
        }
    });    
    restart();
}
更新

var masterNodes = graph.nodes;
var masterLinks = graph.links;
function threshold(thresh) {
    graph.nodes = [];
    masterNodes.forEach(function(d,i){
        if (d.value > thresh){
            graph.nodes.push(d);
        }
    });         
    graph.links = [];
    masterLinks.forEach(function(d,i){
        if (d.value > thresh){
            graph.links.push(d);
        }
    });    
    restart();
}