最新集群力布局示例的问题 [英] problems with latest cluster force layout example
问题描述
根据这项工作: http://bl.ocks.org/mbostock/7882658
如果我使用自动生成的数据的JSON.stringify()输出替换自动节点创建,则...
If I substitute the automatic nodes creation by a JSON.stringify() output of the automatically generated data like this...
var nodes = [
{"cluster":2,"radius":1.6180680659922448},
{"cluster":0,"radius":3.3575295077569},
{"cluster":1,"radius":0.9569281165554346},
{"cluster":3,"radius":10.7245554165012}
];
...我得到一个异常无法读取未定义的属性x p>
...I get an exception "cannot read property x of undefined" on the line:
var x = d.x - cluster.x,
这是在集群(alpha)函数内。因此,显然,自动生成数据的d3.map函数将某些东西放在JSON字符串化还没有捕获的结构中?也许我只是俯瞰一些简单的...帮助是赞赏。谢谢!这里是一个小提琴帮助: http://jsfiddle.net/Nivaldo/FJ3qq/1/
This is inside the cluster(alpha) function. So, apparently the d3.map function that automatically generates the data is putting something in the structure that the JSON stringification has not caught? Maybe I am just overlooking something simple...help is appreciated. Thanks! Here is a fiddle to help out: http://jsfiddle.net/Nivaldo/FJ3qq/1/
我注释掉了无效的代码。另外,另一个细节,它似乎不像原来的代码,因为我离开它(除了我减少了集群和节点的数量)实际上是处理正确数量的不同集群。
I commented out the code that is not working. Also, another detail, it does not seem like the original code as i left it (except that i reduced the count of clusters and nodes) is actually handling the right number of distinct clusters. It should paint 4 different ones but is only painting with 3 colors.
推荐答案
问题是节点
不是唯一需要初始化的数据结构 - 集群
也需要。具体地,将特定节点分配给特定的簇索引。
The problem is that nodes
is not the only data structure that needs to be initialised -- clusters
needs to be as well. In particular, specific nodes are assigned to specific cluster indices. If you don't do that, things will break.
要修复,请执行
nodes.forEach(function(d) { clusters[d.cluster] = d; });
完成jsfiddle 此处。
Complete jsfiddle here.
这篇关于最新集群力布局示例的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!