svgMap is a lightweight JavaScript library that lets you easily create interactive world maps, allowing you to display customizable data for each country in a visually engaging way.
Install
ES6
You can install svgMap via npm:
npm install --save svgmap
import svgMap from 'svgmap'; import 'svgmap/dist/svg-map.css';
CDN
jsDelivr is a great CDN choice for svgMap:
<script src="https://cdn.jsdelivr.net/npm/svgmap@v2.21.0/dist/svg-map.umd.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/svgmap@v2.21.0/dist/svg-map.min.css" rel="stylesheet">
Usage
Create an HTML element where to show your map, then use JavaScript to initialize:
<div id="svgMap"></div>
new svgMap({ targetElementID: 'svgMap', data: { data: { gdp: { name: 'GDP per capita', format: '{0} USD', thousandSeparator: ',', thresholdMax: 50000, thresholdMin: 1000 }, change: { name: 'Change to year before', format: '{0} %' } }, applyData: 'gdp', values: { AF: {gdp: 587, change: 4.73}, AL: {gdp: 4583, change: 11.09}, DZ: {gdp: 4293, change: 10.01} // ... } } });
Demo
The example code above creates a world map displaying the GDP per capita and its change from the previous year:
Options
You can pass the following options into svgMap:
| targetElementID | string |
The ID of the element where the world map will render.
Required |
| minZoom maxZoom |
float |
Minimal and maximal zoom level.
Default: 1 for minZoom, 25 for maxZoom |
| initialZoom | float |
Initial zoom level.
Default: 1.06 |
| initialPan | object |
Initial pan on x and y axis, e.g. { x: 120, y: 60 }.
Default: { x: 0, y: 0 } |
| zoomScaleSensitivity | float |
Sensitivity when zooming.
Default: 0.2 |
| mouseWheelZoomEnabled | boolean |
Enables or disables zooming with the scroll wheel.
Default: true |
| mouseWheelZoomWithKey | boolean |
Allow zooming only when one of the following keys is pressed: SHIFT, CONTROL, ALT, COMMAND, OPTION.
Default: false |
| mouseWheelKeyMessage | string |
The message when trying to scroll without a key.
Default: 'Press the [ALT] key to zoom' |
| mouseWheelKeyMessageMac | string |
The message when trying to scroll without a key on MacOS.
Default: 'Press the [COMMAND] key to zoom' |
| colorMax colorMin colorNoData |
string |
Color for highest value colorMax, lowest value colorMin and no data available colorNoData.
Default: '#CC0033', '#FFE5D9', '#E2E2E2' |
| flagType | 'emoji', 'image' |
The type of the flag in the tooltip.
Default: 'image' |
| flagURL | string |
The URL to the flags when using flag type 'image'. The placeholder {0} will get replaced with the lowercase ISO 3166-1 alpha-2 country code.
Default: 'https://cdn.jsdelivr.net/gh/hjnilsson/country-flags@latest/svg/{0}.svg' |
| hideFlag | boolean |
Hide the flag in tooltips.
Default: false |
| noDataText | string |
The text to be shown when no data is present.
Default: 'No data available' |
| touchLink | boolean | Set to true to open the link (see data.values.link) on mobile devices, by default the tooltip will be shown. |
| showTooltips | boolean | When false, disables hover and touch-following tooltips only. Persistent on-map labels from persistentTooltips are unaffected. On touch devices, countries with a link open it on the first tap instead of using the two-tap pattern (first tap preview, second tap navigate). |
| tooltipTrigger | 'hover', 'click' | How the floating tooltip opens with the mouse: 'hover' opens on mouseenter/mouseleave. 'click' opens on primary click and closes when clicking outside the map countries or tooltip. Only applies when showTooltips is true. |
| persistentTooltips | false, array, function |
Persistent tooltips fixed on the map when it loads: an array of country IDs, or a function (function (countryID, countryValues) { … }) to decide per country. Independent of showTooltips. Best used with showTooltips: false or tooltipTrigger: 'click'. |
| staticPins | false, array, function |
Static pins on the map at load time: an array of ISO 3166-1 alpha-2 country IDs (e.g. ['DE', 'FR']), or a function (function (countryID, countryValues) { … }) to decide per country. Pins are placed at the geographic center of each country (largest landmass). Independent of showTooltips.
Default: false |
| pinColor | string |
Default fill color for circle pins. Accepts CSS vars, color names, rgb or hex values. Can be overridden per country via data.values[id].pinColor.
Default: '#000000' |
| pinStrokeColor | string |
Default stroke color for circle pins. Can be overridden per country via data.values[id].pinStrokeColor.
Default: '#ffffff' |
| pinStrokeWidth | number |
Default stroke width for circle pins, in screen pixels (non-scaling stroke). Can be overridden per country via data.values[id].pinStrokeWidth. Set to 0 for no stroke.
Default: 1 |
| pinSize | number |
Default radius for circle pins, in SVG units (viewBox is 2000 × 1001). Can be overridden per country via data.values[id].pinSize.
Default: 8 |
| pinImage | string | Image URL used as a pin instead of the default circle. Can be overridden per country via data.values[id].pinImage. |
| pinImageWidth pinImageHeight |
number |
Width and height of the pin image in SVG units. Can be overridden per country via data.values[id].pinImageWidth and data.values[id].pinImageHeight.
Default: 20 for both |
| pinOffsetX pinOffsetY |
number |
Horizontal and vertical offset from the pin position, in SVG units. Added after auto placement or pinX/pinY. Can be overridden per country via data.values[id].pinOffsetX and data.values[id].pinOffsetY.
Default: 0 for both |
| onGetPin | function | Custom pin element. Signature: function (countryID, countryValues) { return svgElement; }. Return an SVG element (e.g. <circle>, <image>) to use instead of the default circle or image pin. The library positions it at the pin coordinates via transform. Return null to fall back to the default pin. |
| onGetTooltip | function | Called when a tooltip is created to custimize the tooltip content (function (tooltipDiv, countryID, countryValues) { return 'Custom HTML'; }). |
| onCountryClick | function | Called when the user clicks a country (primary button, pointer released without dragging). Signature: function (countryID, event) { … }. Use this for custom actions instead of or in addition to data.values.link. Return false to skip opening the URL when the country has a link. On touch devices with a link, the callback runs when the tap would navigate (not on the first tap that only shows the tooltip). Countries show a pointer cursor while this option is set. |
| countries: {} | Additional options specific to countries: | |
| ↳ EH | boolean |
When set to false, Western Sahara (EH) will be combined with Morocco (MA).
Default: true |
| data: {} | The chart data to use for coloring and to show in the tooltip. Use a unique data-id as key and provide following options as value: | |
| ↳ name | string | The name of the data, it will be shown in the tooltip. |
| ↳ format | string | The format for the data value, {0} will be replaced with the actual value. |
| ↳ thousandSeparator | string |
The character to use as thousand separator.
Default: ',' |
| ↳ thresholdMax | number | Maximal value to use for coloring calculations. |
| ↳ thresholdMin | number | Minimum value to use for coloring calculations. |
| ↳ applyData | string | The ID (key) of the data that will be used for coloring. |
| ↳ values | object | An object with the ISO 3166-1 alpha-2 country code as key and the chart data for each country as value. |
| ↳ color | string | Forces a color for this country. |
| ↳ link | string | An URL to redirect to when clicking the country. |
| ↳ linkTarget | string | The target of the link. By default the link will be opened in the same tab. Use '_blank' to open the link in a new tab. |
| ↳ pinColor | string | Pin fill color for this country (circle pins only). |
| ↳ pinStrokeColor | string | Pin stroke color for this country (circle pins only). |
| ↳ pinStrokeWidth | number | Pin stroke width for this country, in screen pixels (circle pins only). |
| ↳ pinSize | number | Pin radius for this country (circle pins only). |
| ↳ pinX ↳ pinY |
number | Pin position in SVG units; use together to override auto placement. |
| ↳ pinOffsetX ↳ pinOffsetY |
number | Horizontal and vertical offset from the pin position, in SVG units (after auto placement or pinX/pinY). |
| ↳ pinImage | string | Image URL used as the pin for this country. |
| ↳ pinImageWidth ↳ pinImageHeight |
number | Width and height of the pin image for this country. |
| countryNames | object | An object with the ISO 3166-1 alpha-2 country code as key and the country name as value. |
Localize
Use the option countryNames to translate country names.
In the folder demo/html/local you can find translations in following languages:
Arabic, Chinese, English, French, German, Hindi, Portuguese, Russian, Spanish, Urdu
To create your own translations, check out country-list by Saša Stamenković.
Attribution
If you need more detailed maps or more options for your data, there is a great open source project called datawrapper out there, with a lot more power than svgMap. Highmaps is also a great alternative.
svgMap uses svg-pan-zoom by Anders Riutta.
The country flag images are from country-flags by Hampus Nilsson.
I can see you are in india now from your map. Hopefully you are enjoying it.
Stephan i your svgMap code i need help.
I am dynamically pulling values from sharepoint for different countries and maintaining it in JSON with proper country code. How to initialize svgMap values fiels to this JSON so created.
Please help me here.
Thank you