Skip to content

Instantly share code, notes, and snippets.

@clhenrick
Last active February 14, 2018 03:19
Show Gist options
  • Save clhenrick/1f08fd5a1266c3ef08e7ccdf39e5959e to your computer and use it in GitHub Desktop.
Save clhenrick/1f08fd5a1266c3ef08e7ccdf39e5959e to your computer and use it in GitHub Desktop.
grey scale matrix
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:10px;position:fixed;top:0;right:0;bottom:0;left:0; }
svg {
border: 1px solid #333
}
rect {
stroke: #757575;
stroke-width: 2;
}
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 280)
.attr("height", 160);
var cellSize = 40;
var color = d3.scaleLinear()
.range(['#fff', '#424242'])
.interpolate(d3.interpolateLab);
var data = [
[1.9, 1.9, 2, 1.75, 3.5, 4.2, 8.9],
[1.3, 1.5, 1.55, 1.6, 1.79, 2.6, 4.2],
[0.71, 0.7, 0.75, 0.8, 0.85, 0.9, 1.4],
[0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2]
];
data = data.reduce(function(acc, row, i) {
return acc.concat(row.map(function(d, j) {
return {
v: d,
x: j * cellSize,
y: i * cellSize,
};
}));
}, []);
color.domain([0, d3.max(data, function(d) { return d.v; })]);
svg.selectAll('rect')
.data(data)
.enter().append('rect')
.attr('x', function(d) { return d.x; })
.attr('y', function(d) { return d.y; })
.attr('width', cellSize)
.attr('height', cellSize)
.attr('fill', function(d) { return color(d.v); });
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment