var squareGrid = function setupGrid(_selection) { var instance = {}; var square = 20, width = 900, height = 300 fill = '#130C0E', stroke = '#FDBB30'; var dispatch = d3.dispatch('_hover'); instance.render = function() { // calculate number of rows and columns var squaresRow = _.round(width / square); var squaresColumn = _.round(height / square); // create the svg var svg = d3.select(_selection).append('svg') .attr({ width: width, height: height }); // loop over number of columns _.times(squaresColumn, function(n) { // create each set of rows var rows = svg.selectAll('rect' + ' .row-' + (n + 1)) .data(d3.range(squaresRow)) .enter().append('rect') .attr({ class: function(d, i) { return 'square row-' + (n + 1) + ' ' + 'col-' + (i + 1); }, id: function(d, i) { return 's-' + (n + 1) + (i + 1); }, width: square, height: square, x: function(d, i) { return i * square; }, y: n * square, fill: fill, stroke: stroke }) .on('mouseover', dispatch._hover);; }); return instance; }; instance.square = function(value) { if (!arguments.length) return square; square = value; return this; }; instance.width = function(value) { if (!arguments.length) return width; width = value; return this; }; instance.height = function(value) { if (!arguments.length) return height; height = value; return this; }; instance.fill = function(value) { if (!arguments.length) return fill; fill = value; return this; }; instance.stroke = function(value) { if (!arguments.length) return stroke; stroke = value; return this; }; d3.rebind(instance, dispatch, 'on'); return instance; };