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 the 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, node) {
  const shape = node.findObject("SHAPE");
  shape.fill = "green";
  shape.stroke = "lightgreen";
  const text = node.findObject("TEXT");
  text.stroke = "white";
};

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

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

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

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. Unlike the Part.isSelected property, there are no pre-defined appearance behaviors defined when a Part is highlighted or loses highlight.


diagram.nodeTemplate =
  new 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
          const 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");
        }
    })
    .add(
      new go.Shape("Rectangle", { strokeWidth: 2, stroke: null })
        .bind("fill", "color")
        // the Shape.stroke color depends on whether Node.isHighlighted is true
        .bindObject("stroke", "isHighlighted", h => h ? "red" : "black"),
      new go.TextBlock({ margin: 10, font: "bold 18px Verdana" })
        .bind("text")
    );

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

// 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: 1, text: "Alpha", color: "#96D6D9" },
    { key: 2, text: "Beta",  color: "#96D6D9" },
    { key: 3, text: "Gamma", color: "#EFEBCA" },
    { key: 4, text: "Delta", color: "#EFEBCA" }
  ],
  [
    { from: 1, to: 2 },
    { from: 1, to: 3 },
    { from: 2, to: 2 },
    { from: 3, to: 4 },
    { from: 4, to: 1 }
  ]);
  

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. Note how the call to establish a Binding where the source object is the whole Node instead of the node data is to GraphObject.bindObject instead of the usual GraphObject.bind.

However when the modifications you want to make are too complicated to implement using data binding, you can implement a Part.highlightedChanged event handler. Similar to the restrictions of the analogous Part.selectionChanged event handler, you must not change the highlighting of any other Parts, and we recommend against making any changes that would cause a layout to happen or any links to be rerouted.

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:


  new go.Node(...)
    .add(
      new go.Shape(...)
        .bindObject("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 =
  new 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) => {
          const 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");
        }
    })
    .add(
      new go.Panel("Auto")
        .add(
          new go.Shape("Ellipse", { strokeWidth: 2, portId: "" })
            .bind("fill", "color"),
          new go.TextBlock({ margin: 5, font: "bold 18px Verdana" })
            .bind("text")
        ),
      // the highlight shape, which is always a thick red ellipse
      new 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
        .bindObject("opacity", "isHighlighted", h => h ? 1.0 : 0.0)
    );

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

// 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: 1, text: "Alpha", color: "#96D6D9" },
    { key: 2, text: "Beta",  color: "#96D6D9" },
    { key: 3, text: "Gamma", color: "#EFEBCA" },
    { key: 4, text: "Delta", color: "#EFEBCA" }
  ],
  [
    { from: 1, to: 2 },
    { from: 1, to: 3 },
    { from: 2, to: 2 },
    { from: 3, to: 4 },
    { from: 4, to: 1 }
  ]);

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.