简体   繁体   English

具有相同源和目标的JointJS链接

[英]JointJS Link with same source and target

In some cases I have links where source and target are the same element. 在某些情况下,我有链接,其中源和目标是同一元素。 (same ID). (相同的ID)。

  this.lastLink = this.link.clone().set({
    source: {id: ele1Id},
    target: {id: ele2Id},
    ...

It currently looks like this. 目前看起来像这样。 Links have labels so don't be confused about it. 链接带有标签,所以不要对此感到困惑。 (LINK_TABLE is a link label). (LINK_TABLE是链接标签)。

在此处输入图片说明

What I would like to achieve is that whenever source and target are the same, the link should look something like this. 我想要实现的是,只要源和目标相同,则链接应该看起来像这样。

在此处输入图片说明

My initial try was to get the position of target and then increment it for some value. 我最初的尝试是获取目标的位置,然后将其递增一些值。

    var targetPositionX = ele2.attributes.position.x;
    var targetPositionY = ele2.attributes.position.y;

    source: {id: ele1Id},
    target: {x: targetPositionX+50, y:targetPositionY+50},
    ...

But that try didn't work as I encountered problems while trying to get the target position. 但是这种尝试没有用,因为我在尝试获得目标位置时遇到问题。 If I would console.log(ele2), and then look for position, it would be fine. 如果我要console.log(ele2),然后寻找位置,那就没问题了。 But If I would console.log(ele2.attributes.position) it would log {x:0, y:0} for some reason. 但是,如果我使用console.log(ele2.attributes.position),出于某种原因,它将记录{x:0,y:0}。 But it's not related to this question. 但这与这个问题无关。

So what else can I try? 那我还能尝试什么呢?

I managed to solve this by adding two ports to my element. 我设法通过在元素中添加两个端口来解决此问题。

  var port = {
    id: 'port2',
    position:{
      name: 'right'
    },
    markup: '<rect width="0"  height="1"/>'
  };
  var port2 = {
    id: 'port1',
    position:{
      name: 'right'
    },
    markup: '<rect width="0" height="1"/>'
  };
  ele1Id.addPort(port);
  ele1Id.addPort(port2);

After adding the links I had to make sure that I also add ports to my source and target. 添加链接后,我必须确保还将端口添加到源和目标。

source: {id: ele1Id.id, port: "port1"},
target: {id: ele2Id.id, port: "port2"}

And another thing I added for that 'spinal' link look is this: 我为“ spinal”链接外观添加的另一件事是:

connector: { name: 'rounded' },
router: { name: 'manhattan' }

you can add an extra vertex to the link. 您可以向链接添加额外的顶点。 There is a demo for the inspiration: http://resources.jointjs.com/demos/fsa 有一个启发示例: http : //resources.jointjs.com/demos/fsa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM