'How can I "zoom" into one US state from a topojson of US counties?
I have a D3 topoJSON implementation in React Native that displays US counties' paths and state paths to form a map. I want to essentially "zoom" in or scale up to show only one state with the counties. The bordering paths of the states should be around the focused state. How can I do this?
const COUNTIES = feature(
counties,
counties.objects.counties
).features;
const [countyList, setCountyList] = useState([]);
const mapExtent = useMemo(() => {
return dimensions.width > dimensions.height / 2
? dimensions.height / 2 - 90
: dimensions.width - 90;
}, [dimensions]);
const statePaths = useMemo(() => {
const projection = d3.geoMercator().fitSize([mapExtent, mapExtent], {
type: "FeatureCollection",
features: COUNTIES,
});
const geoPath = d3.geoPath().projection(projection);
const svgPaths = COUNTIES.map(geoPath);
return svgPaths;
}, [dimensions]);
useEffect(() => {
setCountyList(
statePaths.map((path, i) => {
const curCounty = COUNTIES[i].properties.NAME;
const isCountyNameInData = data.some(
(county) => county.name === curCounty + " County"
);
const curCountyData = isCountyNameInData
? data.find((county) => county.name === curCounty + " County")["data"]
: null;
return (
<Path
key={COUNTIES[i].properties.NAME}
d={path}
stroke={"#535252"}
strokeOpacity={0.3}
strokeWidth={0.6}
fill={colorScale(curCountyData)}
/>
);
})
);
}, [data]);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
