jquery.vmap.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /*!
  2. * jQVMap Version 1.0
  3. *
  4. * http://jqvmap.com
  5. *
  6. * Copyright 2012, Peter Schmalfeldt <manifestinteractive@gmail.com>
  7. * Copyright 2011-2012, Kirill Lebedev
  8. * Licensed under the MIT license.
  9. *
  10. * Fork Me @ https://github.com/manifestinteractive/jqvmap
  11. */
  12. (function ($) {
  13. var apiParams = {
  14. colors: 1,
  15. values: 1,
  16. backgroundColor: 1,
  17. scaleColors: 1,
  18. normalizeFunction: 1,
  19. enableZoom: 1,
  20. showTooltip: 1,
  21. borderColor: 1,
  22. borderWidth: 1,
  23. borderOpacity: 1,
  24. selectedRegions: 1,
  25. multiSelectRegion: 1
  26. };
  27. var apiEvents = {
  28. onLabelShow: 'labelShow',
  29. onRegionOver: 'regionMouseOver',
  30. onRegionOut: 'regionMouseOut',
  31. onRegionClick: 'regionClick',
  32. onRegionSelect: 'regionSelect',
  33. onRegionDeselect: 'regionDeselect'
  34. };
  35. $.fn.vectorMap = function (options) {
  36. var defaultParams = {
  37. map: 'world_en',
  38. backgroundColor: '#a5bfdd',
  39. color: '#f4f3f0',
  40. hoverColor: '#c9dfaf',
  41. selectedColor: '#c9dfaf',
  42. scaleColors: ['#b6d6ff', '#005ace'],
  43. normalizeFunction: 'linear',
  44. enableZoom: true,
  45. showTooltip: true,
  46. borderColor: '#818181',
  47. borderWidth: 1,
  48. borderOpacity: 0.25,
  49. selectedRegions: null,
  50. multiSelectRegion: false
  51. }, map = this.data('mapObject');
  52. if (options === 'addMap') {
  53. WorldMap.maps[arguments[1]] = arguments[2];
  54. } else if (options === 'set' && apiParams[arguments[1]]) {
  55. map['set' + arguments[1].charAt(0).toUpperCase() + arguments[1].substr(1)].apply(map, Array.prototype.slice.call(arguments, 2));
  56. } else if (typeof options === 'string' &&
  57. typeof map[options] === 'function') {
  58. return map[options].apply(map, Array.prototype.slice.call(arguments, 1));
  59. } else {
  60. $.extend(defaultParams, options);
  61. defaultParams.container = this;
  62. this.css({position: 'relative', overflow: 'hidden'});
  63. map = new WorldMap(defaultParams);
  64. this.data('mapObject', map);
  65. for (var e in apiEvents) {
  66. if (defaultParams[e]) {
  67. this.bind(apiEvents[e] + '.jqvmap', defaultParams[e]);
  68. }
  69. }
  70. }
  71. };
  72. var VectorCanvas = function (width, height, params) {
  73. this.mode = window.SVGAngle ? 'svg' : 'vml';
  74. this.params = params;
  75. if (this.mode == 'svg') {
  76. this.createSvgNode = function (nodeName) {
  77. return document.createElementNS(this.svgns, nodeName);
  78. };
  79. } else {
  80. try {
  81. if (!document.namespaces.rvml) {
  82. document.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
  83. }
  84. this.createVmlNode = function (tagName) {
  85. return document.createElement('<rvml:' + tagName + ' class="rvml">');
  86. };
  87. } catch (e) {
  88. this.createVmlNode = function (tagName) {
  89. return document.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
  90. };
  91. }
  92. document.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
  93. }
  94. if (this.mode == 'svg') {
  95. this.canvas = this.createSvgNode('svg');
  96. } else {
  97. this.canvas = this.createVmlNode('group');
  98. this.canvas.style.position = 'absolute';
  99. }
  100. this.setSize(width, height);
  101. };
  102. VectorCanvas.prototype = {
  103. svgns: "http://www.w3.org/2000/svg",
  104. mode: 'svg',
  105. width: 0,
  106. height: 0,
  107. canvas: null,
  108. setSize: function (width, height) {
  109. if (this.mode == 'svg') {
  110. this.canvas.setAttribute('width', width);
  111. this.canvas.setAttribute('height', height);
  112. } else {
  113. this.canvas.style.width = width + "px";
  114. this.canvas.style.height = height + "px";
  115. this.canvas.coordsize = width + ' ' + height;
  116. this.canvas.coordorigin = "0 0";
  117. if (this.rootGroup) {
  118. var pathes = this.rootGroup.getElementsByTagName('shape');
  119. for (var i = 0, l = pathes.length; i < l; i++) {
  120. pathes[i].coordsize = width + ' ' + height;
  121. pathes[i].style.width = width + 'px';
  122. pathes[i].style.height = height + 'px';
  123. }
  124. this.rootGroup.coordsize = width + ' ' + height;
  125. this.rootGroup.style.width = width + 'px';
  126. this.rootGroup.style.height = height + 'px';
  127. }
  128. }
  129. this.width = width;
  130. this.height = height;
  131. },
  132. createPath: function (config) {
  133. var node;
  134. if (this.mode == 'svg') {
  135. node = this.createSvgNode('path');
  136. node.setAttribute('d', config.path);
  137. if (this.params.borderColor !== null) {
  138. node.setAttribute('stroke', this.params.borderColor);
  139. }
  140. if (this.params.borderWidth > 0) {
  141. node.setAttribute('stroke-width', this.params.borderWidth);
  142. node.setAttribute('stroke-linecap', 'round');
  143. node.setAttribute('stroke-linejoin', 'round');
  144. }
  145. if (this.params.borderOpacity > 0) {
  146. node.setAttribute('stroke-opacity', this.params.borderOpacity);
  147. }
  148. node.setFill = function (color) {
  149. this.setAttribute("fill", color);
  150. if (this.getAttribute("original") === null) {
  151. this.setAttribute("original", color);
  152. }
  153. };
  154. node.getFill = function (color) {
  155. return this.getAttribute("fill");
  156. };
  157. node.getOriginalFill = function () {
  158. return this.getAttribute("original");
  159. };
  160. node.setOpacity = function (opacity) {
  161. this.setAttribute('fill-opacity', opacity);
  162. };
  163. } else {
  164. node = this.createVmlNode('shape');
  165. node.coordorigin = "0 0";
  166. node.coordsize = this.width + ' ' + this.height;
  167. node.style.width = this.width + 'px';
  168. node.style.height = this.height + 'px';
  169. node.fillcolor = WorldMap.defaultFillColor;
  170. node.stroked = false;
  171. node.path = VectorCanvas.pathSvgToVml(config.path);
  172. var scale = this.createVmlNode('skew');
  173. scale.on = true;
  174. scale.matrix = '0.01,0,0,0.01,0,0';
  175. scale.offset = '0,0';
  176. node.appendChild(scale);
  177. var fill = this.createVmlNode('fill');
  178. node.appendChild(fill);
  179. node.setFill = function (color) {
  180. this.getElementsByTagName('fill')[0].color = color;
  181. if (this.getAttribute("original") === null) {
  182. this.setAttribute("original", color);
  183. }
  184. };
  185. node.getFill = function (color) {
  186. return this.getElementsByTagName('fill')[0].color;
  187. };
  188. node.getOriginalFill = function () {
  189. return this.getAttribute("original");
  190. };
  191. node.setOpacity = function (opacity) {
  192. this.getElementsByTagName('fill')[0].opacity = parseInt(opacity * 100, 10) + '%';
  193. };
  194. }
  195. return node;
  196. },
  197. createGroup: function (isRoot) {
  198. var node;
  199. if (this.mode == 'svg') {
  200. node = this.createSvgNode('g');
  201. } else {
  202. node = this.createVmlNode('group');
  203. node.style.width = this.width + 'px';
  204. node.style.height = this.height + 'px';
  205. node.style.left = '0px';
  206. node.style.top = '0px';
  207. node.coordorigin = "0 0";
  208. node.coordsize = this.width + ' ' + this.height;
  209. }
  210. if (isRoot) {
  211. this.rootGroup = node;
  212. }
  213. return node;
  214. },
  215. applyTransformParams: function (scale, transX, transY) {
  216. if (this.mode == 'svg') {
  217. this.rootGroup.setAttribute('transform', 'scale(' + scale + ') translate(' + transX + ', ' + transY + ')');
  218. } else {
  219. this.rootGroup.coordorigin = (this.width - transX) + ',' + (this.height - transY);
  220. this.rootGroup.coordsize = this.width / scale + ',' + this.height / scale;
  221. }
  222. }
  223. };
  224. VectorCanvas.pathSvgToVml = function (path) {
  225. var result = '';
  226. var cx = 0, cy = 0, ctrlx, ctrly;
  227. return path.replace(/([MmLlHhVvCcSs])((?:-?(?:\d+)?(?:\.\d+)?,?\s?)+)/g, function (segment, letter, coords, index) {
  228. coords = coords.replace(/(\d)-/g, '$1,-').replace(/\s+/g, ',').split(',');
  229. if (!coords[0]) {
  230. coords.shift();
  231. }
  232. for (var i = 0, l = coords.length; i < l; i++) {
  233. coords[i] = Math.round(100 * coords[i]);
  234. }
  235. switch (letter) {
  236. case 'm':
  237. cx += coords[0];
  238. cy += coords[1];
  239. return 't' + coords.join(',');
  240. break;
  241. case 'M':
  242. cx = coords[0];
  243. cy = coords[1];
  244. return 'm' + coords.join(',');
  245. break;
  246. case 'l':
  247. cx += coords[0];
  248. cy += coords[1];
  249. return 'r' + coords.join(',');
  250. break;
  251. case 'L':
  252. cx = coords[0];
  253. cy = coords[1];
  254. return 'l' + coords.join(',');
  255. break;
  256. case 'h':
  257. cx += coords[0];
  258. return 'r' + coords[0] + ',0';
  259. break;
  260. case 'H':
  261. cx = coords[0];
  262. return 'l' + cx + ',' + cy;
  263. break;
  264. case 'v':
  265. cy += coords[0];
  266. return 'r0,' + coords[0];
  267. break;
  268. case 'V':
  269. cy = coords[0];
  270. return 'l' + cx + ',' + cy;
  271. break;
  272. case 'c':
  273. ctrlx = cx + coords[coords.length - 4];
  274. ctrly = cy + coords[coords.length - 3];
  275. cx += coords[coords.length - 2];
  276. cy += coords[coords.length - 1];
  277. return 'v' + coords.join(',');
  278. break;
  279. case 'C':
  280. ctrlx = coords[coords.length - 4];
  281. ctrly = coords[coords.length - 3];
  282. cx = coords[coords.length - 2];
  283. cy = coords[coords.length - 1];
  284. return 'c' + coords.join(',');
  285. break;
  286. case 's':
  287. coords.unshift(cy - ctrly);
  288. coords.unshift(cx - ctrlx);
  289. ctrlx = cx + coords[coords.length - 4];
  290. ctrly = cy + coords[coords.length - 3];
  291. cx += coords[coords.length - 2];
  292. cy += coords[coords.length - 1];
  293. return 'v' + coords.join(',');
  294. break;
  295. case 'S':
  296. coords.unshift(cy + cy - ctrly);
  297. coords.unshift(cx + cx - ctrlx);
  298. ctrlx = coords[coords.length - 4];
  299. ctrly = coords[coords.length - 3];
  300. cx = coords[coords.length - 2];
  301. cy = coords[coords.length - 1];
  302. return 'c' + coords.join(',');
  303. break;
  304. default:
  305. return false;
  306. break;
  307. }
  308. return '';
  309. }).replace(/z/g, '');
  310. };
  311. var WorldMap = function (params) {
  312. params = params || {};
  313. var map = this;
  314. var mapData = WorldMap.maps[params.map];
  315. this.selectedRegions = [];
  316. this.multiSelectRegion = params.multiSelectRegion;
  317. this.container = params.container;
  318. this.defaultWidth = mapData.width;
  319. this.defaultHeight = mapData.height;
  320. this.color = params.color;
  321. this.selectedColor = params.selectedColor;
  322. this.hoverColor = params.hoverColor;
  323. this.hoverOpacity = params.hoverOpacity;
  324. this.setBackgroundColor(params.backgroundColor);
  325. this.width = params.container.width();
  326. this.height = params.container.height();
  327. this.resize();
  328. jQuery(window).resize(function () {
  329. map.width = params.container.width();
  330. map.height = params.container.height();
  331. map.resize();
  332. map.canvas.setSize(map.width, map.height);
  333. map.applyTransform();
  334. });
  335. this.canvas = new VectorCanvas(this.width, this.height, params);
  336. params.container.append(this.canvas.canvas);
  337. this.makeDraggable();
  338. this.rootGroup = this.canvas.createGroup(true);
  339. this.index = WorldMap.mapIndex;
  340. this.label = jQuery('<div/>').addClass('jqvmap-label').appendTo(jQuery('body')).hide();
  341. if (params.enableZoom) {
  342. jQuery('<div/>').addClass('jqvmap-zoomin').text('+').appendTo(params.container);
  343. jQuery('<div/>').addClass('jqvmap-zoomout').html('&#x2212;').appendTo(params.container);
  344. }
  345. map.countries = [];
  346. for (var key in mapData.pathes) {
  347. var path = this.canvas.createPath({
  348. path: mapData.pathes[key].path
  349. });
  350. path.setFill(this.color);
  351. path.id = map.getCountryId(key);
  352. map.countries[key] = path;
  353. if (this.canvas.mode == 'svg') {
  354. path.setAttribute('class', 'jvectormap-region');
  355. } else {
  356. jQuery(path).addClass('jvectormap-region');
  357. }
  358. jQuery(this.rootGroup).append(path);
  359. }
  360. jQuery(params.container).delegate(this.canvas.mode == 'svg' ? 'path' : 'shape', 'mouseover mouseout', function (e) {
  361. var path = e.target,
  362. code = e.target.id.split('_').pop(),
  363. labelShowEvent = $.Event('labelShow.jqvmap'),
  364. regionMouseOverEvent = $.Event('regionMouseOver.jqvmap');
  365. if (e.type == 'mouseover') {
  366. jQuery(params.container).trigger(regionMouseOverEvent, [code, mapData.pathes[code].name]);
  367. if (!regionMouseOverEvent.isDefaultPrevented()) {
  368. map.highlight(code, path);
  369. }
  370. if (params.showTooltip) {
  371. map.label.text(mapData.pathes[code].name);
  372. jQuery(params.container).trigger(labelShowEvent, [map.label, code]);
  373. if (!labelShowEvent.isDefaultPrevented()) {
  374. map.label.show();
  375. map.labelWidth = map.label.width();
  376. map.labelHeight = map.label.height();
  377. }
  378. }
  379. } else {
  380. map.unhighlight(code, path);
  381. map.label.hide();
  382. jQuery(params.container).trigger('regionMouseOut.jqvmap', [code, mapData.pathes[code].name]);
  383. }
  384. });
  385. jQuery(params.container).delegate(this.canvas.mode == 'svg' ? 'path' : 'shape', 'click', function (e) {
  386. if (!params.multiSelectRegion) {
  387. for (var key in mapData.pathes) {
  388. map.countries[key].currentFillColor = map.countries[key].getOriginalFill();
  389. map.countries[key].setFill(map.countries[key].getOriginalFill());
  390. }
  391. }
  392. var path = e.target;
  393. var code = e.target.id.split('_').pop();
  394. jQuery(params.container).trigger('regionClick.jqvmap', [code, mapData.pathes[code].name]);
  395. if (!regionClickEvent.isDefaultPrevented()) {
  396. if (map.selectedRegions.indexOf(code) !== -1) {
  397. map.deselect(code, path);
  398. } else {
  399. map.select(code, path);
  400. }
  401. }
  402. //console.log(selectedRegions);
  403. });
  404. if (params.showTooltip) {
  405. params.container.mousemove(function (e) {
  406. if (map.label.is(':visible')) {
  407. var left = e.pageX - 15 - map.labelWidth;
  408. var top = e.pageY - 15 - map.labelHeight;
  409. if (left < 0)
  410. left = e.pageX + 15;
  411. if (top < 0)
  412. top = e.pageY + 15;
  413. map.label.css({
  414. left: left,
  415. top: top
  416. });
  417. }
  418. });
  419. }
  420. this.setColors(params.colors);
  421. this.canvas.canvas.appendChild(this.rootGroup);
  422. this.applyTransform();
  423. this.colorScale = new ColorScale(params.scaleColors, params.normalizeFunction, params.valueMin, params.valueMax);
  424. if (params.values) {
  425. this.values = params.values;
  426. this.setValues(params.values);
  427. }
  428. if (params.selectedRegions) {
  429. if (params.selectedRegions instanceof Array) {
  430. for (var k in params.selectedRegions) {
  431. this.select(params.selectedRegions[k].toLowerCase());
  432. }
  433. } else {
  434. this.select(params.selectedRegions.toLowerCase());
  435. }
  436. }
  437. this.bindZoomButtons();
  438. if (params.pins) {
  439. /*if(params.pinMode) {
  440. if(params.pinMode != "id" && params.pinMode != "content") {
  441. params.pinMode = "content";
  442. }
  443. } else {
  444. params.pinMode = "content";
  445. }*/
  446. this.pinHandlers = false;
  447. this.placePins(params.pins, params.pinMode);
  448. }
  449. WorldMap.mapIndex++;
  450. };
  451. WorldMap.prototype = {
  452. transX: 0,
  453. transY: 0,
  454. scale: 1,
  455. baseTransX: 0,
  456. baseTransY: 0,
  457. baseScale: 1,
  458. width: 0,
  459. height: 0,
  460. countries: {},
  461. countriesColors: {},
  462. countriesData: {},
  463. zoomStep: 1.4,
  464. zoomMaxStep: 4,
  465. zoomCurStep: 1,
  466. setColors: function (key, color) {
  467. if (typeof key == 'string') {
  468. this.countries[key].setFill(color);
  469. this.countries[key].setAttribute("original", color);
  470. } else {
  471. var colors = key;
  472. for (var code in colors) {
  473. if (this.countries[code]) {
  474. this.countries[code].setFill(colors[code]);
  475. this.countries[code].setAttribute("original", colors[code]);
  476. }
  477. }
  478. }
  479. },
  480. setValues: function (values) {
  481. var max = 0,
  482. min = Number.MAX_VALUE,
  483. val;
  484. for (var cc in values) {
  485. val = parseFloat(values[cc]);
  486. if (val > max) {
  487. max = values[cc];
  488. }
  489. if (val && val < min) {
  490. min = val;
  491. }
  492. }
  493. this.colorScale.setMin(min);
  494. this.colorScale.setMax(max);
  495. var colors = {};
  496. for (cc in values) {
  497. val = parseFloat(values[cc]);
  498. if (val) {
  499. colors[cc] = this.colorScale.getColor(val);
  500. } else {
  501. colors[cc] = this.color;
  502. }
  503. }
  504. this.setColors(colors);
  505. this.values = values;
  506. },
  507. setBackgroundColor: function (backgroundColor) {
  508. this.container.css('background-color', backgroundColor);
  509. },
  510. setScaleColors: function (colors) {
  511. this.colorScale.setColors(colors);
  512. if (this.values) {
  513. this.setValues(this.values);
  514. }
  515. },
  516. setNormalizeFunction: function (f) {
  517. this.colorScale.setNormalizeFunction(f);
  518. if (this.values) {
  519. this.setValues(this.values);
  520. }
  521. },
  522. highlight: function (cc, path) {
  523. path = path || $('#' + this.getCountryId(cc))[0];
  524. if (this.hoverOpacity) {
  525. path.setOpacity(this.hoverOpacity);
  526. } else if (this.hoverColor) {
  527. path.currentFillColor = path.getFill() + '';
  528. path.setFill(this.hoverColor);
  529. }
  530. },
  531. unhighlight: function (cc, path) {
  532. path = path || $('#' + this.getCountryId(cc))[0];
  533. path.setOpacity(1);
  534. if (path.currentFillColor) {
  535. path.setFill(path.currentFillColor);
  536. }
  537. },
  538. select: function (cc, path) {
  539. path = path || $('#' + this.getCountryId(cc))[0];
  540. if (this.selectedRegions.indexOf(cc) < 0) {
  541. if (this.multiSelectRegion) {
  542. this.selectedRegions.push(cc);
  543. } else {
  544. this.selectedRegions = [cc];
  545. }
  546. // MUST BE after the change of selectedRegions
  547. // Otherwise, we might loop
  548. $(this.container).trigger('regionSelect.jqvmap', [cc]);
  549. if (this.selectedColor) {
  550. path.currentFillColor = this.selectedColor;
  551. path.setFill(this.selectedColor);
  552. }
  553. }
  554. },
  555. deselect: function (cc, path) {
  556. path = path || $('#' + this.getCountryId(cc))[0];
  557. if (this.selectedRegions.indexOf(cc) >= 0) {
  558. this.selectedRegions.splice(this.selectedRegions.indexOf(cc), 1);
  559. // MUST BE after the change of selectedRegions
  560. // Otherwise, we might loop
  561. $(this.container).trigger('regionDeselect.jqvmap', [cc]);
  562. path.currentFillColor = path.getOriginalFill();
  563. path.setFill(path.getOriginalFill());
  564. }
  565. },
  566. isSelected: function (cc) {
  567. return this.selectedRegions.indexOf(cc) >= 0;
  568. },
  569. resize: function () {
  570. var curBaseScale = this.baseScale;
  571. if (this.width / this.height > this.defaultWidth / this.defaultHeight) {
  572. this.baseScale = this.height / this.defaultHeight;
  573. this.baseTransX = Math.abs(this.width - this.defaultWidth * this.baseScale) / (2 * this.baseScale);
  574. } else {
  575. this.baseScale = this.width / this.defaultWidth;
  576. this.baseTransY = Math.abs(this.height - this.defaultHeight * this.baseScale) / (2 * this.baseScale);
  577. }
  578. this.scale *= this.baseScale / curBaseScale;
  579. this.transX *= this.baseScale / curBaseScale;
  580. this.transY *= this.baseScale / curBaseScale;
  581. },
  582. reset: function () {
  583. this.countryTitle.reset();
  584. for (var key in this.countries) {
  585. this.countries[key].setFill(WorldMap.defaultColor);
  586. }
  587. this.scale = this.baseScale;
  588. this.transX = this.baseTransX;
  589. this.transY = this.baseTransY;
  590. this.applyTransform();
  591. },
  592. applyTransform: function () {
  593. var maxTransX, maxTransY, minTransX, minTransY;
  594. if (this.defaultWidth * this.scale <= this.width) {
  595. maxTransX = (this.width - this.defaultWidth * this.scale) / (2 * this.scale);
  596. minTransX = (this.width - this.defaultWidth * this.scale) / (2 * this.scale);
  597. } else {
  598. maxTransX = 0;
  599. minTransX = (this.width - this.defaultWidth * this.scale) / this.scale;
  600. }
  601. if (this.defaultHeight * this.scale <= this.height) {
  602. maxTransY = (this.height - this.defaultHeight * this.scale) / (2 * this.scale);
  603. minTransY = (this.height - this.defaultHeight * this.scale) / (2 * this.scale);
  604. } else {
  605. maxTransY = 0;
  606. minTransY = (this.height - this.defaultHeight * this.scale) / this.scale;
  607. }
  608. if (this.transY > maxTransY) {
  609. this.transY = maxTransY;
  610. } else if (this.transY < minTransY) {
  611. this.transY = minTransY;
  612. }
  613. if (this.transX > maxTransX) {
  614. this.transX = maxTransX;
  615. } else if (this.transX < minTransX) {
  616. this.transX = minTransX;
  617. }
  618. this.canvas.applyTransformParams(this.scale, this.transX, this.transY);
  619. },
  620. makeDraggable: function () {
  621. var mouseDown = false;
  622. var oldPageX, oldPageY;
  623. var self = this;
  624. self.isMoving = false;
  625. self.isMovingTimeout = false;
  626. this.container.mousemove(function (e) {
  627. if (mouseDown) {
  628. var curTransX = self.transX;
  629. var curTransY = self.transY;
  630. self.transX -= (oldPageX - e.pageX) / self.scale;
  631. self.transY -= (oldPageY - e.pageY) / self.scale;
  632. self.applyTransform();
  633. oldPageX = e.pageX;
  634. oldPageY = e.pageY;
  635. self.isMoving = true;
  636. if (self.isMovingTimeout) {
  637. clearTimeout(self.isMovingTimeout);
  638. }
  639. self.container.trigger('drag');
  640. }
  641. return false;
  642. }).mousedown(function (e) {
  643. mouseDown = true;
  644. oldPageX = e.pageX;
  645. oldPageY = e.pageY;
  646. return false;
  647. }).mouseup(function () {
  648. mouseDown = false;
  649. self.isMovingTimeout = setTimeout(function () {
  650. self.isMoving = false;
  651. }, 100);
  652. return false;
  653. });
  654. },
  655. bindZoomButtons: function () {
  656. var map = this;
  657. this.container.find('.jqvmap-zoomin').click(function () {
  658. map.zoomIn();
  659. });
  660. this.container.find('.jqvmap-zoomout').click(function () {
  661. map.zoomOut();
  662. });
  663. },
  664. zoomIn: function () {
  665. var map = this;
  666. var sliderDelta = (jQuery('#zoom').innerHeight() - 6 * 2 - 15 * 2 - 3 * 2 - 7 - 6) / (this.zoomMaxStep - this.zoomCurStep);
  667. if (map.zoomCurStep < map.zoomMaxStep) {
  668. var curTransX = map.transX;
  669. var curTransY = map.transY;
  670. var curScale = map.scale;
  671. map.transX -= (map.width / map.scale - map.width / (map.scale * map.zoomStep)) / 2;
  672. map.transY -= (map.height / map.scale - map.height / (map.scale * map.zoomStep)) / 2;
  673. map.setScale(map.scale * map.zoomStep);
  674. map.zoomCurStep++;
  675. jQuery('#zoomSlider').css('top', parseInt(jQuery('#zoomSlider').css('top'), 10) - sliderDelta);
  676. map.container.trigger("zoomIn");
  677. }
  678. },
  679. zoomOut: function () {
  680. var map = this;
  681. var sliderDelta = (jQuery('#zoom').innerHeight() - 6 * 2 - 15 * 2 - 3 * 2 - 7 - 6) / (this.zoomMaxStep - this.zoomCurStep);
  682. if (map.zoomCurStep > 1) {
  683. var curTransX = map.transX;
  684. var curTransY = map.transY;
  685. var curScale = map.scale;
  686. map.transX += (map.width / (map.scale / map.zoomStep) - map.width / map.scale) / 2;
  687. map.transY += (map.height / (map.scale / map.zoomStep) - map.height / map.scale) / 2;
  688. map.setScale(map.scale / map.zoomStep);
  689. map.zoomCurStep--;
  690. jQuery('#zoomSlider').css('top', parseInt(jQuery('#zoomSlider').css('top'), 10) + sliderDelta);
  691. map.container.trigger("zoomOut");
  692. }
  693. },
  694. setScale: function (scale) {
  695. this.scale = scale;
  696. this.applyTransform();
  697. },
  698. getCountryId: function (cc) {
  699. return 'jqvmap' + this.index + '_' + cc;
  700. },
  701. getPinId: function (cc) {
  702. return this.getCountryId(cc) + '_pin';
  703. },
  704. placePins: function (pins, pinMode) {
  705. var map = this;
  706. if (!pinMode || (pinMode != "content" && pinMode != "id")) {
  707. pinMode = "content";
  708. }
  709. if (pinMode == "content") {//treat pin as content
  710. jQuery.each(pins, function (index, pin) {
  711. if (jQuery('#' + map.getCountryId(index)).length == 0) {
  712. return;
  713. }
  714. //mapData.pathes[code].name
  715. var pinIndex = map.getPinId(index);
  716. if (jQuery('#' + pinIndex).length > 0) {
  717. jQuery('#' + pinIndex).remove();
  718. }
  719. map.container.append('<div id="' + pinIndex + '" for="' + index + '" class="jqvmap_pin" style="position:absolute">' + pin + '</div>');
  720. });
  721. } else { //treat pin as id of an html content
  722. jQuery.each(pins, function (index, pin) {
  723. if (jQuery('#' + map.getCountryId(index)).length == 0) {
  724. return;
  725. }
  726. var pinIndex = map.getPinId(index);
  727. if (jQuery('#' + pinIndex).length > 0) {
  728. jQuery('#' + pinIndex).remove();
  729. }
  730. map.container.append('<div id="' + pinIndex + '" for="' + index + '" class="jqvmap_pin" style="position:absolute"></div>');
  731. jQuery('#' + pinIndex).append(jQuery('#' + pin));
  732. });
  733. }
  734. this.positionPins();
  735. if (!this.pinHandlers) {
  736. this.pinHandlers = true;//do only once
  737. var positionFix = function () {
  738. map.positionPins();
  739. };
  740. this.container.bind('zoomIn', positionFix)
  741. .bind('zoomOut', positionFix)
  742. .bind('drag', positionFix);
  743. }
  744. },
  745. positionPins: function () {
  746. var map = this;
  747. var pins = this.container.find('.jqvmap_pin');
  748. jQuery.each(pins, function (index, pinObj) {
  749. pinObj = jQuery(pinObj);
  750. var countryId = map.getCountryId(pinObj.attr('for'));
  751. var countryObj = jQuery('#' + countryId);
  752. var bbox = document.getElementById(countryId).getBBox();
  753. var position = countryObj.position();
  754. var scale = map.scale;
  755. var left = position.left + (bbox.width / 2) * scale - pinObj.width() / 2,
  756. top = position.top + (bbox.height / 2) * scale - pinObj.height() / 2;
  757. pinObj.css('left', left).css('top', top);
  758. });
  759. },
  760. getPin: function (cc) {
  761. var pinObj = jQuery('#' + this.getPinId(cc));
  762. return pinObj.html();
  763. },
  764. getPins: function () {
  765. var pins = this.container.find('.jqvmap_pin');
  766. var ret = new Object();
  767. jQuery.each(pins, function (index, pinObj) {
  768. pinObj = jQuery(pinObj);
  769. var cc = pinObj.attr('for');
  770. var pinContent = pinObj.html();
  771. eval("ret." + cc + "=pinContent");
  772. });
  773. return JSON.stringify(ret);
  774. },
  775. removePin: function (cc) {
  776. jQuery('#' + this.getPinId(cc)).remove();
  777. },
  778. removePins: function () {
  779. this.container.find('.jqvmap_pin').remove();
  780. }
  781. };
  782. WorldMap.xlink = "http://www.w3.org/1999/xlink";
  783. WorldMap.mapIndex = 1;
  784. WorldMap.maps = {};
  785. var ColorScale = function (colors, normalizeFunction, minValue, maxValue) {
  786. if (colors) {
  787. this.setColors(colors);
  788. }
  789. if (normalizeFunction) {
  790. this.setNormalizeFunction(normalizeFunction);
  791. }
  792. if (minValue) {
  793. this.setMin(minValue);
  794. }
  795. if (minValue) {
  796. this.setMax(maxValue);
  797. }
  798. };
  799. ColorScale.prototype = {
  800. colors: [],
  801. setMin: function (min) {
  802. this.clearMinValue = min;
  803. if (typeof this.normalize === 'function') {
  804. this.minValue = this.normalize(min);
  805. } else {
  806. this.minValue = min;
  807. }
  808. },
  809. setMax: function (max) {
  810. this.clearMaxValue = max;
  811. if (typeof this.normalize === 'function') {
  812. this.maxValue = this.normalize(max);
  813. } else {
  814. this.maxValue = max;
  815. }
  816. },
  817. setColors: function (colors) {
  818. for (var i = 0; i < colors.length; i++) {
  819. colors[i] = ColorScale.rgbToArray(colors[i]);
  820. }
  821. this.colors = colors;
  822. },
  823. setNormalizeFunction: function (f) {
  824. if (f === 'polynomial') {
  825. this.normalize = function (value) {
  826. return Math.pow(value, 0.2);
  827. };
  828. } else if (f === 'linear') {
  829. delete this.normalize;
  830. } else {
  831. this.normalize = f;
  832. }
  833. this.setMin(this.clearMinValue);
  834. this.setMax(this.clearMaxValue);
  835. },
  836. getColor: function (value) {
  837. if (typeof this.normalize === 'function') {
  838. value = this.normalize(value);
  839. }
  840. var lengthes = [];
  841. var fullLength = 0;
  842. var l;
  843. for (var i = 0; i < this.colors.length - 1; i++) {
  844. l = this.vectorLength(this.vectorSubtract(this.colors[i + 1], this.colors[i]));
  845. lengthes.push(l);
  846. fullLength += l;
  847. }
  848. var c = (this.maxValue - this.minValue) / fullLength;
  849. for (i = 0; i < lengthes.length; i++) {
  850. lengthes[i] *= c;
  851. }
  852. i = 0;
  853. value -= this.minValue;
  854. while (value - lengthes[i] >= 0) {
  855. value -= lengthes[i];
  856. i++;
  857. }
  858. var color;
  859. if (i == this.colors.length - 1) {
  860. color = this.vectorToNum(this.colors[i]).toString(16);
  861. } else {
  862. color = (this.vectorToNum(this.vectorAdd(this.colors[i], this.vectorMult(this.vectorSubtract(this.colors[i + 1], this.colors[i]), (value) / (lengthes[i]))))).toString(16);
  863. }
  864. while (color.length < 6) {
  865. color = '0' + color;
  866. }
  867. return '#' + color;
  868. },
  869. vectorToNum: function (vector) {
  870. var num = 0;
  871. for (var i = 0; i < vector.length; i++) {
  872. num += Math.round(vector[i]) * Math.pow(256, vector.length - i - 1);
  873. }
  874. return num;
  875. },
  876. vectorSubtract: function (vector1, vector2) {
  877. var vector = [];
  878. for (var i = 0; i < vector1.length; i++) {
  879. vector[i] = vector1[i] - vector2[i];
  880. }
  881. return vector;
  882. },
  883. vectorAdd: function (vector1, vector2) {
  884. var vector = [];
  885. for (var i = 0; i < vector1.length; i++) {
  886. vector[i] = vector1[i] + vector2[i];
  887. }
  888. return vector;
  889. },
  890. vectorMult: function (vector, num) {
  891. var result = [];
  892. for (var i = 0; i < vector.length; i++) {
  893. result[i] = vector[i] * num;
  894. }
  895. return result;
  896. },
  897. vectorLength: function (vector) {
  898. var result = 0;
  899. for (var i = 0; i < vector.length; i++) {
  900. result += vector[i] * vector[i];
  901. }
  902. return Math.sqrt(result);
  903. }
  904. };
  905. ColorScale.arrayToRgb = function (ar) {
  906. var rgb = '#';
  907. var d;
  908. for (var i = 0; i < ar.length; i++) {
  909. d = ar[i].toString(16);
  910. rgb += d.length == 1 ? '0' + d : d;
  911. }
  912. return rgb;
  913. };
  914. ColorScale.rgbToArray = function (rgb) {
  915. rgb = rgb.substr(1);
  916. return [parseInt(rgb.substr(0, 2), 16), parseInt(rgb.substr(2, 2), 16), parseInt(rgb.substr(4, 2), 16)];
  917. };
  918. })(jQuery);