Skip to content

Instantly share code, notes, and snippets.

@toja
Last active June 2, 2016 19:41
Show Gist options
  • Save toja/9cdc2eb3ab9610a20a850a435843e1f8 to your computer and use it in GitHub Desktop.
Save toja/9cdc2eb3ab9610a20a850a435843e1f8 to your computer and use it in GitHub Desktop.
Random Color Chart (RGB)
height: 480
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<script src="//d3js.org/d3.v3.min.js"></script>
<body>
<script>
var width = 960,
height = 480,
grid = [32,32],
space = 1.0;
var rectangleWidth = width / grid[0] - space * 2,
rectangleHeight = height / grid[1] - space * 2;
var xz = d3.range(0, width, width / grid[0]),
yz = d3.range(0, height, height / grid[1]);
var offset = function (x, y) { return { x: x + space, y: y + space };}
var offsets = d3.merge(xz.map(function(x) { return yz.map(function(y) { return offset(x, y); }); }));
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("g")
.selectAll("rect")
.data (offsets)
.enter().append("rect")
.attr("x", function (d) { return d.x; })
.attr("y", function (d) { return d.y; })
.attr("width", rectangleWidth)
.attr("height", rectangleHeight)
.style("fill", function () { return randomColor().toString(); });
function randomColor() {
return d3.rgb(
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256)
);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment