【发布时间】:2013-10-25 12:12:19
【问题描述】:
我想使用 d3 中的强制布局算法来可视化有向图。 节点应在矩形like this 内显示为它们的名称。 我的问题是计算箭头应该指向的矩形外的位置。
我认为应该在 tick() 函数中完成。为了避免像角度计算这样的极端努力,我可以使用截距定理。
我不知道如何获取将使用属性设置的边的参数,我找不到这方面的示例。
var force = d3.layout.force().nodes(dataset.nodes).links(dataset.edges)...
var edges = svg.selectAll("line").data(dataset.edges).enter().append("line")...
var nodes = svg.selectAll("text").data(dataset.nodes).enter().append("text")...
force.on("tick", function(){
// Why does the following function not work? How to implement this correctly?
edges.attr(function(d){
var x1 = d.source.x
var y1 = d.source.y
var x2 = d.target.x
var y2 = d.target.y
// ... calulate new source and targetcoordinates ... I can do this myself
return {
"x1": newSourceX,
"y1": newSourceY,
"x2": newTargetX,
"y2": newTargetY
}
});
});
如何实现每条边提取任意源参数和目标参数并保存新位置的功能?
您认为这是性能最佳的解决方案吗?
【问题讨论】:
-
不确定你的意思。您不需要计算箭头指向自己的位置,例如参见this example.
-
我想将文本用作this example 显示的节点。箭头必须指向文本元素之外,但每个文本元素都有自己的 witdh。确切的问题是从 tick() 函数内的每个边缘获取源和目标坐标。
-
你见过this question吗?
标签: javascript graph d3.js force-layout