9951 explained code solutions for 126 technologies


javascript-d3How do I create a US map using D3.js?


To create a US map using D3.js, you can use the d3.json method to load a GeoJSON file that contains the US state boundaries. Then, you can use the d3.geoPath method to create the SVG paths that will define the map. Finally, you can use the d3.select method to add the SVG paths to the DOM.

Example code

d3.json("us-states.json", function(json) {
  svg.selectAll("path")
    .data(json.features)
    .enter()
    .append("path")
    .attr("d", d3.geoPath()
    .projection(d3.geoAlbersUsa()))
});

This code will take the GeoJSON file us-states.json and use the d3.geoPath and d3.geoAlbersUsa methods to create the SVG paths for the US map and add them to the DOM.

Parts of the code:

  • d3.json: Loads a GeoJSON file containing the US state boundaries
  • d3.geoPath: Creates the SVG paths that will define the map
  • d3.select: Adds the SVG paths to the DOM
  • d3.geoAlbersUsa: Specifies the projection to use when creating the SVG paths

Helpful links

Edit this code on GitHub