我想使用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一步一步创建节点链接图或给我一个演示和解释每一块代码我都会非常感激。 如果我问这个问题的方式有问题或有什么不清楚请说出来我可以编辑它。谢谢!
答案 0 :(得分:0)
好吧,简而言之,力将所有节点放在一个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.
编辑:无论如何这是我的理解,我认为上面链接的资源可能要好得多!