Created
October 28, 2014 19:58
-
-
Save kosamari/5bcb6ab8ef292732463b to your computer and use it in GitHub Desktop.
D3 Map with Data
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
{"type":"FeatureCollection","features":[ | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-119.355165,35.458606]},"properties":{"name":"California","population":33871648}}, | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-72.874365,41.494852]},"properties":{"name":"Connecticut","population":3405565}}, | |
{"type":"Feature","id":"08","geometry":{"type":"Point","coordinates":[-105.203628,39.500656]},"properties":{"name":"Colorado","population":4301261}}, | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-75.561908,39.397164]},"properties":{"name":"Delaware","population":783600}}, | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-77.014001,38.910092]},"properties":{"name":"District of Columbia","population":572059}}, | |
{"type":"Feature","id":"04","geometry":{"type":"Point","coordinates":[-111.828711,33.373506]},"properties":{"name":"Arizona","population":5130632}}, | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-81.634622,27.795850]},"properties":{"name":"Florida","population":15982378}}, | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-86.261515,40.163935]},"properties":{"name":"Indiana","population":6080485}}, | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-69.719931,44.313614]},"properties":{"name":"Maine","population":1274923}}, | |
{"type":"Feature","id":"53","geometry":{"type":"Point","coordinates":[-121.624501,47.341728]},"properties":{"name":"Washington","population":5894121}}, | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-76.797396,39.145653]},"properties":{"name":"Maryland","population":5296486}}, | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-71.463342,43.153046]},"properties":{"name":"New Hampshire","population":1235786}}, | |
{"type":"Feature","id":"04","geometry":{"type":"Point","coordinates":[-111.828711,33.373506]},"properties":{"name":"Arizona","population":5130632}}, | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-74.645228,41.507548]},"properties":{"name":"New York","population":18976457}}, | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-79.667654,35.553334]},"properties":{"name":"North Carolina","population":8049313}}, | |
{"type":"Feature","id":"40","geometry":{"type":"Point","coordinates":[-96.834653,35.597940]},"properties":{"name":"Oklahoma","population":3450654}}, | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-71.448902,41.753318]},"properties":{"name":"Rhode Island","population":1048319}}, | |
{"type":"Feature","geometry":{"type":"Point","coordinates":[-81.032387,34.034551]},"properties":{"name":"South Carolina","population":4012012}} | |
]} |
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> | |
<html> | |
<head> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://d3js.org/topojson.v1.min.js"></script> | |
<script src="http://d3js.org/queue.v1.min.js"></script> | |
<style type="text/css"> | |
.states { | |
fill: RGBA(10, 166, 153, 0.5); | |
stroke: #E5E9EC; | |
stroke-width:1px; | |
} | |
.symbol { | |
fill: orange; | |
stroke: orange; | |
stroke-width:10px; | |
stroke-opacity: .5; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="graph"></div> | |
<script type="text/javascript"> | |
/* | |
* D3 script to create map | |
* This map is based on @mbostock 's Symbol Map | |
* original : http://bl.ocks.org/mbostock/4342045 | |
* Removed some states and added random fade in animation | |
*/ | |
var us, centroid; | |
var radius = d3.scale.sqrt() | |
.domain([0, 1e7]) | |
.range([0, 10]); | |
function mapRender() { | |
d3.select('#graph svg').remove(); | |
var width = 960, | |
height = 500; | |
var projection = d3.geo.albersUsa() | |
.translate([width / 2, height / 2]); | |
var path = d3.geo.path() | |
.projection(projection); | |
var svg = d3.select('#graph').append('svg') | |
.attr('width', width) | |
.attr('height', height) | |
.attr('transform', 'translate(' + (width/2) + | |
',' + (height/2) + ')'); | |
svg.append('path') | |
.attr('class', 'states') | |
.datum(topojson.feature(us, us.objects.states)) | |
.attr('d', path); | |
var circle = svg.selectAll('.symbol') | |
.data(centroid.features.sort(function(a, b) { return b.properties.population - a.properties.population; })) | |
.enter().append('path') | |
.attr('d', path.pointRadius(function(d) { return radius(d.properties.population); })); | |
function annimateCircle() { | |
circle.attr('class', 'symbol') | |
.attr('opacity', 0) | |
.transition().delay(function(d,i){ return i*500 ; }).duration(500) | |
.attr('opacity', function(){ | |
var rand = Math.ceil(Math.random()*10); | |
if(rand > 8 ){ | |
return 1; | |
} | |
return 0; | |
}) | |
.transition().duration(500) | |
.attr('opacity',0) | |
.each('end', annimateCircle); | |
} | |
annimateCircle(); | |
} | |
function setData(error, usdata, centroiddata){ | |
us = usdata; | |
centroid= centroiddata; | |
mapRender(); | |
} | |
queue().defer(d3.json, './us.json') | |
.defer(d3.json, './city.json') | |
.await(setData); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment