Skip to content

Instantly share code, notes, and snippets.

@kosamari
Created October 28, 2014 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosamari/5bcb6ab8ef292732463b to your computer and use it in GitHub Desktop.
Save kosamari/5bcb6ab8ef292732463b to your computer and use it in GitHub Desktop.
D3 Map with Data
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<!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>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment