jquery.vmap.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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'));
  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 (map.selectedRegions.indexOf(code) !== -1) {
  396. map.deselect(code, path);
  397. } else {
  398. map.select(code, path);
  399. }
  400. //console.log(selectedRegions);
  401. });
  402. if (params.showTooltip) {
  403. params.container.mousemove(function (e) {
  404. if (map.label.is(':visible')) {
  405. map.label.css({
  406. left: e.pageX - 15 - map.labelWidth,
  407. top: e.pageY - 15 - map.labelHeight
  408. });
  409. }
  410. });
  411. }
  412. this.setColors(params.colors);
  413. this.canvas.canvas.appendChild(this.rootGroup);
  414. this.applyTransform();
  415. this.colorScale = new ColorScale(params.scaleColors, params.normalizeFunction, params.valueMin, params.valueMax);
  416. if (params.values) {
  417. this.values = params.values;
  418. this.setValues(params.values);
  419. }
  420. if (params.selectedRegions) {
  421. if (params.selectedRegions instanceof Array) {
  422. for(var k in params.selectedRegions) {
  423. this.select(params.selectedRegions[k].toLowerCase());
  424. }
  425. } else {
  426. this.select(params.selectedRegions.toLowerCase());
  427. }
  428. }
  429. this.bindZoomButtons();
  430. WorldMap.mapIndex++;
  431. };
  432. WorldMap.prototype = {
  433. transX: 0,
  434. transY: 0,
  435. scale: 1,
  436. baseTransX: 0,
  437. baseTransY: 0,
  438. baseScale: 1,
  439. width: 0,
  440. height: 0,
  441. countries: {},
  442. countriesColors: {},
  443. countriesData: {},
  444. zoomStep: 1.4,
  445. zoomMaxStep: 4,
  446. zoomCurStep: 1,
  447. setColors: function (key, color) {
  448. if (typeof key == 'string') {
  449. this.countries[key].setFill(color);
  450. this.countries[key].setAttribute("original", color);
  451. } else {
  452. var colors = key;
  453. for (var code in colors) {
  454. if (this.countries[code]) {
  455. this.countries[code].setFill(colors[code]);
  456. this.countries[code].setAttribute("original", colors[code]);
  457. }
  458. }
  459. }
  460. },
  461. setValues: function (values) {
  462. var max = 0,
  463. min = Number.MAX_VALUE,
  464. val;
  465. for (var cc in values) {
  466. val = parseFloat(values[cc]);
  467. if (val > max) {
  468. max = values[cc];
  469. }
  470. if (val && val < min) {
  471. min = val;
  472. }
  473. }
  474. this.colorScale.setMin(min);
  475. this.colorScale.setMax(max);
  476. var colors = {};
  477. for (cc in values) {
  478. val = parseFloat(values[cc]);
  479. if (val) {
  480. colors[cc] = this.colorScale.getColor(val);
  481. } else {
  482. colors[cc] = this.color;
  483. }
  484. }
  485. this.setColors(colors);
  486. this.values = values;
  487. },
  488. setBackgroundColor: function (backgroundColor) {
  489. this.container.css('background-color', backgroundColor);
  490. },
  491. setScaleColors: function (colors) {
  492. this.colorScale.setColors(colors);
  493. if (this.values) {
  494. this.setValues(this.values);
  495. }
  496. },
  497. setNormalizeFunction: function (f) {
  498. this.colorScale.setNormalizeFunction(f);
  499. if (this.values) {
  500. this.setValues(this.values);
  501. }
  502. },
  503. highlight: function (cc, path) {
  504. path = path || $('#' + this.getCountryId(cc))[0];
  505. if (this.hoverOpacity) {
  506. path.setOpacity(this.hoverOpacity);
  507. } else if (this.hoverColor) {
  508. path.currentFillColor = path.getFill() + '';
  509. path.setFill(this.hoverColor);
  510. }
  511. },
  512. unhighlight: function (cc, path) {
  513. path = path || $('#' + this.getCountryId(cc))[0];
  514. path.setOpacity(1);
  515. if (path.currentFillColor) {
  516. path.setFill(path.currentFillColor);
  517. }
  518. },
  519. select: function (cc, path) {
  520. path = path || $('#' + this.getCountryId(cc))[0];
  521. if(this.selectedRegions.indexOf(cc) < 0) {
  522. if (this.multiSelectRegion) {
  523. this.selectedRegions.push(cc);
  524. } else {
  525. this.selectedRegions = [cc];
  526. }
  527. // MUST BE after the change of selectedRegions
  528. // Otherwise, we might loop
  529. $(this.container).trigger('regionSelect.jqvmap', [cc]);
  530. if (this.selectedColor) {
  531. path.currentFillColor = this.selectedColor;
  532. path.setFill(this.selectedColor);
  533. }
  534. }
  535. },
  536. deselect: function (cc, path) {
  537. path = path || $('#' + this.getCountryId(cc))[0];
  538. if(this.selectedRegions.indexOf(cc) >= 0) {
  539. this.selectedRegions.splice(this.selectedRegions.indexOf(cc), 1);
  540. // MUST BE after the change of selectedRegions
  541. // Otherwise, we might loop
  542. $(this.container).trigger('regionDeselect.jqvmap', [cc]);
  543. path.currentFillColor = path.getOriginalFill();
  544. path.setFill(path.getOriginalFill());
  545. }
  546. },
  547. isSelected: function(cc) {
  548. return this.selectedRegions.indexOf(cc) >= 0;
  549. },
  550. resize: function () {
  551. var curBaseScale = this.baseScale;
  552. if (this.width / this.height > this.defaultWidth / this.defaultHeight) {
  553. this.baseScale = this.height / this.defaultHeight;
  554. this.baseTransX = Math.abs(this.width - this.defaultWidth * this.baseScale) / (2 * this.baseScale);
  555. } else {
  556. this.baseScale = this.width / this.defaultWidth;
  557. this.baseTransY = Math.abs(this.height - this.defaultHeight * this.baseScale) / (2 * this.baseScale);
  558. }
  559. this.scale *= this.baseScale / curBaseScale;
  560. this.transX *= this.baseScale / curBaseScale;
  561. this.transY *= this.baseScale / curBaseScale;
  562. },
  563. reset: function () {
  564. this.countryTitle.reset();
  565. for (var key in this.countries) {
  566. this.countries[key].setFill(WorldMap.defaultColor);
  567. }
  568. this.scale = this.baseScale;
  569. this.transX = this.baseTransX;
  570. this.transY = this.baseTransY;
  571. this.applyTransform();
  572. },
  573. applyTransform: function () {
  574. var maxTransX, maxTransY, minTransX, minTransY;
  575. if (this.defaultWidth * this.scale <= this.width) {
  576. maxTransX = (this.width - this.defaultWidth * this.scale) / (2 * this.scale);
  577. minTransX = (this.width - this.defaultWidth * this.scale) / (2 * this.scale);
  578. } else {
  579. maxTransX = 0;
  580. minTransX = (this.width - this.defaultWidth * this.scale) / this.scale;
  581. }
  582. if (this.defaultHeight * this.scale <= this.height) {
  583. maxTransY = (this.height - this.defaultHeight * this.scale) / (2 * this.scale);
  584. minTransY = (this.height - this.defaultHeight * this.scale) / (2 * this.scale);
  585. } else {
  586. maxTransY = 0;
  587. minTransY = (this.height - this.defaultHeight * this.scale) / this.scale;
  588. }
  589. if (this.transY > maxTransY) {
  590. this.transY = maxTransY;
  591. }
  592. else if (this.transY < minTransY) {
  593. this.transY = minTransY;
  594. }
  595. if (this.transX > maxTransX) {
  596. this.transX = maxTransX;
  597. }
  598. else if (this.transX < minTransX) {
  599. this.transX = minTransX;
  600. }
  601. this.canvas.applyTransformParams(this.scale, this.transX, this.transY);
  602. },
  603. makeDraggable: function () {
  604. var mouseDown = false;
  605. var oldPageX, oldPageY;
  606. var self = this;
  607. self.isMoving = false;
  608. self.isMovingTimeout = false;
  609. this.container.mousemove(function (e) {
  610. if (mouseDown) {
  611. var curTransX = self.transX;
  612. var curTransY = self.transY;
  613. self.transX -= (oldPageX - e.pageX) / self.scale;
  614. self.transY -= (oldPageY - e.pageY) / self.scale;
  615. self.applyTransform();
  616. oldPageX = e.pageX;
  617. oldPageY = e.pageY;
  618. self.isMoving = true;
  619. if (self.isMovingTimeout) {
  620. clearTimeout(self.isMovingTimeout);
  621. }
  622. }
  623. return false;
  624. }).mousedown(function (e) {
  625. mouseDown = true;
  626. oldPageX = e.pageX;
  627. oldPageY = e.pageY;
  628. return false;
  629. }).mouseup(function () {
  630. mouseDown = false;
  631. self.isMovingTimeout = setTimeout(function () {
  632. self.isMoving = false;
  633. }, 100);
  634. return false;
  635. });
  636. },
  637. bindZoomButtons: function () {
  638. var map = this;
  639. var sliderDelta = (jQuery('#zoom').innerHeight() - 6 * 2 - 15 * 2 - 3 * 2 - 7 - 6) / (this.zoomMaxStep - this.zoomCurStep);
  640. this.container.find('.jqvmap-zoomin').click(function () {
  641. if (map.zoomCurStep < map.zoomMaxStep) {
  642. var curTransX = map.transX;
  643. var curTransY = map.transY;
  644. var curScale = map.scale;
  645. map.transX -= (map.width / map.scale - map.width / (map.scale * map.zoomStep)) / 2;
  646. map.transY -= (map.height / map.scale - map.height / (map.scale * map.zoomStep)) / 2;
  647. map.setScale(map.scale * map.zoomStep);
  648. map.zoomCurStep++;
  649. jQuery('#zoomSlider').css('top', parseInt(jQuery('#zoomSlider').css('top'), 10) - sliderDelta);
  650. }
  651. });
  652. this.container.find('.jqvmap-zoomout').click(function () {
  653. if (map.zoomCurStep > 1) {
  654. var curTransX = map.transX;
  655. var curTransY = map.transY;
  656. var curScale = map.scale;
  657. map.transX += (map.width / (map.scale / map.zoomStep) - map.width / map.scale) / 2;
  658. map.transY += (map.height / (map.scale / map.zoomStep) - map.height / map.scale) / 2;
  659. map.setScale(map.scale / map.zoomStep);
  660. map.zoomCurStep--;
  661. jQuery('#zoomSlider').css('top', parseInt(jQuery('#zoomSlider').css('top'), 10) + sliderDelta);
  662. }
  663. });
  664. },
  665. setScale: function (scale) {
  666. this.scale = scale;
  667. this.applyTransform();
  668. },
  669. getCountryId: function (cc) {
  670. return 'jqvmap' + this.index + '_' + cc;
  671. }
  672. };
  673. WorldMap.xlink = "http://www.w3.org/1999/xlink";
  674. WorldMap.mapIndex = 1;
  675. WorldMap.maps = {};
  676. var ColorScale = function (colors, normalizeFunction, minValue, maxValue) {
  677. if (colors) {
  678. this.setColors(colors);
  679. }
  680. if (normalizeFunction) {
  681. this.setNormalizeFunction(normalizeFunction);
  682. }
  683. if (minValue) {
  684. this.setMin(minValue);
  685. }
  686. if (minValue) {
  687. this.setMax(maxValue);
  688. }
  689. };
  690. ColorScale.prototype = {
  691. colors: [],
  692. setMin: function (min) {
  693. this.clearMinValue = min;
  694. if (typeof this.normalize === 'function') {
  695. this.minValue = this.normalize(min);
  696. } else {
  697. this.minValue = min;
  698. }
  699. },
  700. setMax: function (max) {
  701. this.clearMaxValue = max;
  702. if (typeof this.normalize === 'function') {
  703. this.maxValue = this.normalize(max);
  704. } else {
  705. this.maxValue = max;
  706. }
  707. },
  708. setColors: function (colors) {
  709. for (var i = 0; i < colors.length; i++) {
  710. colors[i] = ColorScale.rgbToArray(colors[i]);
  711. }
  712. this.colors = colors;
  713. },
  714. setNormalizeFunction: function (f) {
  715. if (f === 'polynomial') {
  716. this.normalize = function (value) {
  717. return Math.pow(value, 0.2);
  718. };
  719. }
  720. else if (f === 'linear') {
  721. delete this.normalize;
  722. } else {
  723. this.normalize = f;
  724. }
  725. this.setMin(this.clearMinValue);
  726. this.setMax(this.clearMaxValue);
  727. },
  728. getColor: function (value) {
  729. if (typeof this.normalize === 'function') {
  730. value = this.normalize(value);
  731. }
  732. var lengthes = [];
  733. var fullLength = 0;
  734. var l;
  735. for (var i = 0; i < this.colors.length - 1; i++) {
  736. l = this.vectorLength(this.vectorSubtract(this.colors[i + 1], this.colors[i]));
  737. lengthes.push(l);
  738. fullLength += l;
  739. }
  740. var c = (this.maxValue - this.minValue) / fullLength;
  741. for (i = 0; i < lengthes.length; i++) {
  742. lengthes[i] *= c;
  743. }
  744. i = 0;
  745. value -= this.minValue;
  746. while (value - lengthes[i] >= 0) {
  747. value -= lengthes[i];
  748. i++;
  749. }
  750. var color;
  751. if (i == this.colors.length - 1) {
  752. color = this.vectorToNum(this.colors[i]).toString(16);
  753. } else {
  754. color = (this.vectorToNum(this.vectorAdd(this.colors[i], this.vectorMult(this.vectorSubtract(this.colors[i + 1], this.colors[i]), (value) / (lengthes[i]))))).toString(16);
  755. }
  756. while (color.length < 6) {
  757. color = '0' + color;
  758. }
  759. return '#' + color;
  760. },
  761. vectorToNum: function (vector) {
  762. var num = 0;
  763. for (var i = 0; i < vector.length; i++) {
  764. num += Math.round(vector[i]) * Math.pow(256, vector.length - i - 1);
  765. }
  766. return num;
  767. },
  768. vectorSubtract: function (vector1, vector2) {
  769. var vector = [];
  770. for (var i = 0; i < vector1.length; i++) {
  771. vector[i] = vector1[i] - vector2[i];
  772. }
  773. return vector;
  774. },
  775. vectorAdd: function (vector1, vector2) {
  776. var vector = [];
  777. for (var i = 0; i < vector1.length; i++) {
  778. vector[i] = vector1[i] + vector2[i];
  779. }
  780. return vector;
  781. },
  782. vectorMult: function (vector, num) {
  783. var result = [];
  784. for (var i = 0; i < vector.length; i++) {
  785. result[i] = vector[i] * num;
  786. }
  787. return result;
  788. },
  789. vectorLength: function (vector) {
  790. var result = 0;
  791. for (var i = 0; i < vector.length; i++) {
  792. result += vector[i] * vector[i];
  793. }
  794. return Math.sqrt(result);
  795. }
  796. };
  797. ColorScale.arrayToRgb = function (ar) {
  798. var rgb = '#';
  799. var d;
  800. for (var i = 0; i < ar.length; i++) {
  801. d = ar[i].toString(16);
  802. rgb += d.length == 1 ? '0' + d : d;
  803. }
  804. return rgb;
  805. };
  806. ColorScale.rgbToArray = function (rgb) {
  807. rgb = rgb.substr(1);
  808. return [parseInt(rgb.substr(0, 2), 16), parseInt(rgb.substr(2, 2), 16), parseInt(rgb.substr(4, 2), 16)];
  809. };
  810. })(jQuery);