Map.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. import O from '../Core/Options.js';
  14. var defaultOptions = O.defaultOptions;
  15. import SVGRenderer from '../Core/Renderer/SVG/SVGRenderer.js';
  16. import U from '../Core/Utilities.js';
  17. var extend = U.extend, getOptions = U.getOptions, merge = U.merge, pick = U.pick;
  18. var Renderer = H.Renderer, VMLRenderer = H.VMLRenderer;
  19. // Add language
  20. extend(defaultOptions.lang, {
  21. zoomIn: 'Zoom in',
  22. zoomOut: 'Zoom out'
  23. });
  24. // Set the default map navigation options
  25. /**
  26. * @product highmaps
  27. * @optionparent mapNavigation
  28. */
  29. defaultOptions.mapNavigation = {
  30. /**
  31. * General options for the map navigation buttons. Individual options
  32. * can be given from the [mapNavigation.buttons](#mapNavigation.buttons)
  33. * option set.
  34. *
  35. * @sample {highmaps} maps/mapnavigation/button-theme/
  36. * Theming the navigation buttons
  37. */
  38. buttonOptions: {
  39. /**
  40. * What box to align the buttons to. Possible values are `plotBox`
  41. * and `spacingBox`.
  42. *
  43. * @type {Highcharts.ButtonRelativeToValue}
  44. */
  45. alignTo: 'plotBox',
  46. /**
  47. * The alignment of the navigation buttons.
  48. *
  49. * @type {Highcharts.AlignValue}
  50. */
  51. align: 'left',
  52. /**
  53. * The vertical alignment of the buttons. Individual alignment can
  54. * be adjusted by each button's `y` offset.
  55. *
  56. * @type {Highcharts.VerticalAlignValue}
  57. */
  58. verticalAlign: 'top',
  59. /**
  60. * The X offset of the buttons relative to its `align` setting.
  61. */
  62. x: 0,
  63. /**
  64. * The width of the map navigation buttons.
  65. */
  66. width: 18,
  67. /**
  68. * The pixel height of the map navigation buttons.
  69. */
  70. height: 18,
  71. /**
  72. * Padding for the navigation buttons.
  73. *
  74. * @since 5.0.0
  75. */
  76. padding: 5,
  77. /**
  78. * Text styles for the map navigation buttons.
  79. *
  80. * @type {Highcharts.CSSObject}
  81. * @default {"fontSize": "15px", "fontWeight": "bold"}
  82. */
  83. style: {
  84. /** @ignore */
  85. fontSize: '15px',
  86. /** @ignore */
  87. fontWeight: 'bold'
  88. },
  89. /**
  90. * A configuration object for the button theme. The object accepts
  91. * SVG properties like `stroke-width`, `stroke` and `fill`. Tri-state
  92. * button styles are supported by the `states.hover` and `states.select`
  93. * objects.
  94. *
  95. * @sample {highmaps} maps/mapnavigation/button-theme/
  96. * Themed navigation buttons
  97. *
  98. * @type {Highcharts.SVGAttributes}
  99. * @default {"stroke-width": 1, "text-align": "center"}
  100. */
  101. theme: {
  102. /** @ignore */
  103. 'stroke-width': 1,
  104. /** @ignore */
  105. 'text-align': 'center'
  106. }
  107. },
  108. /**
  109. * The individual buttons for the map navigation. This usually includes
  110. * the zoom in and zoom out buttons. Properties for each button is
  111. * inherited from
  112. * [mapNavigation.buttonOptions](#mapNavigation.buttonOptions), while
  113. * individual options can be overridden. But default, the `onclick`, `text`
  114. * and `y` options are individual.
  115. */
  116. buttons: {
  117. /**
  118. * Options for the zoom in button. Properties for the zoom in and zoom
  119. * out buttons are inherited from
  120. * [mapNavigation.buttonOptions](#mapNavigation.buttonOptions), while
  121. * individual options can be overridden. By default, the `onclick`,
  122. * `text` and `y` options are individual.
  123. *
  124. * @extends mapNavigation.buttonOptions
  125. */
  126. zoomIn: {
  127. // eslint-disable-next-line valid-jsdoc
  128. /**
  129. * Click handler for the button.
  130. *
  131. * @type {Function}
  132. * @default function () { this.mapZoom(0.5); }
  133. */
  134. onclick: function () {
  135. this.mapZoom(0.5);
  136. },
  137. /**
  138. * The text for the button. The tooltip (title) is a language option
  139. * given by [lang.zoomIn](#lang.zoomIn).
  140. */
  141. text: '+',
  142. /**
  143. * The position of the zoomIn button relative to the vertical
  144. * alignment.
  145. */
  146. y: 0
  147. },
  148. /**
  149. * Options for the zoom out button. Properties for the zoom in and
  150. * zoom out buttons are inherited from
  151. * [mapNavigation.buttonOptions](#mapNavigation.buttonOptions), while
  152. * individual options can be overridden. By default, the `onclick`,
  153. * `text` and `y` options are individual.
  154. *
  155. * @extends mapNavigation.buttonOptions
  156. */
  157. zoomOut: {
  158. // eslint-disable-next-line valid-jsdoc
  159. /**
  160. * Click handler for the button.
  161. *
  162. * @type {Function}
  163. * @default function () { this.mapZoom(2); }
  164. */
  165. onclick: function () {
  166. this.mapZoom(2);
  167. },
  168. /**
  169. * The text for the button. The tooltip (title) is a language option
  170. * given by [lang.zoomOut](#lang.zoomIn).
  171. */
  172. text: '-',
  173. /**
  174. * The position of the zoomOut button relative to the vertical
  175. * alignment.
  176. */
  177. y: 28
  178. }
  179. },
  180. /**
  181. * Whether to enable navigation buttons. By default it inherits the
  182. * [enabled](#mapNavigation.enabled) setting.
  183. *
  184. * @type {boolean}
  185. * @apioption mapNavigation.enableButtons
  186. */
  187. /**
  188. * Whether to enable map navigation. The default is not to enable
  189. * navigation, as many choropleth maps are simple and don't need it.
  190. * Additionally, when touch zoom and mousewheel zoom is enabled, it breaks
  191. * the default behaviour of these interactions in the website, and the
  192. * implementer should be aware of this.
  193. *
  194. * Individual interactions can be enabled separately, namely buttons,
  195. * multitouch zoom, double click zoom, double click zoom to element and
  196. * mousewheel zoom.
  197. *
  198. * @type {boolean}
  199. * @default false
  200. * @apioption mapNavigation.enabled
  201. */
  202. /**
  203. * Enables zooming in on an area on double clicking in the map. By default
  204. * it inherits the [enabled](#mapNavigation.enabled) setting.
  205. *
  206. * @type {boolean}
  207. * @apioption mapNavigation.enableDoubleClickZoom
  208. */
  209. /**
  210. * Whether to zoom in on an area when that area is double clicked.
  211. *
  212. * @sample {highmaps} maps/mapnavigation/doubleclickzoomto/
  213. * Enable double click zoom to
  214. *
  215. * @type {boolean}
  216. * @default false
  217. * @apioption mapNavigation.enableDoubleClickZoomTo
  218. */
  219. /**
  220. * Enables zooming by mouse wheel. By default it inherits the [enabled](
  221. * #mapNavigation.enabled) setting.
  222. *
  223. * @type {boolean}
  224. * @apioption mapNavigation.enableMouseWheelZoom
  225. */
  226. /**
  227. * Whether to enable multitouch zooming. Note that if the chart covers the
  228. * viewport, this prevents the user from using multitouch and touchdrag on
  229. * the web page, so you should make sure the user is not trapped inside the
  230. * chart. By default it inherits the [enabled](#mapNavigation.enabled)
  231. * setting.
  232. *
  233. * @type {boolean}
  234. * @apioption mapNavigation.enableTouchZoom
  235. */
  236. /**
  237. * Sensitivity of mouse wheel or trackpad scrolling. 1 is no sensitivity,
  238. * while with 2, one mousewheel delta will zoom in 50%.
  239. *
  240. * @since 4.2.4
  241. */
  242. mouseWheelSensitivity: 1.1
  243. // enabled: false,
  244. // enableButtons: null, // inherit from enabled
  245. // enableTouchZoom: null, // inherit from enabled
  246. // enableDoubleClickZoom: null, // inherit from enabled
  247. // enableDoubleClickZoomTo: false
  248. // enableMouseWheelZoom: null, // inherit from enabled
  249. };
  250. /* eslint-disable valid-jsdoc */
  251. /**
  252. * Utility for reading SVG paths directly.
  253. *
  254. * @requires modules/map
  255. *
  256. * @function Highcharts.splitPath
  257. *
  258. * @param {string|Array<string|number>} path
  259. *
  260. * @return {Highcharts.SVGPathArray}
  261. */
  262. var splitPath = H.splitPath = function (path) {
  263. var arr;
  264. if (typeof path === 'string') {
  265. path = path
  266. // Move letters apart
  267. .replace(/([A-Za-z])/g, ' $1 ')
  268. // Trim
  269. .replace(/^\s*/, '').replace(/\s*$/, '');
  270. // Split on spaces and commas. The semicolon is bogus, designed to
  271. // circumvent string replacement in the pre-v7 assembler that built
  272. // specific styled mode files.
  273. var split = path.split(/[ ,;]+/);
  274. arr = split.map(function (item) {
  275. if (!/[A-za-z]/.test(item)) {
  276. return parseFloat(item);
  277. }
  278. return item;
  279. });
  280. }
  281. else {
  282. arr = path;
  283. }
  284. return SVGRenderer.prototype.pathToSegments(arr);
  285. };
  286. /**
  287. * Contains all loaded map data for Highmaps.
  288. *
  289. * @requires modules/map
  290. *
  291. * @name Highcharts.maps
  292. * @type {Record<string,*>}
  293. */
  294. H.maps = {};
  295. /**
  296. * Create symbols for the zoom buttons
  297. * @private
  298. */
  299. function selectiveRoundedRect(x, y, w, h, rTopLeft, rTopRight, rBottomRight, rBottomLeft) {
  300. return [
  301. ['M', x + rTopLeft, y],
  302. // top side
  303. ['L', x + w - rTopRight, y],
  304. // top right corner
  305. ['C', x + w - rTopRight / 2, y, x + w, y + rTopRight / 2, x + w, y + rTopRight],
  306. // right side
  307. ['L', x + w, y + h - rBottomRight],
  308. // bottom right corner
  309. ['C', x + w, y + h - rBottomRight / 2, x + w - rBottomRight / 2, y + h, x + w - rBottomRight, y + h],
  310. // bottom side
  311. ['L', x + rBottomLeft, y + h],
  312. // bottom left corner
  313. ['C', x + rBottomLeft / 2, y + h, x, y + h - rBottomLeft / 2, x, y + h - rBottomLeft],
  314. // left side
  315. ['L', x, y + rTopLeft],
  316. // top left corner
  317. ['C', x, y + rTopLeft / 2, x + rTopLeft / 2, y, x + rTopLeft, y],
  318. ['Z']
  319. ];
  320. }
  321. SVGRenderer.prototype.symbols.topbutton = function (x, y, w, h, options) {
  322. var r = (options && options.r) || 0;
  323. return selectiveRoundedRect(x - 1, y - 1, w, h, r, r, 0, 0);
  324. };
  325. SVGRenderer.prototype.symbols.bottombutton = function (x, y, w, h, options) {
  326. var r = (options && options.r) || 0;
  327. return selectiveRoundedRect(x - 1, y - 1, w, h, 0, 0, r, r);
  328. };
  329. // The symbol callbacks are generated on the SVGRenderer object in all browsers.
  330. // Even VML browsers need this in order to generate shapes in export. Now share
  331. // them with the VMLRenderer.
  332. if (Renderer === VMLRenderer) {
  333. ['topbutton', 'bottombutton'].forEach(function (shape) {
  334. VMLRenderer.prototype.symbols[shape] =
  335. SVGRenderer.prototype.symbols[shape];
  336. });
  337. }
  338. /**
  339. * The factory function for creating new map charts. Creates a new {@link
  340. * Highcharts.Chart|Chart} object with different default options than the basic
  341. * Chart.
  342. *
  343. * @requires modules/map
  344. *
  345. * @function Highcharts.mapChart
  346. *
  347. * @param {string|Highcharts.HTMLDOMElement} [renderTo]
  348. * The DOM element to render to, or its id.
  349. *
  350. * @param {Highcharts.Options} options
  351. * The chart options structure as described in the
  352. * [options reference](https://api.highcharts.com/highstock).
  353. *
  354. * @param {Highcharts.ChartCallbackFunction} [callback]
  355. * A function to execute when the chart object is finished loading and
  356. * rendering. In most cases the chart is built in one thread, but in
  357. * Internet Explorer version 8 or less the chart is sometimes
  358. * initialized before the document is ready, and in these cases the
  359. * chart object will not be finished synchronously. As a consequence,
  360. * code that relies on the newly built Chart object should always run in
  361. * the callback. Defining a
  362. * [chart.events.load](https://api.highcharts.com/highstock/chart.events.load)
  363. * handler is equivalent.
  364. *
  365. * @return {Highcharts.Chart}
  366. * The chart object.
  367. */
  368. var mapChart = H.Map /* fake class for jQuery */ = H.mapChart = function (a, b, c) {
  369. var hasRenderToArg = typeof a === 'string' || a.nodeName, options = arguments[hasRenderToArg ? 1 : 0], userOptions = options, hiddenAxis = {
  370. endOnTick: false,
  371. visible: false,
  372. minPadding: 0,
  373. maxPadding: 0,
  374. startOnTick: false
  375. }, seriesOptions, defaultCreditsOptions = getOptions().credits;
  376. /* For visual testing
  377. hiddenAxis.gridLineWidth = 1;
  378. hiddenAxis.gridZIndex = 10;
  379. hiddenAxis.tickPositions = undefined;
  380. // */
  381. // Don't merge the data
  382. seriesOptions = options.series;
  383. options.series = null;
  384. options = merge({
  385. chart: {
  386. panning: {
  387. enabled: true,
  388. type: 'xy'
  389. },
  390. type: 'map'
  391. },
  392. credits: {
  393. mapText: pick(defaultCreditsOptions.mapText, ' \u00a9 <a href="{geojson.copyrightUrl}">' +
  394. '{geojson.copyrightShort}</a>'),
  395. mapTextFull: pick(defaultCreditsOptions.mapTextFull, '{geojson.copyright}')
  396. },
  397. tooltip: {
  398. followTouchMove: false
  399. },
  400. xAxis: hiddenAxis,
  401. yAxis: merge(hiddenAxis, { reversed: true })
  402. }, options, // user's options
  403. {
  404. chart: {
  405. inverted: false,
  406. alignTicks: false
  407. }
  408. });
  409. options.series = userOptions.series = seriesOptions;
  410. return hasRenderToArg ?
  411. new Chart(a, options, c) :
  412. new Chart(options, b);
  413. };
  414. var mapModule = {
  415. mapChart: mapChart,
  416. maps: H.maps,
  417. splitPath: splitPath
  418. };
  419. export default mapModule;