react-svg-worldmap: SVG World Map Component for React

Description:

react-svg-worldmap is a React component that turns country-level data into a shaded SVG world map.

It includes the base geography, so a country code and a value are enough to plot each record.

Features

  • Mercator and Equal Earth projections for different geographic comparisons.
  • Country tooltips and callbacks for custom fills, clicks, and links.
  • An atlas theme with blue water and thin country borders.
  • Optional zoom and pan with country labels at closer scales.
  • Captioned location pins and an opt-in disputed-territory overlay.

Preview

svg-world-map

Render country-level data

Install the package in your React project.

npm install react-svg-worldmap

Use two-letter ISO country codes in country and a numeric value for shading. Data<number> checks the shape of the sample order records, and size="responsive" fits the SVG to its container.

import WorldMap, { type Data } from "react-svg-worldmap";
const orders: Data<number> = [
  { country: "us", value: 320 },
  { country: "de", value: 140 },
  { country: "jp", value: 95 },
];
export function OrdersByCountry() {
  return (
    <WorldMap
      data={orders}
      title="Orders by country"
      valueSuffix="orders"
      color="#2563eb"
      size="responsive"
      projection="equalEarth"
    />
  );
}

WorldMap props

PropEffect
data, titlePass country/value records and a map title. Use numeric values for a color scale.
sizeUse "responsive", an exact pixel width, or a preset from "sm" to "xxl". Presets can shrink to fit the container.
projectionDefaults to "mercator". Use "equalEarth" for an equal-area projection in a 2:1 frame.
theme"atlas" adds blue water, thin borders, and a neutral color for countries with no data. Props such as backgroundColor and borderColor override the theme.
color, valuePrefix, valueSuffixSet the highlight color and format the units displayed in the tooltip.
styleFunction(context)Return per-country CSS styles. Use countryValue to apply a custom color scale.
tooltipTextFunction(context)Return custom tooltip text. Use countryName, countryValue, and the unit fields to build the label.
onClickFunction(context), hrefFunction(context)Respond to a click or return a URL or anchor props for a country. The click callback also receives the React mouse event.
zoom, onZoomChange(state)Set zoom to true for default zoom and pan, or pass options such as maxScale and showCountryLabels. The callback reports changes in zoom or pan.
pinsAdd markers with a caption and coordinates: [longitude, latitude]. Use degrees and pass a stable id when markers can move or change.

Add zoom and a location pin

Use this map after the order data example to mark the Berlin office while keeping the country-level order counts.

The default maximum zoom is 8×. Detailed country borders load at 2× by default. For subnational boundaries, set detailLevel="regions" and supply a detailProvider with supports(countryCode) and loadRegions(countryCode). The regional overlay requires zoom and starts at 4× by default. Set zoom.regionDetailMinScale to change that threshold.

export function OrdersWithOffice() {
  return (
    <WorldMap
      data={orders}
      title="Orders and Berlin office"
      size="responsive"
      zoom={{ maxScale: 6, showCountryLabels: true }}
      pins={[{ id: "berlin", coordinates: [13.405, 52.52], caption: "Berlin" }]}
    />
  );
}

Show disputed territories

Set showDisputedTerritories to display the optional overlay. Use disputedTerritoryTooltipFunction to change its tooltip or onDisputedTerritoryClick to read the clicked territory’s metadata.

Related resources

Tags:

Add Comment