在arbor.js中为边添加标签(查询插件) [英] Add label to edges in arbor.js (Query plugin)
问题描述
如何在arbor.js中为边添加标签
这是一个图表可视化库。
How do I add labels to edges in arbor.js It is a graph visualization library.
假设A和B是节点,E是edge
一种粗略的方法是插入文本节点T
并加入AT和TB
Suppose A and B are nodes and E is the edge One crude way would be insert a "text node" T and join A-T and T-B
但我不想这样做,还有其他方法吗?
But i don't want to this, is there any other way?
以下是示例代码
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.
如何向边缘提供标签?
In this, A is connected to B, C and D. How to supply label to edges?
推荐答案
arbor.js允许你编写代码来渲染整个图形。您可以在渲染方法中执行任何操作,包括绘制可以存储在单独地图中的边标题。
arbor.js allows you to write a code to render the whole graph. You can do whatever you want in render method including drawing edges titles which you can store in a separate map.
只需在Renderer中以这种方式覆盖方法渲染:
Just override method render in Renderer this way:
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);
});
};
此代码期望在初始化时填充每条边的数据属性。
This code expected data property of each edge to be filled while initialization.
我使用自定义地图和方法addNode / addEdge手动创建所有节点和边缘,我想你可以改变一些代码来用这种方式用自定义数据初始化边缘:
I create all nodes and edges manually using my custom map and methods addNode/addEdge, bu I suppose you can change a little your code to initialize edges with custom data this way:
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"}}
}
}
}
PS:看看这个例子,我从中学到了很多东西。
P.S.: take a look at this example, I learned a lot from it.
这篇关于在arbor.js中为边添加标签(查询插件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!