Javascript d3第二个图形杀死第一个图形';s部队布局

Javascript d3第二个图形杀死第一个图形';s部队布局,javascript,svg,d3.js,force-layout,Javascript,Svg,D3.js,Force Layout,当我想显示多个网络可视化图形时,我在使用d3.js可视化网络库时遇到一些问题。 因为我画了一个新的图表,只有最后一个有它的力布局工作,我无法找出哪里出了问题 以下是JSFIDLE: 以下是我的资料来源: HTML <div id="graph1"></div> <div id="graph2"></div> JS #graph1, #graph2 { width: 250px; height: 250px; border

当我想显示多个网络可视化图形时,我在使用d3.js可视化网络库时遇到一些问题。 因为我画了一个新的图表,只有最后一个有它的力布局工作,我无法找出哪里出了问题

以下是JSFIDLE:

以下是我的资料来源:

HTML

<div id="graph1"></div>
<div id="graph2"></div>
JS

#graph1, #graph2 {
    width: 250px;
    height: 250px;
    border: 1px solid #000;
}
.link {
    stroke: #626262;
    strokeWidth: 2px;
}
var graph = {};

function myGraph(el) {
    this.link = {};
    this.node = {};
    this.container = el;

    // Add and remove elements on the graph object
    this.addNode = function (id) {
        nodes.push({"id":id});
        update();
    };

    this.removeNode = function (id) {
        var i = 0;
        var n = findNode(id);
        while (i < links.length) {
            if ((links[i]['source'] == n)||(links[i]['target'] == n))
            {
                links.splice(i,1);
            }
            else i++;
        }
        nodes.splice(findNodeIndex(id),1);
        update();
    };

    this.removeLink = function (source,target){
        for(var i=0;i<links.length;i++)
        {
            if(links[i].source.id == source && links[i].target.id == target)
            {
                links.splice(i,1);
                break;
            }
        }
        update();
    };

    this.removeallLinks = function(){
        links.splice(0,links.length);
        update();
    };

    this.removeAllNodes = function(){
        nodes.splice(0,links.length);
        update();
    };

    this.addLink = function (source, target, value) {
        links.push({"source":findNode(source),"target":findNode(target),"value":value});
        update();
    };

    var findNode = function(id) {
        for (var i in nodes) {
            if (nodes[i]["id"] === id) return nodes[i];
        };
        return null;
    };

    var findNodeIndex = function(id) {
        for (var i=0;i<nodes.length;i++) {
            if (nodes[i].id==id){
                return i;
            }
        };
        return null;
    };

    // set up the D3 visualisation in the specified element
    var w = 250,
        h = 250;

    this.vis = d3.select(el)
        .append("svg:svg")
        .attr("width", w)
        .attr("height", h)
        .attr("id","svg")
        .attr("pointer-events", "all")
        .attr("viewBox","0 0 "+w+" "+h)
        .attr("perserveAspectRatio","xMinYMid")
        .append('svg:g');

    this.force = d3.layout.force();

    var nodes = this.force.nodes(),
        links = this.force.links();

    self = this;
    var update = function () {
        self.link = self.vis.selectAll("line")
                .data(links, function(d) {
                    return d.source.id + "-" + d.target.id; 
                });

        self.link.enter().append("line")
            .attr("id",function(d){return d.source.id + "-" + d.target.id;})
            .attr("class","link")
            .append("title")
            .text(function(d){
                return d.value;
            });
        self.link.exit().remove();

        self.node = self.vis.selectAll("g.node")
            .data(nodes, function(d) { 
                return d.id;
            });

        var nodeEnter = self.node.enter().append("g")
            .attr("class", "node")
            .call(self.force.drag);

        nodeEnter.append("svg:circle")
            .attr("r", 16)
            .attr("id",function(d) { return "svgNode_"+self.container+"_"+d.id;})
            .attr("class","nodeStrokeClass");

        nodeEnter.append("svg:text")
            .attr("class","textClass")
            .text( function(d){return d.id;}) ;

        self.node.exit().remove();
        self.force.on("tick", function() {
            //console.log(self.container);
            /*self.node.attr("cx", function(d) { return d.x = Math.max(d.radius, Math.min(svg[spaceId].attr('width') - d.radius, d.x)); })
                    .attr("cy", function(d) { return d.y = Math.max(d.radius, Math.min(svg[spaceId].attr('height') - d.radius, d.y)); })
                    .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
            */self.node.attr("transform", function(d) { return "translate("+d.x+","+d.y+")"; });

            self.link.attr("x1", function(d) { return d.source.x; })
                .attr("y1", function(d) { return d.source.y; })
                .attr("x2", function(d) { return d.target.x; })
                .attr("y2", function(d) { return d.target.y; });
        });

        // Restart the force layout.
        self.force
            .gravity(.06)
            .distance(100)
            .charge(-300)
            .size([w, h])
            .start();
    };
    // Make it all go
    update();
}
graph["#graph1"] = new myGraph("#graph1");
graph["#graph1"].addNode('A');
graph["#graph1"].addNode('B');
graph["#graph1"].addNode('C');
graph["#graph1"].addLink('A','B','10');
graph["#graph1"].addLink('A','C','8');
graph["#graph1"].addLink('B','C','15');
setTimeout(function() {
    graph["#graph2"] = new myGraph("#graph2");
    graph["#graph2"].addNode('D');
    graph["#graph2"].addNode('E');
    graph["#graph2"].addNode('F');
    graph["#graph2"].addLink('D','E','10');
    graph["#graph2"].addLink('D','F','8');
    graph["#graph2"].addLink('E','F','15');
}, 2000);
var图={};
函数图(el){
this.link={};
this.node={};
this.container=el;
//在图形对象上添加和删除元素
this.addNode=函数(id){
nodes.push({“id”:id});
更新();
};
this.removeNode=函数(id){
var i=0;
var n=findNode(id);
而(i
self = this;
缺少一个
var
关键字。没有它,
self
被分配到全局
window
范围,而不是本地
myGraph
范围。在第二次运行
myGraph
构造函数时,第一次
myGraph
窗口。self
被新值覆盖。因此
myGraph>和
中的事件对象引用第二个
自身
,这会导致断裂

您可能希望启用,以便编译器将对这种严重可跟踪的错误发出警告