![](/img/trans.png)
[英]Why create arrays of properties in D3 Force-Directed Graph example?
[英]modifying the d3 force-directed graph example
我是javascript和D3.js的新手,我试图了解它是如何工作的。 我一直在玩强制导向图示例: http : //bl.ocks.org/mbostock/4062045
我想要做的是将JSON链接从数组编号更改为节点名称。 我正在尝试可视化一个小型网络拓扑,我已经设置了所有节点邻居。 这是我想要使用的JSON数据:
{
"nodes":[
{"name":"stkbl0001","group":1},
{"name":"stkbl0002","group":1},
{"name":"stkbl0003","group":1},
{"name":"stkbl0004","group":1},
{"name":"stkbl0005","group":1}
],
"links":[
{"source":"stkbl0001","target":"stkbl0005","value":3},
{"source":"stkbl0002","target":"stkbl0005","value":3},
{"source":"stkbl0003","target":"stkbl0005","value":3},
{"source":"stkbl0004","target":"stkbl0005","value":3}
]
我真的不知道如何改变D3代码将所有这些结合在一起。 我没有看到获取数组编号的部分并将其粘合在一起作为链接。 这可能是一个愚蠢的问题,但它会帮助我很多!
编辑:
这是我到目前为止基于Lars Kotthoff输入的javascript代码:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("mini.json", function(error, graph) {
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
var nodeMap = {};
nodes.forEach(function(d) { nodeMap[d.name] = d; });
links.forEach(function(l) {
l.source = nodeMap[l.source];
l.target = nodeMap[l.target];
})
node.append("title")
.text(function(d) { return d.name; });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
});
</script>
这在第55行(nodes.forEach(function(d){nodeMap [d.name] = d;});)失败并出现错误:
Uncaught ReferenceError: nodes is not defined
此链接链接到基于您的示例的工作示例。
关键代码放在强制布局初始化之前:
var nodeMap = {};
graph.nodes.forEach(function(d) { nodeMap[d.name] = d; });
graph.links.forEach(function(l) {
l.source = nodeMap[l.source];
l.target = nodeMap[l.target];
})
force.nodes(graph.nodes)
.links(graph.links)
.start();
这样您就可以使用与原始格式相同的方式使用您的数据格式(网上的许多示例都遵循原始格式,因此您可以将其中的许多示例适应您的格式而不会出现问题)。
(由于jsfiddle的限制,我的示例中没有使用json文件;相反,函数getData()用于返回数据;但这对于您的问题并不重要;您也可以将此解决方案与json文件一起使用)
希望这可以帮助。
D3提供了两种为力布局指定链接源和目标的方法。 您链接到的示例中使用的第一个是提供节点数组中节点的索引。 强制布局启动后,将替换为对实际节点的引用。 第二个是明确地提供对实际节点的引用。
要按名称引用节点,您需要一些允许您解析该引用的内容。 例如:
var nodeMap = {};
graph.nodes.forEach(function(d) { nodeMap[d.name] = d; });
那你可以做
graph.links.forEach(function(l) {
l.source = nodeMap[l.source];
l.target = nodeMap[l.target];
})
您当然也可以使用它来定义开头的links
:
"links":[
{"source":nodeMap["stkbl0001"],"target":nodeMap["stkbl0005"],"value":3}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.