Introduction to GoJS Diagramming Components
A Simple GoJS Diagram
The following code defines a node template and model data, which produces a small diagram with a handful of nodes and links.
This creates the following Diagram:
You can interact with this diagram in many ways:
- You can select a part by clicking on it. Selected nodes are highlighted with an Adornment that is a blue rectangle surrounding the node. Selected links are highlighted with a blue line following the path of the link.
- Multiple parts may be selected at once. Hold the Shift key down when clicking to add to the selection. Hold the Control key down when clicking to toggle whether that part is selected.
- Another way to multi-select is to mouse-down at a point in the background (not on a part), wait a moment, and then drag a box. Parts that are in the box when the mouse-up occurs are selected. The Shift and Control modifiers work then as well.
- Ctrl-A selects all parts in the diagram.
- Move one or more parts by selecting them and dragging.
- Copying selected parts works with either copy/paste (Ctrl-C/Ctrl-V) or with Ctrl-mouse-drag.
- Delete selected parts with the Delete key.
- If scrollbars are visible or if the whole collection of parts is smaller than the viewable area of the diagram (the "viewport"), you can pan the diagram with a mouse-down in the background (not on a part) if you drag without waiting.
- Use the mouse wheel to scroll up and down and Shift-mouse-wheel to scroll left and right. Ctrl-mouse-wheel zooms in and out.
You can also pan, pinch zoom, select, copy, move, delete, undo, and redo with your fingers on a touch device. Most commands that can be invoked from a keyboard can be invoked from the default context menu that you get by pressing your finger and holding it motionless for a moment.
What is unique about all of the examples in the documentation is that they are all "live" -- there are no screenshots! They are actual Diagrams implemented by the source code shown. You can interact with them -- some even display animation.
Each diagram has a Model that holds and interprets your application data to determine node-to-node link relationships and group-member relationships. Most parts are data-bound to your application data. The diagram automatically creates a Node or a Group for each data item in the model's Model.nodeDataArray and a Link for each data item in the model's GraphLinksModel.linkDataArray.
Each Node or Link is normally defined by a template that declares its appearance and behavior. Each template consists of Panels of GraphObjects such as TextBlocks or Shapes. There are default templates for all parts, but almost all applications will specify custom templates in order to achieve the desired appearance and behavior.
Tools handle mouse and keyboard events. Each diagram has a number of tools that perform interactive tasks such as selecting parts or dragging them or drawing a new link between two nodes. The ToolManager determines which tool should be running, depending on the mouse events and current circumstances.
Each diagram also has a CommandHandler that implements various commands, such as Delete or Copy. The CommandHandler interprets keyboard events, such as control-Z, when the ToolManager is running.
You can select one or more parts in the diagram. The template implementation may change the appearance of the node or link when it is selected. The diagram may also add Adornments to indicate selection and to support tools such as resizing a node or reconnecting a link. Adornments are also how tooltips and context menus are implemented.
All programmatic changes to Diagram, GraphObject, Model or model data state should be performed within a single transaction per user action, to make sure updating happens correctly and to support undo/redo. All of the predefined tools and commands perform transactions, so each user action is automatically undoable if the UndoManager is enabled. DiagramEvents on Diagrams, and event handlers on Diagrams and GraphObjects, are all documented whether they are raised within a transaction or whether you need to conduct a transaction in order to change the model or the diagram.
Creating a Diagram
When you include the library, the "
During development we recommend that you load "go-debug.js" instead of "go.js", for additional run-time error checking and debugging ability.
<!DOCTYPE html> <!-- Declare standards mode. --> <html> <head> . . . <!-- Include the GoJS library. --> <script src="go.js"></script>
Every Diagram must be hosted by an HTML Div element. GoJS will manage the contents of that Div element, but you may position and size and style the Div as you would any HTML element. The diagram will add a Canvas element to that Div element that the diagram will draw in -- this is what users actually see. The Canvas element is automatically sized to have the same size as the Div element.
<body> . . . <!-- The DIV for a Diagram needs an explicit size or else we won't see anything. In this case we also add a border to help see the edges. --> <div id="myDiagramDiv" style="border: solid 1px blue; width:400px; height:150px"></div>
This completes the implementation of the "Hello World!" live diagram that you see above.
GoJS outputs error or warning messages when something goes wrong. When developing with GoJS, be sure to check your browser's developer console for information. The "go-debug.js" version of the library contains extra type-checking and error-checking code, and should be used during development. The "go.js" version has less error checking, but is faster as a result, and should be used in production.
Do not modify the prototypes of the GoJS classes.
Only use the properties and methods documented in the API.
You can also use TypeScript in order to get better "compile-time" type-checking. In your TypeScript code, just reference the "goJS.d.ts" definition file for the "go.js" library:
///<reference path="goJS.d.ts"/>If you are using Visual Studio, this definition file also greatly improves Intellisense feedback while editing TypeScript code.
You can see the variety of kinds of diagrams that you can build at GoJS Samples.
In the next introduction page we discuss building GoJS Parts and adding them into Diagrams.