Working with TopoJSON and d3.js projections for mapping geospatial data.
forked from mbostock's block: U.S. Counties TopoJSON
forked from almccon's block: d3 + TopoJSON
license: gpl-3.0 |
Working with TopoJSON and d3.js projections for mapping geospatial data.
forked from mbostock's block: U.S. Counties TopoJSON
forked from almccon's block: d3 + TopoJSON
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
path { | |
fill: #ccc; | |
} | |
path:hover { | |
fill: red; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script src="//d3js.org/topojson.v1.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var path = d3.geo.path(); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var url = "https://gist.githubusercontent.com/mbostock/4090846/raw/d534aba169207548a8a3d670c9c2cc719ff05c47/us.json" | |
d3.json(url, function(error, topology) { | |
if (error) throw error; | |
console.log("topojson", topology) | |
var geojson = topojson.feature(topology, topology.objects.counties); | |
console.log("geojson", geojson) | |
var paths = svg.selectAll("path") | |
.data(geojson.features) | |
.enter().append("path") | |
.attr("d", path) | |
.attr("stroke",'white') | |
.attr("stroke-width",0.5); | |
paths | |
.on("mouseover", function(d){ | |
console.log(d) | |
d3.select(this) | |
.transition('stroke') | |
.duration(200) | |
.ease('cubic') | |
.attr('stroke','purple') | |
.attr("stroke-width",6); | |
}) | |
.on("mouseout", function(){ | |
d3.select(this) | |
.transition('stroke') | |
.duration(300) | |
.delay(250) | |
.ease('cubic-out') | |
.attr("stroke",'white') | |
.attr("stroke-width",0.5) | |
}) | |
.on("click",function(){ | |
d3.select(this).remove() | |
}) | |
}); | |
</script> |