react-simple-maps
Beautiful React SVG maps with d3-geo and topojson using a declarative api.
Top Related Projects
React friendly API wrapper around MapboxGL JS
🐯 visx | visualization components
A plotly.js React component from Plotly 📈
Open source library for using D3 in React
Quick Overview
The react-simple-maps
library is a React component that provides a simple and customizable way to render maps in a React application. It is built on top of the popular D3.js library and offers a wide range of features for creating interactive and responsive maps.
Pros
- Simplicity: The library provides a straightforward and easy-to-use API, making it accessible for developers of all skill levels.
- Customization:
react-simple-maps
offers a high degree of customization, allowing developers to adjust the appearance and behavior of the maps to fit their specific needs. - Performance: The library is built on top of D3.js, which is known for its performance and efficiency, ensuring smooth rendering of maps even with large datasets.
- Flexibility: The library supports a variety of map projections and data formats, making it suitable for a wide range of use cases.
Cons
- Limited Functionality: While
react-simple-maps
provides a solid foundation for rendering maps, it may lack some advanced features or functionality that more complex mapping applications might require. - Dependency on D3.js: The library's reliance on D3.js may be a drawback for developers who are not familiar with or comfortable working with the D3.js ecosystem.
- Potential Learning Curve: Although the library is relatively easy to use, developers may still need to invest time in understanding the concepts and APIs provided by
react-simple-maps
. - Limited Community Support: Compared to some other popular React libraries,
react-simple-maps
may have a smaller community and fewer resources available for troubleshooting and learning.
Code Examples
Here are a few examples of how to use the react-simple-maps
library:
- Rendering a Basic Map:
import React from 'react';
import { ComposableMap, Geographies, Geography } from 'react-simple-maps';
const BasicMap = () => {
return (
<ComposableMap>
<Geographies geography="/path/to/your/geography/data.json">
{(geographies) =>
geographies.map((geo) => (
<Geography key={geo.rsmKey} geography={geo} />
))
}
</Geographies>
</ComposableMap>
);
};
export default BasicMap;
This example demonstrates how to render a basic map using the ComposableMap
, Geographies
, and Geography
components provided by react-simple-maps
.
- Adding Tooltips:
import React, { useState } from 'react';
import { ComposableMap, Geographies, Geography, Tooltip } from 'react-simple-maps';
const MapWithTooltips = () => {
const [tooltipContent, setTooltipContent] = useState('');
const handleGeographyClick = (geo) => {
setTooltipContent(geo.properties.name);
};
return (
<ComposableMap>
<Geographies geography="/path/to/your/geography/data.json">
{(geographies) =>
geographies.map((geo) => (
<Geography
key={geo.rsmKey}
geography={geo}
onMouseEnter={() => {
handleGeographyClick(geo);
}}
onMouseLeave={() => {
setTooltipContent('');
}}
/>
))
}
</Geographies>
{tooltipContent && (
<Tooltip>{tooltipContent}</Tooltip>
)}
</ComposableMap>
);
};
export default MapWithTooltips;
This example shows how to add tooltips to the map by using the Tooltip
component and handling the onMouseEnter
and onMouseLeave
events on the Geography
component.
- Customizing the Map Appearance:
import React from 'react';
import { ComposableMap, Geographies, Geography } from 'react-simple-maps';
const CustomizedMap = () => {
return (
<ComposableMap
projection="geoAzimuthalEqualArea"
Competitor Comparisons
React friendly API wrapper around MapboxGL JS
Pros of react-map-gl
- Provides a comprehensive set of features for building interactive maps, including support for various map providers, overlays, and controls.
- Integrates well with other popular React libraries, such as Redux and React Router.
- Offers a wide range of customization options, allowing developers to tailor the map to their specific needs.
Cons of react-map-gl
- Steeper learning curve compared to react-simple-maps, as it requires a deeper understanding of map-related concepts and APIs.
- Larger bundle size due to the extensive set of features and dependencies.
- May be overkill for simple map-related use cases, where react-simple-maps could be a more lightweight and easier-to-use alternative.
Code Comparison
react-simple-maps:
<ComposableMap>
<Geographies geography="/features.json">
{({ geographies }) =>
geographies.map((geo) => (
<Geography key={geo.rsmKey} geography={geo} />
))
}
</Geographies>
</ComposableMap>
react-map-gl:
<ReactMapGL
mapboxApiAccessToken={process.env.REACT_APP_MAPBOX_TOKEN}
width="100%"
height="100%"
mapStyle="mapbox://styles/mapbox/streets-v11"
>
<Source type="geojson" data="/features.json">
<Layer type="fill" paint={{ 'fill-color': '#d6d6d6' }} />
</Source>
</ReactMapGL>
🐯 visx | visualization components
Pros of visx
- Modular Design: visx is a collection of reusable low-level components, allowing for more flexibility and customization compared to React Simple Maps.
- Extensive Visualization Options: visx provides a wide range of visualization components, including charts, graphs, and maps, making it a more comprehensive data visualization library.
- Active Development: visx has a larger and more active community, with more frequent updates and a wider range of available plugins and extensions.
Cons of visx
- Steeper Learning Curve: visx has a more complex API and requires a deeper understanding of data visualization concepts, which may be a barrier for some developers.
- Larger Bundle Size: visx has a larger bundle size compared to React Simple Maps, which can impact performance, especially in smaller projects.
- Limited Geographic Visualization: While visx offers a range of visualization options, its geographic visualization capabilities may not be as robust as those provided by React Simple Maps.
Code Comparison
React Simple Maps:
<ComposableMap>
<Geographies geography="/features.json">
{({ geographies }) =>
geographies.map((geo) => (
<Geography key={geo.rsmKey} geography={geo} />
))
}
</Geographies>
</ComposableMap>
visx:
<GeoMap
projection={geoMercator()}
data={data}
features={features}
onGeographyClick={(geography) => console.log(geography)}
>
{(map) => (
<Group>
{map.features.map((feature) => (
<Geography key={feature.id} feature={feature} />
))}
</Group>
)}
</GeoMap>
A plotly.js React component from Plotly 📈
Pros of react-plotly.js
- More extensive and diverse chart types, including 3D plots and statistical charts
- Built-in interactivity features like zooming, panning, and hover tooltips
- Robust documentation and community support
Cons of react-plotly.js
- Larger bundle size, which may impact load times for web applications
- Steeper learning curve due to more complex API and configuration options
- May be overkill for simple mapping needs
Code Comparison
react-plotly.js:
import Plot from 'react-plotly.js';
const MyPlot = () => (
<Plot
data={[{ x: [1, 2, 3], y: [2, 6, 3], type: 'scatter' }]}
layout={{ width: 600, height: 400, title: 'Simple Plot' }}
/>
);
react-simple-maps:
import { ComposableMap, Geographies, Geography } from "react-simple-maps";
const MyMap = () => (
<ComposableMap>
<Geographies geography={geoUrl}>
{({ geographies }) => geographies.map(geo => <Geography key={geo.rsmKey} geography={geo} />)}
</Geographies>
</ComposableMap>
);
Open source library for using D3 in React
Pros of react-d3-library
- Seamless integration of D3.js visualizations with React components
- Allows direct use of existing D3 code within React applications
- Provides a bridge between D3's imperative approach and React's declarative paradigm
Cons of react-d3-library
- Less active maintenance and updates compared to react-simple-maps
- Steeper learning curve for developers not familiar with D3.js
- May require more complex setup for certain types of visualizations
Code Comparison
react-d3-library:
import rd3 from 'react-d3-library';
const RD3Component = rd3.Component;
class MyComponent extends React.Component {
render() {
return <RD3Component data={this.state.d3} />;
}
}
react-simple-maps:
import { ComposableMap, Geographies, Geography } from "react-simple-maps";
const Map = () => (
<ComposableMap>
<Geographies geography={geoUrl}>
{({ geographies }) => geographies.map(geo => <Geography key={geo.rsmKey} geography={geo} />)}
</Geographies>
</ComposableMap>
);
The code comparison shows that react-d3-library requires wrapping D3 code in a React component, while react-simple-maps provides React components for map creation, offering a more declarative approach.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
react-simple-maps
Create beautiful SVG maps in react with d3-geo and topojson using a declarative api.
Read the docs, or check out the examples.
Why
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 d3-geo and topojson-client instead of relying on the entire d3 library.
Since react-simple-maps
leaves DOM work to react, it can also easily be used with other libraries, such as react-spring and react-annotation.
Install
To install react-simple-maps
$ npm install react-simple-maps
...or if you use yarn:
$ yarn add react-simple-maps
Usage
React-simple-maps
exposes a set of components that can be combined to create svg maps with markers and annotations. In order to render a map you have to provide a reference to a valid topojson file. You can find example topojson files on here 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 from "react";
import ReactDOM from "react-dom";
import { ComposableMap, Geographies, Geography } from "react-simple-maps";
// url to a valid topojson file
const geoUrl =
"https://raw.githubusercontent.com/deldersveld/topojson/master/world-countries.json";
const App = () => {
return (
<div>
<ComposableMap>
<Geographies geography={geoUrl}>
{({ geographies }) =>
geographies.map((geo) => (
<Geography key={geo.rsmKey} geography={geo} />
))
}
</Geographies>
</ComposableMap>
</div>
);
};
document.addEventListener("DOMContentLoaded", () => {
ReactDOM.render(<App />, document.getElementById("app"));
});
Check out the live example
The above will render a world map using the equal earth projection. You can read more about this projection on Shaded Relief and on Wikipedia.
For other examples and components, check out the documentation.
Map files
React-simple-maps does not restrict you to one specific map and relies on custom map files that you can modify in any way necessary for the project. This means that you can visualise countries, regions, and continents in various resolutions, as long as they can be represented using geojson/topojson.
In order for this to work properly, you will however need to provide these valid map files to react-simple-maps yourself. Luckily, there are decent sources for map files on github and elsewhere. Here are some you can check out:
License
MIT licensed. Copyright (c) Richard Zimerman 2017. See LICENSE.md for more details.
Top Related Projects
React friendly API wrapper around MapboxGL JS
🐯 visx | visualization components
A plotly.js React component from Plotly 📈
Open source library for using D3 in React
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot