Skip to content

Instantly share code, notes, and snippets.

@enjalot
Last active June 25, 2016 22:24
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 enjalot/b8a64e0be3358ee90060962edb3db64a to your computer and use it in GitHub Desktop.
Save enjalot/b8a64e0be3358ee90060962edb3db64a to your computer and use it in GitHub Desktop.
WWSD #6-a: d3 mouse interaction (topojson)
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
fill: #ccc;
stroke: #fff;
stroke-width: .5px;
}
circle {
fill: #fff;
fill-opacity: 0.4;
stroke: #111;
}
path.active {
fill: #ff7f7f;
}
circle.active {
fill: #f00;
}
</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 projection = d3.geo.mercator();
var path = d3.geo.path()
.projection(projection)
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var url = "http://enjalot.github.io/wwsd/data/world/ne_50m_admin_0_countries.topojson"
var url2 = "http://enjalot.github.io/wwsd/data/world/ne_50m_populated_places_simple.topojson"
d3.json(url, function(error, countries) {
d3.json(url2, function(error, places) {
if (error) throw error;
console.log("topojson", countries, places);
var geocountries = topojson.feature(countries, countries.objects.ne_50m_admin_0_countries);
console.log("geojson", geocountries)
var geoplaces = topojson.feature(places, places.objects.ne_50m_populated_places_simple);
svg.selectAll("path")
.data(geocountries.features)
.enter().append("path")
.attr("d", path)
svg.selectAll("circle")
.data(geoplaces.features)
.enter().append("circle")
.attr({
r: 5,
cx: function(d) { return projection(d.geometry.coordinates)[0]},
cy: function(d) { return projection(d.geometry.coordinates)[1]}
})
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment