I want to create MLM type binary tree with OrgChart. I need to limit node to have maximum of two child nodes and when chart is ready i want to export result to JSON file. Is this possible or there is some kind of other chart that will better suit my needs?
The Org Chart Editor sample already demonstrates writing out the model as JSON-formatted text, and reading it in again. Look at the Save and Load buttons on the page: Org Chart Editor.
Limiting the number of children is quite possible. I’ll look into the steps that one needs to take in changing that sample.
So far I have found three places where relationships might change in a manner that would violate your rule about at most two tree child nodes per node.
By the way, I hope you have already read GoJS Validation -- Northwoods Software as a general discussion about limiting the relationships that users can make. So the obvious thing to do is to change the node’s port element’s properties by setting:
// set the port properties:
portId: "", fromLinkable: true, toLinkable: true, cursor: "pointer",
fromMaxLinks: 2, toMaxLinks: 1
I have added the setting of GraphObject.fromMaxLinks and GraphObject.toMaxLinks.
But there are two other ways in which the sample could create links resulting in additional children. Although with the fromMaxLinks setting users might be prevented from drawing or reconnecting too many links with the predefined linking tools, there is no such limitation to your application code.
One way is when the user double-clicks the node. The nodeDoubleClick
function creates a new node that is a “child” of the clicked node. Clearly you would not want to do that when there already are two links coming out of the node. So, just add such a check:
function nodeDoubleClick(e, obj) {
var clicked = obj.part;
if (clicked !== null) {
if (clicked.findLinksOutOf().count >= 2) return; // limit to two tree children
. . .
Also, the sample supports “reparenting” by dropping a node onto another node. Those checks are embodied in the function mayWorkFor
. Again, it just needs an additional check:
function mayWorkFor(node1, node2) {
if (!(node1 instanceof go.Node)) return false; // must be a Node
if (node2.findLinksOutOf().count >= 2) return false; // limit to two tree children
. . .