Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic How to use TreeLayout so it doesn't rearrange nodes horizontally (Read 2770 times)
Rennie
Full Member
***
Offline


I Love MindFusion!

Posts: 108
Location: Copenhagen, Denmark
Joined: Jul 17th, 2012
How to use TreeLayout so it doesn't rearrange nodes horizontally
Jan 2nd, 2013 at 2:01am
Print Post  
I have a diagram that represents a company organization chart, somewhat like the composite node demo program. I have a "Format Chart" button to let the user tidy up the layout, using the TreeLayout class.

My problem is that this apparently always places "sister nodes" (nodes with same parent) in the order in which they were created, even if the user has later changed his/her mind and rearranged them horizontally. At least when I use TreeLayoutBalance.Original, which is default - the other options do not seem relevant for my application.

Is there some way to tell TreeLayout to not reorder sister nodes horizontally?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to use TreeLayout so it doesn't rearrange nodes horizontally
Reply #1 - Jan 2nd, 2013 at 9:50am
Print Post  
The order of a node's children in the tree corresponds to the order of the node's OutgoingLinks, so you could sort them by their destination positions before applying TreeLayout:

Code
Select All
foreach (var node in diagram.Nodes)
{
	var links = new List<DiagramLink>(node.OutgoingLinks);
	links.Sort((l1, l2) => l1.EndPoint.X.CompareTo(l2.EndPoint.X));
	node.OutgoingLinks.Clear();
	foreach (var link in links)
		node.OutgoingLinks.Add(link);
}

new TreeLayout().Arrange(diagram); 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Rennie
Full Member
***
Offline


I Love MindFusion!

Posts: 108
Location: Copenhagen, Denmark
Joined: Jul 17th, 2012
Re: How to use TreeLayout so it doesn't rearrange nodes horizontally
Reply #2 - Jan 2nd, 2013 at 10:35pm
Print Post  
Thanks, Stoyo.

Just in case anyone else encounters this problem, and are still desperately clinging C# 2.0 syntax, here's my version:

Code
Select All
         foreach (DiagramNode diagramNode in _diagramView.Diagram.Nodes)
         {
            if (diagramNode.OutgoingLinks.Count >= 2)
            {
               List<DiagramLink> diagramLinks = new List<DiagramLink>(diagramNode.OutgoingLinks);

               diagramLinks.Sort(delegate(DiagramLink link1, DiagramLink link2)
                                 { return link1.EndPoint.X.CompareTo(link2.EndPoint.X); });

               diagramNode.OutgoingLinks.Clear();
               foreach (DiagramLink diagramLink in diagramLinks)
                  diagramNode.OutgoingLinks.Add(diagramLink);
            }
         } 



But I do have a suggestion. I can imagine a situation where you want to format the tree the way I'm requesting, but would prefer to not reorder the nodes. How about adding a new option

Code
Select All
TreeLayoutBalance.Current 



(or something else) that does that?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to use TreeLayout so it doesn't rearrange nodes horizontally
Reply #3 - Jan 3rd, 2013 at 11:25am
Print Post  
We'll have it in mind for next releases Smiley
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to use TreeLayout so it doesn't rearrange nodes horizontally
Reply #4 - Jan 8th, 2013 at 8:25am
Print Post  
This version adds a Preserve option to TreeLayoutBalance, which should preserve the initial geometric order of nodes in the tree:
https://mindfusion.eu/_beta/fcnet_treebalance.zip

It does not work for radial layouts at this time.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Rennie
Full Member
***
Offline


I Love MindFusion!

Posts: 108
Location: Copenhagen, Denmark
Joined: Jul 17th, 2012
Re: How to use TreeLayout so it doesn't rearrange nodes horizontally
Reply #5 - Jan 8th, 2013 at 9:14am
Print Post  
If by "initial" order you mean the current order when you issue the TreeLayout operation, then that sounds great. Thank you very much.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint