Utilities.js 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549
  1. /* *
  2. *
  3. * (c) 2010-2019 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 H from './Globals.js';
  12. /**
  13. * An animation configuration. Animation configurations can also be defined as
  14. * booleans, where `false` turns off animation and `true` defaults to a duration
  15. * of 500ms.
  16. *
  17. * @interface Highcharts.AnimationOptionsObject
  18. */ /**
  19. * A callback function to exectute when the animation finishes.
  20. * @name Highcharts.AnimationOptionsObject#complete
  21. * @type {Function|undefined}
  22. */ /**
  23. * The animation duration in milliseconds.
  24. * @name Highcharts.AnimationOptionsObject#duration
  25. * @type {number|undefined}
  26. */ /**
  27. * The name of an easing function as defined on the `Math` object.
  28. * @name Highcharts.AnimationOptionsObject#easing
  29. * @type {string|Function|undefined}
  30. */ /**
  31. * A callback function to execute on each step of each attribute or CSS property
  32. * that's being animated. The first argument contains information about the
  33. * animation and progress.
  34. * @name Highcharts.AnimationOptionsObject#step
  35. * @type {Function|undefined}
  36. */
  37. /**
  38. * Creates a frame for the animated SVG element.
  39. *
  40. * @callback Highcharts.AnimationStepCallbackFunction
  41. *
  42. * @param {Highcharts.SVGElement} this
  43. * The SVG element to animate.
  44. *
  45. * @return {void}
  46. */
  47. /**
  48. * Interface description for a class.
  49. *
  50. * @interface Highcharts.Class<T>
  51. * @extends Function
  52. */ /**
  53. * Class costructor.
  54. * @function Highcharts.Class<T>#new
  55. * @param {...Array<*>} args
  56. * Constructor arguments.
  57. * @return {T}
  58. * Class instance.
  59. */
  60. /**
  61. * A style object with camel case property names to define visual appearance of
  62. * a SVG element or HTML element. The properties can be whatever styles are
  63. * supported on the given SVG or HTML element.
  64. *
  65. * @example
  66. * {
  67. * fontFamily: 'monospace',
  68. * fontSize: '1.2em'
  69. * }
  70. *
  71. * @interface Highcharts.CSSObject
  72. */ /**
  73. * @name Highcharts.CSSObject#[key:string]
  74. * @type {boolean|number|string|undefined}
  75. */ /**
  76. * Background style for the element.
  77. * @name Highcharts.CSSObject#background
  78. * @type {string|undefined}
  79. */ /**
  80. * Background color of the element.
  81. * @name Highcharts.CSSObject#backgroundColor
  82. * @type {Highcharts.ColorString|undefined}
  83. */ /**
  84. * Border style for the element.
  85. * @name Highcharts.CSSObject#border
  86. * @type {string|undefined}
  87. */ /**
  88. * Radius of the element border.
  89. * @name Highcharts.CSSObject#borderRadius
  90. * @type {number|undefined}
  91. */ /**
  92. * Color used in the element. The 'contrast' option is a Highcharts custom
  93. * property that results in black or white, depending on the background of the
  94. * element.
  95. * @name Highcharts.CSSObject#color
  96. * @type {'contrast'|Highcharts.ColorString|undefined}
  97. */ /**
  98. * Style of the mouse cursor when resting over the element.
  99. * @name Highcharts.CSSObject#cursor
  100. * @type {Highcharts.CursorValue|undefined}
  101. */ /**
  102. * Font family of the element text. Multiple values have to be in decreasing
  103. * preference order and separated by comma.
  104. * @name Highcharts.CSSObject#fontFamily
  105. * @type {string|undefined}
  106. */ /**
  107. * Font size of the element text.
  108. * @name Highcharts.CSSObject#fontSize
  109. * @type {string|undefined}
  110. */ /**
  111. * Font weight of the element text.
  112. * @name Highcharts.CSSObject#fontWeight
  113. * @type {string|undefined}
  114. */ /**
  115. * Height of the element.
  116. * @name Highcharts.CSSObject#height
  117. * @type {number|undefined}
  118. */ /**
  119. * Width of the element border.
  120. * @name Highcharts.CSSObject#lineWidth
  121. * @type {number|undefined}
  122. */ /**
  123. * Opacity of the element.
  124. * @name Highcharts.CSSObject#opacity
  125. * @type {number|undefined}
  126. */ /**
  127. * Space around the element content.
  128. * @name Highcharts.CSSObject#padding
  129. * @type {string|undefined}
  130. */ /**
  131. * Behaviour of the element when the mouse cursor rests over it.
  132. * @name Highcharts.CSSObject#pointerEvents
  133. * @type {string|undefined}
  134. */ /**
  135. * Positioning of the element.
  136. * @name Highcharts.CSSObject#position
  137. * @type {string|undefined}
  138. */ /**
  139. * Alignment of the element text.
  140. * @name Highcharts.CSSObject#textAlign
  141. * @type {string|undefined}
  142. */ /**
  143. * Additional decoration of the element text.
  144. * @name Highcharts.CSSObject#textDecoration
  145. * @type {string|undefined}
  146. */ /**
  147. * Outline style of the element text.
  148. * @name Highcharts.CSSObject#textOutline
  149. * @type {string|undefined}
  150. */ /**
  151. * Line break style of the element text. Highcharts SVG elements support
  152. * `ellipsis` when a `width` is set.
  153. * @name Highcharts.CSSObject#textOverflow
  154. * @type {string|undefined}
  155. */ /**
  156. * Top spacing of the element relative to the parent element.
  157. * @name Highcharts.CSSObject#top
  158. * @type {string|undefined}
  159. */ /**
  160. * Animated transition of selected element properties.
  161. * @name Highcharts.CSSObject#transition
  162. * @type {string|undefined}
  163. */ /**
  164. * Line break style of the element text.
  165. * @name Highcharts.CSSObject#whiteSpace
  166. * @type {string|undefined}
  167. */ /**
  168. * Width of the element.
  169. * @name Highcharts.CSSObject#width
  170. * @type {number|undefined}
  171. */
  172. /**
  173. * All possible cursor styles.
  174. *
  175. * @typedef {'alias'|'all-scroll'|'auto'|'cell'|'col-resize'|'context-menu'|'copy'|'crosshair'|'default'|'e-resize'|'ew-resize'|'grab'|'grabbing'|'help'|'move'|'n-resize'|'ne-resize'|'nesw-resize'|'no-drop'|'none'|'not-allowed'|'ns-resize'|'nw-resize'|'nwse-resize'|'pointer'|'progress'|'row-resize'|'s-resize'|'se-resize'|'sw-resize'|'text'|'vertical-text'|'w-resize'|'wait'|'zoom-in'|'zoom-out'} Highcharts.CursorValue
  176. */
  177. /**
  178. * All possible dash styles.
  179. *
  180. * @typedef {'Dash'|'DashDot'|'Dot'|'LongDash'|'LongDashDot'|'LongDashDotDot'|'ShortDash'|'ShortDashDot'|'ShortDashDotDot'|'ShortDot'|'Solid'} Highcharts.DashStyleValue
  181. */
  182. /**
  183. * Generic dictionary in TypeScript notation.
  184. *
  185. * @interface Highcharts.Dictionary<T>
  186. */ /**
  187. * @name Highcharts.Dictionary<T>#[key:string]
  188. * @type {T}
  189. */
  190. /**
  191. * The function callback to execute when the event is fired. The `this` context
  192. * contains the instance, that fired the event.
  193. *
  194. * @callback Highcharts.EventCallbackFunction<T>
  195. *
  196. * @param {T} this
  197. *
  198. * @param {Highcharts.Dictionary<*>|Event} [eventArguments]
  199. * Event arguments.
  200. *
  201. * @return {boolean|void}
  202. */
  203. /**
  204. * The event options for adding function callback.
  205. *
  206. * @interface Highcharts.EventOptionsObject
  207. */ /**
  208. * The order the event handler should be called. This opens for having one
  209. * handler be called before another, independent of in which order they were
  210. * added.
  211. * @name Highcharts.EventOptionsObject#order
  212. * @type {number}
  213. */
  214. /**
  215. * Formats data as a string. Usually the data is accessible throught the `this`
  216. * keyword.
  217. *
  218. * @callback Highcharts.FormatterCallbackFunction<T>
  219. *
  220. * @param {T} this
  221. * Context to format
  222. *
  223. * @return {string}
  224. * Formatted text
  225. */
  226. /**
  227. * An object of key-value pairs for HTML attributes.
  228. *
  229. * @typedef {Highcharts.Dictionary<boolean|number|string|Function>} Highcharts.HTMLAttributes
  230. */
  231. /**
  232. * An HTML DOM element. The type is a reference to the regular HTMLElement in
  233. * the global scope.
  234. *
  235. * @typedef {global.HTMLElement} Highcharts.HTMLDOMElement
  236. *
  237. * @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement
  238. */
  239. /**
  240. * The iterator callback.
  241. *
  242. * @callback Highcharts.ObjectEachCallbackFunction<T>
  243. *
  244. * @param {T} this
  245. * The context.
  246. *
  247. * @param {*} value
  248. * The property value.
  249. *
  250. * @param {string} key
  251. * The property key.
  252. *
  253. * @param {*} obj
  254. * The object that objectEach is being applied to.
  255. */
  256. /**
  257. * An object containing `left` and `top` properties for the position in the
  258. * page.
  259. *
  260. * @interface Highcharts.OffsetObject
  261. */ /**
  262. * Left distance to the page border.
  263. * @name Highcharts.OffsetObject#left
  264. * @type {number}
  265. */ /**
  266. * Top distance to the page border.
  267. * @name Highcharts.OffsetObject#top
  268. * @type {number}
  269. */
  270. /**
  271. * Describes a range.
  272. *
  273. * @interface Highcharts.RangeObject
  274. */ /**
  275. * Maximum number of the range.
  276. * @name Highcharts.RangeObject#max
  277. * @type {number}
  278. */ /**
  279. * Minimum number of the range.
  280. * @name Highcharts.RangeObject#min
  281. * @type {number}
  282. */
  283. /**
  284. * If a number is given, it defines the pixel length. If a percentage string is
  285. * given, like for example `'50%'`, the setting defines a length relative to a
  286. * base size, for example the size of a container.
  287. *
  288. * @typedef {number|string} Highcharts.RelativeSize
  289. */
  290. /**
  291. * Proceed function to call original (wrapped) function.
  292. *
  293. * @callback Highcharts.WrapProceedFunction
  294. *
  295. * @param {*} [arg1]
  296. * Optional argument. Without any arguments defaults to first argument of
  297. * the wrapping function.
  298. *
  299. * @param {*} [arg2]
  300. * Optional argument. Without any arguments defaults to second argument
  301. * of the wrapping function.
  302. *
  303. * @param {*} [arg3]
  304. * Optional argument. Without any arguments defaults to third argument of
  305. * the wrapping function.
  306. *
  307. * @return {*}
  308. * Return value of the original function.
  309. */
  310. /**
  311. * The Highcharts object is the placeholder for all other members, and various
  312. * utility functions. The most important member of the namespace would be the
  313. * chart constructor.
  314. *
  315. * @example
  316. * var chart = Highcharts.chart('container', { ... });
  317. *
  318. * @namespace Highcharts
  319. */
  320. H.timers = [];
  321. var charts = H.charts, doc = H.doc, win = H.win;
  322. /**
  323. * Provide error messages for debugging, with links to online explanation. This
  324. * function can be overridden to provide custom error handling.
  325. *
  326. * @sample highcharts/chart/highcharts-error/
  327. * Custom error handler
  328. *
  329. * @function Highcharts.error
  330. *
  331. * @param {number|string} code
  332. * The error code. See
  333. * [errors.xml](https://github.com/highcharts/highcharts/blob/master/errors/errors.xml)
  334. * for available codes. If it is a string, the error message is printed
  335. * directly in the console.
  336. *
  337. * @param {boolean} [stop=false]
  338. * Whether to throw an error or just log a warning in the console.
  339. *
  340. * @param {Highcharts.Chart} [chart]
  341. * Reference to the chart that causes the error. Used in 'debugger'
  342. * module to display errors directly on the chart.
  343. * Important note: This argument is undefined for errors that lack
  344. * access to the Chart instance.
  345. *
  346. * @param {Highcharts.Dictionary<string>} [params]
  347. * Additional parameters for the generated message.
  348. *
  349. * @return {void}
  350. */
  351. H.error = function (code, stop, chart, params) {
  352. var isCode = isNumber(code), message = isCode ?
  353. "Highcharts error #" + code + ": www.highcharts.com/errors/" + code + "/" :
  354. code.toString(), defaultHandler = function () {
  355. if (stop) {
  356. throw new Error(message);
  357. }
  358. // else ...
  359. if (win.console) {
  360. console.log(message); // eslint-disable-line no-console
  361. }
  362. };
  363. if (typeof params !== 'undefined') {
  364. var additionalMessages_1 = '';
  365. if (isCode) {
  366. message += '?';
  367. }
  368. H.objectEach(params, function (value, key) {
  369. additionalMessages_1 += ('\n' + key + ': ' + value);
  370. if (isCode) {
  371. message += encodeURI(key) + '=' + encodeURI(value);
  372. }
  373. });
  374. message += additionalMessages_1;
  375. }
  376. if (chart) {
  377. H.fireEvent(chart, 'displayError', { code: code, message: message, params: params }, defaultHandler);
  378. }
  379. else {
  380. defaultHandler();
  381. }
  382. };
  383. /* eslint-disable no-invalid-this, valid-jsdoc */
  384. /**
  385. * An animator object used internally. One instance applies to one property
  386. * (attribute or style prop) on one element. Animation is always initiated
  387. * through {@link SVGElement#animate}.
  388. *
  389. * @example
  390. * var rect = renderer.rect(0, 0, 10, 10).add();
  391. * rect.animate({ width: 100 });
  392. *
  393. * @private
  394. * @class
  395. * @name Highcharts.Fx
  396. *
  397. * @param {Highcharts.HTMLDOMElement|Highcharts.SVGElement} elem
  398. * The element to animate.
  399. *
  400. * @param {Highcharts.AnimationOptionsObject} options
  401. * Animation options.
  402. *
  403. * @param {string} prop
  404. * The single attribute or CSS property to animate.
  405. */
  406. H.Fx = function (elem, options, prop) {
  407. this.options = options;
  408. this.elem = elem;
  409. this.prop = prop;
  410. /* eslint-enable no-invalid-this, valid-jsdoc */
  411. };
  412. H.Fx.prototype = {
  413. /**
  414. * Set the current step of a path definition on SVGElement.
  415. *
  416. * @function Highcharts.Fx#dSetter
  417. *
  418. * @return {void}
  419. */
  420. dSetter: function () {
  421. var start = this.paths[0], end = this.paths[1], ret = [], now = this.now, i = start.length, startVal;
  422. // Land on the final path without adjustment points appended in the ends
  423. if (now === 1) {
  424. ret = this.toD;
  425. }
  426. else if (i === end.length && now < 1) {
  427. while (i--) {
  428. startVal = parseFloat(start[i]);
  429. ret[i] = (
  430. // A letter instruction like M or L
  431. isNaN(startVal) ||
  432. // Arc boolean flags:
  433. end[i - 4] === 'A' || // large-arc-flag
  434. end[i - 5] === 'A' // sweep-flag
  435. ) ?
  436. end[i] :
  437. (now *
  438. parseFloat('' + (end[i] - startVal)) +
  439. startVal);
  440. }
  441. // If animation is finished or length not matching, land on right value
  442. }
  443. else {
  444. ret = end;
  445. }
  446. this.elem.attr('d', ret, null, true);
  447. },
  448. /**
  449. * Update the element with the current animation step.
  450. *
  451. * @function Highcharts.Fx#update
  452. *
  453. * @return {void}
  454. */
  455. update: function () {
  456. var elem = this.elem, prop = this.prop, // if destroyed, it is null
  457. now = this.now, step = this.options.step;
  458. // Animation setter defined from outside
  459. if (this[prop + 'Setter']) {
  460. this[prop + 'Setter']();
  461. // Other animations on SVGElement
  462. }
  463. else if (elem.attr) {
  464. if (elem.element) {
  465. elem.attr(prop, now, null, true);
  466. }
  467. // HTML styles, raw HTML content like container size
  468. }
  469. else {
  470. elem.style[prop] = now + this.unit;
  471. }
  472. if (step) {
  473. step.call(elem, now, this);
  474. }
  475. },
  476. /**
  477. * Run an animation.
  478. *
  479. * @function Highcharts.Fx#run
  480. *
  481. * @param {number} from
  482. * The current value, value to start from.
  483. *
  484. * @param {number} to
  485. * The end value, value to land on.
  486. *
  487. * @param {string} unit
  488. * The property unit, for example `px`.
  489. *
  490. * @return {void}
  491. */
  492. run: function (from, to, unit) {
  493. var self = this, options = self.options, timer = function (gotoEnd) {
  494. return timer.stopped ? false : self.step(gotoEnd);
  495. }, requestAnimationFrame = win.requestAnimationFrame ||
  496. function (step) {
  497. setTimeout(step, 13);
  498. }, step = function () {
  499. for (var i = 0; i < H.timers.length; i++) {
  500. if (!H.timers[i]()) {
  501. H.timers.splice(i--, 1);
  502. }
  503. }
  504. if (H.timers.length) {
  505. requestAnimationFrame(step);
  506. }
  507. };
  508. if (from === to && !this.elem['forceAnimate:' + this.prop]) {
  509. delete options.curAnim[this.prop];
  510. if (options.complete && Object.keys(options.curAnim).length === 0) {
  511. options.complete.call(this.elem);
  512. }
  513. }
  514. else { // #7166
  515. this.startTime = +new Date();
  516. this.start = from;
  517. this.end = to;
  518. this.unit = unit;
  519. this.now = this.start;
  520. this.pos = 0;
  521. timer.elem = this.elem;
  522. timer.prop = this.prop;
  523. if (timer() && H.timers.push(timer) === 1) {
  524. requestAnimationFrame(step);
  525. }
  526. }
  527. },
  528. /**
  529. * Run a single step in the animation.
  530. *
  531. * @function Highcharts.Fx#step
  532. *
  533. * @param {boolean} [gotoEnd]
  534. * Whether to go to the endpoint of the animation after abort.
  535. *
  536. * @return {boolean}
  537. * Returns `true` if animation continues.
  538. */
  539. step: function (gotoEnd) {
  540. var t = +new Date(), ret, done, options = this.options, elem = this.elem, complete = options.complete, duration = options.duration, curAnim = options.curAnim;
  541. if (elem.attr && !elem.element) { // #2616, element is destroyed
  542. ret = false;
  543. }
  544. else if (gotoEnd || t >= duration + this.startTime) {
  545. this.now = this.end;
  546. this.pos = 1;
  547. this.update();
  548. curAnim[this.prop] = true;
  549. done = true;
  550. objectEach(curAnim, function (val) {
  551. if (val !== true) {
  552. done = false;
  553. }
  554. });
  555. if (done && complete) {
  556. complete.call(elem);
  557. }
  558. ret = false;
  559. }
  560. else {
  561. this.pos = options.easing((t - this.startTime) / duration);
  562. this.now = this.start + ((this.end - this.start) * this.pos);
  563. this.update();
  564. ret = true;
  565. }
  566. return ret;
  567. },
  568. /**
  569. * Prepare start and end values so that the path can be animated one to one.
  570. *
  571. * @function Highcharts.Fx#initPath
  572. *
  573. * @param {Highcharts.SVGElement} elem
  574. * The SVGElement item.
  575. *
  576. * @param {string} fromD
  577. * Starting path definition.
  578. *
  579. * @param {Highcharts.SVGPathArray} toD
  580. * Ending path definition.
  581. *
  582. * @return {Array<Highcharts.SVGPathArray,Highcharts.SVGPathArray>}
  583. * An array containing start and end paths in array form so that
  584. * they can be animated in parallel.
  585. */
  586. initPath: function (elem, fromD, toD) {
  587. fromD = fromD || '';
  588. var shift, startX = elem.startX, endX = elem.endX, bezier = fromD.indexOf('C') > -1, numParams = bezier ? 7 : 3, fullLength, slice, i, start = fromD.split(' '), end = toD.slice(), // copy
  589. isArea = elem.isArea, positionFactor = isArea ? 2 : 1, reverse;
  590. /**
  591. * In splines make moveTo and lineTo points have six parameters like
  592. * bezier curves, to allow animation one-to-one.
  593. * @private
  594. * @param {Highcharts.SVGPathArray} arr - array
  595. * @return {void}
  596. */
  597. function sixify(arr) {
  598. var isOperator, nextIsOperator;
  599. i = arr.length;
  600. while (i--) {
  601. // Fill in dummy coordinates only if the next operator comes
  602. // three places behind (#5788)
  603. isOperator = arr[i] === 'M' || arr[i] === 'L';
  604. nextIsOperator = /[a-zA-Z]/.test(arr[i + 3]);
  605. if (isOperator && nextIsOperator) {
  606. arr.splice(i + 1, 0, arr[i + 1], arr[i + 2], arr[i + 1], arr[i + 2]);
  607. }
  608. }
  609. }
  610. /**
  611. * Insert an array at the given position of another array
  612. * @private
  613. * @param {Array<*>} arr - array
  614. * @param {Array<*>} subArr - array
  615. * @param {number} index - number
  616. * @return {void}
  617. */
  618. function insertSlice(arr, subArr, index) {
  619. [].splice.apply(arr, [index, 0].concat(subArr));
  620. }
  621. /**
  622. * If shifting points, prepend a dummy point to the end path.
  623. * @private
  624. * @param {Highcharts.SVGPathArray} arr - array
  625. * @param {Highcharts.SVGPathArray} other - array
  626. * @return {void}
  627. */
  628. function prepend(arr, other) {
  629. while (arr.length < fullLength) {
  630. // Move to, line to or curve to?
  631. arr[0] = other[fullLength - arr.length];
  632. // Prepend a copy of the first point
  633. insertSlice(arr, arr.slice(0, numParams), 0);
  634. // For areas, the bottom path goes back again to the left, so we
  635. // need to append a copy of the last point.
  636. if (isArea) {
  637. insertSlice(arr, arr.slice(arr.length - numParams), arr.length);
  638. i--;
  639. }
  640. }
  641. arr[0] = 'M';
  642. }
  643. /**
  644. * Copy and append last point until the length matches the end length.
  645. * @private
  646. * @param {Highcharts.SVGPathArray} arr - array
  647. * @param {Highcharts.SVGPathArray} other - array
  648. * @return {void}
  649. */
  650. function append(arr, other) {
  651. var i = (fullLength - arr.length) / numParams;
  652. while (i > 0 && i--) {
  653. // Pull out the slice that is going to be appended or inserted.
  654. // In a line graph, the positionFactor is 1, and the last point
  655. // is sliced out. In an area graph, the positionFactor is 2,
  656. // causing the middle two points to be sliced out, since an area
  657. // path starts at left, follows the upper path then turns and
  658. // follows the bottom back.
  659. slice = arr.slice().splice((arr.length / positionFactor) - numParams, numParams * positionFactor);
  660. // Move to, line to or curve to?
  661. slice[0] = other[fullLength - numParams - (i * numParams)];
  662. // Disable first control point
  663. if (bezier) {
  664. slice[numParams - 6] = slice[numParams - 2];
  665. slice[numParams - 5] = slice[numParams - 1];
  666. }
  667. // Now insert the slice, either in the middle (for areas) or at
  668. // the end (for lines)
  669. insertSlice(arr, slice, arr.length / positionFactor);
  670. if (isArea) {
  671. i--;
  672. }
  673. }
  674. }
  675. if (bezier) {
  676. sixify(start);
  677. sixify(end);
  678. }
  679. // For sideways animation, find out how much we need to shift to get the
  680. // start path Xs to match the end path Xs.
  681. if (startX && endX) {
  682. for (i = 0; i < startX.length; i++) {
  683. // Moving left, new points coming in on right
  684. if (startX[i] === endX[0]) {
  685. shift = i;
  686. break;
  687. // Moving right
  688. }
  689. else if (startX[0] ===
  690. endX[endX.length - startX.length + i]) {
  691. shift = i;
  692. reverse = true;
  693. break;
  694. // Fixed from the right side, "scaling" left
  695. }
  696. else if (startX[startX.length - 1] ===
  697. endX[endX.length - startX.length + i]) {
  698. shift = startX.length - i;
  699. break;
  700. }
  701. }
  702. if (typeof shift === 'undefined') {
  703. start = [];
  704. }
  705. }
  706. if (start.length && isNumber(shift)) {
  707. // The common target length for the start and end array, where both
  708. // arrays are padded in opposite ends
  709. fullLength = (end.length + shift * positionFactor * numParams);
  710. if (!reverse) {
  711. prepend(end, start);
  712. append(start, end);
  713. }
  714. else {
  715. prepend(start, end);
  716. append(end, start);
  717. }
  718. }
  719. return [start, end];
  720. },
  721. /**
  722. * Handle animation of the color attributes directly.
  723. *
  724. * @function Highcharts.Fx#fillSetter
  725. *
  726. * @return {void}
  727. */
  728. fillSetter: function () {
  729. H.Fx.prototype.strokeSetter.apply(this, arguments);
  730. },
  731. /**
  732. * Handle animation of the color attributes directly.
  733. *
  734. * @function Highcharts.Fx#strokeSetter
  735. *
  736. * @return {void}
  737. */
  738. strokeSetter: function () {
  739. this.elem.attr(this.prop, H.color(this.start).tweenTo(H.color(this.end), this.pos), null, true);
  740. }
  741. }; // End of Fx prototype
  742. /* eslint-disable valid-jsdoc */
  743. /**
  744. * Utility function to deep merge two or more objects and return a third object.
  745. * If the first argument is true, the contents of the second object is copied
  746. * into the first object. The merge function can also be used with a single
  747. * object argument to create a deep copy of an object.
  748. *
  749. * @function Highcharts.merge<T>
  750. *
  751. * @param {boolean} extend
  752. * Whether to extend the left-side object (a) or return a whole new
  753. * object.
  754. *
  755. * @param {T|undefined} a
  756. * The first object to extend. When only this is given, the function
  757. * returns a deep copy.
  758. *
  759. * @param {...Array<object|undefined>} [n]
  760. * An object to merge into the previous one.
  761. *
  762. * @return {T}
  763. * The merged object. If the first argument is true, the return is the
  764. * same as the second argument.
  765. */ /**
  766. * Utility function to deep merge two or more objects and return a third object.
  767. * The merge function can also be used with a single object argument to create a
  768. * deep copy of an object.
  769. *
  770. * @function Highcharts.merge<T>
  771. *
  772. * @param {T|undefined} a
  773. * The first object to extend. When only this is given, the function
  774. * returns a deep copy.
  775. *
  776. * @param {...Array<object|undefined>} [n]
  777. * An object to merge into the previous one.
  778. *
  779. * @return {T}
  780. * The merged object. If the first argument is true, the return is the
  781. * same as the second argument.
  782. */
  783. H.merge = function () {
  784. /* eslint-enable valid-jsdoc */
  785. var i, args = arguments, len, ret = {}, doCopy = function (copy, original) {
  786. // An object is replacing a primitive
  787. if (typeof copy !== 'object') {
  788. copy = {};
  789. }
  790. objectEach(original, function (value, key) {
  791. // Copy the contents of objects, but not arrays or DOM nodes
  792. if (isObject(value, true) &&
  793. !isClass(value) &&
  794. !isDOMElement(value)) {
  795. copy[key] = doCopy(copy[key] || {}, value);
  796. // Primitives and arrays are copied over directly
  797. }
  798. else {
  799. copy[key] = original[key];
  800. }
  801. });
  802. return copy;
  803. };
  804. // If first argument is true, copy into the existing object. Used in
  805. // setOptions.
  806. if (args[0] === true) {
  807. ret = args[1];
  808. args = Array.prototype.slice.call(args, 2);
  809. }
  810. // For each argument, extend the return
  811. len = args.length;
  812. for (i = 0; i < len; i++) {
  813. ret = doCopy(ret, args[i]);
  814. }
  815. return ret;
  816. };
  817. /**
  818. * Constrain a value to within a lower and upper threshold.
  819. *
  820. * @private
  821. * @param {number} value The initial value
  822. * @param {number} min The lower threshold
  823. * @param {number} max The upper threshold
  824. * @return {number} Returns a number value within min and max.
  825. */
  826. function clamp(value, min, max) {
  827. return value > min ? value < max ? value : max : min;
  828. }
  829. /**
  830. * Shortcut for parseInt
  831. *
  832. * @private
  833. * @function Highcharts.pInt
  834. *
  835. * @param {*} s
  836. * any
  837. *
  838. * @param {number} [mag]
  839. * Magnitude
  840. *
  841. * @return {number}
  842. * number
  843. */
  844. function pInt(s, mag) {
  845. return parseInt(s, mag || 10);
  846. }
  847. /**
  848. * Utility function to check for string type.
  849. *
  850. * @function Highcharts.isString
  851. *
  852. * @param {*} s
  853. * The item to check.
  854. *
  855. * @return {boolean}
  856. * True if the argument is a string.
  857. */
  858. function isString(s) {
  859. return typeof s === 'string';
  860. }
  861. /**
  862. * Utility function to check if an item is an array.
  863. *
  864. * @function Highcharts.isArray
  865. *
  866. * @param {*} obj
  867. * The item to check.
  868. *
  869. * @return {boolean}
  870. * True if the argument is an array.
  871. */
  872. function isArray(obj) {
  873. var str = Object.prototype.toString.call(obj);
  874. return str === '[object Array]' || str === '[object Array Iterator]';
  875. }
  876. /**
  877. * Utility function to check if an item is of type object.
  878. *
  879. * @function Highcharts.isObject
  880. *
  881. * @param {*} obj
  882. * The item to check.
  883. *
  884. * @param {boolean} [strict=false]
  885. * Also checks that the object is not an array.
  886. *
  887. * @return {boolean}
  888. * True if the argument is an object.
  889. */
  890. function isObject(obj, strict) {
  891. return (!!obj &&
  892. typeof obj === 'object' &&
  893. (!strict || !isArray(obj))); // eslint-disable-line @typescript-eslint/no-explicit-any
  894. }
  895. /**
  896. * Utility function to check if an Object is a HTML Element.
  897. *
  898. * @function Highcharts.isDOMElement
  899. *
  900. * @param {*} obj
  901. * The item to check.
  902. *
  903. * @return {boolean}
  904. * True if the argument is a HTML Element.
  905. */
  906. function isDOMElement(obj) {
  907. return isObject(obj) && typeof obj.nodeType === 'number';
  908. }
  909. /**
  910. * Utility function to check if an Object is a class.
  911. *
  912. * @function Highcharts.isClass
  913. *
  914. * @param {object|undefined} obj
  915. * The item to check.
  916. *
  917. * @return {boolean}
  918. * True if the argument is a class.
  919. */
  920. function isClass(obj) {
  921. var c = obj && obj.constructor;
  922. return !!(isObject(obj, true) &&
  923. !isDOMElement(obj) &&
  924. (c && c.name && c.name !== 'Object'));
  925. }
  926. /**
  927. * Utility function to check if an item is a number and it is finite (not NaN,
  928. * Infinity or -Infinity).
  929. *
  930. * @function Highcharts.isNumber
  931. *
  932. * @param {*} n
  933. * The item to check.
  934. *
  935. * @return {boolean}
  936. * True if the item is a finite number
  937. */
  938. function isNumber(n) {
  939. return typeof n === 'number' && !isNaN(n) && n < Infinity && n > -Infinity;
  940. }
  941. /**
  942. * Remove the last occurence of an item from an array.
  943. *
  944. * @function Highcharts.erase
  945. *
  946. * @param {Array<*>} arr
  947. * The array.
  948. *
  949. * @param {*} item
  950. * The item to remove.
  951. *
  952. * @return {void}
  953. */
  954. function erase(arr, item) {
  955. var i = arr.length;
  956. while (i--) {
  957. if (arr[i] === item) {
  958. arr.splice(i, 1);
  959. break;
  960. }
  961. }
  962. }
  963. /**
  964. * Check if an object is null or undefined.
  965. *
  966. * @function Highcharts.defined
  967. *
  968. * @param {*} obj
  969. * The object to check.
  970. *
  971. * @return {boolean}
  972. * False if the object is null or undefined, otherwise true.
  973. */
  974. function defined(obj) {
  975. return typeof obj !== 'undefined' && obj !== null;
  976. }
  977. /**
  978. * Set or get an attribute or an object of attributes. To use as a setter, pass
  979. * a key and a value, or let the second argument be a collection of keys and
  980. * values. To use as a getter, pass only a string as the second argument.
  981. *
  982. * @function Highcharts.attr
  983. *
  984. * @param {Highcharts.HTMLDOMElement|Highcharts.SVGDOMElement} elem
  985. * The DOM element to receive the attribute(s).
  986. *
  987. * @param {string|Highcharts.HTMLAttributes|Highcharts.SVGAttributes} [prop]
  988. * The property or an object of key-value pairs.
  989. *
  990. * @param {number|string} [value]
  991. * The value if a single property is set.
  992. *
  993. * @return {string|null|undefined}
  994. * When used as a getter, return the value.
  995. */
  996. function attr(elem, prop, value) {
  997. var ret;
  998. // if the prop is a string
  999. if (isString(prop)) {
  1000. // set the value
  1001. if (defined(value)) {
  1002. elem.setAttribute(prop, value);
  1003. // get the value
  1004. }
  1005. else if (elem && elem.getAttribute) {
  1006. ret = elem.getAttribute(prop);
  1007. // IE7 and below cannot get class through getAttribute (#7850)
  1008. if (!ret && prop === 'class') {
  1009. ret = elem.getAttribute(prop + 'Name');
  1010. }
  1011. }
  1012. // else if prop is defined, it is a hash of key/value pairs
  1013. }
  1014. else {
  1015. objectEach(prop, function (val, key) {
  1016. elem.setAttribute(key, val);
  1017. });
  1018. }
  1019. return ret;
  1020. }
  1021. /**
  1022. * Check if an element is an array, and if not, make it into an array.
  1023. *
  1024. * @function Highcharts.splat
  1025. *
  1026. * @param {*} obj
  1027. * The object to splat.
  1028. *
  1029. * @return {Array}
  1030. * The produced or original array.
  1031. */
  1032. function splat(obj) {
  1033. return isArray(obj) ? obj : [obj];
  1034. }
  1035. /**
  1036. * Set a timeout if the delay is given, otherwise perform the function
  1037. * synchronously.
  1038. *
  1039. * @function Highcharts.syncTimeout
  1040. *
  1041. * @param {Function} fn
  1042. * The function callback.
  1043. *
  1044. * @param {number} delay
  1045. * Delay in milliseconds.
  1046. *
  1047. * @param {*} [context]
  1048. * An optional context to send to the function callback.
  1049. *
  1050. * @return {number}
  1051. * An identifier for the timeout that can later be cleared with
  1052. * Highcharts.clearTimeout. Returns -1 if there is no timeout.
  1053. */
  1054. function syncTimeout(fn, delay, context) {
  1055. if (delay > 0) {
  1056. return setTimeout(fn, delay, context);
  1057. }
  1058. fn.call(0, context);
  1059. return -1;
  1060. }
  1061. /**
  1062. * Internal clear timeout. The function checks that the `id` was not removed
  1063. * (e.g. by `chart.destroy()`). For the details see
  1064. * [issue #7901](https://github.com/highcharts/highcharts/issues/7901).
  1065. *
  1066. * @function Highcharts.clearTimeout
  1067. *
  1068. * @param {number} id
  1069. * Id of a timeout.
  1070. *
  1071. * @return {void}
  1072. */
  1073. H.clearTimeout = function (id) {
  1074. if (defined(id)) {
  1075. clearTimeout(id);
  1076. }
  1077. };
  1078. /* eslint-disable valid-jsdoc */
  1079. /**
  1080. * Utility function to extend an object with the members of another.
  1081. *
  1082. * @function Highcharts.extend<T>
  1083. *
  1084. * @param {T|undefined} a
  1085. * The object to be extended.
  1086. *
  1087. * @param {object} b
  1088. * The object to add to the first one.
  1089. *
  1090. * @return {T}
  1091. * Object a, the original object.
  1092. */
  1093. function extend(a, b) {
  1094. /* eslint-enable valid-jsdoc */
  1095. var n;
  1096. if (!a) {
  1097. a = {};
  1098. }
  1099. for (n in b) { // eslint-disable-line guard-for-in
  1100. a[n] = b[n];
  1101. }
  1102. return a;
  1103. }
  1104. /* eslint-disable valid-jsdoc */
  1105. /**
  1106. * Return the first value that is not null or undefined.
  1107. *
  1108. * @function Highcharts.pick<T>
  1109. *
  1110. * @param {...Array<T|null|undefined>} items
  1111. * Variable number of arguments to inspect.
  1112. *
  1113. * @return {T}
  1114. * The value of the first argument that is not null or undefined.
  1115. */
  1116. function pick() {
  1117. var args = arguments;
  1118. var length = args.length;
  1119. for (var i = 0; i < length; i++) {
  1120. var arg = args[i];
  1121. if (typeof arg !== 'undefined' && arg !== null) {
  1122. return arg;
  1123. }
  1124. }
  1125. }
  1126. /**
  1127. * Set CSS on a given element.
  1128. *
  1129. * @function Highcharts.css
  1130. *
  1131. * @param {Highcharts.HTMLDOMElement|Highcharts.SVGDOMElement} el
  1132. * An HTML DOM element.
  1133. *
  1134. * @param {Highcharts.CSSObject} styles
  1135. * Style object with camel case property names.
  1136. *
  1137. * @return {void}
  1138. */
  1139. H.css = function (el, styles) {
  1140. if (H.isMS && !H.svg) { // #2686
  1141. if (styles && typeof styles.opacity !== 'undefined') {
  1142. styles.filter =
  1143. 'alpha(opacity=' + (styles.opacity * 100) + ')';
  1144. }
  1145. }
  1146. extend(el.style, styles);
  1147. };
  1148. /**
  1149. * Utility function to create an HTML element with attributes and styles.
  1150. *
  1151. * @function Highcharts.createElement
  1152. *
  1153. * @param {string} tag
  1154. * The HTML tag.
  1155. *
  1156. * @param {Highcharts.HTMLAttributes} [attribs]
  1157. * Attributes as an object of key-value pairs.
  1158. *
  1159. * @param {Highcharts.CSSObject} [styles]
  1160. * Styles as an object of key-value pairs.
  1161. *
  1162. * @param {Highcharts.HTMLDOMElement} [parent]
  1163. * The parent HTML object.
  1164. *
  1165. * @param {boolean} [nopad=false]
  1166. * If true, remove all padding, border and margin.
  1167. *
  1168. * @return {Highcharts.HTMLDOMElement}
  1169. * The created DOM element.
  1170. */
  1171. H.createElement = function (tag, attribs, styles, parent, nopad) {
  1172. var el = doc.createElement(tag), css = H.css;
  1173. if (attribs) {
  1174. extend(el, attribs);
  1175. }
  1176. if (nopad) {
  1177. css(el, { padding: '0', border: 'none', margin: '0' });
  1178. }
  1179. if (styles) {
  1180. css(el, styles);
  1181. }
  1182. if (parent) {
  1183. parent.appendChild(el);
  1184. }
  1185. return el;
  1186. };
  1187. // eslint-disable-next-line valid-jsdoc
  1188. /**
  1189. * Extend a prototyped class by new members.
  1190. *
  1191. * @function Highcharts.extendClass<T>
  1192. *
  1193. * @param {Highcharts.Class<T>} parent
  1194. * The parent prototype to inherit.
  1195. *
  1196. * @param {Highcharts.Dictionary<*>} members
  1197. * A collection of prototype members to add or override compared to the
  1198. * parent prototype.
  1199. *
  1200. * @return {Highcharts.Class<T>}
  1201. * A new prototype.
  1202. */
  1203. function extendClass(parent, members) {
  1204. var obj = (function () { });
  1205. obj.prototype = new parent(); // eslint-disable-line new-cap
  1206. extend(obj.prototype, members);
  1207. return obj;
  1208. }
  1209. /**
  1210. * Left-pad a string to a given length by adding a character repetetively.
  1211. *
  1212. * @function Highcharts.pad
  1213. *
  1214. * @param {number} number
  1215. * The input string or number.
  1216. *
  1217. * @param {number} [length]
  1218. * The desired string length.
  1219. *
  1220. * @param {string} [padder=0]
  1221. * The character to pad with.
  1222. *
  1223. * @return {string}
  1224. * The padded string.
  1225. */
  1226. function pad(number, length, padder) {
  1227. return new Array((length || 2) +
  1228. 1 -
  1229. String(number)
  1230. .replace('-', '')
  1231. .length).join(padder || '0') + number;
  1232. }
  1233. /**
  1234. * Return a length based on either the integer value, or a percentage of a base.
  1235. *
  1236. * @function Highcharts.relativeLength
  1237. *
  1238. * @param {Highcharts.RelativeSize} value
  1239. * A percentage string or a number.
  1240. *
  1241. * @param {number} base
  1242. * The full length that represents 100%.
  1243. *
  1244. * @param {number} [offset=0]
  1245. * A pixel offset to apply for percentage values. Used internally in
  1246. * axis positioning.
  1247. *
  1248. * @return {number}
  1249. * The computed length.
  1250. */
  1251. function relativeLength(value, base, offset) {
  1252. return (/%$/).test(value) ?
  1253. (base * parseFloat(value) / 100) + (offset || 0) :
  1254. parseFloat(value);
  1255. }
  1256. /**
  1257. * Wrap a method with extended functionality, preserving the original function.
  1258. *
  1259. * @function Highcharts.wrap
  1260. *
  1261. * @param {*} obj
  1262. * The context object that the method belongs to. In real cases, this is
  1263. * often a prototype.
  1264. *
  1265. * @param {string} method
  1266. * The name of the method to extend.
  1267. *
  1268. * @param {Highcharts.WrapProceedFunction} func
  1269. * A wrapper function callback. This function is called with the same
  1270. * arguments as the original function, except that the original function
  1271. * is unshifted and passed as the first argument.
  1272. *
  1273. * @return {void}
  1274. */
  1275. function wrap(obj, method, func) {
  1276. var proceed = obj[method];
  1277. obj[method] = function () {
  1278. var args = Array.prototype.slice.call(arguments), outerArgs = arguments, ctx = this, ret;
  1279. ctx.proceed = function () {
  1280. proceed.apply(ctx, arguments.length ? arguments : outerArgs);
  1281. };
  1282. args.unshift(proceed);
  1283. ret = func.apply(this, args);
  1284. ctx.proceed = null;
  1285. return ret;
  1286. };
  1287. }
  1288. /**
  1289. * Recursively converts all Date properties to timestamps.
  1290. *
  1291. * @private
  1292. * @function Highcharts.datePropsToTimestamps
  1293. *
  1294. * @param {*} obj - any object to convert properties of
  1295. *
  1296. * @return {void}
  1297. */
  1298. H.datePropsToTimestamps = function (obj) {
  1299. objectEach(obj, function (val, key) {
  1300. if (isObject(val) && typeof val.getTime === 'function') {
  1301. obj[key] = val.getTime();
  1302. }
  1303. else if (isObject(val) || isArray(val)) {
  1304. H.datePropsToTimestamps(val);
  1305. }
  1306. });
  1307. };
  1308. /**
  1309. * Format a single variable. Similar to sprintf, without the % prefix.
  1310. *
  1311. * @example
  1312. * formatSingle('.2f', 5); // => '5.00'.
  1313. *
  1314. * @function Highcharts.formatSingle
  1315. *
  1316. * @param {string} format
  1317. * The format string.
  1318. *
  1319. * @param {*} val
  1320. * The value.
  1321. *
  1322. * @param {Highcharts.Chart} [chart]
  1323. * A `Chart` instance used to get numberFormatter and time.
  1324. *
  1325. * @return {string}
  1326. * The formatted representation of the value.
  1327. */
  1328. H.formatSingle = function (format, val, chart) {
  1329. var floatRegex = /f$/, decRegex = /\.([0-9])/, lang = H.defaultOptions.lang, decimals;
  1330. var time = chart && chart.time || H.time;
  1331. var numberFormatter = chart && chart.numberFormatter || numberFormat;
  1332. if (floatRegex.test(format)) { // float
  1333. decimals = format.match(decRegex);
  1334. decimals = decimals ? decimals[1] : -1;
  1335. if (val !== null) {
  1336. val = numberFormatter(val, decimals, lang.decimalPoint, format.indexOf(',') > -1 ? lang.thousandsSep : '');
  1337. }
  1338. }
  1339. else {
  1340. val = time.dateFormat(format, val);
  1341. }
  1342. return val;
  1343. };
  1344. /**
  1345. * Format a string according to a subset of the rules of Python's String.format
  1346. * method.
  1347. *
  1348. * @example
  1349. * var s = Highcharts.format(
  1350. * 'The {color} fox was {len:.2f} feet long',
  1351. * { color: 'red', len: Math.PI }
  1352. * );
  1353. * // => The red fox was 3.14 feet long
  1354. *
  1355. * @function Highcharts.format
  1356. *
  1357. * @param {string} str
  1358. * The string to format.
  1359. *
  1360. * @param {*} ctx
  1361. * The context, a collection of key-value pairs where each key is
  1362. * replaced by its value.
  1363. *
  1364. * @param {Highcharts.Chart} [chart]
  1365. * A `Chart` instance used to get numberFormatter and time.
  1366. *
  1367. * @return {string}
  1368. * The formatted string.
  1369. */
  1370. H.format = function (str, ctx, chart) {
  1371. var splitter = '{', isInside = false, segment, valueAndFormat, path, i, len, ret = [], val, index;
  1372. while (str) {
  1373. index = str.indexOf(splitter);
  1374. if (index === -1) {
  1375. break;
  1376. }
  1377. segment = str.slice(0, index);
  1378. if (isInside) { // we're on the closing bracket looking back
  1379. valueAndFormat = segment.split(':');
  1380. // get first and leave
  1381. path = valueAndFormat.shift().split('.');
  1382. len = path.length;
  1383. val = ctx;
  1384. // Assign deeper paths
  1385. for (i = 0; i < len; i++) {
  1386. if (val) {
  1387. val = val[path[i]];
  1388. }
  1389. }
  1390. // Format the replacement
  1391. if (valueAndFormat.length) {
  1392. val = H.formatSingle(valueAndFormat.join(':'), val, chart);
  1393. }
  1394. // Push the result and advance the cursor
  1395. ret.push(val);
  1396. }
  1397. else {
  1398. ret.push(segment);
  1399. }
  1400. str = str.slice(index + 1); // the rest
  1401. isInside = !isInside; // toggle
  1402. splitter = isInside ? '}' : '{'; // now look for next matching bracket
  1403. }
  1404. ret.push(str);
  1405. return ret.join('');
  1406. };
  1407. /**
  1408. * Get the magnitude of a number.
  1409. *
  1410. * @function Highcharts.getMagnitude
  1411. *
  1412. * @param {number} num
  1413. * The number.
  1414. *
  1415. * @return {number}
  1416. * The magnitude, where 1-9 are magnitude 1, 10-99 magnitude 2 etc.
  1417. */
  1418. H.getMagnitude = function (num) {
  1419. return Math.pow(10, Math.floor(Math.log(num) / Math.LN10));
  1420. };
  1421. /**
  1422. * Take an interval and normalize it to multiples of round numbers.
  1423. *
  1424. * @deprecated
  1425. * @function Highcharts.normalizeTickInterval
  1426. *
  1427. * @param {number} interval
  1428. * The raw, un-rounded interval.
  1429. *
  1430. * @param {Array<*>} [multiples]
  1431. * Allowed multiples.
  1432. *
  1433. * @param {number} [magnitude]
  1434. * The magnitude of the number.
  1435. *
  1436. * @param {boolean} [allowDecimals]
  1437. * Whether to allow decimals.
  1438. *
  1439. * @param {boolean} [hasTickAmount]
  1440. * If it has tickAmount, avoid landing on tick intervals lower than
  1441. * original.
  1442. *
  1443. * @return {number}
  1444. * The normalized interval.
  1445. *
  1446. * @todo
  1447. * Move this function to the Axis prototype. It is here only for historical
  1448. * reasons.
  1449. */
  1450. H.normalizeTickInterval = function (interval, multiples, magnitude, allowDecimals, hasTickAmount) {
  1451. var normalized, i, retInterval = interval;
  1452. // round to a tenfold of 1, 2, 2.5 or 5
  1453. magnitude = pick(magnitude, 1);
  1454. normalized = interval / magnitude;
  1455. // multiples for a linear scale
  1456. if (!multiples) {
  1457. multiples = hasTickAmount ?
  1458. // Finer grained ticks when the tick amount is hard set, including
  1459. // when alignTicks is true on multiple axes (#4580).
  1460. [1, 1.2, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10] :
  1461. // Else, let ticks fall on rounder numbers
  1462. [1, 2, 2.5, 5, 10];
  1463. // the allowDecimals option
  1464. if (allowDecimals === false) {
  1465. if (magnitude === 1) {
  1466. multiples = multiples.filter(function (num) {
  1467. return num % 1 === 0;
  1468. });
  1469. }
  1470. else if (magnitude <= 0.1) {
  1471. multiples = [1 / magnitude];
  1472. }
  1473. }
  1474. }
  1475. // normalize the interval to the nearest multiple
  1476. for (i = 0; i < multiples.length; i++) {
  1477. retInterval = multiples[i];
  1478. // only allow tick amounts smaller than natural
  1479. if ((hasTickAmount &&
  1480. retInterval * magnitude >= interval) ||
  1481. (!hasTickAmount &&
  1482. (normalized <=
  1483. (multiples[i] +
  1484. (multiples[i + 1] || multiples[i])) / 2))) {
  1485. break;
  1486. }
  1487. }
  1488. // Multiply back to the correct magnitude. Correct floats to appropriate
  1489. // precision (#6085).
  1490. retInterval = correctFloat(retInterval * magnitude, -Math.round(Math.log(0.001) / Math.LN10));
  1491. return retInterval;
  1492. };
  1493. /**
  1494. * Sort an object array and keep the order of equal items. The ECMAScript
  1495. * standard does not specify the behaviour when items are equal.
  1496. *
  1497. * @function Highcharts.stableSort
  1498. *
  1499. * @param {Array<*>} arr
  1500. * The array to sort.
  1501. *
  1502. * @param {Function} sortFunction
  1503. * The function to sort it with, like with regular Array.prototype.sort.
  1504. *
  1505. * @return {void}
  1506. */
  1507. H.stableSort = function (arr, sortFunction) {
  1508. // @todo It seems like Chrome since v70 sorts in a stable way internally,
  1509. // plus all other browsers do it, so over time we may be able to remove this
  1510. // function
  1511. var length = arr.length, sortValue, i;
  1512. // Add index to each item
  1513. for (i = 0; i < length; i++) {
  1514. arr[i].safeI = i; // stable sort index
  1515. }
  1516. arr.sort(function (a, b) {
  1517. sortValue = sortFunction(a, b);
  1518. return sortValue === 0 ? a.safeI - b.safeI : sortValue;
  1519. });
  1520. // Remove index from items
  1521. for (i = 0; i < length; i++) {
  1522. delete arr[i].safeI; // stable sort index
  1523. }
  1524. };
  1525. /**
  1526. * Non-recursive method to find the lowest member of an array. `Math.min` raises
  1527. * a maximum call stack size exceeded error in Chrome when trying to apply more
  1528. * than 150.000 points. This method is slightly slower, but safe.
  1529. *
  1530. * @function Highcharts.arrayMin
  1531. *
  1532. * @param {Array<*>} data
  1533. * An array of numbers.
  1534. *
  1535. * @return {number}
  1536. * The lowest number.
  1537. */
  1538. function arrayMin(data) {
  1539. var i = data.length, min = data[0];
  1540. while (i--) {
  1541. if (data[i] < min) {
  1542. min = data[i];
  1543. }
  1544. }
  1545. return min;
  1546. }
  1547. /**
  1548. * Non-recursive method to find the lowest member of an array. `Math.max` raises
  1549. * a maximum call stack size exceeded error in Chrome when trying to apply more
  1550. * than 150.000 points. This method is slightly slower, but safe.
  1551. *
  1552. * @function Highcharts.arrayMax
  1553. *
  1554. * @param {Array<*>} data
  1555. * An array of numbers.
  1556. *
  1557. * @return {number}
  1558. * The highest number.
  1559. */
  1560. function arrayMax(data) {
  1561. var i = data.length, max = data[0];
  1562. while (i--) {
  1563. if (data[i] > max) {
  1564. max = data[i];
  1565. }
  1566. }
  1567. return max;
  1568. }
  1569. /**
  1570. * Utility method that destroys any SVGElement instances that are properties on
  1571. * the given object. It loops all properties and invokes destroy if there is a
  1572. * destroy method. The property is then delete.
  1573. *
  1574. * @function Highcharts.destroyObjectProperties
  1575. *
  1576. * @param {*} obj
  1577. * The object to destroy properties on.
  1578. *
  1579. * @param {*} [except]
  1580. * Exception, do not destroy this property, only delete it.
  1581. *
  1582. * @return {void}
  1583. */
  1584. function destroyObjectProperties(obj, except) {
  1585. objectEach(obj, function (val, n) {
  1586. // If the object is non-null and destroy is defined
  1587. if (val && val !== except && val.destroy) {
  1588. // Invoke the destroy
  1589. val.destroy();
  1590. }
  1591. // Delete the property from the object.
  1592. delete obj[n];
  1593. });
  1594. }
  1595. /**
  1596. * Discard a HTML element by moving it to the bin and delete.
  1597. *
  1598. * @function Highcharts.discardElement
  1599. *
  1600. * @param {Highcharts.HTMLDOMElement} element
  1601. * The HTML node to discard.
  1602. *
  1603. * @return {void}
  1604. */
  1605. function discardElement(element) {
  1606. var garbageBin = H.garbageBin;
  1607. // create a garbage bin element, not part of the DOM
  1608. if (!garbageBin) {
  1609. garbageBin = H.createElement('div');
  1610. }
  1611. // move the node and empty bin
  1612. if (element) {
  1613. garbageBin.appendChild(element);
  1614. }
  1615. garbageBin.innerHTML = '';
  1616. }
  1617. /**
  1618. * Fix JS round off float errors.
  1619. *
  1620. * @function Highcharts.correctFloat
  1621. *
  1622. * @param {number} num
  1623. * A float number to fix.
  1624. *
  1625. * @param {number} [prec=14]
  1626. * The precision.
  1627. *
  1628. * @return {number}
  1629. * The corrected float number.
  1630. */
  1631. function correctFloat(num, prec) {
  1632. return parseFloat(num.toPrecision(prec || 14));
  1633. }
  1634. /**
  1635. * Set the global animation to either a given value, or fall back to the given
  1636. * chart's animation option.
  1637. *
  1638. * @function Highcharts.setAnimation
  1639. *
  1640. * @param {boolean|Highcharts.AnimationOptionsObject|undefined} animation
  1641. * The animation object.
  1642. *
  1643. * @param {Highcharts.Chart} chart
  1644. * The chart instance.
  1645. *
  1646. * @return {void}
  1647. *
  1648. * @todo
  1649. * This function always relates to a chart, and sets a property on the renderer,
  1650. * so it should be moved to the SVGRenderer.
  1651. */
  1652. function setAnimation(animation, chart) {
  1653. chart.renderer.globalAnimation = pick(animation, chart.options.chart.animation, true);
  1654. }
  1655. /**
  1656. * Get the animation in object form, where a disabled animation is always
  1657. * returned as `{ duration: 0 }`.
  1658. *
  1659. * @function Highcharts.animObject
  1660. *
  1661. * @param {boolean|Highcharts.AnimationOptionsObject} [animation=0]
  1662. * An animation setting. Can be an object with duration, complete and
  1663. * easing properties, or a boolean to enable or disable.
  1664. *
  1665. * @return {Highcharts.AnimationOptionsObject}
  1666. * An object with at least a duration property.
  1667. */
  1668. function animObject(animation) {
  1669. return isObject(animation) ?
  1670. H.merge(animation) :
  1671. { duration: animation ? 500 : 0 };
  1672. }
  1673. /**
  1674. * The time unit lookup
  1675. *
  1676. * @ignore
  1677. */
  1678. H.timeUnits = {
  1679. millisecond: 1,
  1680. second: 1000,
  1681. minute: 60000,
  1682. hour: 3600000,
  1683. day: 24 * 3600000,
  1684. week: 7 * 24 * 3600000,
  1685. month: 28 * 24 * 3600000,
  1686. year: 364 * 24 * 3600000
  1687. };
  1688. /**
  1689. * Format a number and return a string based on input settings.
  1690. *
  1691. * @sample highcharts/members/highcharts-numberformat/
  1692. * Custom number format
  1693. *
  1694. * @function Highcharts.numberFormat
  1695. *
  1696. * @param {number} number
  1697. * The input number to format.
  1698. *
  1699. * @param {number} decimals
  1700. * The amount of decimals. A value of -1 preserves the amount in the
  1701. * input number.
  1702. *
  1703. * @param {string} [decimalPoint]
  1704. * The decimal point, defaults to the one given in the lang options, or
  1705. * a dot.
  1706. *
  1707. * @param {string} [thousandsSep]
  1708. * The thousands separator, defaults to the one given in the lang
  1709. * options, or a space character.
  1710. *
  1711. * @return {string}
  1712. * The formatted number.
  1713. */
  1714. function numberFormat(number, decimals, decimalPoint, thousandsSep) {
  1715. number = +number || 0;
  1716. decimals = +decimals;
  1717. var lang = H.defaultOptions.lang, origDec = (number.toString().split('.')[1] || '').split('e')[0].length, strinteger, thousands, ret, roundedNumber, exponent = number.toString().split('e'), fractionDigits;
  1718. if (decimals === -1) {
  1719. // Preserve decimals. Not huge numbers (#3793).
  1720. decimals = Math.min(origDec, 20);
  1721. }
  1722. else if (!isNumber(decimals)) {
  1723. decimals = 2;
  1724. }
  1725. else if (decimals && exponent[1] && exponent[1] < 0) {
  1726. // Expose decimals from exponential notation (#7042)
  1727. fractionDigits = decimals + +exponent[1];
  1728. if (fractionDigits >= 0) {
  1729. // remove too small part of the number while keeping the notation
  1730. exponent[0] = (+exponent[0]).toExponential(fractionDigits)
  1731. .split('e')[0];
  1732. decimals = fractionDigits;
  1733. }
  1734. else {
  1735. // fractionDigits < 0
  1736. exponent[0] = exponent[0].split('.')[0] || 0;
  1737. if (decimals < 20) {
  1738. // use number instead of exponential notation (#7405)
  1739. number = (exponent[0] * Math.pow(10, exponent[1]))
  1740. .toFixed(decimals);
  1741. }
  1742. else {
  1743. // or zero
  1744. number = 0;
  1745. }
  1746. exponent[1] = 0;
  1747. }
  1748. }
  1749. // Add another decimal to avoid rounding errors of float numbers. (#4573)
  1750. // Then use toFixed to handle rounding.
  1751. roundedNumber = (Math.abs(exponent[1] ? exponent[0] : number) +
  1752. Math.pow(10, -Math.max(decimals, origDec) - 1)).toFixed(decimals);
  1753. // A string containing the positive integer component of the number
  1754. strinteger = String(pInt(roundedNumber));
  1755. // Leftover after grouping into thousands. Can be 0, 1 or 2.
  1756. thousands = strinteger.length > 3 ? strinteger.length % 3 : 0;
  1757. // Language
  1758. decimalPoint = pick(decimalPoint, lang.decimalPoint);
  1759. thousandsSep = pick(thousandsSep, lang.thousandsSep);
  1760. // Start building the return
  1761. ret = number < 0 ? '-' : '';
  1762. // Add the leftover after grouping into thousands. For example, in the
  1763. // number 42 000 000, this line adds 42.
  1764. ret += thousands ? strinteger.substr(0, thousands) + thousandsSep : '';
  1765. // Add the remaining thousands groups, joined by the thousands separator
  1766. ret += strinteger
  1767. .substr(thousands)
  1768. .replace(/(\d{3})(?=\d)/g, '$1' + thousandsSep);
  1769. // Add the decimal point and the decimal component
  1770. if (decimals) {
  1771. // Get the decimal component
  1772. ret += decimalPoint + roundedNumber.slice(-decimals);
  1773. }
  1774. if (exponent[1] && +ret !== 0) {
  1775. ret += 'e' + exponent[1];
  1776. }
  1777. return ret;
  1778. }
  1779. /**
  1780. * Easing definition
  1781. *
  1782. * @private
  1783. * @function Math.easeInOutSine
  1784. *
  1785. * @param {number} pos
  1786. * Current position, ranging from 0 to 1.
  1787. *
  1788. * @return {number}
  1789. * Ease result
  1790. */
  1791. Math.easeInOutSine = function (pos) {
  1792. return -0.5 * (Math.cos(Math.PI * pos) - 1);
  1793. };
  1794. /**
  1795. * Get the computed CSS value for given element and property, only for numerical
  1796. * properties. For width and height, the dimension of the inner box (excluding
  1797. * padding) is returned. Used for fitting the chart within the container.
  1798. *
  1799. * @function Highcharts.getStyle
  1800. *
  1801. * @param {Highcharts.HTMLDOMElement} el
  1802. * An HTML element.
  1803. *
  1804. * @param {string} prop
  1805. * The property name.
  1806. *
  1807. * @param {boolean} [toInt=true]
  1808. * Parse to integer.
  1809. *
  1810. * @return {number|string}
  1811. * The numeric value.
  1812. */
  1813. H.getStyle = function (el, prop, toInt) {
  1814. var style;
  1815. // For width and height, return the actual inner pixel size (#4913)
  1816. if (prop === 'width') {
  1817. var offsetWidth = Math.min(el.offsetWidth, el.scrollWidth);
  1818. // In flex boxes, we need to use getBoundingClientRect and floor it,
  1819. // because scrollWidth doesn't support subpixel precision (#6427) ...
  1820. var boundingClientRectWidth = el.getBoundingClientRect &&
  1821. el.getBoundingClientRect().width;
  1822. // ...unless if the containing div or its parents are transform-scaled
  1823. // down, in which case the boundingClientRect can't be used as it is
  1824. // also scaled down (#9871, #10498).
  1825. if (boundingClientRectWidth < offsetWidth &&
  1826. boundingClientRectWidth >= offsetWidth - 1) {
  1827. offsetWidth = Math.floor(boundingClientRectWidth);
  1828. }
  1829. return Math.max(0, // #8377
  1830. (offsetWidth -
  1831. H.getStyle(el, 'padding-left') -
  1832. H.getStyle(el, 'padding-right')));
  1833. }
  1834. if (prop === 'height') {
  1835. return Math.max(0, // #8377
  1836. Math.min(el.offsetHeight, el.scrollHeight) -
  1837. H.getStyle(el, 'padding-top') -
  1838. H.getStyle(el, 'padding-bottom'));
  1839. }
  1840. if (!win.getComputedStyle) {
  1841. // SVG not supported, forgot to load oldie.js?
  1842. H.error(27, true);
  1843. }
  1844. // Otherwise, get the computed style
  1845. style = win.getComputedStyle(el, undefined); // eslint-disable-line no-undefined
  1846. if (style) {
  1847. style = style.getPropertyValue(prop);
  1848. if (pick(toInt, prop !== 'opacity')) {
  1849. style = pInt(style);
  1850. }
  1851. }
  1852. return style;
  1853. };
  1854. /**
  1855. * Search for an item in an array.
  1856. *
  1857. * @function Highcharts.inArray
  1858. *
  1859. * @deprecated
  1860. *
  1861. * @param {*} item
  1862. * The item to search for.
  1863. *
  1864. * @param {Array<*>} arr
  1865. * The array or node collection to search in.
  1866. *
  1867. * @param {number} [fromIndex=0]
  1868. * The index to start searching from.
  1869. *
  1870. * @return {number}
  1871. * The index within the array, or -1 if not found.
  1872. */
  1873. H.inArray = function (item, arr, fromIndex) {
  1874. return arr.indexOf(item, fromIndex);
  1875. };
  1876. /* eslint-disable valid-jsdoc */
  1877. /**
  1878. * Return the value of the first element in the array that satisfies the
  1879. * provided testing function.
  1880. *
  1881. * @function Highcharts.find<T>
  1882. *
  1883. * @param {Array<T>} arr
  1884. * The array to test.
  1885. *
  1886. * @param {Function} callback
  1887. * The callback function. The function receives the item as the first
  1888. * argument. Return `true` if this item satisfies the condition.
  1889. *
  1890. * @return {T|undefined}
  1891. * The value of the element.
  1892. */
  1893. H.find = Array.prototype.find ?
  1894. /* eslint-enable valid-jsdoc */
  1895. function (arr, callback) {
  1896. return arr.find(callback);
  1897. } :
  1898. // Legacy implementation. PhantomJS, IE <= 11 etc. #7223.
  1899. function (arr, callback) {
  1900. var i, length = arr.length;
  1901. for (i = 0; i < length; i++) {
  1902. if (callback(arr[i], i)) { // eslint-disable-line callback-return
  1903. return arr[i];
  1904. }
  1905. }
  1906. };
  1907. /**
  1908. * Returns an array of a given object's own properties.
  1909. *
  1910. * @function Highcharts.keys
  1911. * @deprecated
  1912. *
  1913. * @param {*} obj
  1914. * The object of which the properties are to be returned.
  1915. *
  1916. * @return {Array<string>}
  1917. * An array of strings that represents all the properties.
  1918. */
  1919. H.keys = Object.keys;
  1920. /**
  1921. * Get the element's offset position, corrected for `overflow: auto`.
  1922. *
  1923. * @function Highcharts.offset
  1924. *
  1925. * @param {Highcharts.HTMLDOMElement} el
  1926. * The HTML element.
  1927. *
  1928. * @return {Highcharts.OffsetObject}
  1929. * An object containing `left` and `top` properties for the position in
  1930. * the page.
  1931. */
  1932. function offset(el) {
  1933. var docElem = doc.documentElement, box = (el.parentElement || el.parentNode) ?
  1934. el.getBoundingClientRect() :
  1935. { top: 0, left: 0 };
  1936. return {
  1937. top: box.top + (win.pageYOffset || docElem.scrollTop) -
  1938. (docElem.clientTop || 0),
  1939. left: box.left + (win.pageXOffset || docElem.scrollLeft) -
  1940. (docElem.clientLeft || 0)
  1941. };
  1942. }
  1943. /**
  1944. * Stop running animation.
  1945. *
  1946. * @function Highcharts.stop
  1947. *
  1948. * @param {Highcharts.SVGElement} el
  1949. * The SVGElement to stop animation on.
  1950. *
  1951. * @param {string} [prop]
  1952. * The property to stop animating. If given, the stop method will stop a
  1953. * single property from animating, while others continue.
  1954. *
  1955. * @return {void}
  1956. *
  1957. * @todo
  1958. * A possible extension to this would be to stop a single property, when
  1959. * we want to continue animating others. Then assign the prop to the timer
  1960. * in the Fx.run method, and check for the prop here. This would be an
  1961. * improvement in all cases where we stop the animation from .attr. Instead of
  1962. * stopping everything, we can just stop the actual attributes we're setting.
  1963. */
  1964. H.stop = function (el, prop) {
  1965. var i = H.timers.length;
  1966. // Remove timers related to this element (#4519)
  1967. while (i--) {
  1968. if (H.timers[i].elem === el && (!prop || prop === H.timers[i].prop)) {
  1969. H.timers[i].stopped = true; // #4667
  1970. }
  1971. }
  1972. };
  1973. /* eslint-disable valid-jsdoc */
  1974. /**
  1975. * Iterate over object key pairs in an object.
  1976. *
  1977. * @function Highcharts.objectEach<T>
  1978. *
  1979. * @param {*} obj
  1980. * The object to iterate over.
  1981. *
  1982. * @param {Highcharts.ObjectEachCallbackFunction<T>} fn
  1983. * The iterator callback. It passes three arguments:
  1984. * * value - The property value.
  1985. * * key - The property key.
  1986. * * obj - The object that objectEach is being applied to.
  1987. *
  1988. * @param {T} [ctx]
  1989. * The context.
  1990. *
  1991. * @return {void}
  1992. */
  1993. function objectEach(obj, fn, ctx) {
  1994. /* eslint-enable valid-jsdoc */
  1995. for (var key in obj) {
  1996. if (Object.hasOwnProperty.call(obj, key)) {
  1997. fn.call(ctx || obj[key], obj[key], key, obj);
  1998. }
  1999. }
  2000. }
  2001. /**
  2002. * Iterate over an array.
  2003. *
  2004. * @deprecated
  2005. * @function Highcharts.each
  2006. *
  2007. * @param {Array<*>} arr
  2008. * The array to iterate over.
  2009. *
  2010. * @param {Function} fn
  2011. * The iterator callback. It passes three arguments:
  2012. * - `item`: The array item.
  2013. * - `index`: The item's index in the array.
  2014. * - `arr`: The array that each is being applied to.
  2015. *
  2016. * @param {*} [ctx]
  2017. * The context.
  2018. *
  2019. * @return {void}
  2020. */
  2021. /**
  2022. * Filter an array by a callback.
  2023. *
  2024. * @deprecated
  2025. * @function Highcharts.grep
  2026. *
  2027. * @param {Array<*>} arr
  2028. * The array to filter.
  2029. *
  2030. * @param {Function} callback
  2031. * The callback function. The function receives the item as the first
  2032. * argument. Return `true` if the item is to be preserved.
  2033. *
  2034. * @return {Array<*>}
  2035. * A new, filtered array.
  2036. */
  2037. /**
  2038. * Map an array by a callback.
  2039. *
  2040. * @deprecated
  2041. * @function Highcharts.map
  2042. *
  2043. * @param {Array<*>} arr
  2044. * The array to map.
  2045. *
  2046. * @param {Function} fn
  2047. * The callback function. Return the new value for the new array.
  2048. *
  2049. * @return {Array<*>}
  2050. * A new array item with modified items.
  2051. */
  2052. /**
  2053. * Reduce an array to a single value.
  2054. *
  2055. * @deprecated
  2056. * @function Highcharts.reduce
  2057. *
  2058. * @param {Array<*>} arr
  2059. * The array to reduce.
  2060. *
  2061. * @param {Function} fn
  2062. * The callback function. Return the reduced value. Receives 4
  2063. * arguments: Accumulated/reduced value, current value, current array
  2064. * index, and the array.
  2065. *
  2066. * @param {*} initialValue
  2067. * The initial value of the accumulator.
  2068. *
  2069. * @return {*}
  2070. * The reduced value.
  2071. */
  2072. /**
  2073. * Test whether at least one element in the array passes the test implemented by
  2074. * the provided function.
  2075. *
  2076. * @deprecated
  2077. * @function Highcharts.some
  2078. *
  2079. * @param {Array<*>} arr
  2080. * The array to test
  2081. *
  2082. * @param {Function} fn
  2083. * The function to run on each item. Return truty to pass the test.
  2084. * Receives arguments `currentValue`, `index` and `array`.
  2085. *
  2086. * @param {*} ctx
  2087. * The context.
  2088. *
  2089. * @return {boolean}
  2090. */
  2091. objectEach({
  2092. map: 'map',
  2093. each: 'forEach',
  2094. grep: 'filter',
  2095. reduce: 'reduce',
  2096. some: 'some'
  2097. }, function (val, key) {
  2098. H[key] = function (arr) {
  2099. return Array.prototype[val].apply(arr, [].slice.call(arguments, 1));
  2100. };
  2101. });
  2102. /* eslint-disable valid-jsdoc */
  2103. /**
  2104. * Add an event listener.
  2105. *
  2106. * @function Highcharts.addEvent<T>
  2107. *
  2108. * @param {Highcharts.Class<T>|T} el
  2109. * The element or object to add a listener to. It can be a
  2110. * {@link HTMLDOMElement}, an {@link SVGElement} or any other object.
  2111. *
  2112. * @param {string} type
  2113. * The event type.
  2114. *
  2115. * @param {Highcharts.EventCallbackFunction<T>|Function} fn
  2116. * The function callback to execute when the event is fired.
  2117. *
  2118. * @param {Highcharts.EventOptionsObject} [options]
  2119. * Options for adding the event.
  2120. *
  2121. * @return {Function}
  2122. * A callback function to remove the added event.
  2123. */
  2124. H.addEvent = function (el, type, fn, options) {
  2125. if (options === void 0) { options = {}; }
  2126. /* eslint-enable valid-jsdoc */
  2127. var events, addEventListener = (el.addEventListener || H.addEventListenerPolyfill);
  2128. // If we're setting events directly on the constructor, use a separate
  2129. // collection, `protoEvents` to distinguish it from the item events in
  2130. // `hcEvents`.
  2131. if (typeof el === 'function' && el.prototype) {
  2132. events = el.prototype.protoEvents = el.prototype.protoEvents || {};
  2133. }
  2134. else {
  2135. events = el.hcEvents = el.hcEvents || {};
  2136. }
  2137. // Allow click events added to points, otherwise they will be prevented by
  2138. // the TouchPointer.pinch function after a pinch zoom operation (#7091).
  2139. if (H.Point &&
  2140. el instanceof H.Point &&
  2141. el.series &&
  2142. el.series.chart) {
  2143. el.series.chart.runTrackerClick = true;
  2144. }
  2145. // Handle DOM events
  2146. if (addEventListener) {
  2147. addEventListener.call(el, type, fn, false);
  2148. }
  2149. if (!events[type]) {
  2150. events[type] = [];
  2151. }
  2152. var eventObject = {
  2153. fn: fn,
  2154. order: typeof options.order === 'number' ? options.order : Infinity
  2155. };
  2156. events[type].push(eventObject);
  2157. // Order the calls
  2158. events[type].sort(function (a, b) {
  2159. return a.order - b.order;
  2160. });
  2161. // Return a function that can be called to remove this event.
  2162. return function () {
  2163. H.removeEvent(el, type, fn);
  2164. };
  2165. };
  2166. /* eslint-disable valid-jsdoc */
  2167. /**
  2168. * Remove an event that was added with {@link Highcharts#addEvent}.
  2169. *
  2170. * @function Highcharts.removeEvent<T>
  2171. *
  2172. * @param {Highcharts.Class<T>|T} el
  2173. * The element to remove events on.
  2174. *
  2175. * @param {string} [type]
  2176. * The type of events to remove. If undefined, all events are removed
  2177. * from the element.
  2178. *
  2179. * @param {Highcharts.EventCallbackFunction<T>} [fn]
  2180. * The specific callback to remove. If undefined, all events that match
  2181. * the element and optionally the type are removed.
  2182. *
  2183. * @return {void}
  2184. */
  2185. H.removeEvent = function (el, type, fn) {
  2186. /* eslint-enable valid-jsdoc */
  2187. var events;
  2188. /**
  2189. * @private
  2190. * @param {string} type - event type
  2191. * @param {Highcharts.EventCallbackFunction<T>} fn - callback
  2192. * @return {void}
  2193. */
  2194. function removeOneEvent(type, fn) {
  2195. var removeEventListener = (el.removeEventListener || H.removeEventListenerPolyfill);
  2196. if (removeEventListener) {
  2197. removeEventListener.call(el, type, fn, false);
  2198. }
  2199. }
  2200. /**
  2201. * @private
  2202. * @param {any} eventCollection - collection
  2203. * @return {void}
  2204. */
  2205. function removeAllEvents(eventCollection) {
  2206. var types, len;
  2207. if (!el.nodeName) {
  2208. return; // break on non-DOM events
  2209. }
  2210. if (type) {
  2211. types = {};
  2212. types[type] = true;
  2213. }
  2214. else {
  2215. types = eventCollection;
  2216. }
  2217. objectEach(types, function (val, n) {
  2218. if (eventCollection[n]) {
  2219. len = eventCollection[n].length;
  2220. while (len--) {
  2221. removeOneEvent(n, eventCollection[n][len].fn);
  2222. }
  2223. }
  2224. });
  2225. }
  2226. ['protoEvents', 'hcEvents'].forEach(function (coll, i) {
  2227. var eventElem = i ? el : el.prototype;
  2228. var eventCollection = eventElem && eventElem[coll];
  2229. if (eventCollection) {
  2230. if (type) {
  2231. events = (eventCollection[type] || []);
  2232. if (fn) {
  2233. eventCollection[type] = events.filter(function (obj) {
  2234. return fn !== obj.fn;
  2235. });
  2236. removeOneEvent(type, fn);
  2237. }
  2238. else {
  2239. removeAllEvents(eventCollection);
  2240. eventCollection[type] = [];
  2241. }
  2242. }
  2243. else {
  2244. removeAllEvents(eventCollection);
  2245. eventElem[coll] = {};
  2246. }
  2247. }
  2248. });
  2249. };
  2250. /* eslint-disable valid-jsdoc */
  2251. /**
  2252. * Fire an event that was registered with {@link Highcharts#addEvent}.
  2253. *
  2254. * @function Highcharts.fireEvent<T>
  2255. *
  2256. * @param {T} el
  2257. * The object to fire the event on. It can be a {@link HTMLDOMElement},
  2258. * an {@link SVGElement} or any other object.
  2259. *
  2260. * @param {string} type
  2261. * The type of event.
  2262. *
  2263. * @param {Highcharts.Dictionary<*>|Event} [eventArguments]
  2264. * Custom event arguments that are passed on as an argument to the event
  2265. * handler.
  2266. *
  2267. * @param {Highcharts.EventCallbackFunction<T>|Function} [defaultFunction]
  2268. * The default function to execute if the other listeners haven't
  2269. * returned false.
  2270. *
  2271. * @return {void}
  2272. */
  2273. H.fireEvent = function (el, type, eventArguments, defaultFunction) {
  2274. /* eslint-enable valid-jsdoc */
  2275. var e, i;
  2276. eventArguments = eventArguments || {};
  2277. if (doc.createEvent &&
  2278. (el.dispatchEvent || el.fireEvent)) {
  2279. e = doc.createEvent('Events');
  2280. e.initEvent(type, true, true);
  2281. extend(e, eventArguments);
  2282. if (el.dispatchEvent) {
  2283. el.dispatchEvent(e);
  2284. }
  2285. else {
  2286. el.fireEvent(type, e);
  2287. }
  2288. }
  2289. else {
  2290. if (!eventArguments.target) {
  2291. // We're running a custom event
  2292. extend(eventArguments, {
  2293. // Attach a simple preventDefault function to skip
  2294. // default handler if called. The built-in
  2295. // defaultPrevented property is not overwritable (#5112)
  2296. preventDefault: function () {
  2297. eventArguments.defaultPrevented = true;
  2298. },
  2299. // Setting target to native events fails with clicking
  2300. // the zoom-out button in Chrome.
  2301. target: el,
  2302. // If the type is not set, we're running a custom event
  2303. // (#2297). If it is set, we're running a browser event,
  2304. // and setting it will cause en error in IE8 (#2465).
  2305. type: type
  2306. });
  2307. }
  2308. var fireInOrder = function (protoEvents, hcEvents) {
  2309. if (protoEvents === void 0) { protoEvents = []; }
  2310. if (hcEvents === void 0) { hcEvents = []; }
  2311. var iA = 0;
  2312. var iB = 0;
  2313. var length = protoEvents.length + hcEvents.length;
  2314. for (i = 0; i < length; i++) {
  2315. var obj = (!protoEvents[iA] ?
  2316. hcEvents[iB++] :
  2317. !hcEvents[iB] ?
  2318. protoEvents[iA++] :
  2319. protoEvents[iA].order <= hcEvents[iB].order ?
  2320. protoEvents[iA++] :
  2321. hcEvents[iB++]);
  2322. // If the event handler return false, prevent the default
  2323. // handler from executing
  2324. if (obj.fn.call(el, eventArguments) === false) {
  2325. eventArguments.preventDefault();
  2326. }
  2327. }
  2328. };
  2329. fireInOrder(el.protoEvents && el.protoEvents[type], el.hcEvents && el.hcEvents[type]);
  2330. }
  2331. // Run the default if not prevented
  2332. if (defaultFunction && !eventArguments.defaultPrevented) {
  2333. defaultFunction.call(el, eventArguments);
  2334. }
  2335. };
  2336. /**
  2337. * The global animate method, which uses Fx to create individual animators.
  2338. *
  2339. * @function Highcharts.animate
  2340. *
  2341. * @param {Highcharts.HTMLDOMElement|Highcharts.SVGElement} el
  2342. * The element to animate.
  2343. *
  2344. * @param {Highcharts.CSSObject|Highcharts.SVGAttributes} params
  2345. * An object containing key-value pairs of the properties to animate.
  2346. * Supports numeric as pixel-based CSS properties for HTML objects and
  2347. * attributes for SVGElements.
  2348. *
  2349. * @param {Highcharts.AnimationOptionsObject} [opt]
  2350. * Animation options.
  2351. *
  2352. * @return {void}
  2353. */
  2354. H.animate = function (el, params, opt) {
  2355. var start, unit = '', end, fx, args;
  2356. if (!isObject(opt)) { // Number or undefined/null
  2357. args = arguments;
  2358. opt = {
  2359. duration: args[2],
  2360. easing: args[3],
  2361. complete: args[4]
  2362. };
  2363. }
  2364. if (!isNumber(opt.duration)) {
  2365. opt.duration = 400;
  2366. }
  2367. opt.easing = typeof opt.easing === 'function' ?
  2368. opt.easing :
  2369. (Math[opt.easing] || Math.easeInOutSine);
  2370. opt.curAnim = H.merge(params);
  2371. objectEach(params, function (val, prop) {
  2372. // Stop current running animation of this property
  2373. H.stop(el, prop);
  2374. fx = new H.Fx(el, opt, prop);
  2375. end = null;
  2376. if (prop === 'd') {
  2377. fx.paths = fx.initPath(el, el.d, params.d);
  2378. fx.toD = params.d;
  2379. start = 0;
  2380. end = 1;
  2381. }
  2382. else if (el.attr) {
  2383. start = el.attr(prop);
  2384. }
  2385. else {
  2386. start = parseFloat(H.getStyle(el, prop)) || 0;
  2387. if (prop !== 'opacity') {
  2388. unit = 'px';
  2389. }
  2390. }
  2391. if (!end) {
  2392. end = val;
  2393. }
  2394. if (end && end.match && end.match('px')) {
  2395. end = end.replace(/px/g, ''); // #4351
  2396. }
  2397. fx.run(start, end, unit);
  2398. });
  2399. };
  2400. /**
  2401. * Factory to create new series prototypes.
  2402. *
  2403. * @function Highcharts.seriesType
  2404. *
  2405. * @param {string} type
  2406. * The series type name.
  2407. *
  2408. * @param {string} parent
  2409. * The parent series type name. Use `line` to inherit from the basic
  2410. * {@link Series} object.
  2411. *
  2412. * @param {Highcharts.SeriesOptionsType|Highcharts.Dictionary<*>} options
  2413. * The additional default options that are merged with the parent's
  2414. * options.
  2415. *
  2416. * @param {Highcharts.Dictionary<*>} [props]
  2417. * The properties (functions and primitives) to set on the new
  2418. * prototype.
  2419. *
  2420. * @param {Highcharts.Dictionary<*>} [pointProps]
  2421. * Members for a series-specific extension of the {@link Point}
  2422. * prototype if needed.
  2423. *
  2424. * @return {Highcharts.Series}
  2425. * The newly created prototype as extended from {@link Series} or its
  2426. * derivatives.
  2427. */
  2428. // docs: add to API + extending Highcharts
  2429. H.seriesType = function (type, parent, options, props, pointProps) {
  2430. var defaultOptions = H.getOptions(), seriesTypes = H.seriesTypes;
  2431. // Merge the options
  2432. defaultOptions.plotOptions[type] = H.merge(defaultOptions.plotOptions[parent], options);
  2433. // Create the class
  2434. seriesTypes[type] = extendClass(seriesTypes[parent] || function () { }, props);
  2435. seriesTypes[type].prototype.type = type;
  2436. // Create the point class if needed
  2437. if (pointProps) {
  2438. seriesTypes[type].prototype.pointClass =
  2439. extendClass(H.Point, pointProps);
  2440. }
  2441. return seriesTypes[type];
  2442. };
  2443. /**
  2444. * Get a unique key for using in internal element id's and pointers. The key is
  2445. * composed of a random hash specific to this Highcharts instance, and a
  2446. * counter.
  2447. *
  2448. * @example
  2449. * var id = H.uniqueKey(); // => 'highcharts-x45f6hp-0'
  2450. *
  2451. * @function Highcharts.uniqueKey
  2452. *
  2453. * @return {string}
  2454. * A unique key.
  2455. */
  2456. H.uniqueKey = (function () {
  2457. var uniqueKeyHash = Math.random().toString(36).substring(2, 9), idCounter = 0;
  2458. return function () {
  2459. return 'highcharts-' + uniqueKeyHash + '-' + idCounter++;
  2460. };
  2461. }());
  2462. H.isFunction = function (obj) {
  2463. return typeof obj === 'function';
  2464. };
  2465. // Register Highcharts as a plugin in jQuery
  2466. if (win.jQuery) {
  2467. /**
  2468. * Highcharts-extended JQuery.
  2469. *
  2470. * @external JQuery
  2471. */
  2472. /**
  2473. * Helper function to return the chart of the current JQuery selector
  2474. * element.
  2475. *
  2476. * @function external:JQuery#highcharts
  2477. *
  2478. * @return {Highcharts.Chart}
  2479. * The chart that is linked to the JQuery selector element.
  2480. */ /**
  2481. * Factory function to create a chart in the current JQuery selector
  2482. * element.
  2483. *
  2484. * @function external:JQuery#highcharts
  2485. *
  2486. * @param {'Chart'|'Map'|'StockChart'|string} [className]
  2487. * Name of the factory class in the Highcharts namespace.
  2488. *
  2489. * @param {Highcharts.Options} [options]
  2490. * The chart options structure.
  2491. *
  2492. * @param {Highcharts.ChartCallbackFunction} [callback]
  2493. * Function to run when the chart has loaded and and all external
  2494. * images are loaded. Defining a
  2495. * [chart.events.load](https://api.highcharts.com/highcharts/chart.events.load)
  2496. * handler is equivalent.
  2497. *
  2498. * @return {JQuery}
  2499. * The current JQuery selector.
  2500. */
  2501. win.jQuery.fn.highcharts = function () {
  2502. var args = [].slice.call(arguments);
  2503. if (this[0]) { // this[0] is the renderTo div
  2504. // Create the chart
  2505. if (args[0]) {
  2506. new H[ // eslint-disable-line computed-property-spacing, no-new
  2507. // Constructor defaults to Chart
  2508. isString(args[0]) ? args.shift() : 'Chart'](this[0], args[0], args[1]);
  2509. return this;
  2510. }
  2511. // When called without parameters or with the return argument,
  2512. // return an existing chart
  2513. return charts[attr(this[0], 'data-highcharts-chart')];
  2514. }
  2515. };
  2516. }
  2517. // TODO use named exports when supported.
  2518. var utils = {
  2519. animObject: animObject,
  2520. arrayMax: arrayMax,
  2521. arrayMin: arrayMin,
  2522. attr: attr,
  2523. clamp: clamp,
  2524. correctFloat: correctFloat,
  2525. defined: defined,
  2526. destroyObjectProperties: destroyObjectProperties,
  2527. discardElement: discardElement,
  2528. erase: erase,
  2529. extend: extend,
  2530. extendClass: extendClass,
  2531. isArray: isArray,
  2532. isClass: isClass,
  2533. isDOMElement: isDOMElement,
  2534. isNumber: isNumber,
  2535. isObject: isObject,
  2536. isString: isString,
  2537. numberFormat: numberFormat,
  2538. objectEach: objectEach,
  2539. offset: offset,
  2540. pad: pad,
  2541. pick: pick,
  2542. pInt: pInt,
  2543. relativeLength: relativeLength,
  2544. setAnimation: setAnimation,
  2545. splat: splat,
  2546. syncTimeout: syncTimeout,
  2547. wrap: wrap
  2548. };
  2549. export default utils;