提示框

GoJS provides a way to create customized tooltips for any object or for the diagram background.

A tooltip is an Adornment that is shown when the mouse hovers over an object that has its GraphObject.toolTip set. The tooltip part is bound to the same data as the part itself.

See samples that make use of tooltips in the samples index.

It is typical to implement a tooltip as a "ToolTip" Panel holding a TextBlock or a Panel of TextBlocks and other objects. Each "ToolTip" is just an "Auto" Panel Adornment that is shadowed, and where the border is a rectangular Shape with a light gray fill. However you can implement the tooltip as any arbitrarily complicated Adornment.

You can see how the "ToolTip" builder is defined at Buttons.js.

In this example each Node has its GraphObject.toolTip property set to a Part that shows the data.color property via a normal data binding. The diagram gets its own tooltip by setting Diagram.toolTip.

  diagram.nodeTemplate =
    $(go.Node, "Auto",
      $(go.Shape, "RoundedRectangle",
        { fill: "white" },
        new go.Binding("fill", "color")),
      $(go.TextBlock, { margin: 5 },
        new go.Binding("text", "key")),
      {
        toolTip:  // define a tooltip for each node that displays the color as text
          $("ToolTip",
            $(go.TextBlock, { margin: 4 },
              new go.Binding("text", "color"))
          )  // end of Adornment
      }
    );

  // a function that produces the content of the diagram tooltip
  function diagramInfo(model) {
    return "Model:\n" + model.nodeDataArray.length + " nodes, " +
                        model.linkDataArray.length + " links";
  }

  // provide a tooltip for the background of the Diagram, when not over any Part
  diagram.toolTip =
    $("ToolTip",
      $(go.TextBlock, { margin: 4 },
        // use a converter to display information about the diagram model
        new go.Binding("text", "", diagramInfo))
    );

  var nodeDataArray = [
    { key: "Alpha", color: "lightblue" },
    { key: "Beta", color: "pink" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta" }
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

Try pausing the mouse over each of the nodes or in the background of the diagram. If you copy some parts, you will see that the tooltip for the diagram displays newer information about the diagram.

You can change how long for the mouse has to wait motionless before a tooltip appears by setting ToolManager.hoverDelay. For example, when initializing a Diagram, "toolManager.hoverDelay": 600 changes the delay to be 6/10ths of one second.

You can change how long the tooltip remains visible by setting ToolManager.toolTipDuration. For example, "toolManager.toolTipDuration": 10000 changes the visible time to 10 seconds.

Positioning

There are two ways to customize the positioning of the tooltip relative to the adorned GraphObject. One way is to override ToolManager.positionToolTip. Another way is to have the tooltip Adornment include a Placeholder. The Placeholder is positioned to have the same size and position as the adorned object. When creating tooltips with Placeholders, don't use the predefined "ToolTip" builder as it will introduce an extra shape typically used as the border for the "Auto" Panel.

  // this is a normal Node template that also has a toolTip defined for it
  diagram.nodeTemplate =
    $(go.Node, "Auto",
      $(go.Shape, "RoundedRectangle",
        { fill: "white" },
        new go.Binding("fill", "color")),
      $(go.TextBlock, { margin: 5 },
        new go.Binding("text", "key")),
      {
        toolTip:                       // define a tooltip for each node
          $(go.Adornment, "Spot",      // that has several labels around it
            { background: "transparent" },  // avoid hiding tooltip when mouse moves
            $(go.Placeholder, { padding: 5 }),
            $(go.TextBlock,
              { alignment: go.Spot.Top, alignmentFocus: go.Spot.Bottom, stroke: "red" },
              new go.Binding("text", "key", function(s) { return "key: " + s; })),
            $(go.TextBlock, "Bottom",
              { alignment: go.Spot.Bottom, alignmentFocus: go.Spot.Top, stroke: "red" },
              new go.Binding("text", "color", function(s) { return "color: " + s; }))
          )  // end Adornment
      }
    );

  var nodeDataArray = [
    { key: "Alpha", color: "lightyellow" },
    { key: "Beta", color: "orange" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta" }
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

Note how the Adornment implementing the tooltip uses a "transparent" background so that the tooltip is not automatically removed when the mouse moves.

HTML Tooltips

It is possible to define custom tooltips using HTML instead of Adornments using the HTMLInfo class. The Data Visualization sample shows such tooltips. See HTML Interaction for more discussion.

HTML tooltips require more effort to implement than using the default GoJS "ToolTip" and GraphObjects. However you would have the full power of HTML/CSS/JavaScript to show whatever you want.

加入 GoJS 交流群
GoJS 交流群 (769862113)