我想使用 D3.js 将此 JSON 中的数据表示为节点链接。我是 JavaScript 和 D3.js 的新手。我有 3 种类型的数据,我想在这 3 种类型的数据之间建立层次结构。父级 > 源 > 子级,我想将每个父级放置在源之上并将每个父级链接到源,每个子级都应位于源下方并将它们链接到源:
script.js
var width = 960,
height = 500;
// i don't really understand what this does
// except the .linkDistance - gives the dimension of the link
var force = d3.layout.force()
.size([width, height])
.charge(-400)
.linkDistance(40)
.on("tick", tick);
// Drag the nodes
var drag = force.drag()
.on("dragstart", dragstart);
//Appends the svg - the place where i draw all my items
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
// Select all the links and nodes , from an array ? i don't really get it
var link = svg.selectAll(".link"),
node = svg.selectAll(".node");
// The function where i take the data from the JSON
d3.json("graph.json", function(error, graph) {
if (error) throw error;
force
.nodes(graph.nodes)
.links(graph.links)
.start();
link = link.data(graph.links)
.enter().append("line")
.attr("class", "link");
node = node.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 12)
.on("dblclick", dblclick)
.call(drag);
});
// Here is the function where i should asign the position of the nodes and the links
// This is the most problematic and i really don't understand it
function tick(){}
// The function to fix and to clear the fix from a node
function dblclick(d) {
d3.select(this).classed("fixed", d.fixed = false);
}
function dragstart(d) {
d3.select(this).classed("fixed", d.fixed = true);
}
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<meta charset="utf-8">
<title>D3 Test</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="script.js"></script>
</head>
</body>
style.css
.link {
stroke: #777;
stroke-width: 2px;
}
.nodes circle{
cursor: move;
fill: #ccc;
stroke: #000;
stroke-width: 1.5px;
}
.node.fixed {
fill: #f00;
}
graph.json
{
"nodes":[
{
"id": "Source" , "group" :0
},
{
"id":"Parent_1" , "group" : 1
},
{
"id": "Parent_2" , "group" :1
},
{
"id": "Parent_3" , "group" :1
},
{
"id":"Child_1" , "group":2
},
{
"id":"Child_2" , "group":2
},
{
"id":"Child_3" , "group":2}
],
"links":[
{
"source":"Source","target":"Parent_1"
},
{
"source":"Source","target":"Parent_2"
},
{
"source":"Source","target":"Parent_3"
},
{
"source":"Source","target":"Child_1"
},
{
"source":"Source","target":"Child_2"
},
{
"source":"Source","target":"Child_3"
},
]
}
如果有人有时间和心情向我解释如何格式化 JSON,以便我可以更有效地使用它,并向我解释如何逐步使用 d3 创建节点链接图,或者给我一个演示并解释我将非常感激每一段代码。 如果我提出问题的方式有问题或者有不清楚的地方,请说出来,以便我可以编辑它。谢谢!
最佳答案
好吧,简而言之,力将所有节点放置在 svg 上,并赋予它们很多属性,一些属性与数据相关,一些与力图的行为相关。
一般来说,节点会互相排斥,但可以通过设置 d3.forceManyBody 的强度来改变这种行为。
链接将在节点之间产生力,可用于将节点拉到一起,或将它们保持在设定的距离处。
通常,力会将所有节点吸引到图表的中心,但您可以将它们设置为吸引到任何地方。在您的情况下,您希望顶部中心的一个点(例如 SVG 下方 25%)来吸引父节点(组 1),并需要一个底部中心点(SVG 下方 75%)来吸引子节点(组 2) )。可以将源节点设置为居中;
var position = d3.forceSimulation(graph.nodes).force("charge", d3.forceManyBody()).force("x", d3.forceCenter(width / 2, height/ 2)).force("collide", d3.forceCollide(15 * R));
nodes.each(function(d) {
if (d.group == 0) {
d.fx = wid / 2;//fix X value
d.fy = wid / 2//fix Y value
}
});
如果你可以设置一个jsfiddle或类似的东西并让一些东西工作,我也许能够看到你被困在哪里(看起来你的订单有点偏离在数据加载之前创建链接)。另外,您正在加载 d3 版本 3,当您开始时,您不妨立即切换到版本 4。
编辑:无论如何,这是我的理解,我认为我上面链接的资源可能更好!
关于javascript - 我应该如何格式化 JSON 并向我解释一些基本的 d3 图节点链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45608409/