
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
react-simple-maps
Advanced tools
An svg map component built with and for React. Please note that this is a work in progress!
React-simple-maps
aims to make working with svg maps in react easier. It handles tasks such as panning, zooming and simple rendering optimization, and takes advantage of parts of d3v4 and d3-geo instead of relying on the entire d3 library.
Since react-simple-maps
uses d3-fetch internally to fetch topojson files, you need to install whatwg-fetch and promise-polyfill depending on which browsers you need to support.
To install react-simple-maps
$ npm install react-simple-maps --save
React-simple-map
exposes a simple component that can be used to create any kind of map. In order to render the map you must provide a reference to a valid topojson file. You can find example topojson files in the topojson-maps
folder or here. To learn how to make your own topojson maps from shapefiles, please read "How to convert and prepare TopoJSON files for interactive mapping with d3" on medium.
import React, { Component } from "react"
import ReactDOM from "react-dom"
import ReactSimpleMap from "react-simple-map"
class App extends Component {
render() {
return(
<div>
<ReactSimpleMap
geographyUrl={ "/path/to/your/topojson-map-file.json" }
/>
</div>
)
}
}
document.addEventListener("DOMContentLoaded", () => {
ReactDOM.render(<App />, document.getElementById("app"))
})
Property | Type | Default |
---|---|---|
geographyUrl | String | null |
geographyPaths | Array | [] |
projection | String/Function | "Times" |
projectionConfig | Object | *see projection |
markers | Array | [] |
choropleth | Object | {} |
include | Array | [] |
exclude | Array | [] |
width | Number | 800 |
height | Number | 450 |
center | Array | [0,0] |
Zoom | Number | 1 |
minZoom | Number | 1 |
maxZoom | Number | 8 |
events | Object | *see events |
styles | Object | *see styles |
showControls | Boolean | false |
The projection can be set and configured in a number of ways. Basic projections such as mercator
, miller
, and times
are offered out of the box and can be set via the projection property.
<ReactSimpleMap
geographyUrl={ "/path/to/your/topojson-map-file.json" }
projection={ "mercator" }
/>
These basic projections can also be customized via the projectionConfig
property. The following example shows the default settings for the projectionConfig
property.
<ReactSimpleMap
geographyUrl={ "/path/to/your/topojson-map-file.json" }
projection={ "mercator" }
projectionConfig={{
scale: 160,
xOffset: 0,
yOffset: 0,
rotation: [0,0,0],
precision: 0.1,
}}
/>
If another projection is needed, a custom projection function can be passed to the projection
property. You can also access parameters such as the map width and height, as well as the original projectionConfig options in case you need them.
import { geoEckert1 } from "d3-geo-projections"
...
<ReactSimpleMap
geographyUrl={ "/path/to/your/topojson-map-file.json" }
projection={(width, height, projectionConfig) => {
return geoEckert1()
.scale(160)
.translate([ width / 2, height / 2 ])
.rotate([0,0,0])
.precision(0.1)
}}
/>
There are two groups of events, one for the geographies (any country or administrative area paths), and one for the markers. Currently supported events are onMouseEnter
, onMouseLeave
, onMouseMove
, and onClick
.
<ReactSimpleMap
geographyUrl={ "/path/to/your/topojson-map-file.json" }
events={{
geography: {
onClick: (geography, evt) => {
console.log(`Clicked geography: ${geography}`)
}
},
marker: {
onClick: (marker, evt) => {
console.log(`Clicked marker: ${marker}`)
}
},
}}
/>
The styles object consists of a set of functions, which expose a number of parameters. The following examples also shows the default settings of the map.
<ReactSimpleMap
geographyUrl={ "/path/to/your/topojson-map-file.json" }
styles={{
svg() {
return {
width: "100%",
height: "100%",
}
},
wrapper() {
return {
position: "relative",
}
},
loader() {
return {
position: "absolute",
left: 0,
right: 0,
top: 20,
padding: "1rem",
textAlign: "center",
}
},
geographies(zoom) {
return {
strokeWidth: 0.5 / zoom,
cursor: "all-scroll",
}
},
geography(choroplethValue, geography) {
return {
default: {
stroke: "#ffffff",
fill: choroplethValue ? choroplethValue.value : "#e9e7e5",
},
hover: {
fill: choroplethValue ? choroplethValue.value : "#d9d7d5",
stroke: "#ffffff",
cursor: "pointer",
},
}
},
marker(marker, zoom) {
return {
default: {
stroke: "#ffffff",
strokeWidth: 1.5,
fill: marker.fill || "#F44336",
},
hover: {
stroke: "#ffffff",
strokeWidth: 1.5,
fill: "#E53935",
cursor: "pointer",
},
}
}
}}
/>
MIT licensed. Copyright (c) Richard Zimerman 2017. See LICENSE.md for more details.
FAQs
An svg map chart component built with and for React
The npm package react-simple-maps receives a total of 96,999 weekly downloads. As such, react-simple-maps popularity was classified as popular.
We found that react-simple-maps demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.