ThePartyTurtle ThePartyTurtle - 15 days ago 7
Javascript Question

What are all the GoJS NodeData and LinkData property fields?

So I'm wanting to dynamically build a GraphLinksModel in GoJS using addNodeData(nodedata) and addLinkData(linkdata). My issue is that I don't know what the fields of nodedata and linkdata are, other than the fields that are in example code. I can build the Node object and Link object for each of my nodes and links, but addNodeData and addLinkData don't actually want those, they want "nodedata" and "linkdata". When I try to addNodeData(Node) then I get an error.

Here are the descriptions of addNodeData(nodedata) and addLinkData(linkdata). As you can see, it just defines the parameter as an Object, with no hints, other that in the examples about what's supposed to be in it. Any direction or explanation would be appreciated.

Answer

There are no "all", the Node data can contain anything, arbitrarily, but must contain a key. If no key is defined, the GoJS model will automatically assign one. In other words, if you add { } as your node data and look at it, you'll find it is:

{ __gohashid: 1975, key: -5 }

__gohashid is internal to GoJS and should not be modified or used. They key is unique.

You can change the uniqueness function of the model, and you can even change the keyword for key.

You can read more in the Model overview in the API.

Other than there are a few built in data properties:

  • category, which specifies which Node or Link template to use, but the default is "" and its not necessary to specify.
  • In TreeModel there's also parent, which specifies the key of the parent Node.

Any other properties you find on the Model don't mean anything unless the Diagram's Node and Link templates have data-bindings to those names. For example this template has two data bindings:

// define a simple Node template
myDiagram.nodeTemplate =
  $(go.Node, "Auto",  // the Shape will go around the TextBlock
    $(go.Shape, "RoundedRectangle", { strokeWidth: 0},
      // Shape.fill is bound to Node.data.color
      new go.Binding("fill", "color")),
    $(go.TextBlock,
      { margin: 8 },  // some room around the text
      // TextBlock.text is bound to Node.data.key
      new go.Binding("text", "key"))
  );

If color exists in the Node data, it will be used as the Shape.fill in this Node template's Shape.

If key exists (it has to) in the Node data, it will be used as the TextBlock.text.

You can see that in action here: http://codepen.io/simonsarris/pen/ORwoLA?editors=1010

Let me know if you think there ought to be additional clarifications and I'll update the answer.

Comments