Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Is there a way to automatically space out the links on a node? (Read 1581 times)
twojtylak
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 9
Joined: Jan 9th, 2019
Is there a way to automatically space out the links on a node?
Jan 10th, 2019 at 5:08pm
Print Post  
Title says it all. I have nodes with multiple links coming in and going out of each one and I am wondering if there's a way to automatically space them out so the links aren't all going into the middle of the node?
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Online


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Is there a way to automatically space out the links on a node?
Reply #1 - Jan 11th, 2019 at 6:28am
Print Post  
There's no direct API for this outside of layout classes. You could distribute links with some custom code as below, e.g. call the distributeLinks function from linkCreated event handler, or for each node after building your graph / tree dynamically:

Code
Select All
function distributeLinks(node)
{
	var rect = node.getBounds();

	var bottomLinks = collectLinks(node,
		p => Math.abs(p.y - rect.bottom()) < 1);
	distributeX(node, bottomLinks);
}

function distributeX(node, links)
{
	var rect = node.getBounds();
	var nl = links.length;
	if (nl == 0)
		return;

	links.sort((l1, l2) =>
		otherEnd(l1, node).getCenter().x - otherEnd(l2, node).getCenter().x);
	var dx = rect.width / (nl + 1);
	var x = rect.left() + dx;
	for (var link of links)
	{
		if (link.getOrigin() == node)
		{
			var p = link.getStartPoint();
			p.x = x;
			link.setStartPoint(p);
		}
		else
		{
			var p = link.getEndPoint();
			p.x = x;
			link.setEndPoint(p);
		}
		x += dx;
		link.updateFromPoints();
	}
}

function collectLinks(node, predicate)
{
	var links = [];
	for (var link of node.getOutgoingLinks())
	{
		if (predicate(link.getStartPoint()))
			links.push(link);
	}
	for (var link of node.getIncomingLinks())
	{
		if (predicate(link.getEndPoint()))
			links.push(link);
	}
	return links;
}

function otherEnd(link, node)
{
	if (link.getOrigin() == node)
		return link.getDestination();
	else
		return link.getOrigin();
} 



You can add similar handlers for top / left / right sides.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
twojtylak
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 9
Joined: Jan 9th, 2019
Re: Is there a way to automatically space out the links on a node?
Reply #2 - Jan 11th, 2019 at 4:47pm
Print Post  
You, my friend, are a godsend! I love this library and you are making it so much easier for me to utilize!
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint