TreeLayout Comparer

Hi,

I want to write a custom comparer for TreeLayout. I am looking at [EDIT: removed obsolete sample, read this instead] http://gojs.net/latest/intro/trees.html#CommonTreeLayoutProperties custom TreeLayout comparer sample and trying to figure out what it does. I know that v and w are TreeVertexes, but I can not figure out what index is. Also, what does the return value do and mean?

The comparer that I am trying to write, places nodes that are color1 on top and nodes with color2 on the bottom, the nodes are split up in those two groups by nodes that have links at the top and nodes without links at the bottom. I know how to get the amount of links a node has and its color, but what value would I return?

Thanks in advance for any help.



In that sample, the comparer appears to use the TreeVertex.index property. But as you can see in the documentation, TreeVertex, there is no “index” property.
Â
That is because that sample’s LayeredTreeLayout computes and remembers the “index” property on the TreeVertex instances in the makeNetwork method override.
Â
Unlike traditional compiled languages, JavaScript makes it too easy to dynamically add properties to any object, which makes finding uses (in this case initialization) of a property much harder.

The return value of a comparer function is a number that is less than zero or greater than zero depending on the intended relative order of the two vertexes.

Thank you for the help. I was able to get a custom comparer function working correctly.

Would it be possible to add a comparer function for LayeredDigraphLayout? My boss prefers this layout over the TreeLayout.

The problem is that LayeredDigraphLayout is designed to try to re-order the vertexes in a layer in order to reduce the number of link crossings. Trying to impose an order when there are multiple links coming in and going out of nodes would only make sense in the narrowest of situations.