如何在arbor.js中将标签添加到边它是一个图形可视化库。
假设A和B是节点,E是边,一种粗略的方法是插入一个“文本节点”T,并将A-T和T-B连接起来
但是我不想这么做,有没有别的办法?
以下是示例代码
var theUI = {
nodes:{A:{color:"red", shape:"dot", alpha:1},
B:{color:"#b2b19d", shape:"dot", alpha:1},
C:{color:"#b2b19d", shape:"dot", alpha:1},
D:{color:"#b2b19d", shape:"dot", alpha:1},
},
edges:{
A:{
B:{length:.8},
C:{length:.8},
D:{length:.8}
}
}
}
var sys = arbor.ParticleSystem()
sys.parameters({stiffness:900, repulsion:2000, gravity:true, dt:0.015})
sys.renderer = Renderer("#sitemap")
sys.graft(theUI) 在这里,A连接到B,C和D。如何为边提供标签?
发布于 2011-09-22 13:37:39
arbor.js允许您编写代码来呈现整个图形。你可以在render方法中做任何你想做的事情,包括绘制边标题,你可以将其存储在单独的地图中。
只需以这种方式覆盖渲染器中的渲染方法:
redraw:function()
{
ctx.fillStyle = "white";
ctx.fillRect (0,0, canvas.width, canvas.height);
particleSystem.eachEdge (function (edge, pt1, pt2)
{
ctx.strokeStyle = "rgba(0,0,0, .333)";
ctx.lineWidth = 1;
ctx.beginPath ();
ctx.moveTo (pt1.x, pt1.y);
ctx.lineTo (pt2.x, pt2.y);
ctx.stroke ();
ctx.fillStyle = "black";
ctx.font = 'italic 13px sans-serif';
ctx.fillText (edge.data.name, (pt1.x + pt2.x) / 2, (pt1.y + pt2.y) / 2);
});
particleSystem.eachNode (function (node, pt)
{
var w = 10;
ctx.fillStyle = "orange";
ctx.fillRect (pt.x-w/2, pt.y-w/2, w,w);
ctx.fillStyle = "black";
ctx.font = 'italic 13px sans-serif';
ctx.fillText (node.name, pt.x+8, pt.y+8);
});
};此代码要求在初始化时填充每个边的数据属性。
我使用我的自定义map和方法addNode/addEdge手动创建所有节点和边,但我想您可以稍微更改代码以使用自定义数据初始化边:
var theUI = {
nodes:{A:{color:"red", shape:"dot", alpha:1},
B:{color:"#b2b19d", shape:"dot", alpha:1},
C:{color:"#b2b19d", shape:"dot", alpha:1},
D:{color:"#b2b19d", shape:"dot", alpha:1},
},
edges:{
A:{
B:{length:.8, data:{name:"A->B"}},
C:{length:.8, data:{name:"A->C"}},
D:{length:.8, data:{name:"A->D"}}
}
}
}附言:看看this example,我从中学到了很多。
发布于 2015-04-17 02:58:08
edge.data为{length:.8, data:{name:"A->B"}},因此将方法particleSystem.eachEdge()的最后一行替换为以下代码
ctx.fillText (edge.data.data.name, (pt1.x + pt2.x) / 2, (pt1.y + pt2.y) / 2);
这对我很有效。
https://stackoverflow.com/questions/7499047
复制相似问题