MapNavigation.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import Chart from '../Core/Chart/Chart.js';
  12. import H from '../Core/Globals.js';
  13. var doc = H.doc;
  14. import U from '../Core/Utilities.js';
  15. var addEvent = U.addEvent, extend = U.extend, merge = U.merge, objectEach = U.objectEach, pick = U.pick;
  16. /* eslint-disable no-invalid-this, valid-jsdoc */
  17. /**
  18. * @private
  19. */
  20. function stopEvent(e) {
  21. if (e) {
  22. if (e.preventDefault) {
  23. e.preventDefault();
  24. }
  25. if (e.stopPropagation) {
  26. e.stopPropagation();
  27. }
  28. e.cancelBubble = true;
  29. }
  30. }
  31. /**
  32. * The MapNavigation handles buttons for navigation in addition to mousewheel
  33. * and doubleclick handlers for chart zooming.
  34. *
  35. * @private
  36. * @class
  37. * @name MapNavigation
  38. *
  39. * @param {Highcharts.Chart} chart
  40. * The Chart instance.
  41. */
  42. function MapNavigation(chart) {
  43. this.init(chart);
  44. }
  45. /**
  46. * Initialize function.
  47. *
  48. * @function MapNavigation#init
  49. *
  50. * @param {Highcharts.Chart} chart
  51. * The Chart instance.
  52. *
  53. * @return {void}
  54. */
  55. MapNavigation.prototype.init = function (chart) {
  56. this.chart = chart;
  57. chart.mapNavButtons = [];
  58. };
  59. /**
  60. * Update the map navigation with new options. Calling this is the same as
  61. * calling `chart.update({ mapNavigation: {} })`.
  62. *
  63. * @function MapNavigation#update
  64. *
  65. * @param {Highcharts.MapNavigationOptions} [options]
  66. * New options for the map navigation.
  67. *
  68. * @return {void}
  69. */
  70. MapNavigation.prototype.update = function (options) {
  71. var chart = this.chart, o = chart.options.mapNavigation, buttonOptions, attr, states, hoverStates, selectStates, outerHandler = function (e) {
  72. this.handler.call(chart, e);
  73. stopEvent(e); // Stop default click event (#4444)
  74. }, mapNavButtons = chart.mapNavButtons;
  75. // Merge in new options in case of update, and register back to chart
  76. // options.
  77. if (options) {
  78. o = chart.options.mapNavigation =
  79. merge(chart.options.mapNavigation, options);
  80. }
  81. // Destroy buttons in case of dynamic update
  82. while (mapNavButtons.length) {
  83. mapNavButtons.pop().destroy();
  84. }
  85. if (pick(o.enableButtons, o.enabled) && !chart.renderer.forExport) {
  86. objectEach(o.buttons, function (button, n) {
  87. buttonOptions = merge(o.buttonOptions, button);
  88. // Presentational
  89. if (!chart.styledMode) {
  90. attr = buttonOptions.theme;
  91. attr.style = merge(buttonOptions.theme.style, buttonOptions.style // #3203
  92. );
  93. states = attr.states;
  94. hoverStates = states && states.hover;
  95. selectStates = states && states.select;
  96. }
  97. button = chart.renderer
  98. .button(buttonOptions.text, 0, 0, outerHandler, attr, hoverStates, selectStates, 0, n === 'zoomIn' ? 'topbutton' : 'bottombutton')
  99. .addClass('highcharts-map-navigation highcharts-' + {
  100. zoomIn: 'zoom-in',
  101. zoomOut: 'zoom-out'
  102. }[n])
  103. .attr({
  104. width: buttonOptions.width,
  105. height: buttonOptions.height,
  106. title: chart.options.lang[n],
  107. padding: buttonOptions.padding,
  108. zIndex: 5
  109. })
  110. .add();
  111. button.handler = buttonOptions.onclick;
  112. // Stop double click event (#4444)
  113. addEvent(button.element, 'dblclick', stopEvent);
  114. mapNavButtons.push(button);
  115. // Align it after the plotBox is known (#12776)
  116. var bo = buttonOptions;
  117. var un = addEvent(chart, 'load', function () {
  118. button.align(extend(bo, {
  119. width: button.width,
  120. height: 2 * button.height
  121. }), null, bo.alignTo);
  122. un();
  123. });
  124. });
  125. }
  126. this.updateEvents(o);
  127. };
  128. /**
  129. * Update events, called internally from the update function. Add new event
  130. * handlers, or unbinds events if disabled.
  131. *
  132. * @function MapNavigation#updateEvents
  133. *
  134. * @param {Highcharts.MapNavigationOptions} options
  135. * Options for map navigation.
  136. *
  137. * @return {void}
  138. */
  139. MapNavigation.prototype.updateEvents = function (options) {
  140. var chart = this.chart;
  141. // Add the double click event
  142. if (pick(options.enableDoubleClickZoom, options.enabled) ||
  143. options.enableDoubleClickZoomTo) {
  144. this.unbindDblClick = this.unbindDblClick || addEvent(chart.container, 'dblclick', function (e) {
  145. chart.pointer.onContainerDblClick(e);
  146. });
  147. }
  148. else if (this.unbindDblClick) {
  149. // Unbind and set unbinder to undefined
  150. this.unbindDblClick = this.unbindDblClick();
  151. }
  152. // Add the mousewheel event
  153. if (pick(options.enableMouseWheelZoom, options.enabled)) {
  154. this.unbindMouseWheel = this.unbindMouseWheel || addEvent(chart.container, typeof doc.onmousewheel === 'undefined' ?
  155. 'DOMMouseScroll' : 'mousewheel', function (e) {
  156. chart.pointer.onContainerMouseWheel(e);
  157. // Issue #5011, returning false from non-jQuery event does
  158. // not prevent default
  159. stopEvent(e);
  160. return false;
  161. });
  162. }
  163. else if (this.unbindMouseWheel) {
  164. // Unbind and set unbinder to undefined
  165. this.unbindMouseWheel = this.unbindMouseWheel();
  166. }
  167. };
  168. // Add events to the Chart object itself
  169. extend(Chart.prototype, /** @lends Chart.prototype */ {
  170. /**
  171. * Fit an inner box to an outer. If the inner box overflows left or right,
  172. * align it to the sides of the outer. If it overflows both sides, fit it
  173. * within the outer. This is a pattern that occurs more places in
  174. * Highcharts, perhaps it should be elevated to a common utility function.
  175. *
  176. * @ignore
  177. * @function Highcharts.Chart#fitToBox
  178. *
  179. * @param {Highcharts.BBoxObject} inner
  180. *
  181. * @param {Highcharts.BBoxObject} outer
  182. *
  183. * @return {Highcharts.BBoxObject}
  184. * The inner box
  185. */
  186. fitToBox: function (inner, outer) {
  187. [['x', 'width'], ['y', 'height']].forEach(function (dim) {
  188. var pos = dim[0], size = dim[1];
  189. if (inner[pos] + inner[size] >
  190. outer[pos] + outer[size]) { // right
  191. // the general size is greater, fit fully to outer
  192. if (inner[size] > outer[size]) {
  193. inner[size] = outer[size];
  194. inner[pos] = outer[pos];
  195. }
  196. else { // align right
  197. inner[pos] = outer[pos] +
  198. outer[size] - inner[size];
  199. }
  200. }
  201. if (inner[size] > outer[size]) {
  202. inner[size] = outer[size];
  203. }
  204. if (inner[pos] < outer[pos]) {
  205. inner[pos] = outer[pos];
  206. }
  207. });
  208. return inner;
  209. },
  210. /**
  211. * Highmaps only. Zoom in or out of the map. See also {@link Point#zoomTo}.
  212. * See {@link Chart#fromLatLonToPoint} for how to get the `centerX` and
  213. * `centerY` parameters for a geographic location.
  214. *
  215. * @function Highcharts.Chart#mapZoom
  216. *
  217. * @param {number} [howMuch]
  218. * How much to zoom the map. Values less than 1 zooms in. 0.5 zooms
  219. * in to half the current view. 2 zooms to twice the current view. If
  220. * omitted, the zoom is reset.
  221. *
  222. * @param {number} [centerX]
  223. * The X axis position to center around if available space.
  224. *
  225. * @param {number} [centerY]
  226. * The Y axis position to center around if available space.
  227. *
  228. * @param {number} [mouseX]
  229. * Fix the zoom to this position if possible. This is used for
  230. * example in mousewheel events, where the area under the mouse
  231. * should be fixed as we zoom in.
  232. *
  233. * @param {number} [mouseY]
  234. * Fix the zoom to this position if possible.
  235. *
  236. * @return {void}
  237. */
  238. mapZoom: function (howMuch, centerXArg, centerYArg, mouseX, mouseY) {
  239. var chart = this, xAxis = chart.xAxis[0], xRange = xAxis.max - xAxis.min, centerX = pick(centerXArg, xAxis.min + xRange / 2), newXRange = xRange * howMuch, yAxis = chart.yAxis[0], yRange = yAxis.max - yAxis.min, centerY = pick(centerYArg, yAxis.min + yRange / 2), newYRange = yRange * howMuch, fixToX = mouseX ? ((mouseX - xAxis.pos) / xAxis.len) : 0.5, fixToY = mouseY ? ((mouseY - yAxis.pos) / yAxis.len) : 0.5, newXMin = centerX - newXRange * fixToX, newYMin = centerY - newYRange * fixToY, newExt = chart.fitToBox({
  240. x: newXMin,
  241. y: newYMin,
  242. width: newXRange,
  243. height: newYRange
  244. }, {
  245. x: xAxis.dataMin,
  246. y: yAxis.dataMin,
  247. width: xAxis.dataMax - xAxis.dataMin,
  248. height: yAxis.dataMax - yAxis.dataMin
  249. }), zoomOut = (newExt.x <= xAxis.dataMin &&
  250. newExt.width >=
  251. xAxis.dataMax - xAxis.dataMin &&
  252. newExt.y <= yAxis.dataMin &&
  253. newExt.height >= yAxis.dataMax - yAxis.dataMin);
  254. // When mousewheel zooming, fix the point under the mouse
  255. if (mouseX && xAxis.mapAxis) {
  256. xAxis.mapAxis.fixTo = [mouseX - xAxis.pos, centerXArg];
  257. }
  258. if (mouseY && yAxis.mapAxis) {
  259. yAxis.mapAxis.fixTo = [mouseY - yAxis.pos, centerYArg];
  260. }
  261. // Zoom
  262. if (typeof howMuch !== 'undefined' && !zoomOut) {
  263. xAxis.setExtremes(newExt.x, newExt.x + newExt.width, false);
  264. yAxis.setExtremes(newExt.y, newExt.y + newExt.height, false);
  265. // Reset zoom
  266. }
  267. else {
  268. xAxis.setExtremes(void 0, void 0, false);
  269. yAxis.setExtremes(void 0, void 0, false);
  270. }
  271. // Prevent zooming until this one is finished animating
  272. /*
  273. chart.holdMapZoom = true;
  274. setTimeout(function () {
  275. chart.holdMapZoom = false;
  276. }, 200);
  277. */
  278. /*
  279. delay = animation ? animation.duration || 500 : 0;
  280. if (delay) {
  281. chart.isMapZooming = true;
  282. setTimeout(function () {
  283. chart.isMapZooming = false;
  284. if (chart.mapZoomQueue) {
  285. chart.mapZoom.apply(chart, chart.mapZoomQueue);
  286. }
  287. chart.mapZoomQueue = null;
  288. }, delay);
  289. }
  290. */
  291. chart.redraw();
  292. }
  293. });
  294. // Extend the Chart.render method to add zooming and panning
  295. addEvent(Chart, 'beforeRender', function () {
  296. // Render the plus and minus buttons. Doing this before the shapes makes
  297. // getBBox much quicker, at least in Chrome.
  298. this.mapNavigation = new MapNavigation(this);
  299. this.mapNavigation.update();
  300. });
  301. H.MapNavigation = MapNavigation;