Last active
March 7, 2017 13:57
-
-
Save carlvlewis/2961378899bc61b775a3ac79856ad939 to your computer and use it in GitHub Desktop.
US State Grid
This file contains 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
<html> | |
<head> | |
<meta charset="utf-8"> | |
<style> | |
body{ | |
-webkit-font-smoothing: antialiased; | |
font-family: Roboto,sans-serif; | |
width:960px; | |
margin:50px auto; | |
} | |
.state-td{ | |
background: rgb(230, 230, 230); | |
border: 2px solid white; | |
text-align: center; | |
} | |
.CA{background:#0366D6;} | |
.SE{background:#C0404E;} | |
.NE{background:#0366D6;} | |
.MW{background:#C0404E;} | |
.SW{background:#4CAF50;} | |
.WE{background:#81D4FA;} | |
</style> | |
</head> | |
<body> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<table id="st-grid" style="width:600px; margin:0 auto;height:400px;table-layout:fixed;border-collapse:collapse;"><tbody></tbody></table> | |
<script> | |
var stateLoc =[ | |
["AK", , , , , , , , , ,"ME"] | |
,[ , , , , , , , , ,"VT","NH"] | |
,["WA","ID","MT","ND","MN", ,"MI", ,"NY","MA","RI"] | |
,["OR","UT","WY","SD","IA","WI","IN","OH","PA","NJ","CT"] | |
,["CA","NV","CO","NE","MO","IL","KY","WV","VA","MD","DE"] | |
,[ ,"AZ","NM","KS","AR","TN","NC","SC","DC", , ] | |
,[ , , ,"OK","LA","MS","AL","GA", , , ] | |
,["HI", , ,"TX", , , , ,"FL", , ] | |
]; | |
var USRegions ={ | |
'CA':['DE', 'KY', 'MD', 'OH', 'TN', 'VA', 'WV', 'DC'] | |
,'SE':['AL', 'AR', 'FL', 'GA', 'MS', 'NC', 'SC'] | |
,'NE':['CT', 'MA', 'ME', 'NH', 'NJ', 'NY', 'PA', 'RI', 'VT'] | |
,'MW':['IA', 'IL', 'IN', 'KS', 'MI', 'MN', 'MO', 'NE', 'ND', 'SD', 'WI'] | |
,'SW':['AZ', 'CO', 'MT', 'NM', 'OK', 'TX', 'UT', 'WY'] | |
,'WE':['CA', 'ID', 'NV', 'OR', 'WA'] | |
}; | |
var stateToRegion ={}; | |
d3.keys(USRegions).forEach(function(r){ | |
USRegions[r].forEach(function(st){ stateToRegion[st]=r; }) | |
}); | |
d3.select("#st-grid").select("tbody") | |
.selectAll("tr").data(stateLoc).enter().append("tr") | |
.selectAll("td").data(function(d){ return d;}).enter().append("td") | |
.attr("class",function(d){ return d==undefined? "blank-td" : "state-td " +stateToRegion[d];}) | |
.text(function(d){ return d}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment