|
| 1 | +import styles from "./map.module.css"; |
| 2 | + |
| 3 | +import { RotateCcwIcon } from "lucide-react"; |
| 4 | +import { useEffect, useMemo, useRef, useState } from "react"; |
| 5 | +import { Tooltip } from "react-tooltip"; |
| 6 | + |
| 7 | +import { type GeoProjection, geoMercator, geoPath } from "d3-geo"; |
| 8 | +import { select } from "d3-selection"; |
| 9 | +import { type ZoomBehavior, zoom as d3Zoom, zoomIdentity } from "d3-zoom"; |
| 10 | + |
| 11 | +import type { Feature, Geometry } from "geojson"; |
| 12 | +import * as topo from "topojson-client"; |
| 13 | +import type { GeometryCollection, Topology } from "topojson-specification"; |
| 14 | + |
| 15 | +import geo from "../../../../data/geo.json"; |
| 16 | +import { type DimensionTableRow, type Metric, metricNames } from "../../api"; |
| 17 | +import { cls, formatMetricVal } from "../../utils"; |
| 18 | + |
| 19 | +const features = topo.feature(geo as unknown as Topology, geo.objects.geo as GeometryCollection).features as Feature< |
| 20 | + Geometry, |
| 21 | + { |
| 22 | + name: string; |
| 23 | + iso: string; |
| 24 | + } |
| 25 | +>[]; |
| 26 | + |
| 27 | +type Location = { |
| 28 | + name: string; |
| 29 | + iso: string; |
| 30 | +}; |
| 31 | + |
| 32 | +export const Worldmap = ({ |
| 33 | + metric, |
| 34 | + data, |
| 35 | +}: { |
| 36 | + metric: Metric; |
| 37 | + data?: DimensionTableRow[]; |
| 38 | +}) => { |
| 39 | + const svgRef = useRef<SVGSVGElement | null>(null); |
| 40 | + const containerRef = useRef<HTMLDivElement | null>(null); |
| 41 | + const [dimensions, setDimensions] = useState({ width: 800, height: 400 }); |
| 42 | + const [moved, setMoved] = useState(false); |
| 43 | + |
| 44 | + const [currentLocation, setCurrentLocation] = useState<Location | null>(null); |
| 45 | + const biggest = useMemo(() => data?.reduce((a, b) => (a.value > b.value ? a : b), data[0]), [data]); |
| 46 | + const countries = useMemo(() => { |
| 47 | + const countries = new Map<string, number>(); |
| 48 | + for (const row of data ?? []) { |
| 49 | + countries.set(row.dimensionValue, row.value); |
| 50 | + } |
| 51 | + return countries; |
| 52 | + }, [data]); |
| 53 | + |
| 54 | + const projection = useRef<GeoProjection>(); |
| 55 | + if (!projection.current) { |
| 56 | + projection.current = geoMercator() |
| 57 | + .scale((dimensions.width / dimensions.width) * 125) |
| 58 | + // .scale((dimensions.width / 800) * 170) |
| 59 | + // .translate([dimensions.width / 2, dimensions.height / 2]) |
| 60 | + .center([45, 45]); |
| 61 | + } |
| 62 | + const pathGenerator = geoPath(projection.current); |
| 63 | + |
| 64 | + const zoomBehavior = useRef<ZoomBehavior<SVGSVGElement, unknown>>(); |
| 65 | + if (!zoomBehavior.current) { |
| 66 | + zoomBehavior.current = d3Zoom<SVGSVGElement, unknown>() |
| 67 | + .scaleExtent([1, 8]) // Min and max zoom levels |
| 68 | + .on("zoom", (event) => { |
| 69 | + select(svgRef.current).select("g").attr("transform", event.transform); |
| 70 | + setMoved(true); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + if (!svgRef.current || !zoomBehavior.current) return; |
| 76 | + |
| 77 | + // Setup zoom behavior |
| 78 | + select(svgRef.current).call(zoomBehavior.current); |
| 79 | + |
| 80 | + const resizeObserver = new ResizeObserver((entries) => { |
| 81 | + if (entries[0].contentRect) { |
| 82 | + const { width, height } = entries[0].contentRect; |
| 83 | + setDimensions({ width, height }); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + if (containerRef.current) { |
| 88 | + resizeObserver.observe(containerRef.current); |
| 89 | + } |
| 90 | + |
| 91 | + return () => { |
| 92 | + if (containerRef.current) { |
| 93 | + resizeObserver.unobserve(containerRef.current); |
| 94 | + } |
| 95 | + }; |
| 96 | + }, []); |
| 97 | + |
| 98 | + return ( |
| 99 | + <div ref={containerRef} className={styles.worldmap} data-tooltip-float={true} data-tooltip-id="map"> |
| 100 | + <button |
| 101 | + type="button" |
| 102 | + className={cls(styles.reset, moved && styles.moved)} |
| 103 | + onClick={() => { |
| 104 | + setCurrentLocation(null); |
| 105 | + |
| 106 | + if (zoomBehavior.current && svgRef.current) { |
| 107 | + select(svgRef.current).call(zoomBehavior.current.transform, zoomIdentity); |
| 108 | + setMoved(false); |
| 109 | + } |
| 110 | + }} |
| 111 | + > |
| 112 | + <RotateCcwIcon size={18} /> |
| 113 | + </button> |
| 114 | + |
| 115 | + <svg ref={svgRef} style={{ display: "block" }} viewBox={"0 0 800 500"}> |
| 116 | + <title>WoldMap</title> |
| 117 | + <g> |
| 118 | + {features.map((feature, index) => ( |
| 119 | + <Landmass |
| 120 | + key={index} |
| 121 | + feature={feature} |
| 122 | + pathGenerator={pathGenerator} |
| 123 | + countries={countries} |
| 124 | + biggest={biggest} |
| 125 | + onSetLocation={setCurrentLocation} |
| 126 | + /> |
| 127 | + ))} |
| 128 | + </g> |
| 129 | + </svg> |
| 130 | + <Tooltip id="map" className={styles.tooltipContainer} classNameArrow={styles.reset} disableStyleInjection> |
| 131 | + {currentLocation && ( |
| 132 | + <div className={styles.tooltip} data-theme="dark"> |
| 133 | + <h2>{metricNames[metric]}</h2> |
| 134 | + <h3> |
| 135 | + {currentLocation.name} <span>{formatMetricVal(countries.get(currentLocation.iso) ?? 0, metric)}</span> |
| 136 | + </h3> |
| 137 | + </div> |
| 138 | + )} |
| 139 | + </Tooltip> |
| 140 | + </div> |
| 141 | + ); |
| 142 | +}; |
| 143 | + |
| 144 | +const Landmass = ({ |
| 145 | + feature, |
| 146 | + pathGenerator, |
| 147 | + countries, |
| 148 | + biggest, |
| 149 | + onSetLocation, |
| 150 | +}: { |
| 151 | + feature: Feature<Geometry, { name: string; iso: string }>; |
| 152 | + pathGenerator: ReturnType<typeof geoPath>; |
| 153 | + countries: Map<string, number>; |
| 154 | + biggest?: DimensionTableRow; |
| 155 | + onSetLocation: (location: Location | null) => void; |
| 156 | +}) => { |
| 157 | + const path = useMemo(() => pathGenerator(feature), [pathGenerator, feature]); |
| 158 | + const percent = (countries.get(feature.properties.iso) ?? 0) / (biggest?.value ?? 100); |
| 159 | + |
| 160 | + return ( |
| 161 | + <path |
| 162 | + d={path || ""} |
| 163 | + className={styles.geo} |
| 164 | + style={{ "--percent": percent } as React.CSSProperties} |
| 165 | + onMouseEnter={() => onSetLocation({ name: feature.properties.name, iso: feature.properties.iso })} |
| 166 | + onMouseLeave={() => onSetLocation(null)} |
| 167 | + /> |
| 168 | + ); |
| 169 | +}; |
0 commit comments