Search code examples
javascriptjointjs

JointJS create multiple links between elements


I'm trying create a diagram like this using JointJS.

However, when I'm adding multiple links between elements, I'm only seeing 1 link show up. How do I go about adding multiple links with automatically adjusted space between them?

This is the code to add the boxes and links. Note that right now I'm just trying to add 3 links between all of the blocks, but I'm only seeing 1 link appear between each.

var steps = [{title: "Step 1"}, {title: "Step 2"}, {title: "Step 3"}];

steps.forEach(function(step, i){
    var title = step.title;
    var yOffset = i*150 + 50; //offsets first block by 50 in y and all others 150
    var xOffset = 60; //offsets all blocks by 60
    createBlock(title, xOffset, yOffset, i);
});

var blocks = [];

function createBlock(title, x, y, loc) {
    var x = (typeof x !== 'undefined') ?  x : 0;
    var y = (typeof y !== 'undefined') ?  y : 0;

    var newBlock = new joint.shapes.html.Element({
        position: { x: x, y: y },
        size: { width: 170, height: 100 },
        label: title,
        attrs: {
            '.label': {
                text: title,
                'ref-x': .5, 
                'ref-y': .4,
                fill: '#FFFFFF'
            },
        }
    });

    blocks.push(newBlock.id);

    graph.addCell(newBlock);

    if(blocks.length > 1) {
        var link = new joint.shapes.devs.Link({
            source: {
                id: blocks[loc-1],
            },
            target: {
                id: blocks[loc],
            },
        });
        graph.addCell(link);

        var link2 = new joint.shapes.devs.Link({
            source: {
                id: blocks[loc-1],
            },
            target: {
                id: blocks[loc],
            },
        });
        graph.addCell(link2);

        var link3 = new joint.shapes.devs.Link({
            source: {
                id: blocks[loc-1],
            },
            target: {
                id: blocks[loc],
            },
        });
        graph.addCell(link3);
    }
}

Solution

  • all the links are lying on top of each other so you see it as a single one. There is code in the demo of jointjs to see each link in different paths. you could add the below code and see that the links show up in different paths. You will need to change the graph to your graph name in the below three lines

    // displaying multiple links between two elements in different paths
          function adjustVertices(graph, cell) {
              // If the cell is a view, find its model.
              cell = cell.model || cell;
              if (cell instanceof joint.dia.Element) {
                  _.chain(graph.getConnectedLinks(cell)).groupBy(function(link) {
                      // the key of the group is the model id of the link's source or target, but not our cell id.
                      return _.omit([link.get('source').id, link.get('target').id], cell.id)[0];
                  }).each(function(group, key) {
                      // If the member of the group has both source and target model adjust vertices.
                      if (key !== 'undefined') adjustVertices(graph, _.first(group));
                  });
                  return;
              }
    
              // The cell is a link. Let's find its source and target models.
              var srcId = cell.get('source').id || cell.previous('source').id;
              var trgId = cell.get('target').id || cell.previous('target').id;
    
              // If one of the ends is not a model, the link has no siblings.
              if (!srcId || !trgId) return;
    
              var siblings = _.filter(graph.getLinks(), function(sibling) {
    
                  var _srcId = sibling.get('source').id;
                  var _trgId = sibling.get('target').id;
    
                  return (_srcId === srcId && _trgId === trgId) || (_srcId === trgId && _trgId === srcId);
              });
    
              switch (siblings.length) {
    
              case 0:
                  // The link was removed and had no siblings.
                  break;
    
              case 1:
                  // There is only one link between the source and target. No vertices needed.
                  cell.unset('vertices');
                  break;
    
              default:
    
                  // There is more than one siblings. We need to create vertices.
                  // First of all we'll find the middle point of the link.
                  var srcCenter = graph.getCell(srcId).getBBox().center();
                  var trgCenter = graph.getCell(trgId).getBBox().center();
                  var midPoint = joint.g.line(srcCenter, trgCenter).midpoint();
    
                  // Then find the angle it forms.
                  var theta = srcCenter.theta(trgCenter);
    
                  // This is the maximum distance between links
                  var gap = 20;
    
                  _.each(siblings, function(sibling, index) {
                      // We want the offset values to be calculated as follows 0, 20, 20, 40, 40, 60, 60 ..
                      var offset = gap * Math.ceil(index / 2);
                      // Now we need the vertices to be placed at points which are 'offset' pixels distant
                      // from the first link and forms a perpendicular angle to it. And as index goes up
                      // alternate left and right.
                      //
                      //  ^  odd indexes
                      //  |
                      //  |---->  index 0 line (straight line between a source center and a target center.
                      //  |
                      //  v  even indexes
                      var sign = index % 2 ? 1 : -1;
                      var angle = joint.g.toRad(theta + sign * 90);
                      // We found the vertex.
                      var vertex = joint.g.point.fromPolar(offset, angle, midPoint);
                      sibling.set('vertices', [{ x: vertex.x, y: vertex.y }]);
                  });
              }
          };
    
    
          var myAdjustVertices = _.partial(adjustVertices, graph);
          // adjust vertices when a cell is removed or its source/target was changed
          graph.on('add remove change:source change:target', myAdjustVertices);
          // also when an user stops interacting with an element.
          graph.on('cell:pointerup', myAdjustVertices);