This takes @mbostock's state grid and turns it into a hop-scotch area.
Last active
September 28, 2016 14:35
-
-
Save queuebit/9203590ccfb4e4d47fc98b15cf91dbe3 to your computer and use it in GitHub Desktop.
Hop-scotch ASCII Choropleth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.state rect { | |
fill: #dedede; | |
} | |
.state text { | |
font: 12px sans-serif; | |
text-anchor: middle; | |
} | |
.state--selected rect { | |
fill: #9f4a6c; | |
} | |
.state--selected text { | |
fill: white; | |
} | |
</style> | |
<svg width="960" height="500"></svg> | |
<script id="grid" type="text/plain"> | |
8 | |
6 7 | |
5 | |
4 | |
2 3 | |
1 | |
</script> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var states = [], | |
selectedStates = ["2", "6", "8"]; | |
d3.select("#grid").text().split("\n").forEach(function(line, i) { | |
var re = /\w+/g, m; | |
while (m = re.exec(line)) states.push({ | |
name: m[0], | |
selected: selectedStates.indexOf(m[0]) >= 0, | |
x: m.index / 2, | |
y: i | |
}); | |
}); | |
var svg = d3.select("svg"), | |
width = +svg.attr("width"), | |
height = +svg.attr("height"); | |
var gridWidth = d3.max(states, function(d) { return d.x; }) + 1, | |
gridHeight = d3.max(states, function(d) { return d.y; }) + 1, | |
cellSize = 40; | |
var state = svg.append("g") | |
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")") | |
.selectAll(".state") | |
.data(states) | |
.enter().append("g") | |
.attr("class", function(d) { return "state" + (d.selected ? " state--selected" : ""); }) | |
.attr("transform", function(d) { return "translate(" + (d.x - gridWidth / 2) * cellSize + "," + (d.y - gridHeight / 2) * cellSize + ")"; }); | |
state.append("rect") | |
.attr("x", -cellSize / 2) | |
.attr("y", -cellSize / 2) | |
.attr("width", cellSize - 1) | |
.attr("height", cellSize - 1); | |
state.append("text") | |
.attr("dy", ".35em") | |
.text(function(d) { return d.name; }); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment