javascript - Custom edge drawing function in vis.js - Stack Overflow

I'm using the vis.js library to draw a network but I need to customize the way the edges are drawn

I'm using the vis.js library to draw a network but I need to customize the way the edges are drawn. For example, I would like to draw let's say 50% of the edge in red and the other 50% in blue.

Is there a way to do this?

I'm using the vis.js library to draw a network but I need to customize the way the edges are drawn. For example, I would like to draw let's say 50% of the edge in red and the other 50% in blue.

Is there a way to do this?

Share Improve this question edited Feb 16, 2018 at 10:01 YakovL 8,40513 gold badges73 silver badges113 bronze badges asked Mar 15, 2017 at 16:15 jroyjroy 5756 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Finally found a way to do it:

var nodes = new vis.DataSet
([
    {id: 1, label: '1'},
    {id: 2, label: '2'},
]);

var edges = new vis.DataSet
([
    {from: 1, to: 2, color:'red'},
]);

var graph = {nodes: nodes, edges: edges};
var network = new vis.Network(container, graph, options);
var percent = 50;
network.on("afterDrawing", function (ctx)
{
    var pos = network.getPositions([1, 2]);
    ctx.strokeStyle = ctx.filStyle = 'green';
    ctx.moveTo(pos[1].x, pos[1].y);
    ctx.lineTo(pos[1].x + (pos[2].x-pos[1].x)*percent/100, pos[1].y + (pos[2].y - pos[1].y)*percent/100);
    ctx.fill();
    ctx.stroke();
});

You cannot directly customize parts of an edge but you can create two different edges (each with its own style) using a dummy invisible node serving as a connector. Since the dummy node is invisible, the two edges are going to look like two parts of the same edge.

For example, say you want to connect two nodes with a half-green-half-red edge:

var nodes = new vis.DataSet([
    {id: 1, label: '1'},
    {id: 2, label: '2'},
    {id: 'dummy', hidden: true}
]);

var edges = new vis.DataSet([
    {from: 1, to: 'dummy', color:'red'},
    {from: 'dummy', to: 2, color:'green'}
]);

var graph = {nodes: nodes, edges: edges};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745393986a4625794.html

相关推荐

  • javascript - Custom edge drawing function in vis.js - Stack Overflow

    I'm using the vis.js library to draw a network but I need to customize the way the edges are drawn

    15小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信