Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-simple-maps

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-simple-maps

An svg map component built with and for React

  • 0.3.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

react-simple-maps

An svg map component built with and for React. Please note that this is a work in progress!

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 d3v4 and d3-geo instead of relying on the entire d3 library.

Prerequisites and installation

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

Usage

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"))
})

Props

PropertyTypeDefault
geographyUrlStringnull
geographyPathsArray[]
projectionString/Function"Times"
projectionConfigObject*see projection
markersArray[]
choroplethObject{}
includeArray[]
excludeArray[]
widthNumber800
heightNumber450
centerArray[0,0]
ZoomNumber1
minZoomNumber1
maxZoomNumber8
eventsObject*see events
stylesObject*see styles
showControlsBooleanfalse

Projection

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)
  }}
/>

Events

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}`)
      }
    },
  }}
/>

Styles

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",
        },
      }
    }
  }}
/>

License

MIT licensed. Copyright (c) Richard Zimerman 2017. See LICENSE.md for more details.

Keywords

FAQs

Package last updated on 07 Apr 2017

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc