Blame | Last modification | View Log | Download | RSS feed
This project is a heavily modified version of [jVectorMap](https://github.com/bjornd/jvectormap). I chose to start fresh rather than fork their project as my intentions were to take it in such a different direction that it would become incompatibale with the original source, rendering it near impossible to merge our projects together without extreme complications.jQuery Vector Map======To get started, all you need to do is include the JavaScript and CSS files for the map you want to load. Here is a sample HTML page for loading the World Map with default settings:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>JQVMap - World Map</title><link href="../jqvmap/jqvmap.css" media="screen" rel="stylesheet" type="text/css" /><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script><script src="../jqvmap/jquery.vmap.js" type="text/javascript"></script><script src="../jqvmap/maps/jquery.vmap.world.js" type="text/javascript"></script><script type="text/javascript">jQuery(document).ready(function() {jQuery('#vmap').vectorMap({ map: 'world_en' });});</script></head><body><div id="vmap" style="width: 600px; height: 400px;"></div></body></html>Making it Pretty======While initializing a map you can provide parameters to change its look and feel.jQuery('#vmap').vectorMap({map: 'world_en',backgroundColor: '#a5bfdd',borderColor: '#818181',borderOpacity: 0.25,borderWidth: 1,color: '#f4f3f0',enableZoom: true,hoverColor: '#c9dfaf',hoverOpacity: null,normalizeFunction: 'linear',scaleColors: ['#b6d6ff', '#005ace'],selectedColor: '#c9dfaf',selectedRegion: null,showTooltip: true,onRegionClick: function(element, code, region){var message = 'You clicked "'+ region+ '" which has the code: '+ code.toUpperCase();alert(message);}});Configuration Settings------**map** *'world_en'*Map you want to load. Must include the javascript file with the name of the map you want. Available maps with this library are world_en, usa_en, europe_en and germany_en**backgroundColor** *'#a5bfdd'*Background color of map container in any CSS compatible format.**borderColor** *'#818181'*Border Color to use to outline map objects**borderOpacity** *0.5*Border Opacity to use to outline map objects ( use anything from 0-1, e.g. 0.5, defaults to 0.25 )**borderWidth** *3*Border Width to use to outline map objects ( defaults to 1 )**color** *'#f4f3f0'*Color of map regions.**colors**Colors of individual map regions. Keys of the colors objects are country codes according to ISO 3166-1 alpha-2 standard. Keys of colors must be in lower case.**enableZoom** *boolean*Whether to Enable Map Zoom ( true or false, defaults to true)**hoverColor** *'#c9dfaf'*Color of the region when mouse pointer is over it.**hoverOpacity** *0.5*Opacity of the region when mouse pointer is over it.**normalizeFunction** *'linear'*This function can be used to improve results of visualizations for data with non-linear nature. Function gets raw value as the first parameter and should return value which will be used in calculations of color, with which particular region will be painted.**scaleColors** *['#b6d6ff', '#005ace']*This option defines colors, with which regions will be painted when you set option values. Array scaleColors can have more then two elements. Elements should be strings representing colors in RGB hex format.**selectedRegion** *'mo'*This is the Region that you are looking to have preselected (two letter ISO code, defaults to null )WORLD------------------------------AE = United Arab EmiratesAF = AfghanistanAG = Antigua and BarbudaAL = AlbaniaAM = ArmeniaAO = AngolaAR = ArgentinaAT = AustriaAU = AustraliaAZ = AzerbaijanBA = Bosnia and HerzegovinaBB = BarbadosBD = BangladeshBE = BelgiumBF = Burkina FasoBG = BulgariaBI = BurundiBJ = BeninBN = Brunei DarussalamBO = BoliviaBR = BrazilBS = BahamasBT = BhutanBW = BotswanaBY = BelarusBZ = BelizeCA = CanadaCD = CongoCF = Central African RepublicCG = CongoCH = SwitzerlandCI = Cote d'IvoireCL = ChileCM = CameroonCN = ChinaCO = ColombiaCR = Costa RicaCU = CubaCV = Cape VerdeCY = CyprusCZ = Czech RepublicDE = GermanyDJ = DjiboutiDK = DenmarkDM = DominicaDO = Dominican RepublicDZ = AlgeriaEC = EcuadorEE = EstoniaEG = EgyptER = EritreaES = SpainET = EthiopiaFI = FinlandFJ = FijiFK = Falkland IslandsFR = FranceGA = GabonGB = United KingdomGD = GrenadaGE = GeorgiaGF = French GuianaGH = GhanaGL = GreenlandGM = GambiaGN = GuineaGQ = Equatorial GuineaGR = GreeceGT = GuatemalaGW = Guinea-BissauGY = GuyanaHN = HondurasHR = CroatiaHT = HaitiHU = HungaryID = IndonesiaIE = IrelandIL = IsraelIN = IndiaIQ = IraqIR = IranIS = IcelandIT = ItalyJM = JamaicaJO = JordanJP = JapanKE = KenyaKG = Kyrgyz RepublicKH = CambodiaKM = ComorosKN = Saint Kitts and NevisKP = North KoreaKR = South KoreaKW = KuwaitKZ = KazakhstanLA = Lao People's Democratic RepublicLB = LebanonLC = Saint LuciaLK = Sri LankaLR = LiberiaLS = LesothoLT = LithuaniaLV = LatviaLY = LibyaMA = MoroccoMD = MoldovaMG = MadagascarMK = MacedoniaML = MaliMM = MyanmarMN = MongoliaMR = MauritaniaMT = MaltaMU = MauritiusMV = MaldivesMW = MalawiMX = MexicoMY = MalaysiaMZ = MozambiqueNA = NamibiaNC = New CaledoniaNE = NigerNG = NigeriaNI = NicaraguaNL = NetherlandsNO = NorwayNP = NepalNZ = New ZealandOM = OmanPA = PanamaPE = PeruPF = French PolynesiaPG = Papua New GuineaPH = PhilippinesPK = PakistanPL = PolandPT = PortugalPY = ParaguayQA = QatarRE = ReunionRO = RomaniaRS = SerbiaRU = Russian FederationßRW = RwandaSA = Saudi ArabiaSB = Solomon IslandsSC = SeychellesSD = SudanSE = SwedenSI = SloveniaSK = SlovakiaSL = Sierra LeoneSN = SenegalSO = SomaliaSR = SurinameST = Sao Tome and PrincipeSV = El SalvadorSY = Syrian Arab RepublicSZ = SwazilandTD = ChadTG = TogoTH = ThailandTJ = TajikistanTL = Timor-LesteTM = TurkmenistanTN = TunisiaTR = TurkeyTT = Trinidad and TobagoTW = TaiwanTZ = TanzaniaUA = UkraineUG = UgandaUS = United States of AmericaUY = UruguayUZ = UzbekistanVE = VenezuelaVN = VietnamVU = VanuatuYE = YemenZA = South AfricaZM = ZambiaZW = ZimbabweUSA------------------------------AK = AlaskaAL = AlabamaAR = ArkansasAZ = ArizonaCA = CaliforniaCO = ColoradoCT = ConnecticutDC = District of ColumbiaDE = DelawareFL = FloridaGA = GeorgiaHI = HawaiiIA = IowaID = IdahoIL = IllinoisIN = IndianaKS = KansasKY = KentuckyLA = LouisianaMA = MassachusettsMD = MarylandME = MaineMI = MichiganMN = MinnesotaMO = MissouriMS = MississippiMT = MontanaNC = North CarolinaND = North DakotaNE = NebraskaNH = New HampshireNJ = New JerseyNM = New MexicoNV = NevadaNY = New YorkOH = OhioOK = OklahomaOR = OregonPA = PennsylvaniaRI = Rhode IslandSC = South CarolinaSD = South DakotaTN = TennesseeTX = TexasUT = UtahVA = VirginiaVT = VermontWA = WashingtonWI = WisconsinWV = West VirginiaWY = WyomingEUROPE------------------------------AD = AndorraAL = AlbaniaAM = ArmeniaAT = AustriaAZ = AzerbaijanBA = Bosnia and HerzegovinaBE = BelgiumBG = BulgariaBY = BelarusCH = SwitzerlandCY = CyprusCZ = Czech RepublicDE = GermanyDK = DenmarkDZ = AlgeriaEE = EstoniaES = SpainFI = FinlandFR = FranceGB = United KingdomGE = GeorgiaGL = GreenlandGR = GreeceHR = CroatiaHU = HungaryIE = IrelandIL = IsraelIQ = IraqIR = IranIS = IcelandIT = ItalyJO = JordanKZ = KazakhstanLB = LebanonLI = LiechtensteinLT = LithuaniaLU = LuxembourgLV = LatviaMA = MoroccoMC = MonacoMD = MoldovaME = MontenegroMK = MacedoniaMT = MaltaNL = NetherlandsNO = NorwayPL = PolandPT = PortugalRO = RomaniaRU = Russian FederationSA = Saudi ArabiaSE = SwedenSI = SloveniaSK = SlovakiaSM = San MarinoSR = SurinameSY = Syrian Arab RepublicTM = TurkmenistanTN = TunisiaTR = TurkeyUA = UkraineGERMANY------------------------------BB = BrandenburgBE = BerlinBW = Baden-WÃrttembergBY = BayernHB = BremenHE = HessenHH = HamburgMV = Mecklenburg-VorpommernNI = NiedersachsenNW = Nordrhein-WestfalenRP = Rheinland-PfalzSH = Schleswig-HolsteinSL = SaarlandSN = SachsenST = Sachsen-AnhaltTH = ThÃriRUSSIA------------------------------CH = Chukotka Autonomous OkrugKA = Kamchatka KraiMA = Magadan OblastSA = Sakha RepublicAM = Amur OblastPR = Primorsky KraiEU = Jewish Autonomous OblastHA = Khabarovsk KraiSH = Sakhalin OblastOM = Omsk OblastNV = Novosibirsk OblastAL = Altai KraiLT = Altai RepublicTV = Tuva RepublicHK = Republic of KhakassiaKM = Kemerovo OblastTM = Tomsk OblastZB = Zabaykalsky KraiBR = Buryat RepublicIR = Irkutsk OblastKR = Krasnoyarsk KraiYA = Yamalo-Nenets Autonomous OkrugHT = Khanty–Mansi Autonomous OkrugTU = Tyumen OblastKU = Kurgan OblastCL = Chelyabinsk OblastSV = Sverdlovsk OblastAR = Arkhangelsk OblastNE = Nenets Autonomous OkrugKO = Komi RepublicMU = Murmansk OblastVO = Vologda OblastNO = Novgorod OblastPS = Pskov OblastLE = Leningrad OblastKL = Republic of KareliaKN = Kaliningrad OblastDA = Republic of DagestanST = Stavropol KraiSO = Republic of North Ossetia–AlaniaKB = Kabardino-Balkar RepublicKH = Karachay–Cherkess RepublicCC = Chechen RepublicIN = Republic of IngushetiaAD = Republic of AdygeaKS = Krasnodar KraiRO = Rostov OblastKK = Republic of KalmykiaAS = Astrakhan OblastVL = Volgograd OblastTR = Tver OblastSM = Smolensk OblastBN = Bryansk OblastKY = Kursk OblastBL = Belgorod OblastOR = Oryol OblastKJ = Kaluga OblastTL = Tula OblastLP = Lipetsk OblastMC = Moscow OblastRZ = Ryazan OblastTB = Tambov OblastVM = Vladimir OblastIV = Ivanovo OblastYR = Yaroslavl OblastKT = Kostroma OblastNN = Nizhny Novgorod OblastMR = Republic of MordoviaPZ = Penza OblastSR = Saratov OblastSS = Samara OblastOB = Orenburg OblastBS = Republic of BashkortostanUL = Ulyanovsk OblastCU = Chuvash RepublicTA = Republic of TatarstanML = Mari El RepublicUD = Udmurt RepublicKI = Kirov OblastPE = Perm KraiVN = Voronezh Oblast**showTooltip** *boolean*Whether to show Tooltips on Mouseover ( true or false, defaults to true)**onLabelShow** *function(event, label, code)*Callback function which will be called before label is shown. Label DOM object and country code will be passed to the callback as arguments.**onRegionOver** *function(event, code, region)*Callback function which will be called when the mouse cursor enters the region path. Country code will be passed to the callback as argument.**onRegionOut** *function(event, code, region)*Callback function which will be called when the mouse cursor leaves the region path. Country code will be passed to the callback as argument.**onRegionClick** *function(event, code, region)*Callback function which will be called when the user clicks the region path. Country code will be passed to the callback as argument. This callback may be called while the user is moving the map. If you need to distinguish between a "real" click and a click resulting from moving the map, you can inspect **$(event.currentTarget).data('mapObject').isMoving**.Dynamic Updating======Most of the options can be changed after initialization using the following code:jQuery('#vmap').vectorMap('set', 'colors', {us: '#0000ff'});Instead of colors can be used any parameter except callbacks. Callbacks can be added and deleted using standard jQuery patterns of working with events.You can define callback function when you initialize JQVMap:jQuery('#vmap').vectorMap({onLabelShow: function(event, label, code){},onRegionOver: function(event, code, region){},onRegionOut: function(event, code, region){},onRegionClick: function(event, code, region){}});Or later using standard jQuery mechanism:jQuery('#vmap').bind('labelShow.jqvmap',function(event, label, code){});jQuery('#vmap').bind('regionMouseOver.jqvmap',function(event, code, region){});jQuery('#vmap').bind('regionMouseOut.jqvmap',function(event, code, region){});jQuery('#vmap').bind('regionClick.jqvmap',function(event, code, region){});Consider that fact that you can use standard features of jQuery events like event.preventDefault() or returning false from the callback to prevent default behavior of JQVMap (showing label or changing country color on hover). In the following example, when user moves mouse cursor over Canada label won't be shown and color of country won't be changed. At the same label for Russia will have custom text.jQuery('#vmap').vectorMap({onLabelShow: function(event, label, code){if (code == 'ca'){event.preventDefault();}else if (code == 'ru'){label.text('Bears, vodka, balalaika');}},onRegionOver: function(event, code){if (code == 'ca'){event.preventDefault();}},});Data Visualization======Here I want to demonstrate how visualization of some geographical-related data can be done using JQVMap. Let's visualize information about GDP in 2010 for every country. At first we need some data. Let it be site of International Monetary Fond. There we can get information in xsl format, which can be converted first to csv and then to json with any scripting language. Now we have file gdp-data.js with such content (globals are evil, I know, but just for the sake of simplification):var sample_data = {"af":16.63,"al":11.58,"dz":158.97,...};Then connect it to the page and add some code to make visualization:var max = 0,min = Number.MAX_VALUE,cc,startColor = [200, 238, 255],endColor = [0, 100, 145],colors = {},hex;//find maximum and minimum valuesfor (cc in gdpData){if (parseFloat(gdpData[cc]) > max){max = parseFloat(gdpData[cc]);}if (parseFloat(gdpData[cc]) < min){min = parseFloat(gdpData[cc]);}}//set colors according to values of GDPfor (cc in gdpData){if (gdpData[cc] > 0){colors[cc] = '#';for (var i = 0; i<3; i++){hex = Math.round(startColor[i]+ (endColor[i]- startColor[i])* (gdpData[cc] / (max - min))).toString(16);if (hex.length == 1){hex = '0'+hex;}colors[cc] += (hex.length == 1 ? '0' : '') + hex;}}}//initialize JQVMapjQuery('#vmap').vectorMap({colors: colors,hoverOpacity: 0.7,hoverColor: false});Custom Maps======The following is the converter instructions directly from [jVectorMap](https://github.com/bjornd/jvectormap) that could be used to create your own maps for JQVMap from the data in various GIS formats like Shapefile. The following command could be used to convert USA map from the data available at [www.naturalearthdata.com](www.naturalearthdata.com):python \path/to/converter.py \path/to/geo-data.shp \path/to/resulting-map.js \--width 900 \--country_name_index 4 \--where "ISO = 'USA'" \--codes_file path/to/codes-en.tsv \--insets '[{"codes": ["US-AK"], "width": 200, "left": 10, "top": 370}, {"codes": ["US-HI"], "width": 100, "left": 220, "top": 400}]' \--minimal_area 4000000 \--buffer_distance -3000 \--simplify_tolerance 1000 \--longtitude0 10w \--name us