simplify bounding box calculations

This commit is contained in:
Nils Schneider 2012-09-04 11:44:43 +02:00
parent 4c61221ccc
commit 05039876ee

View file

@ -263,14 +263,12 @@ force.on("tick", function() {
var nodes = force.nodes() var nodes = force.nodes()
var n = nodes.length var n = nodes.length
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
var o = nodes[i] var n = nodes[i]
if (!o.fixed) { if (!n.fixed) {
node = d3.select(document.getElementById(o.id))[0][0] if (n.x < n.rx) n.x = n.rx
box = bounding_box(node) if (n.x > size[0] - n.rx) n.x = size[0] - n.rx
if (o.x < box.rx) o.x = box.rx if (n.y < n.ry) n.y = n.ry
if (o.x > size[0] - box.rx) o.x = size[0] - box.rx if (n.y > size[1] - n.ry) n.y = size[1] - n.ry
if (o.y < box.ry) o.y = box.ry
if (o.y > size[1] - box.ry) o.y = size[1] - box.ry
} }
} }
@ -484,12 +482,22 @@ function update() {
node.selectAll("ellipse") node.selectAll("ellipse")
.attr("rx", function(d) { .attr("rx", function(d) {
if (d.flags.client) return 4 var r
else return Math.max(10, d.name.length * 5) if (d.flags.client) r = 4
else r = Math.max(10, d.name.length * 5)
d.rx = r
return r
}) })
.attr("ry", function(d) { .attr("ry", function(d) {
if (d.flags.client) return 4 var r
else return 10 if (d.flags.client) r = 4
else r = 10
d.ry = r
return r
}) })
nodeEnter.filter(function(d) { nodeEnter.filter(function(d) {
@ -546,24 +554,6 @@ function update() {
show_node(hashstr) show_node(hashstr)
} }
function bounding_box(d) {
var c = d.firstChild
var r = {}
switch(c.nodeName) {
case "ellipse":
r.rx = c.rx.animVal.value
r.ry = c.ry.animVal.value
break;
case "circle":
r.rx = r.ry = c.r.animVal.value
break;
default:
r.rx = r.ry = 10
}
return r
}
reload() reload()
var timer = window.setInterval(reload, 30000) var timer = window.setInterval(reload, 30000)