Data Model / Overlays

Overlays

Overlays are UI elements that are painted onto Connections, such as Labels or Arrows.

jsPlumb comes with five types of Overlays:

  • Arrow - a configurable arrow that is painted at some point along the connector. You can control the length and width of the Arrow, the 'foldback' point - a point the tail points fold back into, and the direction (allowed values are 1 and -1; 1 is the default and means point in the direction of the connection)
  • Label - a configurable label that is painted at some point along the connector.
  • PlainArrow - an Arrow shaped as a triangle, with no foldback.
  • Diamond - as the name suggests...a diamond.
  • Custom - allows you to create the Overlay yourself - your Overlay may be any DOM element you like.

PlainArrow and Diamond are actually just configured instances of the generic Arrow overlay (see examples).

A key concept with Overlays is that of their location.

For a Connector, the location of an Overlay refers to some point along the path inscribed by the Connector. It can be specified in one of three ways:

  • as a decimal in the range [0..1], which indicates some proportional amount of travel along the path inscribed by the Connector. The default value of 0.5 is in this form, and it means the default location of an Overlay on a Connector is a point halfway along the path.
  • as an integer greater than 1, which indicates some absolute number of pixels to travel along the Connector from the start point
  • as an integer less than zero, which indicates some absolute number of pixels to travel backwards along the Connector from the end point.

For an Endpoint, the same principles apply, but location is specified as an [x,y] array. For instance, this would specify an Overlay that was positioned in the center of an Endpoint:

location:[ 0.5, 0.5 ]

Whereas this would specify an Overlay that was positioned 5 pixels along the x axis from the top left corner:

location: [ 5, 0 ]

And this would specify an Overlay that was positioned 5 pixels along the x axis from the bottom right corner:

location: [ -5, 0 ]

All Overlays support these two methods for getting/setting their location:

  • getLocation - returns the current location
  • setLocation - sets the current location. For Endpoints, location is expressed in terms of an [ x, y ] array whose values are either proportional to the width/height of the Endpoint (decimals in the range 0-1 inclusive), or absolute values (decimals greater than 0).

%toolkit

Overlays are added to connections in the Toolkit via edge definitions in a View:

toolkit.render({
    ...
    view:{
        edges:{
            defaults:{
                [ "Arrow", { width:10, length:30, location:1, id:"arrow" } ], 
                [ "Label", { label:"foo", id:"label" } ]    
            }
        }
    }
})

%/toolkit

Draws an arrow, using four points: the head and two tail points, and a foldback point, which permits the tail of the arrow to be indented. Available constructor arguments for this Overlay are:

  • width - width of the tail of the arrow
  • length - distance from the tail of the arrow to the head
  • location - where, either as a proportional value from 0 to 1 inclusive, or as an absolute value (negative values mean distance from target; positive values greater than 1 mean distance from source) the Arrow should appear on the Connector
  • direction - which way to point. Allowed values are 1 (the default, meaning forwards) and -1, meaning backwards
  • foldback - how far along the axis of the arrow the tail points foldback in to. Default is 0.623.
  • paintStyle - a style object in the form used for paintStyle values for Endpoints and Connectors.

This is just a specialized instance of Arrow in which jsPlumb hardcodes foldback to 1, meaning the tail of the Arrow is a flat edge. All of the constructor parameters from Arrow apply for PlainArrow.

This is a specialized instance of Arrow in which jsPlumb hardcodes 'foldback' to 2, meaning the Arrow turns into a Diamond. All of the constructor parameters from Arrow apply for Diamond.

Provides a text label to decorate Connectors with. The available constructor arguments are:

  • label - The text to display. You can provide a function here instead of plain text: it is passed the Connection as an argument, and it should return a String.
  • cssClass - Optional css class to use for the Label. This is now preferred over using the labelStyle parameter.
  • labelStyle - Optional arguments for the label's appearance. Valid entries in this JS object are:
    • font - a font string in a format suitable for the Canvas element
    • fill - the color to fill the label's background with. Optional.
    • color - the color of the label's text. Optional.
    • padding - optional padding for the label. This is expressed as a proportion of the width of the label, not in pixels or ems.
    • borderWidth - optional width in pixels for the label's border. Defaults to 0.
    • borderStyle - optional. The color to paint the border, if there is one.
  • location - As for Arrow Overlay. Where, either proportionally from 0 to 1 inclusive, or as an absolute offset from either source or target, the label should appear.

%toolkit

See also the Labels page, which has a more thorough discussion of how to work with labels in the Toolkit edition.

%/toolkit


The Custom Overlay allows you to create your own Overlays, which jsPlumb will position for you. You need to implement one method - create(component) - which is passed the component on which the Overlay is located as an argument, and which returns either a DOM element or a valid selector from the underlying library:

var conn = jsPlumb.connect({
  source:"d1",
  target:"d2",
  paintStyle:{
    stroke:"red",
    strokeWidth:3
  },
  overlays:[
    ["Custom", {
      create:function(component) {
        return $("<select id='myDropDown'><option value='foo'>foo</option><option value='bar'>bar</option></select>");                
      },
      location:0.7,
      id:"customOverlay"
    }]
  ]
});

Here we have created a select box with a couple of values, assigned to it the id of 'customOverlay' and placed it at location 0.7. Note that the 'id' we assigned is distinct from the element's id. You can use the id you provided to later retrieve this Overlay using the getOverlay(id) method on a Connection or an Endpoint.


You can control the visibility of Overlays using the setVisible method of Overlays themselves, or with showOverlay(id) or hideOverlay(id) on a Connection.

Remember the id parameter that we specified in the examples above? This can be used to retrieve the Overlay from a Connection:

var connection = jsPlumb.connect({
  ...
  overlays:[ 
    "Arrow", 
    [ "Label", { label:"foo", location:0.25, id:"myLabel" } ]
  ],
  ...
});
    
// time passes
    
var overlay = connection.getOverlay("myLabel");
// now you can hide this Overlay:
overlay.setVisible(false);
// there are also hide/show methods:
overlay.show();
overlay.hide();

However, Connection and Endpoint also have two convenience methods you could use instead:

var connection = jsPlumb.connect({
  ...
  overlays:[ 
    "Arrow", 
    [ "Label", { label:"foo", location:-30 }, id:"myLabel" ]
  ],
  ...
});
    
// time passes
    
connection.hideOverlay("myLabel");
    
// more time passes
    
connection.showOverlay("myLabel");

%toolkit

%/toolkit