Choropleth Chart

A composable geographic map chart for visualizing data across regions with interactive tooltips, zoom controls, and pattern support

Loading map data...

Installation

pnpm dlx shadcn@latest add @bklit/choropleth-chart

Usage

The Choropleth Chart uses a composable API similar to other charts. Build maps by combining components:

import { ChoroplethChart, ChoroplethFeatureComponent, ChoroplethGraticule, ChoroplethTooltip } from "@bklitui/ui/charts";
import * as topojson from "topojson-client";

// Load your GeoJSON data (from TopoJSON or direct GeoJSON)
const geojson = topojson.feature(topology, topology.objects.countries);

export default function WorldMap() {
  return (
    <ChoroplethChart data={geojson} aspectRatio="16 / 9">
      <ChoroplethGraticule />
      <ChoroplethFeatureComponent fill="var(--chart-1)" />
      <ChoroplethTooltip />
    </ChoroplethChart>
  );
}

Components

ChoroplethChart

The root component that sets up the Mercator projection and provides context to children.

PropTypeDefaultDescription
dataFeatureCollectionrequiredGeoJSON FeatureCollection with geographic features
marginPartial<Margin>{ top: 0, right: 0, bottom: 0, left: 0 }Chart margins
animationDurationnumber800Animation duration in ms
aspectRatiostring"16 / 9"CSS aspect ratio
scalenumberautoProjection scale (auto-calculated from width if not set)
center[number, number][0, 20]Center coordinates [longitude, latitude]
translate[number, number]autoTranslate offset [x, y]
zoomEnabledbooleanfalseEnable zoom and pan interactions
zoomMinnumber0.5Minimum zoom scale
zoomMaxnumber4Maximum zoom scale
initialZoomTransformMatrixidentityInitial zoom transform
classNamestring""Additional CSS class

ChoroplethFeatureComponent

Renders the geographic feature paths with hover states and optional patterns.

PropTypeDefaultDescription
fillstring-Fill color for all features (overrides getFeatureColor)
strokestring"var(--chart-grid)"Stroke color for borders
strokeWidthnumber0.5Border stroke width
fadedOpacitynumber0.4Opacity when another feature is hovered
getFeatureColor(feature, index) => string-Custom color function
patternsReactNode-Pattern definitions using @visx/pattern components
getFeaturePattern(feature, index) => string | null-Return pattern ID for a feature

ChoroplethGraticule

Renders optional graticule (latitude/longitude grid lines).

PropTypeDefaultDescription
strokestring"rgba(255,255,255,0.1)"Line color
strokeWidthnumber0.5Line width
step[number, number][10, 10]Grid step intervals [longitude, latitude] in degrees

ChoroplethTooltip

Displays tooltips for features on hover, following the mouse position.

PropTypeDefaultDescription
content(props) => ReactNode-Custom tooltip renderer
formatValue(value) => stringtoLocaleStringValue formatter
getFeatureName(feature, index) => string-Custom name getter
getFeatureValue(feature, index) => number-Value getter for display
valueLabelstring"Value"Label for the value row
classNamestring""Additional CSS class

Data Format

The choropleth expects a GeoJSON FeatureCollection:

interface FeatureCollection {
  type: "FeatureCollection";
  features: Array<{
    type: "Feature";
    geometry: Geometry; // Polygon, MultiPolygon, etc.
    properties: {
      name?: string;
      id?: string | number;
      [key: string]: unknown;
    };
  }>;
}

For TopoJSON data, convert it using topojson-client:

import * as topojson from "topojson-client";

const geojson = topojson.feature(topology, topology.objects.countries);

See the charts gallery for analytics color scales, zoom controls, and pattern fills.

Dependencies

This component requires:

pnpm add @visx/geo @visx/responsive @visx/pattern @visx/zoom topojson-client motion react-use-measure