Highlighting

It is common to make a Node (or a part of a Node or a Link) stand out by "highlighting" it in some way. This happens with selection when a selection Adornment is shown. However one frequently wants to highlight Parts independently of selection. This can be done by changing the fill or stroke of a Shape, replacing a Picture source with another source, adding or removing a shadow, and so on.

Highlighting a Node upon Mouse Over

The most general kind of highlighting is to change appearance when an action occurs, such as mousing over a node. This can draw attention to interactive Nodes or Links or really any GraphObject, such as buttons. This is why predefined buttons in GoJS highlight on mouse-over.

To achieve this effect you just need to define GraphObject.mouseEnter and GraphObject.mouseLeave event handlers.


  function mouseEnter(e, obj) {
    var shape = obj.findObject("SHAPE");
    shape.fill = "#6DAB80";
    shape.stroke = "#A6E6A1";
    var text = obj.findObject("TEXT");
    text.stroke = "white";
  };

  function mouseLeave(e, obj) {
    var shape = obj.findObject("SHAPE");
    // Return the Shape's fill and stroke to the defaults
    shape.fill = obj.data.color;
    shape.stroke = null;
    // Return the TextBlock's stroke to its default
    var text = obj.findObject("TEXT");
    text.stroke = "black";
  };

  diagram.nodeTemplate =
    $(go.Node, "Auto",
      {
        mouseEnter: mouseEnter,
        mouseLeave: mouseLeave
      },
      $(go.Shape, "Rectangle",
        { strokeWidth: 2, stroke: null, name: "SHAPE" },
        new go.Binding("fill", "color")),
      $(go.TextBlock,
        { margin: 10, font: "bold 18px Verdana", name: "TEXT" },
        new go.Binding("text", "key"))
    );

  diagram.model = new go.GraphLinksModel(
  [
    { key: "Alpha", color: "#96D6D9" },
    { key: "Beta",  color: "#96D6D9" },
    { key: "Gamma", color: "#EFEBCA" },
    { key: "Delta", color: "#EFEBCA" }
  ],
  [
    { from: "Alpha", to: "Beta" },
    { from: "Alpha", to: "Gamma" },
    { from: "Beta", to: "Beta" },
    { from: "Gamma", to: "Delta" },
    { from: "Delta", to: "Alpha" }
  ]);

Mouse-over nodes to see them highlight.

It is also commonplace to perform highlighting of stationary Parts during a drag, which is a different case of "mouse over". This can be implemented in a manner similar to the mouseEnter/mouseLeave events by implementing GraphObject.mouseDragEnter and GraphObject.mouseDragLeave event handlers. Several samples demonstrate this: Org Chart Editor, Planogram, Regrouping, and Seating Chart.

It is common to want to show Nodes or Links that are related to a particular Node. Unlike the mouse-over scenarios, one may want to maintain the highlighting for many Parts independent of any mouse state or selection state.

Here is an example of highlighting all of the nodes and links that come out of a node that the user clicks. This example uses the Part.isHighlighted property and data binding of visual properties to that Part.isHighlighted property.


  diagram.nodeTemplate =
    $(go.Node, "Auto",
      { // when the user clicks on a Node, highlight all Links coming out of the node
        // and all of the Nodes at the other ends of those Links.
        click: (e, node) => {
            // highlight all Links and Nodes coming out of a given Node
            var diagram = node.diagram;
            diagram.startTransaction("highlight");
            // remove any previous highlighting
            diagram.clearHighlighteds();
            // for each Link coming out of the Node, set Link.isHighlighted
            node.findLinksOutOf().each(l => l.isHighlighted = true);
            // for each Node destination for the Node, set Node.isHighlighted
            node.findNodesOutOf().each(n => n.isHighlighted = true);
            diagram.commitTransaction("highlight");
          }
      },
      $(go.Shape, "Rectangle",
        { strokeWidth: 2, stroke: null },
        new go.Binding("fill", "color"),
        // the Shape.stroke color depends on whether Node.isHighlighted is true
        new go.Binding("stroke", "isHighlighted", h => h ? "red" : "black")
            .ofObject()),
      $(go.TextBlock,
        { margin: 10, font: "bold 18px Verdana" },
        new go.Binding("text", "key"))
    );

  // define the Link template
  diagram.linkTemplate =
    $(go.Link,
      { toShortLength: 4 },
      $(go.Shape,
        // the Shape.stroke color depends on whether Link.isHighlighted is true
        new go.Binding("stroke", "isHighlighted", h => h ? "red" : "black")
            .ofObject(),
        // the Shape.strokeWidth depends on whether Link.isHighlighted is true
        new go.Binding("strokeWidth", "isHighlighted", h => h ? 3 : 1)
            .ofObject()),
      $(go.Shape,
        { toArrow: "Standard", strokeWidth: 0 },
        // the Shape.fill color depends on whether Link.isHighlighted is true
        new go.Binding("fill", "isHighlighted", h => h ? "red" : "black")
            .ofObject())
    );

  // when the user clicks on the background of the Diagram, remove all highlighting
  diagram.click = e => {
    e.diagram.commit(d => d.clearHighlighteds(), "no highlighteds");
  };

  diagram.model = new go.GraphLinksModel(
    [
      { key: "Alpha", color: "#96D6D9" },
      { key: "Beta",  color: "#96D6D9" },
      { key: "Gamma", color: "#EFEBCA" },
      { key: "Delta", color: "#EFEBCA" }
    ],
    [
      { from: "Alpha", to: "Beta" },
      { from: "Alpha", to: "Gamma" },
      { from: "Beta", to: "Beta" },
      { from: "Gamma", to: "Delta" },
      { from: "Delta", to: "Alpha" }
    ]);

Click on a node to highlight outbound connected links and nodes. Click in the diagram background to remove all highlights. Note that the highlighting is independent of selection.

The use of data binding to modify the Shape properties allows you to avoid specifying names for each Shape and writing code to find the Shape and modify its properties.

Changing Node Size When Highlighting

You may want to increase the size of a node or of an element in a node in order to highlight it. For example you could have a Binding on GraphObject.scale or Shape.strokeWidth:


  $(go.Node, ...
    $(go.Shape, ...,
      new go.Binding("strokeWidth", "isHighlighted", h => h ? 5 : 1)),
    ...
  )

However, doing so will change the size of the object. That is likely to invalidate the route of any links that are connected with that node. That might not matter in many apps, but in some cases the routes of some links may have been reshaped by the user. Any recomputation of the route due to a connected node moving or changing size might lose that route.

If that is a consideration in your app, you might consider instead having each node hold an additional Shape that would provide the highlighting when shown and that would be unseen otherwise. But do not toggle the GraphObject.visible property, because that would cause the node to change size. Instead toggle the GraphObject.opacity property between 0.0 and 1.0.


  diagram.nodeTemplate =
    $(go.Node, "Auto",
      {
        locationSpot: go.Spot.Center,
        // when the user clicks on a Node, highlight all Links coming out of the node
        // and all of the Nodes at the other ends of those Links.
        click: (e, node) => {
            var diagram = node.diagram;
            diagram.startTransaction("highlight");
            diagram.clearHighlighteds();
            node.findLinksOutOf().each(l => l.isHighlighted = true);
            node.findNodesOutOf().each(n => n.isHighlighted = true);
            diagram.commitTransaction("highlight");
          }
      },
      $(go.Panel, "Auto",
        $(go.Shape, "Ellipse",
          { strokeWidth: 2, portId: "" },
          new go.Binding("fill", "color")),
        $(go.TextBlock,
          { margin: 5, font: "bold 18px Verdana" },
          new go.Binding("text", "key"))
      ),
      // the highlight shape, which is always a thick red ellipse
      $(go.Shape, "Ellipse",
        // this shape is the "border" of the Auto Panel, but is drawn in front of the
        // regular Auto Panel holding the black-bordered ellipse and text
        { isPanelMain: true, spot1: go.Spot.TopLeft, spot2: go.Spot.BottomRight },
        { strokeWidth: 6, stroke: "red", fill: null },
        // only show this ellipse when Part.isHighlighted is true
        new go.Binding("opacity", "isHighlighted", h => h ? 1.0 : 0.0)
            .ofObject())
    );

  // define the Link template
  diagram.linkTemplate =
    $(go.Link,
      { toShortLength: 4, reshapable: true, resegmentable: true },
      $(go.Shape,
        // when highlighted, draw as a thick red line
        new go.Binding("stroke", "isHighlighted", h => h ? "red" : "black")
            .ofObject(),
        new go.Binding("strokeWidth", "isHighlighted", h => h ? 3 : 1)
            .ofObject()),
      $(go.Shape,
        { toArrow: "Standard", strokeWidth: 0 },
        new go.Binding("fill", "isHighlighted", h => h ? "red" : "black")
            .ofObject())
    );

  // when the user clicks on the background of the Diagram, remove all highlighting
  diagram.click = e => {
    diagram.startTransaction("no highlighteds");
    diagram.clearHighlighteds();
    diagram.commitTransaction("no highlighteds");
  };

  diagram.model = new go.GraphLinksModel(
    [
      { key: "Alpha", color: "#96D6D9" },
      { key: "Beta",  color: "#96D6D9" },
      { key: "Gamma", color: "#EFEBCA" },
      { key: "Delta", color: "#EFEBCA" }
    ],
    [
      { from: "Alpha", to: "Beta" },
      { from: "Alpha", to: "Gamma" },
      { from: "Beta", to: "Beta" },
      { from: "Gamma", to: "Delta" },
      { from: "Delta", to: "Alpha" }
    ]);

The highlight Shape is the outer ellipse that always has a thick red stroke. It is normally hidden by having zero opacity, but the Binding will change its opacity to one when Part.isHighlighted is true.

That highlight Shape is always shown in front of the panel of the colored ellipse and text by putting it afterwards in the list of the panel's child elements. However since the "Auto" Panel assumes the first element acts as the border, we need to set GraphObject.isPanelMain to true on the highlight Shape so that it is the border for the inner panel.