Skip to content

Instantly share code, notes, and snippets.

@mathisonian
Created January 27, 2016 01:45
Show Gist options
  • Save mathisonian/d5edeae8c805130a09e1 to your computer and use it in GitHub Desktop.
Save mathisonian/d5edeae8c805130a09e1 to your computer and use it in GitHub Desktop.
var svg = d3.select('#example-1')
.append('svg')
.attr('width', 202)
.attr('height', 202);
var data = d3.range(10).map(function (d, i) {
return i;
});
// draw a rect to act as an outline
svg.append('rect')
.style('fill', '#444')
.attr('width', 202)
.attr('height', 202);
svg.selectAll('g')
.data(data)
.enter()
.append('g')
.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('x', function(d) {
return d * 20 + 1;
})
.attr('y', function() {
var i = d3.select(this.parentNode).datum();
return i * 20 + 1;
})
.attr('width', 20)
.attr('height', 20)
.style('fill', function(d) {
var i = d3.select(this.parentNode).datum();
if ((i + d) % 2 !== 0) {
return 'black';
}
return 'white';
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<div id="example-1"></div>
<script type="text/javascript" src="./app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment