Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic DiagramLink connection (Read 2249 times)
sledenev
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 25
Joined: Dec 16th, 2009
DiagramLink connection
Apr 6th, 2011 at 8:11am
Print Post  
Hi,

I need programmaticly build diagram nodes and links like this http://img858.imageshack.us/img858/49/20110406115329.png

Links from top node must be perpendicular to bottom nodes. Direction of links is also important.
The only way I see how to do this is to use AnchorPoints, but this solution puts some restrictions to the way user can draw links.

Can you advise another way to do this? Thanks in advance.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: DiagramLink connection
Reply #1 - Apr 6th, 2011 at 8:56am
Print Post  
Hi,

Assuming it's a tree, you could arrange it recursively like this:
Code
Select All
float FitChildren(DiagramNode root, float minWidth, float padding)
{
	float total = 0;
	foreach (DiagramNode child in root.Query("outlinks/destination"))
		total += FitChildren(child, minWidth, padding) + padding;
	total = Math.Max(total - padding, minWidth);

	RectangleF r = root.Bounds;
	r.Width = total;
	root.Bounds = r;

	return total;
}

void Arrange(DiagramNode root, float levelHeight, float padding)
{
	float x = root.Bounds.Left;
	foreach (DiagramNode child in root.Query("outlinks/destination"))
	{
		child.Move(x, root.Bounds.Top + levelHeight);
		x = child.Bounds.Right + padding;
		Arrange(child, levelHeight, padding);
		StraightenLink(child.IncomingLinks[0]);
	}
}

void StraightenLink(DiagramLink link)
{
	RectangleF r = link.Destination.Bounds;
	float x = r.Left + r.Width / 2;
	link.Style = LinkStyle.Polyline;
	link.SegmentCount = 1;
	link.ControlPoints[0] = new PointF(x, link.ControlPoints[0].Y);
	link.ControlPoints[1] = new PointF(x, link.ControlPoints[1].Y);
}

private void btnTestLayout_Click(object sender, EventArgs e)
{
	DiagramNode root = diagram.ActiveItem as DiagramNode;
	if (root != null)
	{
		FitChildren(root, 32, 8);
		Arrange(root, 32, 8);
	}
} 



If you need to straighten links drawn by the user, without moving nodes, check the SequenceDiagram example.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
sledenev
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 25
Joined: Dec 16th, 2009
Re: DiagramLink connection
Reply #2 - Apr 6th, 2011 at 2:41pm
Print Post  
That's exactly what I need. But after nodes arrangement when I move the root node links are back to their original positions. I couldn't find a way to prevent this. I need links to be "fixed" (I've already created group with root node as master item).
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: DiagramLink connection
Reply #3 - Apr 6th, 2011 at 2:57pm
Print Post  
I forgot to call link.UpdateFromPoints() after the ControlPoints assignments... After you add it, the links should stick to their new end points, as long as they are not Dynamic. This won't make them always stay vertical though, you will have to update all links connected to a node in response to NodeModified or NodeModifying events to achieve that.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
sledenev
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 25
Joined: Dec 16th, 2009
Re: DiagramLink connection
Reply #4 - Apr 6th, 2011 at 3:01pm
Print Post  
That suits me very well. Thank you very much!
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint