Skip to content

Instantly share code, notes, and snippets.

@ccorcos
Created March 20, 2014 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccorcos/9657939 to your computer and use it in GitHub Desktop.
Save ccorcos/9657939 to your computer and use it in GitHub Desktop.
A more complete but still incomplete Bounded Force Layout
I found this: http://bl.ocks.org/mbostock/1129492
But it doesn't work when you want the svg to be responsive to the browser window size:
svg = d3.select("#network").append("svg")
.attr("viewBox", "0 0 " + width + " " + height)
.attr("preserveAspectRatio", "xMidYMid meet");
Since the height and width are the units of the svg now, we need to account for when the height and width aren't the same as when initialized. Note that for this example, height = width = 800. This example needs to be alters if height != width.
var currentWidth = $("svg").width();
var currentHeight = $("svg").height();
var w, h, xMin, yMin, xMax, yMax;
if (currentWidth > currentHeight) {
w = width * currentWidth / currentHeight;
h = height;
xMin = (width - w) / 2;
xMax = width + (w - width) / 2
yMin = 0;
yMax = height;
} else {
w = width;
h = height * currentHeight / currentWidth;
xMin = 0;
xMax = width;
yMin = (height - h) / 2
yMax = height + (h - height) / 2
}
nodeCircle
.attr("cx", function(d) {
return d.x = Math.max(xMin + r, Math.min(xMax - r, d.x));
})
.attr("cy", function(d) {
var r = Session.get("nodeRadius");
return d.y = Math.max(yMin, Math.min(yMax - r, d.y));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment