Built with blockbuilder.org
Created
March 25, 2018 09:57
-
-
Save farazshuja/3413aebeec41a126d75ad0eac2998bfd to your computer and use it in GitHub Desktop.
US States
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
license: mit |
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
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://d3js.org/topojson.v2.min.js"></script> | |
<style> | |
.states { | |
fill: #dadada; | |
} | |
.state-borders { | |
fill: none; | |
stroke: #fff; | |
stroke-width: 0.5px; | |
stroke-linejoin: round; | |
stroke-linecap: round; | |
pointer-events: none; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="app" style="width: 100%; height: 700px;"></div> | |
<script> | |
var width = 960; | |
var height = 600; | |
const svg = d3.select('#app') | |
.append('svg') | |
.attr('id', 'chart') | |
.attr('width', width) | |
.attr('height', height) | |
const description = svg.append('g') | |
.attr('class', 'description') | |
.attr('transform', 'translate(800, 420)') | |
var path = d3.geoPath(); | |
d3.json("us-10m.v1.json", function(us) { | |
d3.json("state-names.json", function(data) { | |
svg.append("g") | |
.attr("class", "states") | |
.selectAll("path") | |
.data(topojson.feature(us, us.objects.states).features) | |
.enter().append("path") | |
.attr("d", path) | |
.on('mouseover', function (d) { | |
const state = data[d.id] | |
if(state) { | |
d3.select(this).attr('fill', generate(state.STATE_NAME)) | |
renderDescription(state) | |
} | |
}) | |
.on('mouseout', function (d) { | |
d3.select(this).attr('fill', '#dadada') | |
renderDescription() | |
}) | |
svg.append("path") | |
.attr("class", "state-borders") | |
.attr("d", path(topojson.mesh(us, us.objects.states, function (a, b) { return a !== b; }))); | |
}) | |
}); | |
function renderDescription (state) { | |
description.selectAll('*').remove() | |
if(state) { | |
description.append('text') | |
.attr('x', 0) | |
.attr('y', 0) | |
.text('State: ' + state.STATE_NAME) | |
description.append('text') | |
.attr('x', 0) | |
.attr('y', 20) | |
.text('ENS: ' + state.STATENS) | |
} | |
} | |
function generate(str) { | |
var hash = 0; | |
for (var i = 0; i < str.length; i++) { | |
hash = str.charCodeAt(i) + ((hash << 5) - hash); | |
} | |
var colour = '#'; | |
for (var i = 0; i < 3; i++) { | |
var value = (hash >> (i * 8)) & 0xFF; | |
colour += ('00' + value.toString(16)).substr(-2); | |
} | |
return colour; | |
} | |
</script> | |
</body> |
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
[ | |
{ | |
"STATE": 1, | |
"STUSAB": "AL", | |
"STATE_NAME": "Alabama", | |
"STATENS": 1779775 | |
}, | |
{ | |
"STATE": 2, | |
"STUSAB": "AK", | |
"STATE_NAME": "Alaska", | |
"STATENS": 1785533 | |
}, | |
{ | |
"STATE": 4, | |
"STUSAB": "AZ", | |
"STATE_NAME": "Arizona", | |
"STATENS": 1779777 | |
}, | |
{ | |
"STATE": 5, | |
"STUSAB": "AR", | |
"STATE_NAME": "Arkansas", | |
"STATENS": 68085 | |
}, | |
{ | |
"STATE": 6, | |
"STUSAB": "CA", | |
"STATE_NAME": "California", | |
"STATENS": 1779778 | |
}, | |
{ | |
"STATE": 8, | |
"STUSAB": "CO", | |
"STATE_NAME": "Colorado", | |
"STATENS": 1779779 | |
}, | |
{ | |
"STATE": 9, | |
"STUSAB": "CT", | |
"STATE_NAME": "Connecticut", | |
"STATENS": 1779780 | |
}, | |
{ | |
"STATE": 10, | |
"STUSAB": "DE", | |
"STATE_NAME": "Delaware", | |
"STATENS": 1779781 | |
}, | |
{ | |
"STATE": 11, | |
"STUSAB": "DC", | |
"STATE_NAME": "District of Columbia", | |
"STATENS": 1702382 | |
}, | |
{ | |
"STATE": 12, | |
"STUSAB": "FL", | |
"STATE_NAME": "Florida", | |
"STATENS": 294478 | |
}, | |
{ | |
"STATE": 13, | |
"STUSAB": "GA", | |
"STATE_NAME": "Georgia", | |
"STATENS": 1705317 | |
}, | |
{ | |
"STATE": 15, | |
"STUSAB": "HI", | |
"STATE_NAME": "Hawaii", | |
"STATENS": 1779782 | |
}, | |
{ | |
"STATE": 16, | |
"STUSAB": "ID", | |
"STATE_NAME": "Idaho", | |
"STATENS": 1779783 | |
}, | |
{ | |
"STATE": 17, | |
"STUSAB": "IL", | |
"STATE_NAME": "Illinois", | |
"STATENS": 1779784 | |
}, | |
{ | |
"STATE": 18, | |
"STUSAB": "IN", | |
"STATE_NAME": "Indiana", | |
"STATENS": 448508 | |
}, | |
{ | |
"STATE": 19, | |
"STUSAB": "IA", | |
"STATE_NAME": "Iowa", | |
"STATENS": 1779785 | |
}, | |
{ | |
"STATE": 20, | |
"STUSAB": "KS", | |
"STATE_NAME": "Kansas", | |
"STATENS": 481813 | |
}, | |
{ | |
"STATE": 21, | |
"STUSAB": "KY", | |
"STATE_NAME": "Kentucky", | |
"STATENS": 1779786 | |
}, | |
{ | |
"STATE": 22, | |
"STUSAB": "LA", | |
"STATE_NAME": "Louisiana", | |
"STATENS": 1629543 | |
}, | |
{ | |
"STATE": 23, | |
"STUSAB": "ME", | |
"STATE_NAME": "Maine", | |
"STATENS": 1779787 | |
}, | |
{ | |
"STATE": 24, | |
"STUSAB": "MD", | |
"STATE_NAME": "Maryland", | |
"STATENS": 1714934 | |
}, | |
{ | |
"STATE": 25, | |
"STUSAB": "MA", | |
"STATE_NAME": "Massachusetts", | |
"STATENS": 606926 | |
}, | |
{ | |
"STATE": 26, | |
"STUSAB": "MI", | |
"STATE_NAME": "Michigan", | |
"STATENS": 1779789 | |
}, | |
{ | |
"STATE": 27, | |
"STUSAB": "MN", | |
"STATE_NAME": "Minnesota", | |
"STATENS": 662849 | |
}, | |
{ | |
"STATE": 28, | |
"STUSAB": "MS", | |
"STATE_NAME": "Mississippi", | |
"STATENS": 1779790 | |
}, | |
{ | |
"STATE": 29, | |
"STUSAB": "MO", | |
"STATE_NAME": "Missouri", | |
"STATENS": 1779791 | |
}, | |
{ | |
"STATE": 30, | |
"STUSAB": "MT", | |
"STATE_NAME": "Montana", | |
"STATENS": 767982 | |
}, | |
{ | |
"STATE": 31, | |
"STUSAB": "NE", | |
"STATE_NAME": "Nebraska", | |
"STATENS": 1779792 | |
}, | |
{ | |
"STATE": 32, | |
"STUSAB": "NV", | |
"STATE_NAME": "Nevada", | |
"STATENS": 1779793 | |
}, | |
{ | |
"STATE": 33, | |
"STUSAB": "NH", | |
"STATE_NAME": "New Hampshire", | |
"STATENS": 1779794 | |
}, | |
{ | |
"STATE": 34, | |
"STUSAB": "NJ", | |
"STATE_NAME": "New Jersey", | |
"STATENS": 1779795 | |
}, | |
{ | |
"STATE": 35, | |
"STUSAB": "NM", | |
"STATE_NAME": "New Mexico", | |
"STATENS": 897535 | |
}, | |
{ | |
"STATE": 36, | |
"STUSAB": "NY", | |
"STATE_NAME": "New York", | |
"STATENS": 1779796 | |
}, | |
{ | |
"STATE": 37, | |
"STUSAB": "NC", | |
"STATE_NAME": "North Carolina", | |
"STATENS": 1027616 | |
}, | |
{ | |
"STATE": 38, | |
"STUSAB": "ND", | |
"STATE_NAME": "North Dakota", | |
"STATENS": 1779797 | |
}, | |
{ | |
"STATE": 39, | |
"STUSAB": "OH", | |
"STATE_NAME": "Ohio", | |
"STATENS": 1085497 | |
}, | |
{ | |
"STATE": 40, | |
"STUSAB": "OK", | |
"STATE_NAME": "Oklahoma", | |
"STATENS": 1102857 | |
}, | |
{ | |
"STATE": 41, | |
"STUSAB": "OR", | |
"STATE_NAME": "Oregon", | |
"STATENS": 1155107 | |
}, | |
{ | |
"STATE": 42, | |
"STUSAB": "PA", | |
"STATE_NAME": "Pennsylvania", | |
"STATENS": 1779798 | |
}, | |
{ | |
"STATE": 44, | |
"STUSAB": "RI", | |
"STATE_NAME": "Rhode Island", | |
"STATENS": 1219835 | |
}, | |
{ | |
"STATE": 45, | |
"STUSAB": "SC", | |
"STATE_NAME": "South Carolina", | |
"STATENS": 1779799 | |
}, | |
{ | |
"STATE": 46, | |
"STUSAB": "SD", | |
"STATE_NAME": "South Dakota", | |
"STATENS": 1785534 | |
}, | |
{ | |
"STATE": 47, | |
"STUSAB": "TN", | |
"STATE_NAME": "Tennessee", | |
"STATENS": 1325873 | |
}, | |
{ | |
"STATE": 48, | |
"STUSAB": "TX", | |
"STATE_NAME": "Texas", | |
"STATENS": 1779801 | |
}, | |
{ | |
"STATE": 49, | |
"STUSAB": "UT", | |
"STATE_NAME": "Utah", | |
"STATENS": 1455989 | |
}, | |
{ | |
"STATE": 50, | |
"STUSAB": "VT", | |
"STATE_NAME": "Vermont", | |
"STATENS": 1779802 | |
}, | |
{ | |
"STATE": 51, | |
"STUSAB": "VA", | |
"STATE_NAME": "Virginia", | |
"STATENS": 1779803 | |
}, | |
{ | |
"STATE": 53, | |
"STUSAB": "WA", | |
"STATE_NAME": "Washington", | |
"STATENS": 1779804 | |
}, | |
{ | |
"STATE": 54, | |
"STUSAB": "WV", | |
"STATE_NAME": "West Virginia", | |
"STATENS": 1779805 | |
}, | |
{ | |
"STATE": 55, | |
"STUSAB": "WI", | |
"STATE_NAME": "Wisconsin", | |
"STATENS": 1779806 | |
}, | |
{ | |
"STATE": 56, | |
"STUSAB": "WY", | |
"STATE_NAME": "Wyoming", | |
"STATENS": 1779807 | |
}, | |
{ | |
"STATE": 60, | |
"STUSAB": "AS", | |
"STATE_NAME": "American Samoa", | |
"STATENS": 1802701 | |
}, | |
{ | |
"STATE": 66, | |
"STUSAB": "GU", | |
"STATE_NAME": "Guam", | |
"STATENS": 1802705 | |
}, | |
{ | |
"STATE": 69, | |
"STUSAB": "MP", | |
"STATE_NAME": "Northern Mariana Islands", | |
"STATENS": 1779809 | |
}, | |
{ | |
"STATE": 72, | |
"STUSAB": "PR", | |
"STATE_NAME": "Puerto Rico", | |
"STATENS": 1779808 | |
}, | |
{ | |
"STATE": 74, | |
"STUSAB": "UM", | |
"STATE_NAME": "U.S. Minor Outlying Islands", | |
"STATENS": 1878752 | |
}, | |
{ | |
"STATE": 78, | |
"STUSAB": "VI", | |
"STATE_NAME": "U.S. Virgin Islands", | |
"STATENS": 1802710 | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment