Links

Use the Link class to implement a visual relationship between nodes.

See samples that make use of customized Links in the samples index.

Links are normally created by the presence of link data objects in the GraphLinksModel.linkDataArray or by a parent key reference as the value of the TreeModel.nodeParentKeyProperty of a node data object in a TreeModel. Users can draw new links by using the LinkingTool: Introduction to the Linking Tools.

You can create new links programmatically by modifying the model. It is most common to operate directly on the model by either calling GraphLinksModel.addLinkData or by calling TreeModel.setParentKeyForNodeData. Such changes are observed by all diagrams that are displaying the model so that they can automatically create the corresponding Links. You can find examples of calls to GraphLinksModel.addLinkData in the samples.

It is also possible to create new links without detailed knowledge of the diagram's model by calling LinkingTool.insertLink. That is how the user's actions to draw a new link actually create it. That method knows how to modify the GraphLinksModel or the TreeModel appropriately, while respecting the additional functionality offered by the LinkingTool.archetypeLinkData and other properties of the LinkingTool. You can find examples of calls to LinkingTool.insertLink in the samples.

The simplest links are those without arrowheads to indicate a visual direction. Either the relationship really is non-directional, or the direction is implicit in the organization of the diagram.

The template just contains a Shape as the main element, as the line that is drawn between nodes. After the link's route is computed the main Shape will get a Geometry based on the points in the route.

  diagram.nodeTemplate =
    $(go.Node, "Auto",
      new go.Binding("location", "loc", go.Point.parse),
      $(go.Shape, "RoundedRectangle", { fill: "lightgray" }),
      $(go.TextBlock, { margin: 5 },
        new go.Binding("text", "key"))
    );

  diagram.linkTemplate =
    $(go.Link,       // the whole link panel
      $(go.Shape));  // the link shape, default black stroke

  var nodeDataArray = [
    { key: "Alpha", loc: "0 0" },
    { key: "Beta", loc: "100 50" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta" }
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

By default the way that the model and diagram know about the node data references of a link data is by looking at its from and to properties. If you want to use a different properties on the link data, set GraphLinksModel.linkFromKeyProperty to be the name of the property that results in the node data's key, and similarly for the GraphLinksModel.linkToKeyProperty.

Arrowheads

Many links do want to indicate directionality by using arrowheads. GoJS makes it easy to create common arrowheads: just add a Shape and set its Shape.toArrow property. Setting that property will automatically assign a Geometry to the Shape.geometry and will set other properties so that the arrowhead is positioned at the head of the link and is pointing in the correct direction. Of course you can set the other Shape properties such as Shape.fill in order to customize the appearance of the arrowhead.

You can also have an arrowhead at the end of the link: set the Shape.fromArrow property.

  diagram.nodeTemplate =
    $(go.Node, "Auto",
      new go.Binding("location", "loc", go.Point.parse),
      $(go.Shape, "RoundedRectangle", { fill: "lightgray" }),
      $(go.TextBlock, { margin: 5 },
        new go.Binding("text", "key"))
    );

  diagram.linkTemplate =
    $(go.Link,
      $(go.Shape),  // the link shape
      $(go.Shape,   // the arrowhead
        { toArrow: "OpenTriangle", fill: null })
    );

  var nodeDataArray = [
    { key: "Alpha", loc: "0 0" },
    { key: "Beta", loc: "100 50" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta" }
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

You can see all of the predefined arrowhead types in the Arrowheads Sample.

Routing

If you want to customize the path that each Link takes, you need to set properties on the link. The property that has the most general effect on the points that the link's route follows is Link.routing.

This example shows the two most common routing values: Link.Normal (the default) and Link.Orthogonal.

  diagram.nodeTemplate =
    $(go.Node, "Auto",
      new go.Binding("location", "loc", go.Point.parse),
      $(go.Shape, "RoundedRectangle", { fill: "lightgray" }),
      $(go.TextBlock, { margin: 5 },
        new go.Binding("text", "key"))
    );

  diagram.linkTemplate =
    $(go.Link,
      new go.Binding("routing", "routing"),
      $(go.Shape),
      $(go.Shape, { toArrow: "Standard" })
    );

  var nodeDataArray = [
    { key: "Alpha", loc: "0 0" },
    { key: "Beta", loc: "50 50" },
    { key: "Gamma", loc: "100 25" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta", routing: go.Link.Normal },
    { from: "Alpha", to: "Gamma", routing: go.Link.Orthogonal }
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

Note that the computed route also depends on the properties of the node, including its shape. There are other properties, including GraphObject.fromSpot and GraphObject.toSpot, that affect the route. Furthermore some Layouts set properties on links to control their routing according to what the layout expects.

You can also set Link.routing to Link.AvoidsNodes:

  diagram.nodeTemplate =
    $(go.Node, "Auto",
      new go.Binding("location", "loc", go.Point.parse),
      $(go.Shape, "RoundedRectangle", { fill: "lightgray" }),
      $(go.TextBlock, { margin: 5 },
        new go.Binding("text", "key"))
    );

  diagram.linkTemplate =
    $(go.Link,
      { routing: go.Link.AvoidsNodes },  // link route should avoid nodes
      $(go.Shape),
      $(go.Shape, { toArrow: "Standard" })
    );

  var nodeDataArray = [
    { key: "Alpha", loc: "0 0" },
    { key: "Beta", loc: "250 40" },
    { key: "Gamma", loc: "100 0" },
    { key: "Delta", loc: "75 50" },
    { key: "Epsilon", loc: "150 30" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta" }
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

If you move the nodes interactively, you can see how the link's route adjusts to avoid crossing over nodes. Notice that a small gap between nodes might not be considered wide enough for links to go through.

If a node is very close to or overlaps with either the link's Link.fromNode or Link.toNode and would block the link's route, it ignores that node, treating it as if it were just an extension of the connected node. Also if no node-avoiding route exists because there is a ring of nodes around one of the connected nodes, the routing algorithm will give up and cross over some nodes anyway.

You can declare that it is OK to route through a node by setting Node.avoidable to false.

Curve, Curviness, Corner

Once the Link.routing determines the route (i.e., the sequence of points) that the link takes, other properties control the details of how the link shape gets its path geometry. The first such property is Link.curve, which controls whether the link shape has basically straight segments or is a big curve.

The default value for Link.curve is Link.None, which produces link shapes with straight segments as you see above.

A value of Link.Bezier produces a naturally curved path for the link shape.

  diagram.nodeTemplate =
    $(go.Node, "Auto",
      new go.Binding("location", "loc", go.Point.parse),
      $(go.Shape, "RoundedRectangle", { fill: "lightgray" }),
      $(go.TextBlock, { margin: 5 },
        new go.Binding("text", "key"))
    );

  diagram.linkTemplate =
    $(go.Link,
      { curve: go.Link.Bezier },  // Bezier curve
      $(go.Shape),
      $(go.Shape, { toArrow: "Standard" })
    );

  var nodeDataArray = [
    { key: "Alpha", loc: "0 0" },
    { key: "Beta", loc: "100 50" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta" }
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

You can control how curved it is by setting the Link.curviness property. The default produces a slight curve.

If there are multiple links, it will automatically compute reasonable values for the curviness of each link, unless you assign Link.curviness explicitly.

  diagram.nodeTemplate =
    $(go.Node, "Auto",
      new go.Binding("location", "loc", go.Point.parse),
      $(go.Shape, "RoundedRectangle", { fill: "lightgray" }),
      $(go.TextBlock, { margin: 5 },
        new go.Binding("text", "key"))
    );

  diagram.linkTemplate =
    $(go.Link,
      { curve: go.Link.Bezier },
      $(go.Shape),
      $(go.Shape, { toArrow: "Standard" })
    );

  var nodeDataArray = [
    { key: "Alpha", loc: "0 0" },
    { key: "Beta", loc: "100 50" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta" },  // multiple links between the same nodes
    { from: "Alpha", to: "Beta" },
    { from: "Alpha", to: "Beta" },
    { from: "Alpha", to: "Beta" }
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

Another kind of curviness comes from rounded corners when the Link.routing is Orthogonal or AvoidsNodes.

  diagram.nodeTemplate =
    $(go.Node, "Auto",
      new go.Binding("location", "loc", go.Point.parse),
      $(go.Shape, "RoundedRectangle", { fill: "lightgray" }),
      $(go.TextBlock, { margin: 5 },
        new go.Binding("text", "key"))
    );

  diagram.linkTemplate =
    $(go.Link,
      { routing: go.Link.AvoidsNodes,
        corner: 10 },                  // rounded corners
      $(go.Shape),
      $(go.Shape, { toArrow: "Standard" })
    );

  var nodeDataArray = [
    { key: "Alpha", loc: "0 0" },
    { key: "Beta", loc: "250 40" },
    { key: "Gamma", loc: "100 0" },
    { key: "Delta", loc: "75 50" },
    { key: "Epsilon", loc: "150 30" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta" }
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

Another kind of curviness comes from setting Link.curve to Link.JumpOver. This causes little "hops" in the path of an orthogonal link that crosses another orthogonal link that also has a JumpOver curve.

  diagram.nodeTemplate =
    $(go.Node, "Auto",
      { locationSpot: go.Spot.Center },
      new go.Binding("location", "loc", go.Point.parse),
      $(go.Shape, "RoundedRectangle", { fill: "lightgray" }),
      $(go.TextBlock, { margin: 5 },
        new go.Binding("text", "key"))
    );

  diagram.linkTemplate =
    $(go.Link,
      { routing: go.Link.Orthogonal,  // may be either Orthogonal or AvoidsNodes
        curve: go.Link.JumpOver },
      $(go.Shape),
      $(go.Shape, { toArrow: "Standard" })
    );

  var nodeDataArray = [
    { key: "Alpha", loc: "0 50" },
    { key: "Beta", loc: "100 50" },
    { key: "Alpha2", loc: "50 0" },
    { key: "Beta2", loc: "50 100" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta" },  // these two links will cross
    { from: "Alpha2", to: "Beta2" }
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

Another kind of curviness (or actually lack of it) comes from setting Link.curve to Link.JumpGap. This causes little "gaps" in the path of an orthogonal link that crosses another orthogonal link that also has a JumpGap curve.

  diagram.nodeTemplate =
    $(go.Node, "Auto",
      { locationSpot: go.Spot.Center },
      new go.Binding("location", "loc", go.Point.parse),
      $(go.Shape, "RoundedRectangle", { fill: "lightgray" }),
      $(go.TextBlock, { margin: 5 },
        new go.Binding("text", "key"))
    );

  diagram.linkTemplate =
    $(go.Link,
      { routing: go.Link.Orthogonal,  // may be either Orthogonal or AvoidsNodes
        curve: go.Link.JumpGap },
      $(go.Shape),
      $(go.Shape, { toArrow: "Standard" })
    );

  var nodeDataArray = [
    { key: "Alpha", loc: "0 50" },
    { key: "Beta", loc: "100 50" },
    { key: "Alpha2", loc: "50 0" },
    { key: "Beta2", loc: "50 100" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta" },  // these two links will cross
    { from: "Alpha2", to: "Beta2" }
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

The normal expectation is that one cannot have a link relationship unless it connects two nodes. However GoJS does support the creation and manipulation of links that have either or both of the Link.fromNode and Link.toNode properties with null values. This is demonstrated by the Draggable Link sample.

Both ends of the link must be connected to nodes in order for the standard link routing to operate. If a link does not know where to start or where to end, it cannot compute a route or a position for the link. However, you can provide a route by setting or binding Link.points to a list of two or more Points. That will automatically give the link a position so that it can be seen in the diagram.

The linking tools, LinkingTool and RelinkingTool, normally do not permit the creation or reconnection of links that connect with "nothing". However, you can set LinkingBaseTool.isUnconnectedLinkValid to true to allow the user to do so, as the Draggable Link sample demonstrates.

Links cannot normally be dragged unless they are part of a collection that includes the connected nodes. However, you can set DraggingTool.dragsLink to true to allow the user to drag a solitary Link. This mode allows the user to disconnect a link by dragging it away from the node(s)/port(s) to which it was attached. It also allows the user to reconnect one or both ends of the link by dropping it so that the end(s) are at valid port(s). This too is demonstrated by the Draggable Link sample.