It is common to add annotations or decorations on a link, particularly text.
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 =
new go.Node("Auto")
.bind("location", "loc", go.Point.parse)
.add(
new go.Shape("RoundedRectangle", { fill: "lightgray" }),
new go.TextBlock({ margin: 5 })
.bind("text", "key")
);
diagram.linkTemplate =
new go.Link()
.add(
new go.Shape(), // this is the link shape (the line)
new go.Shape({ toArrow: "Standard" }), // this is an arrowhead
new go.TextBlock() // this is a Link label
.bind("text")
);
const nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
const 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 =
new go.Node("Auto")
.bind("location", "loc", go.Point.parse)
.add(
new go.Shape("RoundedRectangle", { fill: "lightgray" }),
new go.TextBlock({ margin: 5 })
.bind("text", "key")
);
diagram.linkTemplate =
new go.Link()
.add(
new go.Shape(),
new go.Shape({ toArrow: "Standard" }),
new go.Panel("Auto") // this whole Panel is a link label
.add(
new go.Shape("TenPointedStar", { fill: "yellow", stroke: "gray" }),
new go.TextBlock({ margin: 3 })
.bind("text")
)
);
const nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
const 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 =
new go.Node("Auto")
.bind("location", "loc", go.Point.parse)
.add(
new go.Shape("RoundedRectangle", { fill: "lightgray" }),
new go.TextBlock( { margin: 5 })
.bind("text", "key")
);
diagram.linkTemplate =
new go.Link({ routing: go.Routing.Orthogonal })
.add(
new go.Shape(),
new go.Shape({ toArrow: "Standard" }),
new go.TextBlock({ textAlign: "center" }) // centered multi-line text
.bind("text", "text")
);
const nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
const 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.
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).
When setting the GraphObject.segmentIndex property to NaN, the fraction will be calculated along the entire link route instead of a particular segment.
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 =
new go.Node("Auto")
.bind("location", "loc", go.Point.parse)
.add(
new go.Shape("RoundedRectangle", { fill: "lightgray" }),
new go.TextBlock({ margin: 5 })
.bind("text", "key")
);
diagram.linkTemplate =
new go.Link()
.add(
new go.Shape(),
new go.Shape({ toArrow: "Standard" }),
new go.TextBlock("from", { segmentIndex: 0, segmentFraction: 0.2 }),
new go.TextBlock("mid", { segmentIndex: 0, segmentFraction: 0.5 }),
new go.TextBlock("to", { segmentIndex: 0, segmentFraction: 0.8 })
);
const nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
const 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 =
new go.Node("Auto")
.bind("location", "loc", go.Point.parse)
.add(
new go.Shape("RoundedRectangle", { fill: "lightgray" }),
new go.TextBlock({ margin: 5 })
.bind("text", "key")
);
diagram.linkTemplate =
new go.Link({ routing: go.Routing.Orthogonal })
.add(
new go.Shape(),
new go.Shape({ toArrow: "Standard" }),
new go.TextBlock("from", { segmentIndex: 1, segmentFraction: 0.5 }),
new go.TextBlock("mid", { segmentIndex: 2, segmentFraction: 0.5 }),
new go.TextBlock("to", { segmentIndex: 3, segmentFraction: 0.5 })
);
const nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
const 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.
Lastly, one can specify a segmentIndex of NaN to have the fraction calculated along the entire link route instead of just a particular segment.
diagram.nodeTemplate =
new go.Node("Auto")
.bind("location", "loc", go.Point.parse)
.add(
new go.Shape("RoundedRectangle", { fill: "lightgray" }),
new go.TextBlock({ margin: 5 })
.bind("text", "key")
);
diagram.linkTemplate =
new go.Link({ curve: go.Curve.Bezier })
.add(
new go.Shape(),
new go.Shape({ toArrow: "Standard" }),
new go.TextBlock("1/3", { segmentIndex: NaN, segmentFraction: 0.33 }), // label at 1/3 of link length
new go.TextBlock("2/3", { segmentIndex: NaN, segmentFraction: 0.67 }) // label at 2/3 of link length
);
const nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
const linkDataArray = [
{ from: "Alpha", to: "Beta" }
];
diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
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 =
new go.Node("Auto")
.bind("location", "loc", go.Point.parse)
.add(
new go.Shape("RoundedRectangle", { fill: "lightgray" }),
new go.TextBlock({ margin: 5 })
.bind("text", "key")
);
diagram.linkTemplate =
new go.Link()
.add(
new go.Shape(),
new go.Shape({ toArrow: "Standard" }),
new go.TextBlock("left", { segmentOffset: new go.Point(0, -10) }),
new go.TextBlock("right", { segmentOffset: new go.Point(0, 10) })
);
const nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
const 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 =
new go.Node("Auto")
.bind("location", "loc", go.Point.parse)
.add(
new go.Shape("RoundedRectangle", { fill: "lightgray" }),
new go.TextBlock({ margin: 5 })
.bind("text", "key")
);
diagram.linkTemplate =
new go.Link()
.add(
new go.Shape(),
new go.Shape({ toArrow: "Standard" }),
new go.TextBlock("left", { alignmentFocus: new go.Spot(1, 0.5, 3, 0) }),
new go.TextBlock("right", { alignmentFocus: new go.Spot(0, 0.5, -3, 0) })
);
const nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
const 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.
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 Orientation.None, meaning no rotation at all. Orientation.Along is commonly used to have the object always rotated at the same angle as the link segment. Orientation.Upright is like "OrientAlong", but is often used when there is text in the label, to make it easier to read.
diagram.nodeTemplate =
new go.Node("Auto")
.bind("location", "loc", go.Point.parse)
.add(
new go.Shape("RoundedRectangle", { fill: "lightgray" }),
new go.TextBlock({ margin: 5 })
.bind("text", "key")
);
diagram.linkTemplate =
new go.Link()
.add(
new go.Shape(),
new go.Shape({ toArrow: "Standard" }),
new go.TextBlock("left",
{ segmentOffset: new go.Point(0, -10),
segmentOrientation: go.Orientation.Upright }),
new go.TextBlock("middle",
{ segmentOffset: new go.Point(0, 0),
segmentOrientation: go.Orientation.Upright }),
new go.TextBlock("right",
{ segmentOffset: new go.Point(0, 10),
segmentOrientation: go.Orientation.Upright })
);
const nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
const 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.
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 =
new go.Node("Auto")
.bind("location", "loc", go.Point.parse)
.add(
new go.Shape("RoundedRectangle", { fill: "lightgray" }),
new go.TextBlock({ margin: 5 })
.bind("text", "key")
);
diagram.linkTemplate =
new go.Link()
.add(
new go.Shape(),
new go.Shape({ toArrow: "Standard" }),
new go.TextBlock("from",
{ segmentIndex: 0, segmentOffset: new go.Point(NaN, NaN),
segmentOrientation: go.Orientation.Upright }),
new go.TextBlock("to",
{ segmentIndex: -1, segmentOffset: new go.Point(NaN, NaN),
segmentOrientation: go.Orientation.Upright })
);
const nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
const linkDataArray = [
{ from: "Alpha", to: "Beta" }
];
diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
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 =
new go.Node("Auto")
.bind("location", "loc", go.Point.parse)
.add(
new go.Shape("RoundedRectangle", { fill: "lightgray" }),
new go.TextBlock({ margin: 5 })
.bind("text", "key")
);
diagram.linkTemplate =
new go.Link()
.add(
new go.Shape(),
new go.Shape(
// the following are the same as { toArrow: "Standard" }:
{ segmentIndex: -1,
segmentOrientation: go.Orientation.Along,
alignmentFocus: go.Spot.Right,
geometry: go.Geometry.parse("F1 m0 0 l8 4 -8 4 2 -4 z") })
);
const nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
const linkDataArray = [
{ from: "Alpha", to: "Beta" }
];
diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);