## Link Labels

This is the sixth article of the intermediate section of the JointJS tutorial. Go back to custom links. Alternatively, you can return to index of all articles.

The basic tutorial series offered an introduction into link labels. This section explains everything you need to know to make full use of the powerful label system.

JointJS offers a full suite of methods for working with link labels:

- `link.labels(labels)` - sets the `labels` array of the link. If called without arguments, returns the `labels` array.
- `link.label(index, label)` - sets the provided `label` at the given `index`. If called with `index` only, returns that label.
- `link.insertLabel(index, label)` - inserts the provided `label` at the given `index`.
- `link.appendLabel(label)` - shortcut function. Inserts the `label` at the end of the `labels` array.
- `link.removeLabel(index)` - removes the label at `index`.

### Builtin Default Label

A simple label definition (including markup and attrs) is built into the `joint.dia.Link` class, from which all subtypes, including `joint.shapes.standard.Link`, inherit it. The builtin default label has two tags: `text` (the `<text>` SVGElement of the label), and `rect` (the `<rect>` SVGElement for label background). The builtin default attributes specify a simple vertical-centered text on a white rounded rectangle. Thus, adding a label can be as simple as passing a value for the `text/text` attribute:

    link.appendLabel({
        attrs: {
            text: {
                text: 'Hello, World!'
            }
        }
    });

JointJS source code: links-label-builtin.js

The full builtin default label definition can be found in the documentation.

### Label Position

Labels are positioned at the center point of the link (`distance` of `0.5`) by default. Three kinds of `label.position.distance` values are recognized for setting custom position. A value between `0` and `1` causes the label to be positioned relatively to link length. Positive values signify absolute position in local SVG units away from start point. Finally, negative values mean absolute position away from end point. An animated example is presented below. (Link labels can also be emulated with link subelements and special attributes; this technique is explained elsewhere in the tutorial.)

    link.appendLabel({
        attrs: {
            text: {
                text: '0.25'
            }
        },
        position: {
            distance: 0.25
        }
    });

    link.appendLabel({
        attrs: {
            text: {
                text: '150'
            }
        },
        position: {
            distance: 150
        }
    });

    link.appendLabel({
        attrs: {
            text: {
                text: '-100'
            }
        },
        position: {
            distance: -100
        }
    });

JointJS source code: link-labels-distance.js

### Label Offset

It is also possible to set label offsets. This is done with the `label.position.offset` property. With a positive number, the label is offset relatively and to the right of the link; a negative number causes the label to be offset to the left. An object with `x` and `y` coordinates offsets the label absolutely by that amount in the two dimensions. The following example illustrates these three options. The red asterisk marks the reference point of all labels on the link.

    link.appendLabel({
        attrs: {
            text: {
                text: 'offset: 40'
            }
        },
        position: {
            distance: 0.66,
            offset: 40
        }
    });

    link.appendLabel({
        attrs: {
            text: {
                text: 'offset: -40'
            }
        },
        position: {
            distance: 0.66,
            offset: -40
        }
    });

    link.appendLabel({
        attrs: {
            text: {
                text: 'offset: -40,80'
            }
        },
        position: {
            distance: 0.66,
            offset: {
                x: -40,
                y: 80
            }
        }
    });

JointJS source code: link-labels-offset.js

By default, labels' anchor point is centered horizontally and vertically, as it was in this example. This can be changed by the `textAnchor` and `textVerticalAnchor` attributes, respectively.

### Label Rotation

Link labels are horizontal by default, but JointJS allows you to specify label rotation. If you provide a value for the `label.position.angle` property, the link will rotate clockwise by that amount (with respect to the path of the link). Furthermore, the `label.position.args.keepGradient` boolean flag has another function, which may be combined with providing a specific rotation angle - when set to `true`, it rotates the label according to the slope of the connection path at the given position. Additionally, if the `label.position.args.ensureLegibility` boolean flag is set to `true`, it ensures that the text never rotates to end up upside-down - if necessary, JointJS adds additional 180-degree rotation to make the text legible. The following example shows rotated links in action. The red asterisk marks the reference point of the two labels that are offset from the connection path.

    link.appendLabel({
            attrs: {
                text: {
                    text: '70°\nkeepGradient'
                }
            },
            position: {
                distance: 0.05,
                angle: 70,
                args: {
                    keepGradient: true
                }
            }
        });
        link.appendLabel({
            attrs: {
                text: {
                    text: '0°\nkeepGradient'
                }
            },
            position: {
                distance: 0.3,
                args: {
                    keepGradient: true
                }
            }
        });
        link.appendLabel({
            attrs: {
                text: {
                    text: '45°'
                }
            },
            position: {
                distance: 0.8,
                angle: 45
            }
        });
        link.appendLabel({
            attrs: {
                text: {
                    text: '135°'
                }
            },
            position: {
                distance: 0.9,
                angle: 135
            }
        });
        link.appendLabel({
            attrs: {
                text: {
                    text: '270°\nkeepGradient'
                }
            },
            position: {
                distance: 0.66,
                offset: 80,
                angle: 270,
                args: {
                    keepGradient: true
                }
            }
        });
        link.appendLabel({
            attrs: {
                text: {
                    text: '270°\nkeepGradient\nensureLegibility'
                }
            },
            position: {
                distance: 0.66,
                offset: -80,
                angle: 270,
                args: {
                    keepGradient: true,
                    ensureLegibility: true
                }
            }
        });

JointJS source code: link-labels-rotation.js

### Label Styling

Of course, it is also possible to change the appearance of your labels. Simply specify custom `markup` in JSON format and pass some `attrs`. As a bonus, you can define custom selectors to identify individual components of your label. Let's define a complex circular label that shows what JointJS can do:

    link.appendLabel({
        markup: [
            {
                tagName: 'circle',
                selector: 'body'
            }, {
                tagName: 'text',
                selector: 'label'
            }, {
                tagName: 'circle',
                selector: 'asteriskBody'
            }, {
                tagName: 'text',
                selector: 'asterisk'
            }
        ],
        attrs: {
            label: {
                text: '½',
                fill: '#000000',
                fontSize: 14,
                textAnchor: 'middle',
                yAlignment: 'middle',
                pointerEvents: 'none'
            },
            body: {
                ref: 'label',
                fill: '#ffffff',
                stroke: '#000000',
                strokeWidth: 1,
                refR: 1,
                refCx: 0,
                refCy: 0
            },
            asterisk: {
                ref: 'label',
                text: '＊',
                fill: '#ff0000',
                fontSize: 8,
                textAnchor: 'middle',
                yAlignment: 'middle',
                pointerEvents: 'none',
                refX: 16.5,
                refY: -2
            },
            asteriskBody: {
                ref: 'asterisk',
                fill: '#ffffff',
                stroke: '#000000',
                strokeWidth: 1,
                refR: 1,
                refCx: '50%',
                refCy: '50%',
                refX: 0,
                refY: 0
            }
        }
    });

JointJS source code: link-labels-styling.js

The `ref` attributes are an example of JointJS special attributes. These are discussed in detail elsewhere in the tutorial.

In the next section of the intermediate tutorial, we will learn about link tools.