Get Started with GoJS

GoJS Tutorials

For video tutorials, see our YouTube videos. For textual tutorials, read on.

GoJS is a JavaScript library for implementing interactive diagrams. This page will show you the essentials of using GoJS. We assume you are a programmer who is familiar with HTML, CSS, and JavaScript.

First, let's load the GoJS library. If you have the library downloaded to your machine:


  <!-- use go-debug.js when developing and go.js when deploying -->
  <script src="go-debug.js"></script>

You can download GoJS and all the documentation and samples from here.

Alternatively, you can link straight to a GoJS library provided by a CDN such as:


<script src="https://cdn.jsdelivr.net/npm/gojs/release/go-debug.js"></script>

Each GoJS diagram is contained in an HTML <div> element in your HTML page that you give a size:


<!-- The DIV for a Diagram needs an explicit size or else we will not see anything.
     In this case we also add a background color so we can see that area. -->
<div id="myDiagramDiv"
     style="width:400px; height:150px; background-color: #DAE4E4;"></div>

In JavaScript code you pass the <div>'s id when making a Diagram:


const myDiagram = new go.Diagram("myDiagramDiv");

This HTML and JS together creates an empty diagram:

Notice that go is the "namespace" in which all GoJS types reside. All code uses of GoJS classes such as Diagram or Node or Panel or Shape or TextBlock will be prefixed with "go.".

Diagrams and Models

The Nodes and Links of a Diagram are visualizations of data that is managed by a Model. GoJS has a model-view architecture, where Models hold the data (arrays of JavaScript objects) that describe nodes and links, and Diagrams act as views to visualize this data using actual Node and Link objects. Models, not Diagrams, are what you load and then save after editing. You add whatever properties you need for your app on the data objects in the model; you do not add properties to or modify the prototype of the Diagram and GraphObject classes.

Here's an example of a Model and Diagram, followed by the actual diagram it generates:


// You can specify options in the Diagram's second argument
// These options not only include Diagram properties, but sub-properties, too.
const myDiagram =
  new go.Diagram("myDiagramDiv",
    { // enable Ctrl-Z to undo and Ctrl-Y to redo
      "undoManager.isEnabled": true
    });

myDiagram.model = new go.Model(
  [ // for each object in this Array, the Diagram creates a Node to represent it
    { text: "Alpha" },
    { text: "Beta" },
    { text: "Gamma" }
  ]);

The diagram displays the three nodes that are in the model. Some interaction is already possible:

Styling Nodes

Nodes are styled by creating templates consisting of GraphObjects and setting properties on those objects. To create a Node, we have several building block classes at our disposal:

All of these building blocks are derived from the GraphObject abstract class, so we casually refer to them as GraphObjects or objects or elements. Note that a GraphObject is not an HTML DOM element, so there is much less overhead in creating or modifying such objects.

We want the model data properties to affect our Nodes, and this is done by way of data bindings. Data bindings allow us to change the appearance and behavior of GraphObjects in Nodes by automatically setting properties on those GraphObjects to values that are taken from the model data. The model data objects are plain JavaScript objects. You can choose to use whatever property names you like on the node data in the model.

The default Node template is simple: A Node which contains one TextBlock. There is a data binding between a TextBlock's text property and the model data's key property. In code, the template looks like this:


myDiagram.nodeTemplate =
  new go.Node()
    .add(
      new go.TextBlock()
        .bind("text") // TextBlock.text is bound to Node.data.text
    );

TextBlocks, Shapes, and Pictures are the primitive building blocks of GoJS. TextBlocks cannot contain images; Shapes cannot contain text. If you want your node to show some text, you must use a TextBlock. If you want to draw or fill some geometrical figures, you must use a Shape.

More generally, the skeleton of a Node template will look something like this:


myDiagram.nodeTemplate =
  new go.Node("Vertical", // first argument of a Node (or any Panel) can be a Panel type
      /* set Node properties here */
      { // the Node.location point will be at the center of each node
        locationSpot: go.Spot.Center
      })
    /* then add Bindings here */
    // example Node binding sets Node.location to the value of Node.data.loc
    .bind("location", "loc", go.Point.parse)

    /* add GraphObjects contained within the Node */
    // this Shape will be vertically above the TextBlock
    .add(
      new go.Shape("RoundedRectangle", // string argument can name a predefined figure
          { /* set Shape properties here */ })
        // example Shape binding sets Shape.figure to the value of Node.data.fig
        .bind("figure", "fig"),
      // add the next GraphObject to the Node Panel:
      new go.TextBlock("default text",  // string argument can be initial text string
          { /* set TextBlock properties here */ })
        // example TextBlock binding sets TextBlock.text to the value of Node.data.text
        .bind("text")
    );

Without all the comments, the template looks like this:


// The same Node template as above
myDiagram.nodeTemplate =
  new go.Node("Vertical", { locationSpot: go.Spot.Center })
    .bind("location", "loc", go.Point.parse)
    .add(
      new go.Shape("RoundedRectangle")
        .bind("figure", "fig"),
      new go.TextBlock("default text")
        .bind("text")
    );

The nesting of GraphObjects within Panels can be arbitrarily deep, and every class has its own unique set of properties to utilize, but this shows the general idea.

Now that we have seen how to make a Node template, let's see a live example. We will make a simple template commonly seen in organizational diagrams — an image next to a name. Consider the following Node template:


const myDiagram =
  new go.Diagram("myDiagramDiv",
    { // enable Ctrl-Z to undo and Ctrl-Y to redo
      "undoManager.isEnabled": true
    });

// define a simple Node template
myDiagram.nodeTemplate =
  new go.Node("Horizontal",
      // the entire node will have a light-blue background
      { background: "#44CCFF" })
    .add(
      new go.Picture(
          // Pictures should normally have an explicit width and height.
          // This picture has a red background, only visible when there is no source set
          // or when the image is partially transparent.
          { margin: 10, width: 50, height: 50, background: "red" })
        // Picture.source is data bound to the "source" attribute of the model data
        .bind("source")),
      new go.TextBlock("Default Text",  // the initial value for TextBlock.text
          // some room around the text, a larger font, and a white stroke:
          { margin: 12, stroke: "white", font: "bold 16px sans-serif" })
        // TextBlock.text is data bound to the "name" property of the model data
        .bind("text", "name")
    );

myDiagram.model = new go.Model(
  [ // note that each node data object holds whatever properties it needs;
    // for this app we add the "name" and "source" properties
    // because in our template above, we have defined bindings to expect them
    { name: "Don Meow", source: "cat1.png" },
    { name: "Copricat", source: "cat2.png" },
    { name: "Demeter",  source: "cat3.png" },
    { /* Empty node data, to show a node with no values from bindings */ }
  ]);

That code produces this diagram:

We may want to show some "default" state when not all information is present, for instance when an image does not load or when a name is not known. The "empty" node data in this example is used to show that node templates can work perfectly well without any of the properties on the bound data.

Kinds of Models

With a custom node template our diagram is becoming a pretty sight, but perhaps we want to show more. Perhaps we want an organizational chart to show that Don Meow is really the boss of a cat cartel. So we will create a complete organization chart diagram by adding some Links to show the relationship between individual nodes and a Layout to automatically position the nodes.

In order to get links into our diagram, the basic Model is not going to cut it. We are going to have to pick one of the other two models in GoJS, both of which support Links. These are GraphLinksModel and TreeModel. (Read more about models here.)

In GraphLinksModel, we have a model.linkDataArray in addition to the model.nodeDataArray. It holds an array of JavaScript objects, each describing a link by specifying the "to" and "from" node keys. Here's an example where node A links to node B and where node B links to node C:


myDiagram.model = new go.GraphLinksModel(
  [ // the nodeDataArray
    { key: "A" },
    { key: "B" },
    { key: "C" }
  ],
  [ // the linkDataArray
    { from: "A", to: "B" },
    { from: "B", to: "C" }
  ]);

A GraphLinksModel allows you to have any number of links between nodes, going in any direction. There could be ten links running from A to B, and three more running the opposite way, from B to A.

A TreeModel works a little differently. Instead of maintaining a separate array of link data, the links in a tree model are created by specifying a "parent" for a node data. Links are then created from this association. Here's the same example done as a TreeModel, with node A linking to node B and node B linking to node C:


myDiagram.model = new go.TreeModel(
  [ // the nodeDataArray
    { key: "A" },
    { key: "B", parent: "A" },
    { key: "C", parent: "B" }
  ]);

TreeModel is simpler than GraphLinksModel, but it cannot make arbitrary link relationships, such as multiple links between the same two nodes, or having multiple parents. Our organizational diagram is a simple hierarchical tree-like structure, so we will choose TreeModel for this example.

First, we will complete the data by adding a few more nodes, using a TreeModel, and specifying keys and parents in the data.


const myDiagram = new go.Diagram("myDiagramDiv",
  {
    "undoManager.isEnabled": true
  });

// the template we defined earlier
myDiagram.nodeTemplate =
  new go.Node("Horizontal",
      { background: "#44CCFF" })
    .add(
      new go.Picture({ margin: 10, width: 50, height: 50, background: "red" })
        .bind("source"),
      new go.TextBlock("Default Text",
          { margin: 12, stroke: "white", font: "bold 16px sans-serif" })
        .bind("text", "name")
    );

myDiagram.model = new go.TreeModel(
  [ // the "key" and "parent" property names are required,
    // but you can add whatever data properties you need for your app
    { key: "1",              name: "Don Meow",   source: "cat1.png" },
    { key: "2", parent: "1", name: "Demeter",    source: "cat2.png" },
    { key: "3", parent: "1", name: "Copricat",   source: "cat3.png" },
    { key: "4", parent: "3", name: "Jellylorum", source: "cat4.png" },
    { key: "5", parent: "3", name: "Alonzo",     source: "cat5.png" },
    { key: "6", parent: "2", name: "Munkustrap", source: "cat6.png" }
  ]);

Diagram Layouts

As you can see the TreeModel automatically creates the necessary Links to associate the Nodes, but it's hard to tell whose parent is who.

Diagrams have a default layout which takes all nodes that do not have a location and gives them locations, arranging them in a grid. We could explicitly give each of our nodes a location to sort out this organizational mess, but as an easier solution in our case, we will use a layout that gives us good locations automatically.

We want to show a hierarchy, and are already using a TreeModel, so the most natural layout choice is TreeLayout. TreeLayout defaults to flowing from left to right, so to get it to flow from top to bottom (as is common in organizational diagrams), we will set the angle property to 90.

Using layouts in GoJS is usually simple. Each kind of layout has a number of properties that affect the results. There are samples for each layout (like TreeLayout Demo) that showcase its properties.


// define a TreeLayout that flows from top to bottom
myDiagram.layout = new go.TreeLayout({ angle: 90, layerSpacing: 35 });

GoJS has many other layouts, which you can read about here.

Adding the layout to the diagram and model so far, we can see our results:


const myDiagram = new go.Diagram("myDiagramDiv",
  {
    "undoManager.isEnabled": true,
    layout: new go.TreeLayout({ angle: 90, layerSpacing: 35 })
  });

// the template we defined earlier
myDiagram.nodeTemplate =
  new go.Node("Horizontal",
      { background: "#44CCFF" })
    .add(
      new go.Picture({ margin: 10, width: 50, height: 50, background: "red" })
        .bind("source"),
      new go.TextBlock("Default Text",
          { margin: 12, stroke: "white", font: "bold 16px sans-serif" })
        .bind("text", "name")
    );

// the same model as before
myDiagram.model = new go.TreeModel(
  [
    { key: "1",              name: "Don Meow",   source: "cat1.png" },
    { key: "2", parent: "1", name: "Demeter",    source: "cat2.png" },
    { key: "3", parent: "1", name: "Copricat",   source: "cat3.png" },
    { key: "4", parent: "3", name: "Jellylorum", source: "cat4.png" },
    { key: "5", parent: "3", name: "Alonzo",     source: "cat5.png" },
    { key: "6", parent: "2", name: "Munkustrap", source: "cat6.png" }
  ]);

Our diagram is starting to look like a proper organization chart, but we could do better with the links.

Link Templates

We will construct a new Link template that will better suit our wide, boxy nodes. A Link is a different kind of Part, not like a Node. The main element of a Link is the Link's shape, and must be a Shape that will have its geometry computed dynamically by GoJS. Our link is going to consist of just this shape, with its stroke a little thicker than normal and dark gray instead of black. Unlike the default link template we will not have an arrowhead. And we will change the Link routing property from Normal to Orthogonal, and give it a corner value so that right-angle turns are rounded.


// define a Link template that routes orthogonally, with no arrowhead
myDiagram.linkTemplate =
  new go.Link(
      // default routing is go.Routing.Normal
      // default corner is 0
      { routing: go.Routing.Orthogonal, corner: 5 })
    .add(
      // the link path, a Shape
      new go.Shape({ strokeWidth: 3, stroke: "#555" }),
      // if we wanted an arrowhead we would also add another Shape with toArrow defined:
      //new go.Shape({  toArrow: "Standard", stroke: null  })
    );

Combining our Link template with our Node template, TreeModel, and TreeLayout, we finally have a full organization diagram. The complete code is repeated below, and the resulting diagram follows:


const myDiagram = new go.Diagram("myDiagramDiv",
  {
    "undoManager.isEnabled": true,
    layout: new go.TreeLayout({ angle: 90, layerSpacing: 35 })
  });

myDiagram.nodeTemplate =
  new go.Node("Horizontal",
      { background: "#44CCFF" })
    .add(
      new go.Picture({ margin: 10, width: 50, height: 50, background: "red" })
        .bind("source"),
      new go.TextBlock("Default Text",
          { margin: 12, stroke: "white", font: "bold 16px sans-serif" })
        .bind("text", "name")
    );

// define a Link template that routes orthogonally, with no arrowhead
myDiagram.linkTemplate =
  new go.Link(
      // default routing is go.Routing.Normal
      // default corner is 0
      { routing: go.Routing.Orthogonal, corner: 5 })
    .add(
      // the link path, a Shape
      new go.Shape({ strokeWidth: 3, stroke: "#555" }),
      // if we wanted an arrowhead we would also add another Shape with toArrow defined:
      //new go.Shape({  toArrow: "Standard", stroke: null  })
    );

// it's best to declare all templates before assigning the model
myDiagram.model = new go.TreeModel(
  [
    { key: "1",              name: "Don Meow",   source: "cat1.png" },
    { key: "2", parent: "1", name: "Demeter",    source: "cat2.png" },
    { key: "3", parent: "1", name: "Copricat",   source: "cat3.png" },
    { key: "4", parent: "3", name: "Jellylorum", source: "cat4.png" },
    { key: "5", parent: "3", name: "Alonzo",     source: "cat5.png" },
    { key: "6", parent: "2", name: "Munkustrap", source: "cat6.png" }
  ]);

Learn More

You may want to read more tutorials, such as the GraphObject Manipulation tutorial and the Interactivity tutorial. You can also watch tutorials on YouTube.

Also consider looking at the samples to see some of the diagrams possible with GoJS, or read the technical introduction to get an in-depth look at the components of GoJS.