Skip to content

Instantly share code, notes, and snippets.

@romsson
Last active December 20, 2017 05:18
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 romsson/6fdf368279929dc76a89ea257ad0a590 to your computer and use it in GitHub Desktop.
Save romsson/6fdf368279929dc76a89ea257ad0a590 to your computer and use it in GitHub Desktop.
centered rects size and position (grids)
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v4.js"></script>
<script src="http://romsson.github.io/d3-gridding/build/d3-gridding.js"></script>
<script>
var width = 600,
height = 400;
var gridding = d3.gridding()
.size([width, height])
.valueWidth(function(d) { return d["w"]/ 30; })
.valueHeight(function(d) { return d["h"]/ 30; })
.valueX("w")
.valueY("h")
.mode("coordinate");
// Distribution from https://bl.ocks.org/mbostock/4248145
var randomX = d3.randomNormal(width / 2, 100),
randomY = d3.randomNormal(height / 2, 100),
data = d3.range(2000).map(function() {
return {w: randomX(), h: randomY()};
});
var griddingData = gridding(data);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.selectAll(".square")
.data(griddingData)
.enter().append("rect")
.style("fill", "none")
.style("stroke", "black")
.attr("width", function(d) { return d.width; })
.attr("height", function(d) { return d.height; })
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment