|
var smammal = { |
|
smammalGrid: function smammalGrid() { |
|
var rows = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'X'], |
|
cols = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'X'], |
|
margin = {top: 30, right: 10, bottom: 10, left: 10}, |
|
width = 600 - margin.left - margin.right, |
|
height = 600 - margin.top - margin.bottom, |
|
title = '', |
|
gridCount = rows.length, |
|
gridMargin = 2, |
|
colors = ["#ffffd9", "#edf8b1", "#c7e9b4", "#7fcdbb", "#41b6c4", "#1d91c0", "#225ea8", "#253494", "#081d58"], |
|
buckets = colors.length; |
|
|
|
function grid(selection) { |
|
//set up retained values |
|
var closureWidth = width, |
|
closureHeight = height, |
|
closureTitle = title, |
|
gridSize = Math.floor(closureWidth / gridCount), |
|
closureGridMargin = gridMargin, |
|
rowColsObj = {}, |
|
rowColsArr = []; |
|
|
|
//initialize the backing data for each cell |
|
rows.forEach(function (row, rowindex) { |
|
cols.forEach(function (col, colindex) { |
|
var rc = row + col; |
|
rc = pad(rc); |
|
rowColsObj[rc] = {}; |
|
rowColsObj[rc].row = row; |
|
rowColsObj[rc].col = col; |
|
rowColsObj[rc].count = 0; |
|
rowColsObj[rc].trapCoordinate = rc; |
|
}) |
|
}); |
|
|
|
//for each input data item |
|
selection.each(function (data) { |
|
//count captures |
|
data.forEach(function (d) { |
|
if (d.trapStatus.indexOf("5") != -1) { |
|
rowColsObj[d.trapCoordinate].count = rowColsObj[d.trapCoordinate].count + 1; |
|
} |
|
}); |
|
|
|
//map everything out to an array from the object |
|
rowColsArr = Object.keys(rowColsObj).map(function (key) { |
|
return rowColsObj[key]; |
|
}); |
|
rowColsArr.sort(function (a, b) { |
|
if (a.trapCoordinate < b.trapCoordinate) { return -1;} else { return 1;} |
|
}); |
|
|
|
//TODO: replace with a constant color scale |
|
var colorScale = d3.scaleQuantile() |
|
.domain([0, buckets - 1, d3.max(rowColsArr, function (d) { |
|
return d.count; |
|
})]) |
|
.range(colors); |
|
|
|
//build the top level svg |
|
var svg = d3.select('#smammal').append('svg') |
|
.attr('width', closureWidth + margin.left + margin.right) |
|
.attr('height', closureHeight + margin.top + margin.bottom) |
|
.append('g') |
|
.attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')'); |
|
|
|
//build the grid |
|
var ycount = -1; |
|
svg.selectAll('rect') |
|
.data(rowColsArr) |
|
.enter() |
|
.append('rect') |
|
.attr('x', function (d, i) { |
|
return i % gridCount * gridSize; |
|
}) |
|
.attr('y', function (d, i) { |
|
if (i % gridCount === 0) ycount = ycount + 1; |
|
return ycount * gridSize; |
|
}) |
|
.attr('rx', 3) |
|
.attr('ry', 3) |
|
.attr('width', gridSize - closureGridMargin) |
|
.attr('height', gridSize - closureGridMargin) |
|
.style('fill', function (d) { |
|
return colorScale(d.count); |
|
}) |
|
.append('svg:title') |
|
.text(function (d) { |
|
return "Coordinate " + d.trapCoordinate + ': ' + d.count + ' captures'; |
|
}); |
|
|
|
//build the title |
|
svg.append("text") |
|
.attr("x", (width / 2)) |
|
.attr("y", 0 - (margin.top / 2)) |
|
.attr("text-anchor", "middle") |
|
.style("font-size", "20px") |
|
.text(closureTitle); |
|
}); |
|
}; |
|
|
|
grid.width = function (value) { |
|
if (!arguments.length) return width; |
|
width = value; |
|
return grid; |
|
}; |
|
|
|
grid.height = function (value) { |
|
if (!arguments.length) return height; |
|
height = value; |
|
return grid; |
|
}; |
|
|
|
grid.title = function (value) { |
|
if (!arguments.length) return title; |
|
title = value; |
|
return grid; |
|
}; |
|
|
|
grid.gridMargin = function (value) { |
|
if (!arguments.length) return gridMargin; |
|
gridMargin = value; |
|
return grid; |
|
}; |
|
|
|
return grid; |
|
} |
|
|
|
} |