Socket
Socket
Sign inDemoInstall

svgmap

Package Overview
Dependencies
1
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    svgmap

svgMap is a JavaScript library that lets you easily create an interactable world map comparing customizable data for each country.


Version published
Weekly downloads
5.8K
increased by1.71%
Maintainers
1
Install size
3.50 MB
Created
Weekly downloads
 

Readme

Source

svgMap

svgMap is a JavaScript library that lets you easily create an interactable world map comparing customizable data for each country.

Live demo: https://stephanwagner.me/create-world-map-charts-with-svgmap#svgMapDemoGDP


Install

ES6

npm install --save svgmap
import svgMap from 'svgmap';
import 'svgmap/dist/svgMap.min.css';

CDN

<script src="https://cdn.jsdelivr.net/npm/svg-pan-zoom@3.6.1/dist/svg-pan-zoom.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/StephanWagner/svgMap@v2.10.1/dist/svgMap.min.js"></script>
<link href="https://cdn.jsdelivr.net/gh/StephanWagner/svgMap@v2.10.1/dist/svgMap.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 }
      // ...
    }
  }
});

This example code creates a world map with the GDP per capita and its change to the previous year: https://stephanwagner.me/create-world-map-charts-with-svgmap#svgMapDemoGDP


Options

You can pass the following options into svgMap:

OptionTypeDefault
targetElementIDstringThe ID of the element where the world map will render (Required)
minZoomfloat1Minimal zoom level
maxZoomfloat25Maximal zoom level
initialZoomfloat1.06Initial zoom level
initialPanobjectInitial pan on x and y axis (e.g. { x: 30, y: 60 })
showContinentSelectorbooleanfalseShow continent selector
zoomScaleSensitivityfloat0.2Sensitivity when zooming
showZoomResetbooleanfalseShow zoom reset button
mouseWheelZoomEnabledbooleantrueEnables or disables zooming with the scroll wheel
mouseWheelZoomWithKeybooleanfalseAllow zooming only when one of the following keys is pressed: SHIFT, CONTROL, ALT, COMMAND, OPTION
mouseWheelKeyMessagestring'Press the [ALT] key to zoom'The message when trying to scroll without a key
mouseWheelKeyMessageMacstring Press the [COMMAND] key to zoomThe message when trying to scroll without a key on MacOS
colorMaxstring'#CC0033'Color for highest value
colorMinstring'#FFE5D9'Color for lowest value
colorNoDatastring'#E2E2E2'Color when there is no data
flagType'image', 'emoji''image'The type of the flag in the tooltip
flagURLstringThe 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'
hideFlagbooleanfalseHide the flag in tooltips
noDataTextstring'No data available'The text to be shown when no data is present
touchLinkbooleanfalseSet to true to open the link (see data.values.link) on mobile devices, by default the tooltip will be shown
onGetTooltipfunctionCalled when a tooltip is created to custimize the tooltip content (function (tooltipDiv, countryID, countryValues) { return 'Custom HTML'; })
countriesobjectAdditional options specific to countries:
   ↳ EHbooleantrueWhen set to false, Western Sahara (EH) will be combined with Morocco (MA)
   ↳ Crimea'UA', 'RU''UA'Crimea: Set to 'RU' to make the Crimea part of Russia, by default it is part of the Ukraine
dataobjectThe 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:
   ↳ namestringThe name of the data, it will be shown in the tooltip
   ↳ formatstringThe format for the data value, {0} will be replaced with the actual value
   ↳ thousandSeparatorstring','The character to use as thousand separator
   ↳ thresholdMaxnumbernullMaximal value to use for coloring calculations
   ↳ thresholdMinnumber0Minimum value to use for coloring calculations
   ↳ applyDatastringThe ID (key) of the data that will be used for coloring
   ↳ valuesobjectAn object with the ISO 3166-1 alpha-2 country code as key and the chart data for each country as value
        ↳ colorstringForces a color for this country
        ↳ linkstringAn URL to redirect to when clicking the country
        ↳ linkTargetstringThe 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
countryNamesobjectAn 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 or demo/es6/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.

svgMap uses svg-pan-zoom by Anders Riutta (now maintained by bumpu).

The country flag images are from country-flags by Hampus Joakim Borgos.

Most data in the demos was taken from Wikipedia.

Keywords

FAQs

Last updated on 11 Jul 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc