D3:另一个“无法读取未定义的属性”权重”。 [英] D3: Yet another "Cannot read property 'weight' of undefined"
问题描述
我正在尝试创建一个带有力的图形,它有一个转折:节点和链接数据来自外部定义的类。当我运行它时,我得到了可怕的和熟悉的消息:
I'm trying to create a force-directed graph with one twist: the node and link data comes from externally defined classes. When I run it, I get the dreaded and familiar message:
Uncaught TypeError: Cannot read property 'weight' of undefined
我已阅读 d3:无法读取d3.force实现中未定义的属性'weight'和出色的 https://stackoverflow.com / a / 15060978/558639 。但是没有喜悦。
I've read d3: Cannot read property 'weight' of undefined in d3.force implementation and the excellent https://stackoverflow.com/a/15060978/558639. But no joy.
我在 https:/中创建了一个JSFiddle /jsfiddle.net/3xcaqaLc/8/ 忠实地复制错误。
文档在链接上提到了权重属性,但是并不清楚我是否需要提供该属性。
The docs mentions a 'weights' property on the links, but it wasn't particularly clear if I needed to provide that.
我添加了 weight:每个链接的1.0
属性。
我向每个节点添加了 weight:1.0
属性。
I added a weight: 1.0
property to each node. No joy.
我发现的所有示例都显示了零链接基于索引的节点数组。取而代之的是,我使用指向节点本身的引用来构建链接。但是根据#force.nodes()
的文档,这应该可以工作(注意使用单词 may):
All the examples I found show the links constructed with zero-based indexes into the nodes array. Instead, I'm constructing the links with references to the nodes themselves. But according to the documentation for #force.nodes()
, this should work (note the use of the word 'may'):
注意:源和目标属性的值最初可能是
,它被指定为节点数组的索引;
Note: the values of the source and target attributes may be initially specified as indexes into the nodes array; these will be replaced by references after the call to start.
也许这些文档是不准确的,但是我们欢迎您提出解决方案!
Perhaps the documents are inaccurate, but solutions will be welcome!
推荐答案
真正的原因是链接 g_graph.links
是格式与 https://github.com/mbostock/d3 / wiki / Force-Layout#links
The real reason is that the link g_graph.links
is not in the correct format as mentioned in https://github.com/mbostock/d3/wiki/Force-Layout#links
以下是节点
https://github.com/mbostock/d3/wiki/Force-Layout#nodes
这就是强制布局不起作用的原因。
That is the reason why force layout is not working.
我已注释节点,并绘制了链接部分和图形,因为刻度函数正在放置节点
I have commented the nodes and link section and graph is drawn because tick function is placing the node and the link.
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([view_width, view_height])
//.nodes(g_graph.nodes)
//.links(g_graph.links)
.start();
工作示例这里
希望这会有所帮助!
这篇关于D3:另一个“无法读取未定义的属性”权重”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!