Choropleth Maps in JavaScript

How to make a D3.js-based choropleth map in JavaScript. A choropleth map shades geographic regions by value.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.

d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2010_alcohol_consumption_by_country.csv', function(err, rows){
      function unpack(rows, key) {
          return rows.map(function(row) { return row[key]; });
      }

    var data = [{
        type: 'choropleth',
        locationmode: 'country names',
        locations: unpack(rows, 'location'),
        z: unpack(rows, 'alcohol'),
        text: unpack(rows, 'location'),
        autocolorscale: true
    }];

    var layout = {
      title: {text: 'Pure alcohol consumption<br>among adults (age 15+) in 2010'},
      geo: {
          projection: {
              type: 'robinson'
          }
      }
    };

    Plotly.newPlot("myDiv", data, layout, {showLink: false});

});
</script>\n\t<script src="/?originalUrl=https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fd3%2F3.5.17%2Fd3.min.js"></script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2010_alcohol_consumption_by_country.csv', function(err, rows){\n function unpack(rows, key) {\n return rows.map(function(row) { return row[key]; });\n }\n\n var data = [{\n type: 'choropleth',\n locationmode: 'country names',\n locations: unpack(rows, 'location'),\n z: unpack(rows, 'alcohol'),\n text: unpack(rows, 'location'),\n autocolorscale: true\n }];\n\n var layout = {\n title: {text: 'Pure alcohol consumption<br>among adults (age 15+) in 2010'},\n geo: {\n projection: {\n type: 'robinson'\n }\n }\n };\n\n Plotly.newPlot(\"myDiv\", data, layout, {showLink: false});\n\n});\n"}">
d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv', function(err, rows){
      function unpack(rows, key) {
          return rows.map(function(row) { return row[key]; });
      }

      var data = [{
          type: 'choropleth',
          locationmode: 'USA-states',
          locations: unpack(rows, 'code'),
          z: unpack(rows, 'total exports'),
          text: unpack(rows, 'state'),
          zmin: 0,
          zmax: 17000,
          colorscale: [
              [0, 'rgb(242,240,247)'], [0.2, 'rgb(218,218,235)'],
              [0.4, 'rgb(188,189,220)'], [0.6, 'rgb(158,154,200)'],
              [0.8, 'rgb(117,107,177)'], [1, 'rgb(84,39,143)']
          ],
          colorbar: {
              title: {text: 'Millions USD'},
              thickness: 0.2
          },
          marker: {
              line:{
                  color: 'rgb(255,255,255)',
                  width: 2
              }
          }
      }];


      var layout = {
          title: {text: '2011 US Agriculture Exports by State'},
          geo:{
              scope: 'usa',
              showlakes: true,
              lakecolor: 'rgb(255,255,255)'
          }
      };

      Plotly.newPlot("myDiv", data, layout, {showLink: false});
});
</script>\n\t<script src="/?originalUrl=https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fd3%2F3.5.17%2Fd3.min.js"></script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv', function(err, rows){\n function unpack(rows, key) {\n return rows.map(function(row) { return row[key]; });\n }\n\n var data = [{\n type: 'choropleth',\n locationmode: 'USA-states',\n locations: unpack(rows, 'code'),\n z: unpack(rows, 'total exports'),\n text: unpack(rows, 'state'),\n zmin: 0,\n zmax: 17000,\n colorscale: [\n [0, 'rgb(242,240,247)'], [0.2, 'rgb(218,218,235)'],\n [0.4, 'rgb(188,189,220)'], [0.6, 'rgb(158,154,200)'],\n [0.8, 'rgb(117,107,177)'], [1, 'rgb(84,39,143)']\n ],\n colorbar: {\n title: {text: 'Millions USD'},\n thickness: 0.2\n },\n marker: {\n line:{\n color: 'rgb(255,255,255)',\n width: 2\n }\n }\n }];\n\n\n var layout = {\n title: {text: '2011 US Agriculture Exports by State'},\n geo:{\n scope: 'usa',\n showlakes: true,\n lakecolor: 'rgb(255,255,255)'\n }\n };\n\n Plotly.newPlot(\"myDiv\", data, layout, {showLink: false});\n});\n"}">
d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv', function(err, rows){
      function unpack(rows, key) {
          return rows.map(function(row) { return row[key]; });
      }

       var data = [{
            type: 'choropleth',
            locations: unpack(rows, 'CODE'),
            z: unpack(rows, 'GDP (BILLIONS)'),
            text: unpack(rows, 'COUNTRY'),
            colorscale: [
                [0,'rgb(5, 10, 172)'],[0.35,'rgb(40, 60, 190)'],
                [0.5,'rgb(70, 100, 245)'], [0.6,'rgb(90, 120, 245)'],
                [0.7,'rgb(106, 137, 247)'],[1,'rgb(220, 220, 220)']],
            autocolorscale: false,
            reversescale: true,
            marker: {
                line: {
                    color: 'rgb(180,180,180)',
                    width: 0.5
                }
            },
            tick0: 0,
            zmin: 0,
            dtick: 1000,
            colorbar: {
                autotic: false,
                tickprefix: '$',
                title: {text: 'GDP<br>Billions US$'}
            }
      }];

      var layout = {
          title: {text: '2014 Global GDP<br>Source: <a href="/?originalUrl=https%3A%2F%2Fplotly.com%2F%26quot%3Bhttps%3A%2F%2Fwww.cia.gov%2Flibrary%2Fpublications%2Fthe-world-factbook%2Ffields%2F2195.html%26quot%3B%26gt%3B%2520CIA%2520World%2520Factbook%26lt%3B%2Fa%26gt%3B"},
          geo:{
              showframe: false,
              showcoastlines: false,
              projection:{
                  type: 'mercator'
              }
          }
      };
      Plotly.newPlot("myDiv", data, layout, {showLink: false});
});
</script>\n\t<script src="/?originalUrl=https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fd3%2F3.5.17%2Fd3.min.js"></script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv', function(err, rows){\n function unpack(rows, key) {\n return rows.map(function(row) { return row[key]; });\n }\n\n var data = [{\n type: 'choropleth',\n locations: unpack(rows, 'CODE'),\n z: unpack(rows, 'GDP (BILLIONS)'),\n text: unpack(rows, 'COUNTRY'),\n colorscale: [\n [0,'rgb(5, 10, 172)'],[0.35,'rgb(40, 60, 190)'],\n [0.5,'rgb(70, 100, 245)'], [0.6,'rgb(90, 120, 245)'],\n [0.7,'rgb(106, 137, 247)'],[1,'rgb(220, 220, 220)']],\n autocolorscale: false,\n reversescale: true,\n marker: {\n line: {\n color: 'rgb(180,180,180)',\n width: 0.5\n }\n },\n tick0: 0,\n zmin: 0,\n dtick: 1000,\n colorbar: {\n autotic: false,\n tickprefix: '$',\n title: {text: 'GDP<br>Billions US$'}\n }\n }];\n\n var layout = {\n title: {text: '2014 Global GDP<br>Source: <a href="/?originalUrl=https%3A%2F%2Fplotly.com%2F%26quot%3Bhttps%3A%2F%2Fwww.cia.gov%2Flibrary%2Fpublications%2Fthe-world-factbook%2Ffields%2F2195.html%2F%26quot%3B%26gt%3B%2520CIA%2520World%2520Factbook%26lt%3B%2Fa%26gt%3B%26%2339%3B%7D%2C%5Cn%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520geo%3A%7B%5Cn%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520showframe%3A%2520false%2C%5Cn%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520showcoastlines%3A%2520false%2C%5Cn%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520projection%3A%7B%5Cn%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520type%3A%2520%26%2339%3Bmercator%26%2339%3B%5Cn%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%7D%5Cn%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%7D%5Cn%2520%2520%2520%2520%2520%2520%7D%3B%5Cn%2520%2520%2520%2520%2520%2520Plotly.newPlot(%5C%26quot%3BmyDiv%5C%26quot%3B%2C%2520data%2C%2520layout%2C%2520%7BshowLink%3A%2520false%7D)%3B%5Cn%7D)%3B%5Cn%26quot%3B%7D">
d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv', function(err, rows){
                function unpack(rows, key) {
return rows.map(function(row) { return row[key]; });
}
var data = [{
    type: 'choropleth',
    locationmode: 'USA-states',
    locations: unpack(rows, 'Postal'),
    z: unpack(rows, 'Population'),
    text: unpack(rows, 'State'),
    autocolorscale: true
}];

var layout = {
title: {text: '2014 US Popultaion by State'},
    geo:{
        scope: 'usa',
        countrycolor: 'rgb(255, 255, 255)',
        showland: true,
        landcolor: 'rgb(217, 217, 217)',
        showlakes: true,
        lakecolor: 'rgb(255, 255, 255)',
        subunitcolor: 'rgb(255, 255, 255)',
        lonaxis: {},
        lataxis: {}
    }
};
Plotly.newPlot("myDiv", data, layout, {showLink: false});
});
</script>\n\t<script src="/?originalUrl=https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fd3%2F3.5.17%2Fd3.min.js"></script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv', function(err, rows){\n function unpack(rows, key) {\nreturn rows.map(function(row) { return row[key]; });\n}\nvar data = [{\n type: 'choropleth',\n locationmode: 'USA-states',\n locations: unpack(rows, 'Postal'),\n z: unpack(rows, 'Population'),\n text: unpack(rows, 'State'),\n autocolorscale: true\n}];\n\nvar layout = {\ntitle: {text: '2014 US Popultaion by State'},\n geo:{\n scope: 'usa',\n countrycolor: 'rgb(255, 255, 255)',\n showland: true,\n landcolor: 'rgb(217, 217, 217)',\n showlakes: true,\n lakecolor: 'rgb(255, 255, 255)',\n subunitcolor: 'rgb(255, 255, 255)',\n lonaxis: {},\n lataxis: {}\n }\n};\nPlotly.newPlot(\"myDiv\", data, layout, {showLink: false});\n});\n"}">
d3.json('https://raw.githubusercontent.com/plotly/datasets/master/florida-red-data.json', function(redjson) {

  d3.json('https://raw.githubusercontent.com/plotly/datasets/master/florida-blue-data.json', function(bluejson) {

    Plotly.newPlot('myDiv', [{
      type: 'scattermap',
      lat: [46],
      lon: [-74]
    }], {
      title: {text: "Florida Counties"},
      height: 600,
      width: 600,
      map: {
        center: {
          lat: 28,
          lon: -84
        },
        style: 'light',
        zoom: 4.8,
        layers: [
          {
            sourcetype: 'geojson',
            source: redjson,
            type: 'fill',
            color: 'rgba(163,22,19,0.8)'
          },
          {
            sourcetype: 'geojson',
            source: bluejson,
            type: 'fill',
            color: 'rgba(40,0,113,0.8)'
          },
        ]
      }
    });


});

});
</script>\n\t<script src="/?originalUrl=https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fd3%2F3.5.17%2Fd3.min.js"></script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"d3.json('https://raw.githubusercontent.com/plotly/datasets/master/florida-red-data.json', function(redjson) {\n\n d3.json('https://raw.githubusercontent.com/plotly/datasets/master/florida-blue-data.json', function(bluejson) {\n\n Plotly.newPlot('myDiv', [{\n type: 'scattermap',\n lat: [46],\n lon: [-74]\n }], {\n title: {text: \"Florida Counties\"},\n height: 600,\n width: 600,\n map: {\n center: {\n lat: 28,\n lon: -84\n },\n style: 'light',\n zoom: 4.8,\n layers: [\n {\n sourcetype: 'geojson',\n source: redjson,\n type: 'fill',\n color: 'rgba(163,22,19,0.8)'\n },\n {\n sourcetype: 'geojson',\n source: bluejson,\n type: 'fill',\n color: 'rgba(40,0,113,0.8)'\n },\n ]\n }\n });\n\n\n});\n\n});\n"}">