When writing GoJS we solved this by having the following rules for data:
- Every node has a
key
, which must be unique
- Links can be represented one of two ways: In a Tree-like fashion (TreeModel), or as separate JSON object entities (GraphLinksModel)
If the flowchart or diagram is created in a Tree-like fashion, with only one parent per Node, then you don't need separate JSON data for links. Instead, each node can specify its own key
but also its parent key (as parent
or some other identifier), and this gives you enough information to make every link in the graph.
Some simplified sample JSON for a TreeModel (from Org Chart Editor). The JSON for nodes describes each with a key, and describes the parent relationship with another key:
{"key":"1", "name":"Stella" },
{"key":"2", "name":"Luke", "parent":"1"},
{"key":"3", "name":"Meg", "parent":"2"},
The other way is to have every link represented by its own JSON object, but unlike nodes, links don't actually need to have their own key
values, they only need to have a to
and from
value specifying the two Node keys that the link connects to.
The State Chart sample uses a GraphLinksModel, so one JSON object per each node and each link:
"nodeDataArray": [
{ "id": 0, "text": "Initial" },
{ "id": 1, "text": "First down" },
{ "id": 2, "text": "First up" }
],
"linkDataArray": [
{ "from": 0, "to": 0, "text": "up or timer" },
{ "from": 0, "to": 1, "text": "down" },
{ "from": 1, "to": 0, "text": "up (moved)" },
...
This allows for multiple parents, multiple links between nodes, two-way connections, etc.