Skip to content

Instantly share code, notes, and snippets.

@romsson
Last active January 5, 2018 22:06
Show Gist options
  • Save romsson/e21ae96259816d698e02b606ea4c0d56 to your computer and use it in GitHub Desktop.
Save romsson/e21ae96259816d698e02b606ea4c0d56 to your computer and use it in GitHub Desktop.
divide by two (grids)
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: Helvetica;
font-size: 10px;
}
rect {
fill: none;
stroke: black;
stroke-width: 1;
}
</style>
<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])
.mode("horizontal");
var data = d3.range(20);
var griddingData = gridding(data);
var svgSquares = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g");
svgSquares.selectAll(".square")
.data(griddingData, function(d) { return d.index; })
.enter().append("rect")
.attr("class", "square")
.attr("width", function(d) { return d.width; })
.attr("height", function(d) { return d.height; })
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
griddingData.map(function(d, i) {
var grid = d3.gridding()
.size([d.width, d.height])
.offset([d.x, d.y])
.mode("vertical");
griddingDataSub = grid(d3.range(i + 1));
svgSquares.selectAll(".square_" + i)
.data(griddingDataSub, function(d) { return d.index; })
.enter().append("rect")
.attr("class", "square_" + i)
.attr("width", function(d) { return d.width; })
.attr("height", function(d) { return d.height; })
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
//svgSquares.selectAll(".index_" + i)
// .data(griddingDataSub)
// .enter().append("text")
// .attr("class", ".index_" + i)
// .style('text-anchor', 'middle')
// .style('dominant-baseline', 'central')
// .style("font-size", function(d) { return d.height / 2; })
// .attr("transform", function(d) {
// return "translate(" + d.cx + "," + d.cy + ")";
// })
// .text(function(d, i) { return d.__value; });
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment