Skip to content

Instantly share code, notes, and snippets.

@biovisualize
Created May 14, 2013 07:25
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 biovisualize/5574274 to your computer and use it in GitHub Desktop.
Save biovisualize/5574274 to your computer and use it in GitHub Desktop.
Hexbin experiments
{"editor_editor":{"coffee":false,"vim":false,"emacs":false,"width":600,"height":300,"hide":false},"description":"Hexbin experiments","endpoint":"","display":"svg","public":true,"require":[{"name":"hexbin","url":"http://d3js.org/d3.hexbin.v0.min.js?5c6e4f0"}],"fileconfigs":{"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"inlet.svg":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
//Change these values
var hexRadius = 9;
var radiusOffset = -1.324;
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 800 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var dx = (hexRadius * 2 * Math.sin(Math.PI / 3));
var dy = hexRadius * 1.5
var points = [];
var colNum = ~~(width / hexRadius)-1;
var rowNum = ~~(height / hexRadius)-5;
for (var i = 0; i < rowNum; i++) {
for (var j = 0; j < colNum; j++) {
var offset = (i%2 === 0)? 0 : -dx/2;
points.push([dx * j + offset, dy * i]);
}
}
var hexbin = d3.hexbin()
.size([width, height])
.radius(hexRadius-radiusOffset);
var x = d3.scale.identity()
.domain([0, width]);
var y = d3.scale.linear()
.domain([0, height])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickSize(6, -height);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickSize(6, -width);
var svg = d3.select("svg")
svg.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("class", "mesh")
.attr("width", width)
.attr("height", height);
var clippedGroup = svg.append("g")
.attr("clip-path", "url(#clip)");
clippedGroup.selectAll(".hexagon")
.data(hexbin(points))
.enter().append("path")
.attr("class", "hexagon")
.attr("d", hexbin.hexagon())
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.style("stroke", function(d) { return 'red' ; });
clippedGroup.selectAll('circle')
.data(points)
.enter().append('circle')
.attr({
cx: function(d, i){return d[0];},
cy: function(d, i){return d[1];},
r: 2
});
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.hexagon {
fill: orange;
stroke: #000;
stroke-width: 1px;
}
circle{
fill: green;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment