UNPKG

6.94 kBMarkdownView Raw
1# react-svg-worldmap [![License: MIT](https://img.shields.io/badge/license-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://img.shields.io/npm/v/react-svg-worldmap.svg?style=flat)](https://www.npmjs.com/package/react-svg-worldmap) [![Demo: Simple Example](https://img.shields.io/badge/demo-live-red.svg)](https://react-svg-worldmap-simple-example.imfast.io)
2
3
4A simple, compact and free React SVG world map.
5
6~~~tsx
7import { WorldMap } from "react-svg-worldmap"
8...
9const data =
10 [
11 { country: "cn", value: 1389618778 }, // china
12 { country: "in", value: 1311559204 }, // india
13 ]
14...
15<WorldMap color="red" title="This is My Map" size="lg" data={data} />
16~~~
17
18## Why is it different?
19Focus on simple and free.
20
21* Draw countries on a world map.
22* Free - Really free with no limits.
23* No registration - It is just a pure react component.
24* No internet dependency - All the data is local, no calls to a back-end server.
25* Easy to learn, easy to use, easy to customize.
26
27## Yet another package for world map...but why?
28
29It all started with a fun project that I was building and needed to draw simple yet beautiful world's map. Searching for solutions I found many potential solutions like MapBox and Google Maps, but they were "too smart" for what I needed. They needed to "call home" for the data, they supported tons of options I didn't need, and while they included react-integrations, they were not completely native to the react world. There was definitely something missing. And that's when react-world-countries-map started.
30
31## Install
32
33In order to install, run the following command:
34
35~~~
36$ npm install react-svg-worldmap --save
37~~~
38
39## Usage
40
41Explore the example folder for a simple case for an end-to-end react app using the react-world-countries-map.
42
43Here is a simple example:
44
45~~~tsx
46import React from "react"
47import "./App.css"
48import { WorldMap } from "react-svg-worldmap"
49
50function App() {
51 const data =
52 [
53 { country: "cn", value: 1389618778 }, // china
54 { country: "in", value: 1311559204 }, // india
55 { country: "us", value: 331883986 }, // united states
56 { country: "id", value: 264935824 }, // indonesia
57 { country: "pk", value: 210797836 }, // pakistan
58 { country: "br", value: 210301591 }, // brazil
59 { country: "ng", value: 208679114 }, // nigeria
60 { country: "bd", value: 161062905 }, // bangladesh
61 { country: "ru", value: 141944641 }, // russia
62 { country: "mx", value: 127318112 } // mexico
63 ]
64
65 return (
66 <div className="App" >
67 <WorldMap color="red" title="Top 10 Populous Countries" value-suffix="people" size="lg" data={data} />
68 </div>
69 )
70}
71~~~
72
73## Customization
74
75### Data
76The only mandatory prop. Data contains an array of country/value objects, with values for countries that you have values for, (countries without a value will be blank). The country code is a 2 character string representing the country ([ISO alpha-2] (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)) and value is a number.
77
78Example of valid data prop:
79
80~~~tsx
81 const data =
82 [
83 { country: "cn", value: 1 }, // china
84 { country: "in", value: 2 }, // india
85 { country: "us", value: 3 } // united states
86 ]
87~~~
88
89### Custom Styling
90This is an optional more advanced customization option. When used, the developer has full control to define the color, opacity and any other style element of a country with data record.
91
92This is done by passing your custom implementation of the `styleFunction`. The function recieves as input the country context that includes country,countryValue: colorm, minValue and maxValue, and returns a json object representing the style.
93
94For example:
95~~~tsx
96const stylingFunction = (context : any) => {
97 const opacityLevel = 0.1 + (1.5 * (context.countryValue - context.minValue) / (context.maxValue - context.minValue))
98 return {
99 fill: context.country === "US" ? "blue" : context.color,
100 fillOpacity: opacityLevel,
101 stroke: "green",
102 strokeWidth: 1,
103 strokeOpacity: 0.2,
104 cursor: "pointer"
105 }
106}
107~~~
108
109### Optional Props
110
111| Prop | Type | Description |
112| ------------------- | ------- | ----------- |
113| data | Array | Mandatory. Array of JSON records, each with country/value. |
114| size | string | The size of your map, either "sm", md", or "lg" |
115| title | string | Any string for the title of your map |
116| color | string | Color for highlighted countries. A standard color string. E.g. "red" or "#ff0000" |
117| tooltipBgColor | string | Tooltip background color |
118| tooltipTextColor | string | Tooltip text color |
119| valuePrefix | string | A string to prefix values in tooltips. E.g. "$" |
120| valueSuffix | string | A string to suffix values in tooltips. E.g. "USD" |
121| frame | boolean | true/false for drawing a frame around the map |
122| frameColor | string | Frame color |
123| borderColor | string | Border color around each individual country. "black" by default |
124| type | string | Select type of map you want, either "tooltip" or "marker" |
125| styleFunction | (context: any) => {} | A callback function to customize styling of each country (see custom-style-example) |
126| tooltipTextFunction | (countryName: string, isoCode: string, value: string, prefix?: string, suffix?: string) => {} | A callback function to customize tooltip text (see localization-example) |
127
128## Samples
129
130### examples/simple-example
131 * A simple example of the world map
132 * 4 maps given two different data sets
133 * Example of some simple features using the default styling
134
135
136### examples/custom-style-example
137 * An example of a custom styling function
138 * Context type must remain any so that you can use the inputs that I pass to you
139 The inputs are as follows:
140
141 | Input | Type | Description |
142 | ---------------- | ------- | ----------- |
143 | country | string | ISO value for each country |
144 | countryValue | number | Value inputted for the specific country (this is the input data for the specific country) |
145 | color | string | The color that is inputted by the user for countries with values |
146 | minValue | number | The smallest value of the input data |
147 | maxValue | number | The largest value of the input data |
148
149
150### examples/localization-example
151 * An example showing how to use the tooltipTextFunction to locolize tooltip texts.
152 * The function translates both country names and values to spanish.
153 * For example:
154
155 | Data | Localized text |
156 | ---------------------------------------------- | ----------------------------------- |
157 | ``` { "country": "us", value: 331883986 } ``` | "Estados Unidos: 3.32 mil millónes" |
158
159## License
160MIT
161
\No newline at end of file