Positioning Nodes in force directed Graph

108 views
Skip to first unread message

Sebbi007

unread,
Aug 22, 2011, 7:46:14 AM8/22/11
to d3-js
Hi All, i've a big Problem....

i've build an interactive Graph were Nodes dynamically expand and
remove neighbour's ...

the first node is normally positioned in the center ...

the next 10 neighbours to the first node are positioned all over the
area ( first x:0 y:0 .... the next are for example x:1500 y 900 or x:
900 y:-500 ) so they have very long distances ... how can i position
them shorter to the first node ?

here's my NodePath function where the Nodes are been added :



var node = nodesG.selectAll("path.node")
.data(nodesTree, function(d) { return d.Name;});
var nodeG = node.enter().append("svg:g");
var nodePath = nodeG.append("svg:path")
.attr("class", "node")
.attr("d", d3.svg.symbol()
.type(function(d) { return "cross"; }
)
.attr("id", function(d) { return d.Name; })
.attr("nodeIndex", function(d){ return d.index; })
.style("fill", function(d) { if(d.index == 0){return "violet";}
else { return getColorforVertexType(d); }})
.on("dblclick", clickExpand)
.call(force.drag);


thx for helping

Sebbi007

unread,
Aug 22, 2011, 7:50:12 AM8/22/11
to d3-js
Added :

in my tick function stands:

nodePath.attr("transform", function(d) { return "translate(" + d.x +
"," + d.y + ")"; });

Mike Bostock

unread,
Aug 22, 2011, 12:58:02 PM8/22/11
to d3...@googlegroups.com
> how can i position them shorter to the first node ?

I'm not sure I understand the question. Positions in force-directed
layouts are not static; they move according to the simulation. So even
if a node is at position [1500, 900] it doesn't tend to stay there
very long. If you want to adjust the layout, typically you do that
indirectly by adjusting the simulation parameters: gravity, charge,
linkDistance, etc.

Mike

Sebbi007

unread,
Aug 25, 2011, 2:40:37 AM8/25/11
to d3...@googlegroups.com
Hi Mike,

my problem was the Charge ... so the Nodes "exploded" in the first expand ;-) i've fixed this.

thank you for helping
Reply all
Reply to author
Forward
0 new messages