JavaScript错误“Uncaught TypeError:无法调用未定义的方法'push'”D3.js

时间:2013-10-03 12:39:25

标签: javascript r d3.js

我正在使用R包d3Network来生成D3.js强制布局图。 d3Network产生的代码给我一个“未捕获的TypeError:无法读取未定义的属性'权重'错误。有谁知道可能导致这种情况的原因?

我正在使用示例(但使用不同的数据集):

http://bl.ocks.org/mbostock/4062045

http://christophergandrud.github.io/d3Network/(d3ForceDirected部分)

构建图表的代码是:

var force = d3.layout.force()
.nodes(d3.values(nodes)) 
.links(links) 
.size([width, height]) 
.linkDistance(50) 
.charge(-120) 
.on("tick", tick) 
.start(); 

链接:

var link = svg.selectAll(".link")
.data(force.links())
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });

数据是:

var links = [ { "source" : 1, "target" : 11, "value" : 2,}, 
            { "source" : 1, "target" : 21, "value" : 2 }, 
            { "source" : 1, "target" : 20, "value" : 1 }, 
            { "source" : 1, "target" : 19, "value" : 2 }, 
            { "source" : 2, "target" : 16, "value" : 1 }, 
            { "source" : 2, "target" : 14, "value" : 1 }, 
            { "source" : 2, "target" : 13, "value" : 1 }, 
            { "source" : 2, "target" : 15, "value" : 1 }, 
            { "source" : 2, "target" : 18, "value" : 1 }, 
            { "source" : 3, "target" : 14, "value" : 1 }, 
            { "source" : 3, "target" : 17, "value" : 1 }, 
            { "source" : 3, "target" : 21, "value" : 1 }, 
            { "source" : 3, "target" : 19, "value" : 1 }, 
            { "source" : 5, "target" : 13, "value" : 1 },
            { "source" : 5, "target" : 12, "value" : 1 },
            { "source" : 5, "target" : 10, "value" : 1 }, 
            { "source" : 5, "target" : 19, "value" : 1 }, 
            { "source" : 5, "target" : 22, "value" : 1 }, 
            { "source" : 6, "target" : 21, "value" : 1 }, 
            { "source" : 6, "target" : 20, "value" : 1 }, 
            { "source" : 7, "target" : 11, "value" : 1 }, 
            { "source" : 7, "target" : 19, "value" : 1 }, 
            { "source" : 8, "target" : 15, "value" : 1 }, 
            { "source" : 9, "target" : 17, "value" : 1 }, 
            { "source" : 9, "target" : 22, "value" : 1 } ] ; 
 var nodes = [{ "name" : "c6_Sports", "group" : 1 }, 
            { "name" : "c8_Talk", "group" : 1 }, 
            { "name" : "c10_TV", "group" : 1 }, 
            { "name" : "c3_Movies", "group" : 1 }, 
            { "name" : "c9_Together", "group" : 1 }, 
            { "name" : "c2_Games", "group" : 1 }, 
            { "name" : "c1_ConsoleGames", "group" : 1 }, 
            { "name" : "c5_Outside", "group" : 1 }, 
            { "name" : "c4_Music", "group" : 1 },
            { "name" : "Joe", "group" : 1 }, 
            { "name" : "Fiona", "group" : 1 }, 
            { "name" : "Jill", "group" : 1 }, 
            { "name" : "Bob", "group" : 1 }, 
            { "name" : "Bertha", "group" : 1 }, 
            { "name" : "Jimmy", "group" : 1 }, 
            { "name" : "Amanda", "group" : 1 }, 
            { "name" : "Dmitry", "group" : 1 }, 
            { "name" : "Scott", "group" : 1 }, 
            { "name" : "Patricia", "group" : 1 }, 
            { "name" : "Mike", "group" : 1 }, 
            { "name" : "Lois", "group" : 1 }, 
            { "name" : "Tim", "group" : 1 } ] ; 

1 个答案:

答案 0 :(得分:6)

作为“source”和“target”links给出的数字是nodes数组的索引,应该从0开始。你从1开始,因此最高的数字不存在并给出你这个错误。