我正在使用 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”节点的链接。 相反,我想要类似“继续语句”的内容,跳转到同一“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]
};
});
关于javascript - d3.js 继续条件加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20472348/