Skip to content

Instantly share code, notes, and snippets.

@enjalot
Last active June 18, 2016 21:29
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/a3342029863da04c6f70c679ba5edae4 to your computer and use it in GitHub Desktop.
Save enjalot/a3342029863da04c6f70c679ba5edae4 to your computer and use it in GitHub Desktop.
WWSD #2-b: Leaflet Choropleth
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = L.map('map').setView([30, 0], 2);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var url = "http://enjalot.github.io/wwsd/data/world/ne_50m_admin_0_countries.geojson"
d3.json(url, function(err, data) {
var maxPop = d3.max(data.features, function(d) { return d.properties.pop_est })
var color = d3.scale.threshold()
.domain([0, 1000000, 100000000, 1000000000])
.range(["#ffffcc","#c2e699","#78c679","#31a354","#006837"])
function highlightFeature(e) {
var layer = e.target;
layer.setStyle({
weight: 5,
color: '#666',
dashArray: '',
fillOpacity: 0.7
});
}
function resetHighlight(e) {
geojsonLayer.resetStyle(e.target);
}
function zoomToFeature(e) {
map.fitBounds(e.target.getBounds());
}
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: zoomToFeature
});
}
var geojsonLayer = L.geoJson(data, {
style: function(feature) {
return {
fillColor: color(feature.properties.pop_est),
fillOpacity:0.4,
weight: 1,
}
},
onEachFeature: onEachFeature
}).addTo(map)
})
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment