d3.js:“Can not read property'weight'of undefined”当手动定义两个节点和链接的力布局 [英] d3.js: "Cannot read property 'weight' of undefined" when manually defining both nodes and links for force layout

查看:1099
本文介绍了d3.js:“Can not read property'weight'of undefined”当手动定义两个节点和链接的力布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试以这种方式同时设置节点和链接:

I tried setting both nodes and links at the same time this way:

var force = d3.layout.force()
    .size([w, h])
    .nodes(nodes)
    .links(connections)
    .start();

nodes = [{"name":"data_base_id", "kind":"subgenre"},...]
connections = [{"source":"name_of_node", "target":"name_of_other_node"},...]



我有数据可能没有连接,所以有必要定义节点,以便所有的节点都被渲染。定义类型很容易。
但我收到此错误;

I have data that may not have connections, so it is necessary to defined the nodes, so that all of the nodes get rendered. And defining the genres is pretty easy. but I get this error;

Cannot read property 'weight' of undefined

当我注释掉.links(连接)图形渲染(插入一串点散落在...)连接/链接与d3合作?

And when I comment out .links(connections) the graph renders (juts a bunch of dots scattered throughout...) How do I get the connections / links to cooperate with d3?

我正在阅读文档,显然源和目标必须是节点数组中的节点的INDEXES。有没有反过来改变呢?因此,我可以使用一个节点的名称而不是它在数组中的索引?

I was reading the docs, and apparently the source and target have to be INDEXES of the nodes in the nodes array. Is there anyway to change that? So, I can use the name of a node rather than the index it has in an array?

推荐答案

强制布局使用边权重计算布局。尝试为所有连接添加虚拟weight:1

The force-directed layout uses edge weights to calculate the layout. Try adding a dummy "weight":1 to all of your connections.

初始化链接的代码像这样:

The code that initializes the links looks like this:

links.forEach(function(d) {
    if (typeof d.source == "number") { d.source = nodes[d.source]; }
    if (typeof d.target == "number") { d.target = nodes[d.target]; }
});

推测你可以调整它(在d3源代码中)使用任何属性/类型。

Presumably you could tweak that (in the d3 source) to use any property/type.

这篇关于d3.js:“Can not read property'weight'of undefined”当手动定义两个节点和链接的力布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆