Labels on Links
It is common to add annotations or decorations on a link, particularly text.
Simple Link labels
By default if you add a GraphObject to a Link, it will be positioned at the middle of the link. In this example, we just add a TextBlock to the link and bind its TextBlock.text property to the link data's "text" 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), // this is the link shape (the line) $(go.Shape, { toArrow: "Standard" }), // this is an arrowhead $(go.TextBlock, // this is a Link label new go.Binding("text", "text")) ); var nodeDataArray = [ { key: "Alpha", loc: "0 0" }, { key: "Beta", loc: "200 50" } ]; var linkDataArray = [ { from: "Alpha", to: "Beta", text: "a label" } ]; diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
Note that clicking on the text label results in selection of the whole Link.
Although it is commonplace to use a TextBlock as the link label, it can be any GraphObject such as a Shape or an arbitrarily complex Panel. Here is a simple Panel label:
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), $(go.Shape, { toArrow: "Standard" }), $(go.Panel, "Auto", // this whole Panel is a link label $(go.Shape, "TenPointedStar", { fill: "yellow", stroke: "gray" }), $(go.TextBlock, { margin: 3 }, new go.Binding("text", "text")) ) ); var nodeDataArray = [ { key: "Alpha", loc: "0 0" }, { key: "Beta", loc: "200 50" } ]; var linkDataArray = [ { from: "Alpha", to: "Beta", text: "hello!" } // added information for link label ]; diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
This also works if the link is orthogonally routed or bezier-curved.
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.Orthogonal }, $(go.Shape), $(go.Shape, { toArrow: "Standard" }), $(go.TextBlock, { textAlign: "center" }, // centered multi-line text new go.Binding("text", "text")) ); var nodeDataArray = [ { key: "Alpha", loc: "0 0" }, { key: "Beta", loc: "200 50" } ]; var linkDataArray = [ { from: "Alpha", to: "Beta", text: "a label\non an\northo link" } ]; diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
Although positioning the label at the middle of the link is the default behavior, you can set GraphObject properties that start with "segment" to specify exactly where and how to arrange the object along the route of the link.
Link label segmentIndex and segmentFraction
Set the GraphObject.segmentIndex property in order to specify which segment of the link route the object should be on. Set the GraphObject.segmentFraction property to control how far the object should be, as a fraction from the start of the segment (zero) to the end of the segment (one).
In the case of a link that comes from a node with no GraphObject.fromSpot (i.e. Spot.None) and goes to a node with no GraphObject.toSpot, there may be only one segment in the link, segment number zero.
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), $(go.Shape, { toArrow: "Standard" }), $(go.TextBlock, "from", { segmentIndex: 0, segmentFraction: 0.2 }), $(go.TextBlock, "mid", { segmentIndex: 0, segmentFraction: 0.5 }), $(go.TextBlock, "to", { segmentIndex: 0, segmentFraction: 0.8 }) ); var nodeDataArray = [ { key: "Alpha", loc: "0 0" }, { key: "Beta", loc: "200 50" } ]; var linkDataArray = [ { from: "Alpha", to: "Beta" } ]; diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
In the case of a link that has many segments in it, you will want to specify different segment numbers. Orthogonal links, for example, typically have 6 points in the route, which means five segments numbered from 0 to 4.
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.Orthogonal }, $(go.Shape), $(go.Shape, { toArrow: "Standard" }), $(go.TextBlock, "from", { segmentIndex: 1, segmentFraction: 0.5 }), $(go.TextBlock, "mid", { segmentIndex: 2, segmentFraction: 0.5 }), $(go.TextBlock, "to", { segmentIndex: 3, segmentFraction: 0.5 }) ); var nodeDataArray = [ { key: "Alpha", loc: "0 0" }, { key: "Beta", loc: "200 50" } ]; var linkDataArray = [ { from: "Alpha", to: "Beta" } ]; diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
However, you can also count segments backwards from the "to" end of the link. -1 is the last segment, -2 is the next to last, etc. When you use a negative segment index, the segment fraction goes from 0 closest to the "to" end to 1 for the end of that segment that is farthest back along the route from the "to" end. Thus a segmentIndex of -1 with a segmentFraction of 0 is the very end point of the link route. A segmentIndex of -1 with a segmentFraction of 1 is the same point as segmentIndex -2 and segmentFraction 0.
For labels that belong near the "to" end of a link, you will normally use negative values for GraphObject.segmentIndex. This convention works better when the number of segments in a link is unknown or may vary.
Link label segmentOffset and alignmentFocus
There are two ways of making small adjustments to the position of a label object given a particular point on a link segment specified by the segment index and fractional distance.
The GraphObject.segmentOffset property controls where to position the object relative to the point on a link segment determined by the GraphObject.segmentIndex and GraphObject.segmentFraction properties. The offset is not a simple offset of the point -- it is rotated according to the angle of that link segment. A positive value for the Y offset moves the label element towards the right side of the link, as seen going in the direction of the link. Naturally a negative value for the Y offset moves it towards the left side.
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), $(go.Shape, { toArrow: "Standard" }), $(go.TextBlock, "left", { segmentOffset: new go.Point(0, -10) }), $(go.TextBlock, "right", { segmentOffset: new go.Point(0, 10) }) ); var nodeDataArray = [ { key: "Alpha", loc: "0 0" }, { key: "Beta", loc: "200 50" } ]; var linkDataArray = [ { from: "Alpha", to: "Beta" } ]; diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
If you drag one node around in a circle around the other one, you will see how the "left" and "right" labels are positioned.
Another way to change the effective offset is by changing the spot in the object that is being positioned relative to the link segment point. You can do that by setting the GraphObject.alignmentFocus, which as you have seen above defaults to Spot.Center. (GraphObject.alignmentFocus is also used by other Panel types, which is why its name does not start with "segment".)
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), $(go.Shape, { toArrow: "Standard" }), $(go.TextBlock, "left", { alignmentFocus: new go.Spot(1, 0.5, 3, 0) }), $(go.TextBlock, "right", { alignmentFocus: new go.Spot(0, 0.5, -3, 0) }) ); var nodeDataArray = [ { key: "Alpha", loc: "0 0" }, { key: "Beta", loc: "200 50" } ]; var linkDataArray = [ { from: "Alpha", to: "Beta" } ]; diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
Yet you may instead want to control the angle of the individual labels based on the angle of the link segment.
Link label segmentOrientation
The GraphObject.segmentOrientation property controls the angle of the label object relative to the angle of the link segment. There are several possible values that you can use. The default orientation is Link.None, meaning no rotation at all. Link.OrientAlong is commonly used to have the object always rotated at the same angle as the link segment. Link.OrientUpright is like "OrientAlong", but is often used when there is text in the label, to make it easier to read.
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), $(go.Shape, { toArrow: "Standard" }), $(go.TextBlock, "left", { segmentOffset: new go.Point(0, -10), segmentOrientation: go.Link.OrientUpright }), $(go.TextBlock, "middle", { segmentOffset: new go.Point(0, 0), segmentOrientation: go.Link.OrientUpright }), $(go.TextBlock, "right", { segmentOffset: new go.Point(0, 10), segmentOrientation: go.Link.OrientUpright }) ); var nodeDataArray = [ { key: "Alpha", loc: "0 0" }, { key: "Beta", loc: "200 50" } ]; var linkDataArray = [ { from: "Alpha", to: "Beta" } ]; diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
Now if you move a node around you will always be able to read the label texts, and yet each label stays on its intended side of the link, as seen going in the direction of the link.
This points out a difference between a segmentIndex/segmentFraction pair of 0/1 and 1/0. Although they both refer to the same point, the angle associated with the first pair is the angle of the first segment (segment 0), whereas the angle associated with the second pair is the angle of the second segment.
Link labels near the ends
For labels that are near either end of a link, it may be convenient to set the GraphObject.segmentOffset to Point(NaN, NaN). This causes the offset to be half the width and half the height of the label object.
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), $(go.Shape, { toArrow: "Standard" }), $(go.TextBlock, "from", { segmentIndex: 0, segmentOffset: new go.Point(NaN, NaN), segmentOrientation: go.Link.OrientUpright }), $(go.TextBlock, "to", { segmentIndex: -1, segmentOffset: new go.Point(NaN, NaN), segmentOrientation: go.Link.OrientUpright }) ); var nodeDataArray = [ { key: "Alpha", loc: "0 0" }, { key: "Beta", loc: "200 50" } ]; var linkDataArray = [ { from: "Alpha", to: "Beta" } ]; diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
Arrowheads
Now that you know more about the GraphObject "segment..." properties for controlling the position and angle of objects in a Link, it is easy to explain how arrowheads are defined. Arrowheads are just labels: Shapes that are initialized in a convenient manner.
You can see a copy of all of the built-in arrowhead definitions in this file: Arrowheads.js.
Here are the equivalent settings for initializing an arrowhead Shape by setting Shape.toArrow to "Standard".
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), $(go.Shape, // the following are the same as { toArrow: "Standard" }: { segmentIndex: -1, segmentOrientation: go.Link.OrientAlong, alignmentFocus: go.Spot.Right, geometry: go.Geometry.parse("F1 m0 0 l8 4 -8 4 2 -4 z") }) ); var nodeDataArray = [ { key: "Alpha", loc: "0 0" }, { key: "Beta", loc: "200 50" } ]; var linkDataArray = [ { from: "Alpha", to: "Beta" } ]; diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);