d3.js继续条件加载数据



我正在使用d3.js广告力布局,但我遇到了这个问题:TypeError:c.target未定义,我知道这是什么,以及如何删除它,但我不想要

 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"节点绘制链接。相反,我想要像"continue语句"这样的语句,它会跳转到同一个"d"节点的下一个c.target。

有人能帮我吗

您可以筛选出引用不存在的节点的链接:

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]
    };
});

最新更新