Finding level of node in tree

I m using treeMapper diagram as base the problem is i want to bind the level of each node of each tree to some color but each node is returning 0 for findtreelevel
how to find the level
Screenshot%20from%202020-02-10%2017-10-00

That is because the time that all bindings are evaluated, when the node is first added to the diagram, is before any links have been connected with the node. And the “key” is presumably unchanging.

I believe that the next natural thing to try is to set Node.linkConnected. But that would depend on the links being added from the root node down. Imagine creating the whole tree except for the root – adding the root would change the tree-level for every single existing node.

So I suggest that you set all of the Shape.fill properties in an “InitialLayoutCompleted” DiagramEvent listener.

The Org Chart Editor sample, https://gojs.net/latest/samples/orgcharteditor.html, does it on each layout by overriding TreeLayout.commitNodes where it also has access to the TreeVertex.level property, so it does not need to call Node.findTreeLevel.

in using the function from sample it says level does not exist on layout vertex

The property is there, but the editor/compiler isn’t smart enough to recognize that the LayoutVertex will actually always be a TreeVertex.

It also cannot know that your “SHAPE” GraphObject will be a Shape with a Shape.stroke.

shape is fine the but the compiler will not compile if there are errors in the code

Yes it can. Why haven’t you cast it to a TreeVertex?

the line 129 says this.diagram.layout.network.vertices and it is giving me null pointer exception my diagram has no layout same as in tree mapper example


The TreeLayout is the Group.layout.

yes so this should be done to group layout?

an example would be nice if u can manage

I suggest defining a subclass of TreeLayout and overriding the TreeLayout.commitNodes method, similar to how the Org Chart Editor sample does it.

i tried what u ask but my code still gives error on commitnodes that it doesnot exist on type layout
i saw same kind of error on another forum post this happens in angular


i tried down grading to 1.8 as you said in the post still didnt work

I suggest defining a subclass of TreeLayout and overriding the TreeLayout.commitNodes method, similar to how the Org Chart Editor sample does it.

i didnt knew how to do it so i did the code in “initiallayoutcompleted” and it worked but can u elaborate the suggestion u gave and how to do it in angular

https://gojs.net/latest/intro/extensions.html#OverridingMethodsBySubclassingLayout

The point is that to avoid compiler warnings you need to give the compiler more type information. You could do that by casts everywhere, or you can move the code into a class that will help provide that context. You will still need casts or other type declarations, but not as many.