D3力有向图问题:节点堆叠在坐标(0,0)
D3 force directed graph issue: nodes are stacked at coordinate (0,0)
我正在制作d3力定向图,它工作得很好(这是codepen上的工作版本),直到我尝试为节点添加标签。
根据我的理解,节点必须由'g'元素包裹,但是当我尝试它时,所有的节点都堆叠在左上角。我对JavaScript非常不熟悉,显然我遗漏了一些东西。如果有人能帮我改正我的错误,我会很感激的。我从类似的主题尝试解决方案:标签不会显示在d3受力图
var dataURL = "https://raw.githubusercontent.com/Aeternia-ua/temp1/c50fa778c12c6d355fe08b54cb3f1ce24acfa4e9/graphtest.json";
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20c);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) {
return d.id;
}))
.force("charge", d3.forceManyBody().strength(-150))
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json(dataURL, function(error, graph) {
if (error) throw error;
simulation.nodes(graph.nodes);
simulation.force("link").links(graph.links);
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links)
.enter().append("line");
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(graph.nodes)
.enter().append("circle")
//Setting node radius by group value. If 'group' key doesn't exist, set radius to 8
.attr("r", function(d) {
if (d.hasOwnProperty('group')) {
return d.group * 2;
} else {
return 9;
}
})
//Colors by 'group' value
.style("fill", function(d) {
return color(d.group);
})
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.append("svg:title")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) {
return d.id
});
var labels = svg.append("g")
.attr("class", "label")
.selectAll("text")
.data(graph.nodes)
.enter().append("text")
.attr("dx", 6)
.attr("dy", ".35em")
.style("font-size", 10)
.text(function(d) {
return d.id
});
simulation
.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
});
function ticked() {
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;
});
labels
.attr("x", function(d) {
return d.x;
})
.attr("y", function(d) {
return d.y;
});
}
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
codepen的完整代码
将函数ticked()
移动到d3.json
回调中:
d3.json(dataURL, function(error, graph) {
//...
function ticked(){
//...
};
};
同时,将链接的类别从.links
更改为.link
:
var link = svg.append("g")
.attr("class", "link")
这是你更新的CodePen: http://codepen.io/anon/pen/ORejme?editors=0110
link
、node
和labels
是在d3.json()
调用中的匿名函数的本地定义的,因此在ticked()
中不可用。
相关文章:
- 如何在d3力有向图中最初限制节点数
- 如何在Vis JS有向图中逐个显示节点
- D3有向图中链接动画的速度问题
- D3 AngularJs力有向图
- 在有向图中循环SVG圆
- 强制有向图和本地存储
- 如何使用dagred3.js(javascript库)将点击事件添加到有向图中
- 在Javascript Infovis工具/强制有向图中改变节点形状
- 如何防止力有向图中链接的重叠
- 在d3js的力有向图的链接上显示工具提示
- 缩放到D3强制有向图上的点击节点
- D3力有向图问题:节点堆叠在坐标(0,0)
- 从d3有向图的数组中移除重复的边
- 什么是“价值”?用d3表示力有向图
- 加载JSON对象到D3力有向图
- D3 js力有向图-突出两点之间的路径
- 带有SVG和Javascript的交互式有向图
- 在javascript中从边列表制作有向图
- 为什么我不能得到D3强制有向图链接标签的工作
- D3.js箭头有向图:添加节点/链接的飞行