jsvectormap Documentation
repository·main·Indexed 19 days ago
https://github.com/themustafaomar/jsvectormapA lightweight, jQuery-free JavaScript library for creating interactive maps and data visualizations. It supports all modern browsers (IE9+), providing features such as built-in and custom maps, data series for markers and regions, choropleth visualization via visualizeData, and customizable labels and connecting lines.
What's inside jsvectormap
- jsvectormap is designed for broad compatibility across modern web environments. It supports all modern browsers, including legacy support for Internet Explorer 9 (IE9+).
What is a Data Series in jsVectorMap
mainA series is a visual indicator used to represent data on a map by assigning colors or other SVG attributes to specific regions or markers. This allows you to create choropleth maps or categorize elements (like markers) based on data categories or ranges. Each series entry can target eithermarkersorregions.Add and design region labels
mainYou can render custom text labels for regions using the
labels.regions.renderfunction. Therenderfunction receives the regioncodeas an argument and should return the label string ornullto hide it.To style these labels, use the
regionLabelStyleproperty, which supports the same state-based approach (initial,hover,selected,selectedHover) asregionStyle.const map = new jsVectorMap({ labels: { regions: { render(code) { return ['EG', 'CN'].indexOf(code) > -1 ? 'Hello ' + code : null } } }, regionLabelStyle: { initial: { fill: '#35373e', fontFamily: 'Poppins', fontWeight: 500, fontSize: 13, }, hover: {}, selected: {}, selectedHover: {} } })Integrate jsvectormap with Nuxt.js locally
mainIf you only need the map on a specific page in Nuxt.js, you can import it locally within the component. You must check
process.clientto ensure the library is only loaded and executed on the client side to prevent errors during SSR.<template> <div id="map"></div> </template> <script> const jsVectorMap = process.client ? require('jsvectormap') : {} if (process.client) { require('jsvectormap/dist/maps/world-merc') } export default { data: () => ({ map: null }), mounted() { this.map = new jsVectorMap({ selector: '#map', map: 'world_merc' }) } } </script>Display a Vertical Legend
mainBy default, the map legend is laid out horizontally. To display the legend vertically, set the
verticaloption totruewithin thelegendobject of your series configuration.const series = { markers: [ { attribute: 'fill', legend: { vertical: true, }, // ... other options }, ], }Configure Tooltip visibility in jsVectorMap
mainThe tooltip is an interactive element that appears on hover to show contextual information about markers or regions. It is enabled by default. To disable tooltips globally, set the
showTooltipoption tofalsein yourjsVectorMapconfiguration.To hide tooltips for specific elements while keeping them enabled for others, use the
onRegionTooltipShowevent and callevent.preventDefault()based on the element'scode.Note: For regions, omitting the
nameproperty from an element can also be used to control tooltip behavior.```js // Disable tooltips globally const map = new jsVectorMap({ showTooltip: false, }) // Disable tooltips for specific regions const options = { onRegionTooltipShow(event, tooltip, code) { if (['EG', 'US', 'GL'].indexOf(code) > -1) { event.preventDefault() } }, } const map = new jsVectorMap(options) ```埋Quick start with jsVectorMap
mainTo create an interactive map, you need an HTML container and a JavaScript configuration object passed to the
jsVectorMapconstructor. You can define markers (with coordinates and optional styles), lines connecting different regions or markers, and global styles for markers, labels, and lines.Key configuration options include:
markers: An array of objects containingname,coords(latitude/longitude), and optionalstyle.lines: An array of objects defining connections usingfromandtoproperties.markerStyle: Configuration for marker appearance (e.g.,initial,selected).markerLabelStyle: Configuration for the text labels attached to markers.lineStyle: Configuration for the lines, includingstrokeDasharray,animation, andcurvature.
<div id="map"></div>const markers = [ { name: "Russia", coords: [61.524, 105.3188] }, { name: 'Brazil', coords: [-14.2350, -51.9253], style: { initial: { fill: 'red' } } }, ]; const options = { markers, lines: [ { from: 'Russia', to: 'Greenland' }, ], markerStyle: { initial: { fill: "#3b82f6" }, selected: { fill: "#ff5050" }, }, markerLabelStyle: { initial: { fontFamily: "`Sego UI`, sans-serif", fontSize: 13, }, }, lineStyle: { strokeDasharray: '6 3 6', animation: true, curvature: -0.5, }, }; const map = new jsVectorMap(options);Use Lines to connect markers
mainLines allow you to visually connect two or more markers on the map, representing paths, routes, or relationships between locations.
Note: Lines require markers to be present, as they connect two or more marker points on the map. You define connections by referencing the
nameof the markers in thefromandtoproperties of thelinesarray.const options = { markers: [ { name: 'Egypt', coords: [26.8206, 30.8025] }, { name: 'United Kingdom', coords: [55.3781, 3.4360] }, { name: 'United States', coords: [37.0902, -95.7129], style: { fill: 'red' } }, ], lines: [ { from: 'Egypt', to: 'United Kingdom' }, { from: 'United Kingdom', to: 'United States', style: { stroke: 'grey' } } ], } const map = new jsVectorMap(options)Integrate jsvectormap with Nuxt.js globally
mainTo use
jsVectorMapglobally in Nuxt.js, you must register it as a client-side plugin to avoid SSR (Server-Side Rendering) issues.- Create a plugin file (e.g.,
@/plugins/jsvectormap.js) and useinjectto make the constructor available viathis.$jvmin components. - Register the plugin in
nuxt.config.jswithmode: 'client'.
Once registered, you can access the constructor via
this.$jvmin any component'smountedhook.// nuxt.config.js export default { plugins: [ { src: '@/plugins/jsvectormap.js', mode: 'client' } ] } // @/plugins/jsvectormap.js import jsVectorMap from 'jsvectormap' import 'jsvectormap/dist/maps/jvm-world-merc' import 'jsvectormap/dist/css/jsvectormap.css' export default function (ctx, inject) { inject('jvm', jsVectorMap) }- Create a plugin file (e.g.,
Add markers to a map
mainMarkers allow you to represent data at specific coordinates using icons, shapes, or labels. When you assign a
nameproperty to a marker, it appears in the tooltip on hover and acts as a unique identifier that can be used to connect markers with lines.To use markers, provide a
markersarray in thejsVectorMapoptions containing objects withnameandcoords(an array of[latitude, longitude]).import jsVectorMap from 'jsvectormap' const options = { labels: { markers: { render: marker => marker.name } }, markers: [ { name: 'Egypt', coords: [26.8206, 30.8025] }, { name: 'United Kingdom', coords: [55.3781, 3.4360] }, { name: 'United States', coords: [37.0902, -95.7129], style: { fill: 'red' }, }, ], } const map = new jsVectorMap(options)Integrate jsvectormap with Vue.js
mainWhen using Vue.js, you can either use a provided wrapper component or integrate the library manually. For manual integration, import the
jsVectorMapconstructor, the desired map data, and the SCSS styles in your entry point (e.g.,main.ts). UseonMountedto initialize the map instance to ensure the DOM element is available.<script setup lang="ts"> import { shallowRef, onMounted } from 'vue' import jsVectorMap from 'jsvectormap' import 'jsvectormap/dist/maps/world' import 'jsvectormap/src/scss/jsvectormap.scss' const map = shallowRef() onMounted(() => { map.value = new jsVectorMap({ selector: '#map', }) }) </script> <template> <div id="map"></div> </template>Download jsvectormap from GitHub
mainTo use the source code directly, you can clone the repository or download it as a ZIP file from GitHub.
git clone https://github.com/themustafaomar/jsvectormap.git