Basic hexbin map from geoJson in d3.js

This post describes how to build a very basic choropleth map of the world with d3.js. Note that the same kind of code would work with any geospatial data stored in geojson format. You can see many other examples in the choropleth map section of the gallery.

Hexbin map section

<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src=""></script>
<script src=""></script>
<script src=""></script>

<!-- Create an element where the map will take place -->
<svg id="my_dataviz" width="440" height="300"></svg>


// The svg
var svg ="svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height");

// Map and projection
var projection = d3.geoMercator()
    .scale(350) // This is the zoom
    .translate([850, 440]); // You have to play with these values to center your map

// Load external data and boot
d3.json("", function(data){

  // Draw the map
          .attr("fill", "#69a2a2")
          .attr("d", d3.geoPath()
          .attr("stroke", "black")



Related blocks →