Not drawing a tree with the children in the right order

Hi Walter

I have JSON array object with key and parent. My JSON array is not consistent some childs will come first and some childs will come later. the parents and childs are not in order in the JSON. in Tree layout some of children not mapped to the parent. the tree layout missing the children and listing as orphans. Please advise what is the way to make them order or in gojs any feature.

You can affect the behavior of TreeLayout to control the order of the children for any parent, by setting TreeLayout.sorting and TreeLayout.comparer.

Hi Walter any idea how to invoke the sorting or comparer functions in Tree layout… I have below example.

and one more thing based on data i want to put expand and close tree nodes?? any idea how to do that?
Appreciate your help

myTreeDiagram =
$(go.Diagram, “myTreeDiagramDiv”,
{
allowMove: false,
allowCopy: false,
allowDelete: false,
allowHorizontalScroll: false,
layout:
$(go.TreeLayout,
{
alignment: go.TreeLayout.AlignmentStart,
angle: 0,
sorting: go.TreeLayout.SortingForwards,
compaction: go.TreeLayout.CompactionNone,
layerSpacing: 16,
layerSpacingParentOverlap: 1,
nodeIndent: 2,
nodeIndentPastParent: 0.88,
nodeSpacing: 0,
setsPortSpot: false,
setsChildPortSpot: false
})
});

Have you read GoJS Trees -- Northwoods Software ?

You probably want to use go.TreeLayout.SortingAscending or go.TreeLayout.SortingDescending.

If you search, you should find some examples of TreeLayout.comparer. For example:

Hi Walter
tryed with “sorting: go.TreeLayout.SortingAscending,” no luck

I am using below command to load the data…
myTreeDiagram.model = new go.TreeModel(nodeTreeDataArray);

OK. When you have a specific answerable question I’ll be happy to answer.

sorting: go.TreeLayout.SortingAscending,
is worked. thanks for the help. My data is bad that is messed up tree structure…

I had a similar problem and it was because I was using treeStyle: go.TreeLayout.StyleAlternating but I was only setting sorting and comparer but not alternateSorting and alternateComparer. Argh!

Thanks for pointing that out for future readers of this topic.

But I should clarify that what you say does not apply to this case because in his declaration of the TreeLayout he clearly does not set TreeLayout.treeStyle.

1 Like