GoJS in 12 Minutes: JavaScript Diagramming Library Tutorial

GoJS JavaScript Diagramming Library
30 Jan 201812:01

Summary

TLDRIn 'GoJS in 12 Minutes,' viewers are introduced to GoJS, a JavaScript library for creating interactive diagrams and complex visualizations in web browsers. The video covers GoJS's features, such as no server contact, compatibility with libraries like Angular and React, and built-in functionalities like undo/redo and touch support. It guides through setting up a basic diagram, including creating an HTML page, defining node and link data, and styling with templates. The tutorial also touches on data binding, group organization, and customization, emphasizing GoJS's flexibility and performance.

Takeaways

  • 🌐 GoJS is a JavaScript library for creating interactive diagrams and complex visualizations in web browsers.
  • 💡 It operates entirely in the browser, independent of server contact or third-party libraries, ensuring compatibility with other libraries like Angular and React.
  • 🚀 GoJS is highly beneficial for applications needing diagrams, potentially saving significant development time and offering built-in features like undo/redo and touch support.
  • 🎨 The library is versatile, supporting the creation of various diagram types, from flowcharts to industry-specific visualizations.
  • 🛠️ GoJS is easy to start with, thanks to its model/view architecture, and is highly customizable and extensible, catering to specific development needs.
  • ⚡ It leverages the HTML Canvas for high-performance diagramming, ensuring fast rendering and interaction.
  • 🔗 To use GoJS, basic knowledge of HTML for setting up diagram containers and JavaScript for programming is required.
  • 📚 GoJS can be accessed from gojs.net, which offers numerous samples, introductory pages, and comprehensive API documentation.
  • 🆓 A free, unlimited trial is available, including 30 days of free support, and GoJS can be obtained through npm, NuGet, and GitHub.
  • 🔧 The script demonstrates creating a simple diagram with nodes and links, explaining the setup process, data modeling, and basic styling with templates.

Q & A

  • What is GoJS and what does it specialize in?

    -GoJS is a feature-rich JavaScript library for implementing custom interactive diagrams and complex visualizations across modern web browsers.

  • Does GoJS require server contact or third-party libraries?

    -No, GoJS runs completely in the browser without the need for server contact or dependence on any third-party libraries or software.

  • What types of applications can benefit from using GoJS?

    -Applications that require diagrams and visualizations, such as flowcharts, organizational charts, trees, logic circuits, industry-specific visualizations, and even applications for creating and editing complex layouts like floorplans.

  • What are some of the built-in features that come with GoJS?

    -GoJS comes with commonly expected features like undo/redo, in-place editing, touch and stylus support, and diagram overviews.

  • How does GoJS handle browser and platform differences?

    -GoJS is prepared out-of-the-box to handle differences between various browsers and platforms, ensuring compatibility and interoperability.

  • What is the basic requirement to start developing with GoJS?

    -To start developing with GoJS, you need to know at least a little HTML and be familiar with JavaScript.

  • How can one obtain GoJS and what resources does the official site offer?

    -GoJS can be obtained from its official site, gojs.net, which offers hundreds of samples, dozens of intro pages, and full API documentation.

  • What is the purpose of the 'init' function in setting up a GoJS diagram?

    -The 'init' function is used to initialize the GoJS diagram and is called once the document body has finished loading.

  • How are nodes and links represented in a GoJS diagram?

    -In a GoJS diagram, nodes are represented by an HTML DIV element, and links are defined by connecting nodes through their keys.

  • What are templates in GoJS and how are they used?

    -Templates in GoJS define the style for all Nodes and Links in the diagram. They are used to specify the appearance and behavior of diagram components.

  • How can one customize the appearance of Nodes and Links in GoJS?

    -The appearance of Nodes and Links can be customized by defining templates and using data bindings to link the properties of diagram elements to the data in the model.

  • What is a Group in GoJS and how does it simplify organizing Nodes and Links?

    -A Group in GoJS treats a collection of Nodes and Links as though they were a single Node, simplifying the organization and manipulation of related diagram components.

Outlines

00:00

😀 Introduction to GoJS

The video introduces 'GoJS in 12 Minutes,' a tutorial on setting up a basic diagram using GoJS, a JavaScript library for creating interactive diagrams and complex visualizations. GoJS operates entirely in the browser, is compatible with various libraries like Angular and React, and does not require server interaction or third-party software. It offers features such as undo/redo, in-place editing, and touch support out-of-the-box. The library is suitable for building various types of diagrams and applications, with a focus on ease of use, flexibility, and extensibility. The video emphasizes GoJS's performance, leveraging HTML Canvas for high-speed diagramming. To get started, viewers need basic HTML and JavaScript knowledge. GoJS can be obtained from gojs.net, npm, NuGet, and GitHub, with a free trial available for 30 days of support.

05:01

🛠️ Building a Basic GoJS Diagram

The tutorial demonstrates creating a simple GoJS diagram by setting up an HTML page with a reference to the go.js file. A DIV element with the ID 'myDiagramDiv' is added to the HTML body. The 'init' JavaScript function initializes the diagram once the document body has loaded. GoJS objects are created using 'go.GraphObject.make,' which is assigned to a simpler variable ('$') for convenience. The diagram is created by calling 'go.GraphObject.make' with 'go.Diagram' and the DIV ID as arguments. The size of the diagram is set via CSS, with a width and height of 300px and a border for visibility. Node and link data are added to the model in the form of JSON objects, defining keys for nodes and connections for links. The video explains the process of creating a basic, functional diagram with nodes and links.

10:02

🎨 Styling Nodes and Links in GoJS

The video proceeds to style the nodes and links of the diagram by defining templates. A node template is created using 'go.GraphObject.make' to define the style for all nodes, including a shape and a text block. An Auto Panel is used to contain the shape and text, with the shape set as the main element. Data bindings are added to the shape's fill and the text block's text, linking them to data properties. The node data is updated to include a 'color' property, which the template binds to the shape's fill. The text block's text is bound to the node's 'key' property. The tutorial then moves on to styling links, defining a link template with a main shape and an arrowhead shape. Data bindings are applied to the link's 'stroke' and 'fill' properties, linking them to the link's 'color' data property. The video concludes with a brief mention of further customization options and the availability of the code for download, inviting viewers to explore more features and templates in future videos.

Mindmap

Keywords

💡GoJS

GoJS is a JavaScript library specifically designed for creating interactive diagrams and complex visualizations for web applications. It operates entirely in the browser, which means it doesn't require server-side interaction or third-party software dependencies. In the video, GoJS is highlighted as a powerful tool for developers to build various types of diagrams, such as flowcharts and organizational charts, with built-in features like undo/redo and touch support.

💡Diagram

A diagram in the context of GoJS refers to a visual representation of information, often used to depict the structure or relationships within a system. Diagrams can range from simple flowcharts to complex, industry-specific visualizations. The video emphasizes the versatility of GoJS in creating diagrams, showcasing how it can be used to build everything from basic flowcharts to intricate floorplans.

💡HTML Canvas

The HTML Canvas is an element used to draw graphics via scripting (usually JavaScript). In GoJS, the HTML Canvas is utilized to deliver high-performance diagramming capabilities. The video mentions that GoJS takes full advantage of the HTML Canvas to ensure that the diagrams are rendered quickly and smoothly, which is crucial for applications requiring dynamic and responsive visualizations.

💡Model/View Architecture

The model/view architecture is a design pattern that separates the data model (the logic and data of the application) from the view (the user interface). GoJS uses this architecture, making it easier for developers to build diagrams by keeping the data and presentation logic separate. The video script mentions this as one of the reasons GoJS is straightforward and easy to get started with.

💡Customization

Customization in GoJS refers to the ability to modify and tailor the appearance and behavior of diagram components to meet specific requirements. The video discusses the multitude of editable built-in properties and features that allow for easy customization of diagram elements, making it suitable for a wide range of applications and use cases.

💡Templates

In GoJS, templates are used to define the appearance and behavior of diagram elements such as nodes and links. The video script provides an example of creating a nodeTemplate and a linkTemplate, which are used to style the nodes and links in the diagram. This feature allows developers to create a consistent look and feel across their diagrams.

💡Data Binding

Data binding in GoJS is a feature that links the properties of diagram elements to the data stored in the model. This allows for dynamic changes in the appearance and behavior of diagram parts based on the underlying data. The video demonstrates how to bind the fill color of a shape and the text of a text block to data properties, enabling the diagram to reflect the data's state.

💡GraphObjects

GraphObjects in GoJS are the building blocks of diagrams, including nodes, links, groups, and other graphical elements. The video script describes how to create and manipulate GraphObjects, such as defining a Shape or TextBlock, to construct the visual components of a diagram.

💡Groups

Groups in GoJS are a way to organize and treat a collection of nodes and links as a single entity. This can be useful for creating sub-diagrams or managing complex relationships within a diagram. The video script illustrates how to create a group by setting the 'isGroup' property to true and adding nodes and links to it, which simplifies the management of diagram components.

💡go.js File

The go.js file is the script file that contains the GoJS library. It is referenced in the HTML page to enable the use of GoJS functionalities. The video script instructs viewers to add a reference to the go.js file when setting up a new HTML page for their diagrams, which is a fundamental step in integrating GoJS into a web project.

💡DIV Element

In the context of GoJS, a DIV element is an HTML container used to define the area where the diagram will be displayed. The video script mentions adding a DIV element with a specific ID in the HTML document's body, which is then associated with a GoJS diagram. This is a standard approach for embedding interactive content in web pages.

Highlights

Introduction to 'GoJS in 12 Minutes' tutorial series.

GoJS is a JavaScript library for interactive diagrams and visualizations.

GoJS runs in the browser without server contact or third-party dependencies.

Interoperable with libraries like Angular, React, and Node.

GoJS is suitable for applications requiring diagrams and visualizations.

Features include undo/redo, in-place editing, and touch support.

GoJS can create various diagrams like flowcharts and organizational charts.

GoJS has a model/view architecture for easy diagram building.

Customization is simple with editable built-in properties and features.

Extensible with layouts, tools, methods, and classes that can be overridden.

Performance is optimized using HTML Canvas for high-speed diagramming.

Prerequisites for using GoJS include basic HTML and JavaScript knowledge.

GoJS can be obtained from gojs.net, npm, NuGet, and GitHub.

Creating a simple example diagram involves setting up an HTML page and referencing GoJS.

Diagrams are associated with HTML DIV elements and styled with CSS.

Node and link data is added to the model to populate the diagram.

Templates are used to style nodes and links in the diagram.

Data bindings allow for conditional appearance and behavior based on data.

Groups can organize nodes and links as a single entity.

Further customization is possible with built-in properties and group templates.

Encouragement to explore GoJS with available resources and free trial for support.

Transcripts

play00:05

Hello, and welcome to “GoJS in 12 Minutes.”

play00:08

We’re going to set up a basic Diagram using GoJS and discuss how you can start developing

play00:13

with it, for free, today.

play00:15

GoJS is a feature-rich JavaScript library for implementing custom interactive diagrams

play00:20

and complex visualizations across modern web browsers.

play00:24

It runs completely in the browser; there’s no need for contact with a server, nor does

play00:28

it depend on any third party libraries or software.

play00:31

As such, GoJS is interoperable with virtually any other library, like Angular, React, and Node.

play00:38

GoJS is an excellent investment for any developer or team whose application requires diagrams

play00:43

and visualizations.

play00:44

It can save weeks or months of work, delivers commonly expected features, like undo/redo,

play00:50

in-place editing, touch and stylus support, and Diagram overviews, and comes out-of-the-box

play00:55

prepared to handle differences between various browsers and platforms.

play00:59

With GoJS, there’s virtually no limit to the types of diagrams you can build.

play01:04

Examples include

play01:05

flowcharts, organizational charts, trees, logic circuits, industry-specific visualizations,

play01:12

and even entire applications for creating and editing something as complex as a floorplan.

play01:18

GoJS is straightforward; it’s easy to get started building Diagrams, thanks to the familiar

play01:24

model/view type architecture.

play01:26

GoJS is flexible; the multitude of editable built-in properties and features make customizations

play01:31

for Diagram components a breeze.

play01:33

GoJS is extensible: from Layouts to Tools, methods and classes can be overridden or expanded

play01:38

upon, allowing for even the most specific development requirements to be met.

play01:43

All of this power wouldn’t mean much without speed, but thankfully, GoJS is lightning-fast,

play01:48

taking full advantage of the HTML Canvas to support high-performance diagramming.

play01:53

So, what does it take to get all this power, apart from the GoJS library itself?

play01:58

You’ll need to know at least a little HTML, if only to set up the DIV elements for your

play02:03

Diagrams to reside in.

play02:04

And of course, you’ll need to be familiar with JavaScript.

play02:07

And that’s it.

play02:09

Again, GoJS is not dependent on any third party libraries or software.

play02:14

You can get GoJS from its official site, gojs.net, which also features hundreds of samples, dozens

play02:20

of intro pages to help get you started, and full API documentation.

play02:24

If you register for a free, unlimited trial, you will receive 30 days of free support.

play02:30

GoJS can also be obtained through npm, NuGet, and GitHub.

play02:33

Now, let’s build a simple example Diagram.

play02:38

First, we must create a new HTML page.

play02:42

Then, be sure to add a reference to your go.js file.

play02:48

GoJS Diagrams are usually associated with an HTML DIV element, so let’s add one here

play02:53

in the body of our HTML document, and assign it the id “myDiagramDiv.”

play02:58

We’ll also define a JavaScript function, init(), which fires only once the document

play03:04

body has finished loading.

play03:06

Time to build the Diagram.

play03:07

When we build GoJS objects, we make calls to the function go.GraphObject.make.

play03:13

Since we're going to do this so often, it makes sense to assign that function to a simpler variable

play03:18

-- here we use the dollar sign.

play03:20

In our init function, we’ll call go.GraphObject.make

play03:25

and pass go.Diagram as the first argument, and the ID of the DIV element our Diagram

play03:31

should be associated with as the second argument, in this case, “myDiagramDiv.”

play03:36

We store a reference to this Diagram in the variable myDiagram.

play03:41

We specify the size of the Diagram by assigning the height and width of the DIV element in

play03:45

its CSS.

play03:46

Let’s set width and height both to 300px.

play03:50

Let’s also give it a border so we can see it more clearly.

play03:54

Now we have a Diagram, but nothing in it.

play03:57

We need to give its Model some node and link data.

play03:59

Let’s create two Arrays; one for node data, and one for link data.

play04:03

Let’s add two elements to the nodeDataArray.

play04:07

Each element is an object in JSON format.

play04:10

The node data here is very simple -- it only contains a string value for the “key”

play04:14

property, a unique property that acts an identifier for each Node in GoJS.

play04:19

Let’s add nodes with keys “Alpha” and “Beta.”

play04:23

For our linkDataArray, we’ll have single element, with just two properties: “to”

play04:28

and “from.”

play04:29

These properties’ values correspond with the keys of the Nodes this Link connects.

play04:33

We’ll assign them to “Alpha” and “Beta”, the keys of the nodes we defined our nodeDataArray.

play04:39

Note that we do not need to specify key here.

play04:42

If no key is provided for an element in a Model’s nodeDataArray, a unique one is

play04:47

automatically generated, and keys are not necessary for link data.

play04:51

Next, we set our Diagram’s Model to a new GraphLinksModel.

play04:56

When we construct our GraphLinksModel, we pass it our nodeDataArray and linkDataArray,

play05:00

respectively.

play05:01

Now, if we load the page, we see a simple Diagram with two Nodes, connected by a single Link.

play05:07

Our Diagram works, but it’s not much to look at.

play05:10

We can style Nodes and Links by defining templates.

play05:13

First, we’ll build a nodeTemplate, which will define the style for all Nodes in the

play05:18

Diagram, then we’ll build a linkTemplate, which will define the style for all Links

play05:22

in the Diagram.

play05:23

You can have any number of Node and Link templates in your Diagram, though this example only

play05:27

uses one each.

play05:29

First, let’s set myDiagram.nodeTemplate to a fresh Node, using go.GraphObject.make

play05:36

and passing it go.Node as its first parameter.

play05:39

Nodes are comprised of Panels and other GraphObjects -- a Node itself is actually a Panel.

play05:46

Panels act as containers to hold other objects (such as TextBlocks, Shapes, and Pictures).

play05:52

The type of a Panel decides how its constituent elements are positioned graphically.

play05:57

For this example, we’ll just use one Panel.

play06:00

Since the first argument we passed to go.GraphObject.make was go.Node, the second argument corresponds

play06:07

with the type of Panel that will be used for the Node as a whole.

play06:10

Let’s use an Auto Panel, which will treat its first element as the main element, and

play06:15

position all following elements inside the first element.

play06:19

To do this, pass the string “Auto” as the second parameter.

play06:23

Then we define the elements inside this Auto Panel.

play06:27

We’ll have a Shape, which we define with a call to go.GraphObject.make with parameters

play06:33

go.Shape, a string corresponding with the desired figure for the Shape (in this case,

play06:38

“RoundedRectangle”, one of the built-in figures), and a JSON object with key/value

play06:43

pairs for the properties of the Shape.

play06:46

We will only define a single property, fill, and set its value to “white.”

play06:52

The next element to define is a TextBlock.This is defined with a call to go.GraphObject.make

play06:57

with parameters go.TextBlock, and the default text to show, in this case, the string “text.”

play07:04

Let’s see what we have now.

play07:07

Okay, so now each Node appears as a RoundedRectangle shape with the word “text” inside them.

play07:14

Note how the TextBlock appears inside the Shape element; this is because these elements

play07:18

were declared inside an Auto Panel, and so the first element, the Shape element, is treated

play07:23

as the main element, with all following elements enclosed within it.

play07:27

Let’s improve our nodeTemplate with some data bindings.

play07:31

Data bindings are a powerful feature of GoJS that allow for various appearance and behavior

play07:35

changes of Diagram Parts, such as Nodes, conditional on their data stored in the Model.

play07:40

Let’s bind the fill of the Shape element to a data property called “color,” and

play07:44

the text property of the TextBlock element to the “key” data property.

play07:48

First, let’s update our node data so the data bindings in the nodeTemplate will have

play07:52

data to work with.

play07:54

We already have key properties in the nodeDataArray, but we need to add the color property for

play07:59

each Node.

play08:00

Let’s add color: “lime” and color: “cyan” for “Alpha” and “Beta”, respectively.

play08:06

Now, to add a data binding to the Shape, we pass a new go.Binding instance after the properties object.

play08:14

The first parameter corresponds with the Shape property being bound; the second parameter

play08:19

corresponds with the data property to bind the first property to.

play08:23

In this case, “fill” is the first property and “color” is the second.

play08:27

We can bind the text of the TextBlock to the key property in node data the same way; by

play08:32

passing a new go.Binding to the TextBlock element of the nodeTemplate with the parameters

play08:38

“text” and “key.”

play08:39

Now see what we have.

play08:41

It’s looking better already.

play08:43

Let’s style the Links as well.

play08:46

This time, we’ll define myDiagram.linkTemplate, sort of like we did the nodeTemplate.

play08:51

However, Links are different kinds of Parts than Nodes -- they do not need a Panel type

play08:57

parameter, and the main element of a Link is the first Shape, whose geometry is computed

play09:01

dynamically by GoJS.

play09:03

We’ll have two shapes in our linkTemplate -- the first is the link itself, while the

play09:08

second is the arrow that points to the Node referenced by a Link’s “to” data property.

play09:13

The second Shape is assigned the string “Standard”,one of the built-in arrowhead values, to its "toArrow"

play09:20

property, and null to its "stroke" property.

play09:23

This ensures it looks like an arrow at the “to” end of the Link, and not like a square

play09:27

at the center of the Link.

play09:29

The default Shape is a 100 by 100 rectangle, and setting "toArrow" or "fromArrow" overrides this.

play09:36

We’ll also add data bindings to the Link elements.

play09:39

For the first Shape, the main element that represents the Link itself, we’ll bind the

play09:44

“stroke” property to the “color” data property.

play09:47

For the second Shape element, we’ll bind the “fill” property to the “color”

play09:51

data property.

play09:52

This will make the Link appear as whatever color is stored in the Link’s data.

play09:56

Let’s make our Link red, by setting its “color” property in the linkDataArray.

play10:02

We can also change the appearance of our Nodes and Links by editing some built-in properties

play10:06

on their elements.

play10:07

First, let’s set the "margin" of the TextBlock element in our nodeTemplate to 10 -- this

play10:13

creates some room around the text.

play10:15

We’ll also set the "strokeWidth" of the first Shape element in our linkTemplate to 3, making

play10:21

our links a little bit thicker than normal.

play10:24

Now look at what we have.

play10:28

There are hundreds of properties that can be edited for GoJS GraphObjects.

play10:32

Organizing Nodes and Links together is made easy with Groups.

play10:37

Groups will treat a collection of Nodes and Links as though they were a single Node.

play10:41

We can create a Group by adding a new element in the nodeDataArray, "Zeta", and setting the

play10:47

"isGroup" property to true.

play10:49

Then, we add two new Nodes to the nodeDataArray, Delta and Gamma, set their "color" property,

play10:56

and assign their "group" data property to the key of our new Group element, Zeta.

play11:01

We can also link to our new Group simply by adding an element to the linkDataArray, from,

play11:06

say, "Alpha", to "Zeta".

play11:09

The link will point to the Group, not a specific member of the Group.

play11:13

Now look at the result.

play11:16

We could further style our Groups by defining a groupTemplate, as we did for our nodeTemplate

play11:21

and linkTemplate.

play11:22

For now, this is enough.

play11:24

Keep an eye out for future videos that will explain, in depth, how to define complex templates

play11:29

with various Panels and elements.

play11:31

Please note all this code is available for download in the description below.

play11:35

That’s it for our coding overview.

play11:38

And that’s just the tip of the iceberg.

play11:40

With more features and customization, the possibilities for GoJS applications are limitless.

play11:45

Head over to gojs.net to see hundreds of samples, read more about GoJS features on the Intro

play11:50

pages, or peruse the full API documentation.

play11:54

Sign up for an unlimited free trial and you’ll get 30 days of developer-to-developer support

play11:59

for free.

Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
GoJSDiagramsJavaScriptWeb DevelopmentInteractiveCustom VisualizationsHTML CanvasModel/ViewData BindingWeb Browsers
Besoin d'un résumé en anglais ?