Re-implementation of this example combining React with D3 and TopoJSON. Map data from Swiss Maps.
Last active
May 25, 2021 11:36
-
-
Save jstcki/9204795 to your computer and use it in GitHub Desktop.
Swiss Cantons and Municipalities with React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.country { | |
fill: #222; | |
} | |
.canton-boundaries { | |
fill: none; | |
stroke: #fff; | |
stroke-width: 1; | |
} | |
.municipality-boundaries { | |
fill: none; | |
stroke: #fff; | |
stroke-width: .3; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://d3js.org/topojson.v1.min.js"></script> | |
<script src="https://npmcdn.com/[email protected]/dist/react.min.js"></script> | |
<script src="https://npmcdn.com/[email protected]/dist/react-dom.min.js"></script> | |
<script src="https://npmcdn.com/trafo"></script> | |
<script type="text/babel"> | |
const width = 960; | |
const height = 500; | |
const path = d3.geo.path() | |
.projection(null); | |
const Map = ({ch}) => { | |
const country = topojson.feature(ch, ch.objects.country); | |
const municipalityBoundaries = topojson.mesh(ch, ch.objects.municipalities, function(a, b) { return a !== b; }); | |
const cantonBoundaries = topojson.mesh(ch, ch.objects.cantons, function(a, b) { return a !== b; }); | |
return ( | |
<svg width={width} height={height}> | |
<path className='country' d={path(country)} /> | |
<path className='municipality-boundaries' d={path(municipalityBoundaries)} /> | |
<path className='canton-boundaries' d={path(cantonBoundaries)} /> | |
</svg> | |
); | |
}; | |
d3.json('ch.json', (error, ch) => { | |
ReactDOM.render( | |
<Map ch={ch} />, | |
document.body | |
); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment