Reputation: 1169
Hi i am trying to use force layout of d3js... I am creating g elements with rect and text inside them. And apply force but they overlap. I think it does not solve the size of the rect. What am i doing wrong?
var force = d3.layout.force()
.nodes(nodes)
.links([])
.size([w, h]);
force.on("tick", function(e) {
vis.selectAll("g")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
nodes.push({
w: 100,
h: 50,
val: 'dada'
});
nodes.push({
w: 100,
h: 50,
val: 'zona'
});
// Restart the layout.
force.start();
var node = vis.selectAll("g")
.data(nodes)
.enter()
.append("g")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.call(force.drag);
node.append("rect")
.attr("width", function(d) { return d.w; })
.attr("height", function(d) { return d.h; })
.style("fill", "#eee")
.style("stroke", "white")
.style("stroke-width", "1.5px")
node.append("text")
.text(function(d) { return d.val; })
.style("font-size", "12px")
.attr("dy", "1em")
Upvotes: 2
Views: 1868
Reputation: 5015
There are attributes that you can set in your d3.layout.declaration() that will allow you to deal with the overlap, such as:
var force = d3.layout.force()
.nodes(nodes)
.links([])
.gravity(0.05)
.charge(-1500)
.linkDistance(100)
.friction(0.5)
.size([w, h]);
Important note: I borrowed data from somewhere else and ran the code. So, the values above will need to be tweaked by you. Hope this helps.
Upvotes: 2