Map Choropleth
terrae/choroplethFreeComponent
Thematic choropleth map with color-coded regions based on data values.
Install it
npx shadcn@latest add https://www.terrae.dev/choropleth.jsonSource
1"use client"23import type mapboxgl from "mapbox-gl"45import { useEffect, useId, useRef } from "react"6import { useMap } from "./hooks"78type ChoroplethColorStop = {9 value: number10 color: string11}1213type ChoroplethColorScale = {14 stops: ChoroplethColorStop[]15 interpolation?: "linear" | "step"16 nullColor?: string17}1819type ChoroplethFeature<P extends GeoJSON.GeoJsonProperties = GeoJSON.GeoJsonProperties> = GeoJSON.Feature<20 GeoJSON.Polygon | GeoJSON.MultiPolygon,21 P22>2324type ChoroplethData<P extends GeoJSON.GeoJsonProperties = GeoJSON.GeoJsonProperties> =25 | string26 | GeoJSON.FeatureCollection<GeoJSON.Polygon | GeoJSON.MultiPolygon, P>2728type ChoroplethClickEvent<P extends GeoJSON.GeoJsonProperties> = {29 feature: ChoroplethFeature<P>30 value: number | null31 coordinates: [number, number]32}3334type ChoroplethHoverEvent<P extends GeoJSON.GeoJsonProperties> = {35 feature: ChoroplethFeature<P> | null36 value: number | null37 coordinates: [number, number] | null38}3940type MapChoroplethProps<P extends GeoJSON.GeoJsonProperties = GeoJSON.GeoJsonProperties> = {41 data: ChoroplethData<P>42 valueProperty: keyof P | string43 colorScale: ChoroplethColorScale44 fillOpacity?: number45 strokeColor?: string46 strokeWidth?: number47 strokeOpacity?: number48 hoverEnabled?: boolean49 hoverFillOpacity?: number50 hoverStrokeColor?: string51 hoverStrokeWidth?: number52 neonAnimated?: boolean53 neonColors?: string[]54 neonDuration?: number55 onClick?: (event: ChoroplethClickEvent<P>) => void56 onHover?: (event: ChoroplethHoverEvent<P>) => void57}5859const DEFAULT_FILL_OPACITY = 0.860const DEFAULT_STROKE_COLOR = "#ffffff"61const DEFAULT_STROKE_WIDTH = 262const DEFAULT_STROKE_OPACITY = 163const DEFAULT_NULL_COLOR = "#cccccc"64const DEFAULT_HOVER_FILL_OPACITY = 165const DEFAULT_HOVER_STROKE_COLOR = "#000000"66const DEFAULT_HOVER_STROKE_WIDTH = 467const DEFAULT_NEON_COLORS = ["#00ffff", "#a855f7", "#ec4899", "#f97316", "#00ffff"]68const DEFAULT_NEON_DURATION = 80006970const buildColorExpression = (71 valueProperty: string,72 colorScale: ChoroplethColorScale73): mapboxgl.Expression | string => {74 const { stops, interpolation = "linear", nullColor = DEFAULT_NULL_COLOR } = colorScale7576 if (stops.length === 0) {77 return nullColor78 }7980 const sortedStops = [...stops].sort((a, b) => {81 return a.value - b.value82 })8384 if (interpolation === "step") {85 const stepArgs: (string | number | mapboxgl.Expression)[] = [["get", valueProperty], nullColor]8687 sortedStops.forEach((stop) => {88// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from terrae
All 40 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Map Animated Circleanimated-circle | terrae | Components | Free | 1 dep | |
| Map Animated Footprintanimated-footprint | terrae | Components | Free | 2 deps | |
| Map Animated Polygonanimated-polygon | terrae | Components | Free | 1 dep | |
| Map Animated Pulseanimated-pulse | terrae | Components | Free | 1 dep | |
| Map Arc Animatedarc-animated | terrae | Components | Free | 1 dep | |
| Map Blur Areablur-area | terrae | Components | Free | 1 dep | |
| Map Camera Followcamera-follow | terrae | Components | Free | 1 dep | |
| Map Circlecircle | terrae | Components | Free | 1 dep |
