MapNavigation.js 11 KB

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