Maplace.js

Data dashboards

Choropleth of the world

Every country filled by its GDP per person, its value on hover, with a legend on the map.

Markdown

Map and code

Run the examples on

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

These maps come from Google Maps and OpenFreeMap, which see your IP address. They stay off until you say so, once for the whole site. What loading the maps sends

Fetches countries.jsonStyled by choropleth.css

<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<!-- The world's countries, each filled by its GDP per person (Natural Earth's figures, in
     countries.json), on a globe its toggle flattens: hovering one shows its value, and a legend
     on the map's bottom left holds the classes and a select of every country, for the keyboard. -->
<!-- This example's look lives in the examples' styles/choropleth.css, which every version links. -->
<link rel="stylesheet" href="../../styles/choropleth.css">
<maplace-map id="choropleth-maplibre-map" globe popup-open-on="hover" center="20,10" zoom="2"
  list="choropleth-maplibre-legend" list-position="bottom-left"></maplace-map>
<script>
  // The legend's classes, lowest first, each with its colour: a scale colour-blind readers
  // can tell apart. In a plain script, run as the page is read, so the legend's list view is
  // defined before the map opens; the module below shades the countries with the same classes.
  const choroplethMaplibreClasses = [
    { below: 1500, fill: "#ffffcc", label: "Under $1,500" },
    { below: 4000, fill: "#a1dab4", label: "$1,500–4,000" },
    { below: 10000, fill: "#41b6c4", label: "$4,000–10,000" },
    { below: 30000, fill: "#2c7fb8", label: "$10,000–30,000" },
    { below: Number.POSITIVE_INFINITY, fill: "#253494", label: "Over $30,000" },
  ];
  const choroplethMaplibreNoData = { fill: "#d1d5db", label: "No data" };

  // The legend, a list view of your own: each class with its colour, then a select of every
  // country, for the keyboard.
  Maplace.defineListView("choropleth-maplibre-legend", (maplace) => {
    const countries = Maplace.createListView(maplace, { as: "select", label: "Country" });
    const legend = document.createElement("div");
    legend.className = "choropleth-legend";
    const title = document.createElement("p");
    title.className = "choropleth-legend-title";
    title.textContent = "GDP per person";
    const classes = document.createElement("ul");
    classes.className = "choropleth-classes";
    for (const { fill, label } of [...choroplethMaplibreClasses, choroplethMaplibreNoData]) {
      const entry = document.createElement("li");
      const swatch = document.createElement("span");
      swatch.className = "choropleth-swatch";
      swatch.style.background = fill;
      entry.append(swatch, label);
      classes.append(entry);
    }
    legend.append(title, classes, countries.element);
    return { element: legend, destroy: () => { countries.destroy(); legend.remove(); } };
  });
</script>
<script type="module">
  // Every country's outline, white between neighbours; under the pointer, its colour at full
  // strength and a dark outline; selected, a wider one.
  const OUTLINE = { stroke: "#ffffff", strokeWidth: 1, fillOpacity: 0.75 };
  const HOVERED = { stroke: "#111827", strokeWidth: 2, fillOpacity: 1 };
  const SELECTED = { ...HOVERED, strokeWidth: 3 };
  const dollars = new Intl.NumberFormat("en", { style: "currency", currency: "USD", maximumFractionDigits: 0 });

  const classOf = (value) =>
    value === null ? choroplethMaplibreNoData : (choroplethMaplibreClasses.find(({ below }) => value < below) ?? choroplethMaplibreNoData);

  // A country filled in its class's colour, its value in the popup hovering it opens.
  const shaded = (country) => {
    const value = country.data?.gdpPerPerson ?? null;
    const content = value === null ? choroplethMaplibreNoData.label : `${dollars.format(value)} per person`;
    return { ...country, style: { ...OUTLINE, fill: classOf(value).fill }, hoveredStyle: HOVERED, selectedStyle: SELECTED, content };
  };

  const response = await fetch("/examples/data/countries.json");
  document.getElementById("choropleth-maplibre-map").locations =
    Maplace.locationsFromGeoJSON(await response.json()).map(shaded);
</script>

How it works

The world’s countries, each filled by its GDP per person: a country of several polygons, as the United States with Alaska and Hawaii, is one multipolygon location, hovered, selected and listed as one.

  • The data: countries.json, Natural Earth’s 1:110m countries, which are public domain, as GeoJSON, each with its GDP per person (its GDP over its population, for years from 2007 to 2019, mostly 2019; none under 10,000 people). Its borders are Natural Earth’s, as they stand on the ground; Antarctica is left out.
  • HTML: popup-open-on="hover" on <maplace-map>, which opens a country’s popup under the pointer (Map attributes), and a list view of your own, holding the legend and a select of the countries, placed in a corner with list-position (List views), defined in a plain script so it exists before the map opens; a module script then fetches the countries and assigns locations, shading each one first, which a file named by src couldn’t do.
  • Script and Bundler: locationsFromGeoJSON, each country’s style, hoveredStyle and selectedStyle set from its value (Styles), popup: { openOn: "hover" } (Popup options), and mountControl with a createListView select inside the legend.
  • React: the same locations on <MaplaceProvider popup>, and a <MapControl> holding the legend and <LocationList as="select"> (<MapControl>).
  • On MapLibre, a globe: globe on the map opens it as the earth, with a “Globe” toggle that flattens it (Globe). Google’s map has no globe.