gpt4 book ai didi

javascript - 在 arbor.js(查询插件)中为边缘添加标签

转载 作者:数据小太阳 更新时间:2023-10-29 05:08:44 25 4
gpt4 key购买 nike

如何在 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。如何给边缘提供标签?

最佳答案

arbor.js 允许您编写代码来渲染整个图形。您可以在渲染方法中做任何您想做的事情,包括可以存储在单独 map 中的绘图边缘标题。

以这种方式重写渲染器中的渲染方法:

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"}}
}
}
}

P.S.:看看this example , 我从中学到了很多东西。

关于javascript - 在 arbor.js(查询插件)中为边缘添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7499047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com