2013-12-09 86 views
1

我正在用d3.js ad force布局,但我遇到这个问题: TypeError:c.target未定义,我知道这是什么,以及如何删除它,但我不想d3.js继续条件加载数据

d3.json("myfile.json", function(graph) { 

    var nodeMap = {}; 
    graph.nodes.forEach(function(d) { nodeMap[d.name] = d; }); 
    graph.links = graph.links.map(function(d) { 
    return { 
     source: nodeMap[d.source] , 
     target: nodeMap[d.target] , 
     value: d.value 
      }; 
    }); 

force 
    .nodes(graph.nodes) 
    .links(graph.links) 
    .on("tick", tick) 
    .start(); 

这是我的代码来加载json数据和建立图形。

if i put (|| 0 )in : 

         source: nodeMap[d.source] || 0, 
         target: nodeMap[d.target] ||0, 

这打破了代码,并没有为“d”节点绘制链接。 相反,我想要的东西像“继续声明”,跳转到相同的“d”节点的下一个c.target。

有人可以帮助我

+0

这不是我清楚你的要求。你想知道如何跳过没有定义目标的链接吗? –

+0

做一个jsfiddle和分享...这将有助于帮助 – sam

+0

是的。例如 节点:[{名称:A},{名称:B}],链接:[{source:A,target:B},[source:A,target:C]] 在这种情况下,代码报告错误c.target未定义,但我只想要A-B交互 – andrea

回答

0

您可以过滤掉参考节点不存在链接:

graph.links = graph.links.filter(function(d) { 
    return nodeMap[d.source] && nodeMap[d.target]; 
}).map(function(d) { 
    return { 
     source: nodeMap[d.source] , 
     target: nodeMap[d.target] 
    }; 
}); 
+0

谢谢Lars,但是这个工作原理与|| 0的解决方案完全一样; 结果是,我有一个半节点和链接图,另一半没有链接。 我认为,因为如果节点A有5个“目标”,第一个不匹配其他4跳过。我方面看到其他链接 – andrea

+0

此代码将收集源和目标都定义的所有链接。如果其中一个未定义,则不能有链接。 –

+0

好的,谢谢,原谅我是第一次与d3 e js。我如何找到缺少的c.target?在哪个位置我必须把console.log()。 – andrea