pathfinder.src.js 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714
  1. /**
  2. * @license Highcharts Gantt JS v8.2.0 (2020-08-20)
  3. *
  4. * Pathfinder
  5. *
  6. * (c) 2016-2019 Øystein Moseng
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/pathfinder', ['highcharts'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Extensions/ArrowSymbols.js', [_modules['Core/Renderer/SVG/SVGRenderer.js']], function (SVGRenderer) {
  32. /* *
  33. *
  34. * (c) 2017 Highsoft AS
  35. * Authors: Lars A. V. Cabrera
  36. *
  37. * License: www.highcharts.com/license
  38. *
  39. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  40. *
  41. * */
  42. /**
  43. * Creates an arrow symbol. Like a triangle, except not filled.
  44. * ```
  45. * o
  46. * o
  47. * o
  48. * o
  49. * o
  50. * o
  51. * o
  52. * ```
  53. *
  54. * @private
  55. * @function
  56. *
  57. * @param {number} x
  58. * x position of the arrow
  59. *
  60. * @param {number} y
  61. * y position of the arrow
  62. *
  63. * @param {number} w
  64. * width of the arrow
  65. *
  66. * @param {number} h
  67. * height of the arrow
  68. *
  69. * @return {Highcharts.SVGPathArray}
  70. * Path array
  71. */
  72. SVGRenderer.prototype.symbols.arrow = function (x, y, w, h) {
  73. return [
  74. ['M', x, y + h / 2],
  75. ['L', x + w, y],
  76. ['L', x, y + h / 2],
  77. ['L', x + w, y + h]
  78. ];
  79. };
  80. /**
  81. * Creates a half-width arrow symbol. Like a triangle, except not filled.
  82. * ```
  83. * o
  84. * o
  85. * o
  86. * o
  87. * o
  88. * ```
  89. *
  90. * @private
  91. * @function
  92. *
  93. * @param {number} x
  94. * x position of the arrow
  95. *
  96. * @param {number} y
  97. * y position of the arrow
  98. *
  99. * @param {number} w
  100. * width of the arrow
  101. *
  102. * @param {number} h
  103. * height of the arrow
  104. *
  105. * @return {Highcharts.SVGPathArray}
  106. * Path array
  107. */
  108. SVGRenderer.prototype.symbols['arrow-half'] = function (x, y, w, h) {
  109. return SVGRenderer.prototype.symbols.arrow(x, y, w / 2, h);
  110. };
  111. /**
  112. * Creates a left-oriented triangle.
  113. * ```
  114. * o
  115. * ooooooo
  116. * ooooooooooooo
  117. * ooooooo
  118. * o
  119. * ```
  120. *
  121. * @private
  122. * @function
  123. *
  124. * @param {number} x
  125. * x position of the triangle
  126. *
  127. * @param {number} y
  128. * y position of the triangle
  129. *
  130. * @param {number} w
  131. * width of the triangle
  132. *
  133. * @param {number} h
  134. * height of the triangle
  135. *
  136. * @return {Highcharts.SVGPathArray}
  137. * Path array
  138. */
  139. SVGRenderer.prototype.symbols['triangle-left'] = function (x, y, w, h) {
  140. return [
  141. ['M', x + w, y],
  142. ['L', x, y + h / 2],
  143. ['L', x + w, y + h],
  144. ['Z']
  145. ];
  146. };
  147. /**
  148. * Alias function for triangle-left.
  149. *
  150. * @private
  151. * @function
  152. *
  153. * @param {number} x
  154. * x position of the arrow
  155. *
  156. * @param {number} y
  157. * y position of the arrow
  158. *
  159. * @param {number} w
  160. * width of the arrow
  161. *
  162. * @param {number} h
  163. * height of the arrow
  164. *
  165. * @return {Highcharts.SVGPathArray}
  166. * Path array
  167. */
  168. SVGRenderer.prototype.symbols['arrow-filled'] = SVGRenderer.prototype.symbols['triangle-left'];
  169. /**
  170. * Creates a half-width, left-oriented triangle.
  171. * ```
  172. * o
  173. * oooo
  174. * ooooooo
  175. * oooo
  176. * o
  177. * ```
  178. *
  179. * @private
  180. * @function
  181. *
  182. * @param {number} x
  183. * x position of the triangle
  184. *
  185. * @param {number} y
  186. * y position of the triangle
  187. *
  188. * @param {number} w
  189. * width of the triangle
  190. *
  191. * @param {number} h
  192. * height of the triangle
  193. *
  194. * @return {Highcharts.SVGPathArray}
  195. * Path array
  196. */
  197. SVGRenderer.prototype.symbols['triangle-left-half'] = function (x, y, w, h) {
  198. return SVGRenderer.prototype.symbols['triangle-left'](x, y, w / 2, h);
  199. };
  200. /**
  201. * Alias function for triangle-left-half.
  202. *
  203. * @private
  204. * @function
  205. *
  206. * @param {number} x
  207. * x position of the arrow
  208. *
  209. * @param {number} y
  210. * y position of the arrow
  211. *
  212. * @param {number} w
  213. * width of the arrow
  214. *
  215. * @param {number} h
  216. * height of the arrow
  217. *
  218. * @return {Highcharts.SVGPathArray}
  219. * Path array
  220. */
  221. SVGRenderer.prototype.symbols['arrow-filled-half'] = SVGRenderer.prototype.symbols['triangle-left-half'];
  222. });
  223. _registerModule(_modules, 'Gantt/Connection.js', [_modules['Core/Globals.js'], _modules['Core/Options.js'], _modules['Core/Series/Point.js'], _modules['Core/Utilities.js']], function (H, O, Point, U) {
  224. /* *
  225. *
  226. * (c) 2016 Highsoft AS
  227. * Authors: Øystein Moseng, Lars A. V. Cabrera
  228. *
  229. * License: www.highcharts.com/license
  230. *
  231. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  232. *
  233. * */
  234. /**
  235. * The default pathfinder algorithm to use for a chart. It is possible to define
  236. * your own algorithms by adding them to the
  237. * `Highcharts.Pathfinder.prototype.algorithms`
  238. * object before the chart has been created.
  239. *
  240. * The default algorithms are as follows:
  241. *
  242. * `straight`: Draws a straight line between the connecting
  243. * points. Does not avoid other points when drawing.
  244. *
  245. * `simpleConnect`: Finds a path between the points using right angles
  246. * only. Takes only starting/ending points into
  247. * account, and will not avoid other points.
  248. *
  249. * `fastAvoid`: Finds a path between the points using right angles
  250. * only. Will attempt to avoid other points, but its
  251. * focus is performance over accuracy. Works well with
  252. * less dense datasets.
  253. *
  254. * @typedef {"fastAvoid"|"simpleConnect"|"straight"|string} Highcharts.PathfinderTypeValue
  255. */
  256. ''; // detach doclets above
  257. var defaultOptions = O.defaultOptions;
  258. var addEvent = U.addEvent,
  259. defined = U.defined,
  260. error = U.error,
  261. extend = U.extend,
  262. merge = U.merge,
  263. objectEach = U.objectEach,
  264. pick = U.pick,
  265. splat = U.splat;
  266. var deg2rad = H.deg2rad,
  267. max = Math.max,
  268. min = Math.min;
  269. /*
  270. @todo:
  271. - Document how to write your own algorithms
  272. - Consider adding a Point.pathTo method that wraps creating a connection
  273. and rendering it
  274. */
  275. // Set default Pathfinder options
  276. extend(defaultOptions, {
  277. /**
  278. * The Pathfinder module allows you to define connections between any two
  279. * points, represented as lines - optionally with markers for the start
  280. * and/or end points. Multiple algorithms are available for calculating how
  281. * the connecting lines are drawn.
  282. *
  283. * Connector functionality requires Highcharts Gantt to be loaded. In Gantt
  284. * charts, the connectors are used to draw dependencies between tasks.
  285. *
  286. * @see [dependency](series.gantt.data.dependency)
  287. *
  288. * @sample gantt/pathfinder/demo
  289. * Pathfinder connections
  290. *
  291. * @declare Highcharts.ConnectorsOptions
  292. * @product gantt
  293. * @optionparent connectors
  294. */
  295. connectors: {
  296. /**
  297. * Enable connectors for this chart. Requires Highcharts Gantt.
  298. *
  299. * @type {boolean}
  300. * @default true
  301. * @since 6.2.0
  302. * @apioption connectors.enabled
  303. */
  304. /**
  305. * Set the default dash style for this chart's connecting lines.
  306. *
  307. * @type {string}
  308. * @default solid
  309. * @since 6.2.0
  310. * @apioption connectors.dashStyle
  311. */
  312. /**
  313. * Set the default color for this chart's Pathfinder connecting lines.
  314. * Defaults to the color of the point being connected.
  315. *
  316. * @type {Highcharts.ColorString}
  317. * @since 6.2.0
  318. * @apioption connectors.lineColor
  319. */
  320. /**
  321. * Set the default pathfinder margin to use, in pixels. Some Pathfinder
  322. * algorithms attempt to avoid obstacles, such as other points in the
  323. * chart. These algorithms use this margin to determine how close lines
  324. * can be to an obstacle. The default is to compute this automatically
  325. * from the size of the obstacles in the chart.
  326. *
  327. * To draw connecting lines close to existing points, set this to a low
  328. * number. For more space around existing points, set this number
  329. * higher.
  330. *
  331. * @sample gantt/pathfinder/algorithm-margin
  332. * Small algorithmMargin
  333. *
  334. * @type {number}
  335. * @since 6.2.0
  336. * @apioption connectors.algorithmMargin
  337. */
  338. /**
  339. * Set the default pathfinder algorithm to use for this chart. It is
  340. * possible to define your own algorithms by adding them to the
  341. * Highcharts.Pathfinder.prototype.algorithms object before the chart
  342. * has been created.
  343. *
  344. * The default algorithms are as follows:
  345. *
  346. * `straight`: Draws a straight line between the connecting
  347. * points. Does not avoid other points when drawing.
  348. *
  349. * `simpleConnect`: Finds a path between the points using right angles
  350. * only. Takes only starting/ending points into
  351. * account, and will not avoid other points.
  352. *
  353. * `fastAvoid`: Finds a path between the points using right angles
  354. * only. Will attempt to avoid other points, but its
  355. * focus is performance over accuracy. Works well with
  356. * less dense datasets.
  357. *
  358. * Default value: `straight` is used as default for most series types,
  359. * while `simpleConnect` is used as default for Gantt series, to show
  360. * dependencies between points.
  361. *
  362. * @sample gantt/pathfinder/demo
  363. * Different types used
  364. *
  365. * @type {Highcharts.PathfinderTypeValue}
  366. * @default undefined
  367. * @since 6.2.0
  368. */
  369. type: 'straight',
  370. /**
  371. * Set the default pixel width for this chart's Pathfinder connecting
  372. * lines.
  373. *
  374. * @since 6.2.0
  375. */
  376. lineWidth: 1,
  377. /**
  378. * Marker options for this chart's Pathfinder connectors. Note that
  379. * this option is overridden by the `startMarker` and `endMarker`
  380. * options.
  381. *
  382. * @declare Highcharts.ConnectorsMarkerOptions
  383. * @since 6.2.0
  384. */
  385. marker: {
  386. /**
  387. * Set the radius of the connector markers. The default is
  388. * automatically computed based on the algorithmMargin setting.
  389. *
  390. * Setting marker.width and marker.height will override this
  391. * setting.
  392. *
  393. * @type {number}
  394. * @since 6.2.0
  395. * @apioption connectors.marker.radius
  396. */
  397. /**
  398. * Set the width of the connector markers. If not supplied, this
  399. * is inferred from the marker radius.
  400. *
  401. * @type {number}
  402. * @since 6.2.0
  403. * @apioption connectors.marker.width
  404. */
  405. /**
  406. * Set the height of the connector markers. If not supplied, this
  407. * is inferred from the marker radius.
  408. *
  409. * @type {number}
  410. * @since 6.2.0
  411. * @apioption connectors.marker.height
  412. */
  413. /**
  414. * Set the color of the connector markers. By default this is the
  415. * same as the connector color.
  416. *
  417. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  418. * @since 6.2.0
  419. * @apioption connectors.marker.color
  420. */
  421. /**
  422. * Set the line/border color of the connector markers. By default
  423. * this is the same as the marker color.
  424. *
  425. * @type {Highcharts.ColorString}
  426. * @since 6.2.0
  427. * @apioption connectors.marker.lineColor
  428. */
  429. /**
  430. * Enable markers for the connectors.
  431. */
  432. enabled: false,
  433. /**
  434. * Horizontal alignment of the markers relative to the points.
  435. *
  436. * @type {Highcharts.AlignValue}
  437. */
  438. align: 'center',
  439. /**
  440. * Vertical alignment of the markers relative to the points.
  441. *
  442. * @type {Highcharts.VerticalAlignValue}
  443. */
  444. verticalAlign: 'middle',
  445. /**
  446. * Whether or not to draw the markers inside the points.
  447. */
  448. inside: false,
  449. /**
  450. * Set the line/border width of the pathfinder markers.
  451. */
  452. lineWidth: 1
  453. },
  454. /**
  455. * Marker options specific to the start markers for this chart's
  456. * Pathfinder connectors. Overrides the generic marker options.
  457. *
  458. * @declare Highcharts.ConnectorsStartMarkerOptions
  459. * @extends connectors.marker
  460. * @since 6.2.0
  461. */
  462. startMarker: {
  463. /**
  464. * Set the symbol of the connector start markers.
  465. */
  466. symbol: 'diamond'
  467. },
  468. /**
  469. * Marker options specific to the end markers for this chart's
  470. * Pathfinder connectors. Overrides the generic marker options.
  471. *
  472. * @declare Highcharts.ConnectorsEndMarkerOptions
  473. * @extends connectors.marker
  474. * @since 6.2.0
  475. */
  476. endMarker: {
  477. /**
  478. * Set the symbol of the connector end markers.
  479. */
  480. symbol: 'arrow-filled'
  481. }
  482. }
  483. });
  484. /**
  485. * Override Pathfinder connector options for a series. Requires Highcharts Gantt
  486. * to be loaded.
  487. *
  488. * @declare Highcharts.SeriesConnectorsOptionsObject
  489. * @extends connectors
  490. * @since 6.2.0
  491. * @excluding enabled, algorithmMargin
  492. * @product gantt
  493. * @apioption plotOptions.series.connectors
  494. */
  495. /**
  496. * Connect to a point. This option can be either a string, referring to the ID
  497. * of another point, or an object, or an array of either. If the option is an
  498. * array, each element defines a connection.
  499. *
  500. * @sample gantt/pathfinder/demo
  501. * Different connection types
  502. *
  503. * @declare Highcharts.XrangePointConnectorsOptionsObject
  504. * @type {string|Array<string|*>|*}
  505. * @extends plotOptions.series.connectors
  506. * @since 6.2.0
  507. * @excluding enabled
  508. * @product gantt
  509. * @requires highcharts-gantt
  510. * @apioption series.xrange.data.connect
  511. */
  512. /**
  513. * The ID of the point to connect to.
  514. *
  515. * @type {string}
  516. * @since 6.2.0
  517. * @product gantt
  518. * @apioption series.xrange.data.connect.to
  519. */
  520. /**
  521. * Get point bounding box using plotX/plotY and shapeArgs. If using
  522. * graphic.getBBox() directly, the bbox will be affected by animation.
  523. *
  524. * @private
  525. * @function
  526. *
  527. * @param {Highcharts.Point} point
  528. * The point to get BB of.
  529. *
  530. * @return {Highcharts.Dictionary<number>|null}
  531. * Result xMax, xMin, yMax, yMin.
  532. */
  533. function getPointBB(point) {
  534. var shapeArgs = point.shapeArgs,
  535. bb;
  536. // Prefer using shapeArgs (columns)
  537. if (shapeArgs) {
  538. return {
  539. xMin: shapeArgs.x,
  540. xMax: shapeArgs.x + shapeArgs.width,
  541. yMin: shapeArgs.y,
  542. yMax: shapeArgs.y + shapeArgs.height
  543. };
  544. }
  545. // Otherwise use plotX/plotY and bb
  546. bb = point.graphic && point.graphic.getBBox();
  547. return bb ? {
  548. xMin: point.plotX - bb.width / 2,
  549. xMax: point.plotX + bb.width / 2,
  550. yMin: point.plotY - bb.height / 2,
  551. yMax: point.plotY + bb.height / 2
  552. } : null;
  553. }
  554. /**
  555. * Calculate margin to place around obstacles for the pathfinder in pixels.
  556. * Returns a minimum of 1 pixel margin.
  557. *
  558. * @private
  559. * @function
  560. *
  561. * @param {Array<object>} obstacles
  562. * Obstacles to calculate margin from.
  563. *
  564. * @return {number}
  565. * The calculated margin in pixels. At least 1.
  566. */
  567. function calculateObstacleMargin(obstacles) {
  568. var len = obstacles.length,
  569. i = 0,
  570. j,
  571. obstacleDistance,
  572. distances = [],
  573. // Compute smallest distance between two rectangles
  574. distance = function (a,
  575. b,
  576. bbMargin) {
  577. // Count the distance even if we are slightly off
  578. var margin = pick(bbMargin, 10),
  579. yOverlap = a.yMax + margin > b.yMin - margin &&
  580. a.yMin - margin < b.yMax + margin,
  581. xOverlap = a.xMax + margin > b.xMin - margin &&
  582. a.xMin - margin < b.xMax + margin,
  583. xDistance = yOverlap ? (a.xMin > b.xMax ? a.xMin - b.xMax : b.xMin - a.xMax) : Infinity,
  584. yDistance = xOverlap ? (a.yMin > b.yMax ? a.yMin - b.yMax : b.yMin - a.yMax) : Infinity;
  585. // If the rectangles collide, try recomputing with smaller margin.
  586. // If they collide anyway, discard the obstacle.
  587. if (xOverlap && yOverlap) {
  588. return (margin ?
  589. distance(a, b, Math.floor(margin / 2)) :
  590. Infinity);
  591. }
  592. return min(xDistance, yDistance);
  593. };
  594. // Go over all obstacles and compare them to the others.
  595. for (; i < len; ++i) {
  596. // Compare to all obstacles ahead. We will already have compared this
  597. // obstacle to the ones before.
  598. for (j = i + 1; j < len; ++j) {
  599. obstacleDistance = distance(obstacles[i], obstacles[j]);
  600. // TODO: Magic number 80
  601. if (obstacleDistance < 80) { // Ignore large distances
  602. distances.push(obstacleDistance);
  603. }
  604. }
  605. }
  606. // Ensure we always have at least one value, even in very spaceous charts
  607. distances.push(80);
  608. return max(Math.floor(distances.sort(function (a, b) {
  609. return (a - b);
  610. })[
  611. // Discard first 10% of the relevant distances, and then grab
  612. // the smallest one.
  613. Math.floor(distances.length / 10)] / 2 - 1 // Divide the distance by 2 and subtract 1.
  614. ), 1 // 1 is the minimum margin
  615. );
  616. }
  617. /* eslint-disable no-invalid-this, valid-jsdoc */
  618. /**
  619. * The Connection class. Used internally to represent a connection between two
  620. * points.
  621. *
  622. * @private
  623. * @class
  624. * @name Highcharts.Connection
  625. *
  626. * @param {Highcharts.Point} from
  627. * Connection runs from this Point.
  628. *
  629. * @param {Highcharts.Point} to
  630. * Connection runs to this Point.
  631. *
  632. * @param {Highcharts.ConnectorsOptions} [options]
  633. * Connection options.
  634. */
  635. var Connection = /** @class */ (function () {
  636. function Connection(from, to, options) {
  637. /* *
  638. *
  639. * Properties
  640. *
  641. * */
  642. this.chart = void 0;
  643. this.fromPoint = void 0;
  644. this.graphics = void 0;
  645. this.pathfinder = void 0;
  646. this.toPoint = void 0;
  647. this.init(from, to, options);
  648. }
  649. /**
  650. * Initialize the Connection object. Used as constructor only.
  651. *
  652. * @function Highcharts.Connection#init
  653. *
  654. * @param {Highcharts.Point} from
  655. * Connection runs from this Point.
  656. *
  657. * @param {Highcharts.Point} to
  658. * Connection runs to this Point.
  659. *
  660. * @param {Highcharts.ConnectorsOptions} [options]
  661. * Connection options.
  662. */
  663. Connection.prototype.init = function (from, to, options) {
  664. this.fromPoint = from;
  665. this.toPoint = to;
  666. this.options = options;
  667. this.chart = from.series.chart;
  668. this.pathfinder = this.chart.pathfinder;
  669. };
  670. /**
  671. * Add (or update) this connection's path on chart. Stores reference to the
  672. * created element on this.graphics.path.
  673. *
  674. * @function Highcharts.Connection#renderPath
  675. *
  676. * @param {Highcharts.SVGPathArray} path
  677. * Path to render, in array format. E.g. ['M', 0, 0, 'L', 10, 10]
  678. *
  679. * @param {Highcharts.SVGAttributes} [attribs]
  680. * SVG attributes for the path.
  681. *
  682. * @param {Partial<Highcharts.AnimationOptionsObject>} [animation]
  683. * Animation options for the rendering.
  684. */
  685. Connection.prototype.renderPath = function (path, attribs, animation) {
  686. var connection = this,
  687. chart = this.chart,
  688. styledMode = chart.styledMode,
  689. pathfinder = chart.pathfinder,
  690. animate = !chart.options.chart.forExport && animation !== false,
  691. pathGraphic = connection.graphics && connection.graphics.path,
  692. anim;
  693. // Add the SVG element of the pathfinder group if it doesn't exist
  694. if (!pathfinder.group) {
  695. pathfinder.group = chart.renderer.g()
  696. .addClass('highcharts-pathfinder-group')
  697. .attr({ zIndex: -1 })
  698. .add(chart.seriesGroup);
  699. }
  700. // Shift the group to compensate for plot area.
  701. // Note: Do this always (even when redrawing a path) to avoid issues
  702. // when updating chart in a way that changes plot metrics.
  703. pathfinder.group.translate(chart.plotLeft, chart.plotTop);
  704. // Create path if does not exist
  705. if (!(pathGraphic && pathGraphic.renderer)) {
  706. pathGraphic = chart.renderer.path()
  707. .add(pathfinder.group);
  708. if (!styledMode) {
  709. pathGraphic.attr({
  710. opacity: 0
  711. });
  712. }
  713. }
  714. // Set path attribs and animate to the new path
  715. pathGraphic.attr(attribs);
  716. anim = { d: path };
  717. if (!styledMode) {
  718. anim.opacity = 1;
  719. }
  720. pathGraphic[animate ? 'animate' : 'attr'](anim, animation);
  721. // Store reference on connection
  722. this.graphics = this.graphics || {};
  723. this.graphics.path = pathGraphic;
  724. };
  725. /**
  726. * Calculate and add marker graphics for connection to the chart. The
  727. * created/updated elements are stored on this.graphics.start and
  728. * this.graphics.end.
  729. *
  730. * @function Highcharts.Connection#addMarker
  731. *
  732. * @param {string} type
  733. * Marker type, either 'start' or 'end'.
  734. *
  735. * @param {Highcharts.ConnectorsMarkerOptions} options
  736. * All options for this marker. Not calculated or merged with other
  737. * options.
  738. *
  739. * @param {Highcharts.SVGPathArray} path
  740. * Connection path in array format. This is used to calculate the
  741. * rotation angle of the markers.
  742. */
  743. Connection.prototype.addMarker = function (type, options, path) {
  744. var connection = this,
  745. chart = connection.fromPoint.series.chart,
  746. pathfinder = chart.pathfinder,
  747. renderer = chart.renderer,
  748. point = (type === 'start' ?
  749. connection.fromPoint :
  750. connection.toPoint),
  751. anchor = point.getPathfinderAnchorPoint(options),
  752. markerVector,
  753. radians,
  754. rotation,
  755. box,
  756. width,
  757. height,
  758. pathVector,
  759. segment;
  760. if (!options.enabled) {
  761. return;
  762. }
  763. // Last vector before start/end of path, used to get angle
  764. if (type === 'start') {
  765. segment = path[1];
  766. }
  767. else { // 'end'
  768. segment = path[path.length - 2];
  769. }
  770. if (segment && segment[0] === 'M' || segment[0] === 'L') {
  771. pathVector = {
  772. x: segment[1],
  773. y: segment[2]
  774. };
  775. // Get angle between pathVector and anchor point and use it to
  776. // create marker position.
  777. radians = point.getRadiansToVector(pathVector, anchor);
  778. markerVector = point.getMarkerVector(radians, options.radius, anchor);
  779. // Rotation of marker is calculated from angle between pathVector
  780. // and markerVector.
  781. // (Note:
  782. // Used to recalculate radians between markerVector and pathVector,
  783. // but this should be the same as between pathVector and anchor.)
  784. rotation = -radians / deg2rad;
  785. if (options.width && options.height) {
  786. width = options.width;
  787. height = options.height;
  788. }
  789. else {
  790. width = height = options.radius * 2;
  791. }
  792. // Add graphics object if it does not exist
  793. connection.graphics = connection.graphics || {};
  794. box = {
  795. x: markerVector.x - (width / 2),
  796. y: markerVector.y - (height / 2),
  797. width: width,
  798. height: height,
  799. rotation: rotation,
  800. rotationOriginX: markerVector.x,
  801. rotationOriginY: markerVector.y
  802. };
  803. if (!connection.graphics[type]) {
  804. // Create new marker element
  805. connection.graphics[type] = renderer
  806. .symbol(options.symbol)
  807. .addClass('highcharts-point-connecting-path-' + type + '-marker')
  808. .attr(box)
  809. .add(pathfinder.group);
  810. if (!renderer.styledMode) {
  811. connection.graphics[type].attr({
  812. fill: options.color || connection.fromPoint.color,
  813. stroke: options.lineColor,
  814. 'stroke-width': options.lineWidth,
  815. opacity: 0
  816. })
  817. .animate({
  818. opacity: 1
  819. }, point.series.options.animation);
  820. }
  821. }
  822. else {
  823. connection.graphics[type].animate(box);
  824. }
  825. }
  826. };
  827. /**
  828. * Calculate and return connection path.
  829. * Note: Recalculates chart obstacles on demand if they aren't calculated.
  830. *
  831. * @function Highcharts.Connection#getPath
  832. *
  833. * @param {Highcharts.ConnectorsOptions} options
  834. * Connector options. Not calculated or merged with other options.
  835. *
  836. * @return {object|undefined}
  837. * Calculated SVG path data in array format.
  838. */
  839. Connection.prototype.getPath = function (options) {
  840. var pathfinder = this.pathfinder,
  841. chart = this.chart,
  842. algorithm = pathfinder.algorithms[options.type],
  843. chartObstacles = pathfinder.chartObstacles;
  844. if (typeof algorithm !== 'function') {
  845. error('"' + options.type + '" is not a Pathfinder algorithm.');
  846. return {
  847. path: [],
  848. obstacles: []
  849. };
  850. }
  851. // This function calculates obstacles on demand if they don't exist
  852. if (algorithm.requiresObstacles && !chartObstacles) {
  853. chartObstacles =
  854. pathfinder.chartObstacles =
  855. pathfinder.getChartObstacles(options);
  856. // If the algorithmMargin was computed, store the result in default
  857. // options.
  858. chart.options.connectors.algorithmMargin =
  859. options.algorithmMargin;
  860. // Cache some metrics too
  861. pathfinder.chartObstacleMetrics =
  862. pathfinder.getObstacleMetrics(chartObstacles);
  863. }
  864. // Get the SVG path
  865. return algorithm(
  866. // From
  867. this.fromPoint.getPathfinderAnchorPoint(options.startMarker),
  868. // To
  869. this.toPoint.getPathfinderAnchorPoint(options.endMarker), merge({
  870. chartObstacles: chartObstacles,
  871. lineObstacles: pathfinder.lineObstacles || [],
  872. obstacleMetrics: pathfinder.chartObstacleMetrics,
  873. hardBounds: {
  874. xMin: 0,
  875. xMax: chart.plotWidth,
  876. yMin: 0,
  877. yMax: chart.plotHeight
  878. },
  879. obstacleOptions: {
  880. margin: options.algorithmMargin
  881. },
  882. startDirectionX: pathfinder.getAlgorithmStartDirection(options.startMarker)
  883. }, options));
  884. };
  885. /**
  886. * (re)Calculate and (re)draw the connection.
  887. *
  888. * @function Highcharts.Connection#render
  889. */
  890. Connection.prototype.render = function () {
  891. var connection = this,
  892. fromPoint = connection.fromPoint,
  893. series = fromPoint.series,
  894. chart = series.chart,
  895. pathfinder = chart.pathfinder,
  896. pathResult,
  897. path,
  898. options = merge(chart.options.connectors,
  899. series.options.connectors,
  900. fromPoint.options.connectors,
  901. connection.options),
  902. attribs = {};
  903. // Set path attribs
  904. if (!chart.styledMode) {
  905. attribs.stroke = options.lineColor || fromPoint.color;
  906. attribs['stroke-width'] = options.lineWidth;
  907. if (options.dashStyle) {
  908. attribs.dashstyle = options.dashStyle;
  909. }
  910. }
  911. attribs['class'] = // eslint-disable-line dot-notation
  912. 'highcharts-point-connecting-path ' +
  913. 'highcharts-color-' + fromPoint.colorIndex;
  914. options = merge(attribs, options);
  915. // Set common marker options
  916. if (!defined(options.marker.radius)) {
  917. options.marker.radius = min(max(Math.ceil((options.algorithmMargin || 8) / 2) - 1, 1), 5);
  918. }
  919. // Get the path
  920. pathResult = connection.getPath(options);
  921. path = pathResult.path;
  922. // Always update obstacle storage with obstacles from this path.
  923. // We don't know if future calls will need this for their algorithm.
  924. if (pathResult.obstacles) {
  925. pathfinder.lineObstacles =
  926. pathfinder.lineObstacles || [];
  927. pathfinder.lineObstacles =
  928. pathfinder.lineObstacles.concat(pathResult.obstacles);
  929. }
  930. // Add the calculated path to the pathfinder group
  931. connection.renderPath(path, attribs, series.options.animation);
  932. // Render the markers
  933. connection.addMarker('start', merge(options.marker, options.startMarker), path);
  934. connection.addMarker('end', merge(options.marker, options.endMarker), path);
  935. };
  936. /**
  937. * Destroy connection by destroying the added graphics elements.
  938. *
  939. * @function Highcharts.Connection#destroy
  940. */
  941. Connection.prototype.destroy = function () {
  942. if (this.graphics) {
  943. objectEach(this.graphics, function (val) {
  944. val.destroy();
  945. });
  946. delete this.graphics;
  947. }
  948. };
  949. return Connection;
  950. }());
  951. // Add to Highcharts namespace
  952. H.Connection = Connection;
  953. // Add pathfinding capabilities to Points
  954. extend(Point.prototype, /** @lends Point.prototype */ {
  955. /**
  956. * Get coordinates of anchor point for pathfinder connection.
  957. *
  958. * @private
  959. * @function Highcharts.Point#getPathfinderAnchorPoint
  960. *
  961. * @param {Highcharts.ConnectorsMarkerOptions} markerOptions
  962. * Connection options for position on point.
  963. *
  964. * @return {Highcharts.PositionObject}
  965. * An object with x/y properties for the position. Coordinates are
  966. * in plot values, not relative to point.
  967. */
  968. getPathfinderAnchorPoint: function (markerOptions) {
  969. var bb = getPointBB(this),
  970. x,
  971. y;
  972. switch (markerOptions.align) { // eslint-disable-line default-case
  973. case 'right':
  974. x = 'xMax';
  975. break;
  976. case 'left':
  977. x = 'xMin';
  978. }
  979. switch (markerOptions.verticalAlign) { // eslint-disable-line default-case
  980. case 'top':
  981. y = 'yMin';
  982. break;
  983. case 'bottom':
  984. y = 'yMax';
  985. }
  986. return {
  987. x: x ? bb[x] : (bb.xMin + bb.xMax) / 2,
  988. y: y ? bb[y] : (bb.yMin + bb.yMax) / 2
  989. };
  990. },
  991. /**
  992. * Utility to get the angle from one point to another.
  993. *
  994. * @private
  995. * @function Highcharts.Point#getRadiansToVector
  996. *
  997. * @param {Highcharts.PositionObject} v1
  998. * The first vector, as an object with x/y properties.
  999. *
  1000. * @param {Highcharts.PositionObject} v2
  1001. * The second vector, as an object with x/y properties.
  1002. *
  1003. * @return {number}
  1004. * The angle in degrees
  1005. */
  1006. getRadiansToVector: function (v1, v2) {
  1007. var box;
  1008. if (!defined(v2)) {
  1009. box = getPointBB(this);
  1010. if (box) {
  1011. v2 = {
  1012. x: (box.xMin + box.xMax) / 2,
  1013. y: (box.yMin + box.yMax) / 2
  1014. };
  1015. }
  1016. }
  1017. return Math.atan2(v2.y - v1.y, v1.x - v2.x);
  1018. },
  1019. /**
  1020. * Utility to get the position of the marker, based on the path angle and
  1021. * the marker's radius.
  1022. *
  1023. * @private
  1024. * @function Highcharts.Point#getMarkerVector
  1025. *
  1026. * @param {number} radians
  1027. * The angle in radians from the point center to another vector.
  1028. *
  1029. * @param {number} markerRadius
  1030. * The radius of the marker, to calculate the additional distance to
  1031. * the center of the marker.
  1032. *
  1033. * @param {object} anchor
  1034. * The anchor point of the path and marker as an object with x/y
  1035. * properties.
  1036. *
  1037. * @return {object}
  1038. * The marker vector as an object with x/y properties.
  1039. */
  1040. getMarkerVector: function (radians, markerRadius, anchor) {
  1041. var twoPI = Math.PI * 2.0,
  1042. theta = radians,
  1043. bb = getPointBB(this),
  1044. rectWidth = bb.xMax - bb.xMin,
  1045. rectHeight = bb.yMax - bb.yMin,
  1046. rAtan = Math.atan2(rectHeight,
  1047. rectWidth),
  1048. tanTheta = 1,
  1049. leftOrRightRegion = false,
  1050. rectHalfWidth = rectWidth / 2.0,
  1051. rectHalfHeight = rectHeight / 2.0,
  1052. rectHorizontalCenter = bb.xMin + rectHalfWidth,
  1053. rectVerticalCenter = bb.yMin + rectHalfHeight,
  1054. edgePoint = {
  1055. x: rectHorizontalCenter,
  1056. y: rectVerticalCenter
  1057. },
  1058. markerPoint = {},
  1059. xFactor = 1,
  1060. yFactor = 1;
  1061. while (theta < -Math.PI) {
  1062. theta += twoPI;
  1063. }
  1064. while (theta > Math.PI) {
  1065. theta -= twoPI;
  1066. }
  1067. tanTheta = Math.tan(theta);
  1068. if ((theta > -rAtan) && (theta <= rAtan)) {
  1069. // Right side
  1070. yFactor = -1;
  1071. leftOrRightRegion = true;
  1072. }
  1073. else if (theta > rAtan && theta <= (Math.PI - rAtan)) {
  1074. // Top side
  1075. yFactor = -1;
  1076. }
  1077. else if (theta > (Math.PI - rAtan) || theta <= -(Math.PI - rAtan)) {
  1078. // Left side
  1079. xFactor = -1;
  1080. leftOrRightRegion = true;
  1081. }
  1082. else {
  1083. // Bottom side
  1084. xFactor = -1;
  1085. }
  1086. // Correct the edgePoint according to the placement of the marker
  1087. if (leftOrRightRegion) {
  1088. edgePoint.x += xFactor * (rectHalfWidth);
  1089. edgePoint.y += yFactor * (rectHalfWidth) * tanTheta;
  1090. }
  1091. else {
  1092. edgePoint.x += xFactor * (rectHeight / (2.0 * tanTheta));
  1093. edgePoint.y += yFactor * (rectHalfHeight);
  1094. }
  1095. if (anchor.x !== rectHorizontalCenter) {
  1096. edgePoint.x = anchor.x;
  1097. }
  1098. if (anchor.y !== rectVerticalCenter) {
  1099. edgePoint.y = anchor.y;
  1100. }
  1101. markerPoint.x = edgePoint.x + (markerRadius * Math.cos(theta));
  1102. markerPoint.y = edgePoint.y - (markerRadius * Math.sin(theta));
  1103. return markerPoint;
  1104. }
  1105. });
  1106. /**
  1107. * Warn if using legacy options. Copy the options over. Note that this will
  1108. * still break if using the legacy options in chart.update, addSeries etc.
  1109. * @private
  1110. */
  1111. function warnLegacy(chart) {
  1112. if (chart.options.pathfinder ||
  1113. chart.series.reduce(function (acc, series) {
  1114. if (series.options) {
  1115. merge(true, (series.options.connectors = series.options.connectors ||
  1116. {}), series.options.pathfinder);
  1117. }
  1118. return acc || series.options && series.options.pathfinder;
  1119. }, false)) {
  1120. merge(true, (chart.options.connectors = chart.options.connectors || {}), chart.options.pathfinder);
  1121. error('WARNING: Pathfinder options have been renamed. ' +
  1122. 'Use "chart.connectors" or "series.connectors" instead.');
  1123. }
  1124. }
  1125. return Connection;
  1126. });
  1127. _registerModule(_modules, 'Gantt/PathfinderAlgorithms.js', [_modules['Core/Utilities.js']], function (U) {
  1128. /* *
  1129. *
  1130. * (c) 2016 Highsoft AS
  1131. * Author: Øystein Moseng
  1132. *
  1133. * License: www.highcharts.com/license
  1134. *
  1135. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  1136. *
  1137. * */
  1138. var extend = U.extend,
  1139. pick = U.pick;
  1140. var min = Math.min,
  1141. max = Math.max,
  1142. abs = Math.abs;
  1143. /**
  1144. * Get index of last obstacle before xMin. Employs a type of binary search, and
  1145. * thus requires that obstacles are sorted by xMin value.
  1146. *
  1147. * @private
  1148. * @function findLastObstacleBefore
  1149. *
  1150. * @param {Array<object>} obstacles
  1151. * Array of obstacles to search in.
  1152. *
  1153. * @param {number} xMin
  1154. * The xMin threshold.
  1155. *
  1156. * @param {number} [startIx]
  1157. * Starting index to search from. Must be within array range.
  1158. *
  1159. * @return {number}
  1160. * The index of the last obstacle element before xMin.
  1161. */
  1162. function findLastObstacleBefore(obstacles, xMin, startIx) {
  1163. var left = startIx || 0, // left limit
  1164. right = obstacles.length - 1, // right limit
  1165. min = xMin - 0.0000001, // Make sure we include all obstacles at xMin
  1166. cursor,
  1167. cmp;
  1168. while (left <= right) {
  1169. cursor = (right + left) >> 1;
  1170. cmp = min - obstacles[cursor].xMin;
  1171. if (cmp > 0) {
  1172. left = cursor + 1;
  1173. }
  1174. else if (cmp < 0) {
  1175. right = cursor - 1;
  1176. }
  1177. else {
  1178. return cursor;
  1179. }
  1180. }
  1181. return left > 0 ? left - 1 : 0;
  1182. }
  1183. /**
  1184. * Test if a point lays within an obstacle.
  1185. *
  1186. * @private
  1187. * @function pointWithinObstacle
  1188. *
  1189. * @param {object} obstacle
  1190. * Obstacle to test.
  1191. *
  1192. * @param {Highcharts.Point} point
  1193. * Point with x/y props.
  1194. *
  1195. * @return {boolean}
  1196. * Whether point is within the obstacle or not.
  1197. */
  1198. function pointWithinObstacle(obstacle, point) {
  1199. return (point.x <= obstacle.xMax &&
  1200. point.x >= obstacle.xMin &&
  1201. point.y <= obstacle.yMax &&
  1202. point.y >= obstacle.yMin);
  1203. }
  1204. /**
  1205. * Find the index of an obstacle that wraps around a point.
  1206. * Returns -1 if not found.
  1207. *
  1208. * @private
  1209. * @function findObstacleFromPoint
  1210. *
  1211. * @param {Array<object>} obstacles
  1212. * Obstacles to test.
  1213. *
  1214. * @param {Highcharts.Point} point
  1215. * Point with x/y props.
  1216. *
  1217. * @return {number}
  1218. * Ix of the obstacle in the array, or -1 if not found.
  1219. */
  1220. function findObstacleFromPoint(obstacles, point) {
  1221. var i = findLastObstacleBefore(obstacles,
  1222. point.x + 1) + 1;
  1223. while (i--) {
  1224. if (obstacles[i].xMax >= point.x &&
  1225. // optimization using lazy evaluation
  1226. pointWithinObstacle(obstacles[i], point)) {
  1227. return i;
  1228. }
  1229. }
  1230. return -1;
  1231. }
  1232. /**
  1233. * Get SVG path array from array of line segments.
  1234. *
  1235. * @private
  1236. * @function pathFromSegments
  1237. *
  1238. * @param {Array<object>} segments
  1239. * The segments to build the path from.
  1240. *
  1241. * @return {Highcharts.SVGPathArray}
  1242. * SVG path array as accepted by the SVG Renderer.
  1243. */
  1244. function pathFromSegments(segments) {
  1245. var path = [];
  1246. if (segments.length) {
  1247. path.push(['M', segments[0].start.x, segments[0].start.y]);
  1248. for (var i = 0; i < segments.length; ++i) {
  1249. path.push(['L', segments[i].end.x, segments[i].end.y]);
  1250. }
  1251. }
  1252. return path;
  1253. }
  1254. /**
  1255. * Limits obstacle max/mins in all directions to bounds. Modifies input
  1256. * obstacle.
  1257. *
  1258. * @private
  1259. * @function limitObstacleToBounds
  1260. *
  1261. * @param {object} obstacle
  1262. * Obstacle to limit.
  1263. *
  1264. * @param {object} bounds
  1265. * Bounds to use as limit.
  1266. *
  1267. * @return {void}
  1268. */
  1269. function limitObstacleToBounds(obstacle, bounds) {
  1270. obstacle.yMin = max(obstacle.yMin, bounds.yMin);
  1271. obstacle.yMax = min(obstacle.yMax, bounds.yMax);
  1272. obstacle.xMin = max(obstacle.xMin, bounds.xMin);
  1273. obstacle.xMax = min(obstacle.xMax, bounds.xMax);
  1274. }
  1275. /**
  1276. * Get an SVG path from a starting coordinate to an ending coordinate.
  1277. * Draws a straight line.
  1278. *
  1279. * @function Highcharts.Pathfinder.algorithms.straight
  1280. *
  1281. * @param {Highcharts.PositionObject} start
  1282. * Starting coordinate, object with x/y props.
  1283. *
  1284. * @param {Highcharts.PositionObject} end
  1285. * Ending coordinate, object with x/y props.
  1286. *
  1287. * @return {object}
  1288. * An object with the SVG path in Array form as accepted by the SVG
  1289. * renderer, as well as an array of new obstacles making up this
  1290. * path.
  1291. */
  1292. function straight(start, end) {
  1293. return {
  1294. path: [
  1295. ['M', start.x, start.y],
  1296. ['L', end.x, end.y]
  1297. ],
  1298. obstacles: [{ start: start, end: end }]
  1299. };
  1300. }
  1301. /**
  1302. * Find a path from a starting coordinate to an ending coordinate, using
  1303. * right angles only, and taking only starting/ending obstacle into
  1304. * consideration.
  1305. *
  1306. * @function Highcharts.Pathfinder.algorithms.simpleConnect
  1307. *
  1308. * @param {Highcharts.PositionObject} start
  1309. * Starting coordinate, object with x/y props.
  1310. *
  1311. * @param {Highcharts.PositionObject} end
  1312. * Ending coordinate, object with x/y props.
  1313. *
  1314. * @param {object} options
  1315. * Options for the algorithm:
  1316. * - chartObstacles: Array of chart obstacles to avoid
  1317. * - startDirectionX: Optional. True if starting in the X direction.
  1318. * If not provided, the algorithm starts in the direction that is
  1319. * the furthest between start/end.
  1320. *
  1321. * @return {object}
  1322. * An object with the SVG path in Array form as accepted by the SVG
  1323. * renderer, as well as an array of new obstacles making up this
  1324. * path.
  1325. */
  1326. var simpleConnect = extend(function (start,
  1327. end,
  1328. options) {
  1329. var segments = [],
  1330. endSegment,
  1331. dir = pick(options.startDirectionX,
  1332. abs(end.x - start.x) > abs(end.y - start.y)) ? 'x' : 'y',
  1333. chartObstacles = options.chartObstacles,
  1334. startObstacleIx = findObstacleFromPoint(chartObstacles,
  1335. start),
  1336. endObstacleIx = findObstacleFromPoint(chartObstacles,
  1337. end),
  1338. startObstacle,
  1339. endObstacle,
  1340. prevWaypoint,
  1341. waypoint,
  1342. waypoint2,
  1343. useMax,
  1344. endPoint;
  1345. // eslint-disable-next-line valid-jsdoc
  1346. /**
  1347. * Return a clone of a point with a property set from a target object,
  1348. * optionally with an offset
  1349. * @private
  1350. */
  1351. function copyFromPoint(from, fromKey, to, toKey, offset) {
  1352. var point = {
  1353. x: from.x,
  1354. y: from.y
  1355. };
  1356. point[fromKey] = to[toKey || fromKey] + (offset || 0);
  1357. return point;
  1358. }
  1359. // eslint-disable-next-line valid-jsdoc
  1360. /**
  1361. * Return waypoint outside obstacle.
  1362. * @private
  1363. */
  1364. function getMeOut(obstacle, point, direction) {
  1365. var useMax = abs(point[direction] - obstacle[direction + 'Min']) >
  1366. abs(point[direction] - obstacle[direction + 'Max']);
  1367. return copyFromPoint(point, direction, obstacle, direction + (useMax ? 'Max' : 'Min'), useMax ? 1 : -1);
  1368. }
  1369. // Pull out end point
  1370. if (endObstacleIx > -1) {
  1371. endObstacle = chartObstacles[endObstacleIx];
  1372. waypoint = getMeOut(endObstacle, end, dir);
  1373. endSegment = {
  1374. start: waypoint,
  1375. end: end
  1376. };
  1377. endPoint = waypoint;
  1378. }
  1379. else {
  1380. endPoint = end;
  1381. }
  1382. // If an obstacle envelops the start point, add a segment to get out,
  1383. // and around it.
  1384. if (startObstacleIx > -1) {
  1385. startObstacle = chartObstacles[startObstacleIx];
  1386. waypoint = getMeOut(startObstacle, start, dir);
  1387. segments.push({
  1388. start: start,
  1389. end: waypoint
  1390. });
  1391. // If we are going back again, switch direction to get around start
  1392. // obstacle.
  1393. if (
  1394. // Going towards max from start:
  1395. waypoint[dir] >= start[dir] ===
  1396. // Going towards min to end:
  1397. waypoint[dir] >= endPoint[dir]) {
  1398. dir = dir === 'y' ? 'x' : 'y';
  1399. useMax = start[dir] < end[dir];
  1400. segments.push({
  1401. start: waypoint,
  1402. end: copyFromPoint(waypoint, dir, startObstacle, dir + (useMax ? 'Max' : 'Min'), useMax ? 1 : -1)
  1403. });
  1404. // Switch direction again
  1405. dir = dir === 'y' ? 'x' : 'y';
  1406. }
  1407. }
  1408. // We are around the start obstacle. Go towards the end in one
  1409. // direction.
  1410. prevWaypoint = segments.length ?
  1411. segments[segments.length - 1].end :
  1412. start;
  1413. waypoint = copyFromPoint(prevWaypoint, dir, endPoint);
  1414. segments.push({
  1415. start: prevWaypoint,
  1416. end: waypoint
  1417. });
  1418. // Final run to end point in the other direction
  1419. dir = dir === 'y' ? 'x' : 'y';
  1420. waypoint2 = copyFromPoint(waypoint, dir, endPoint);
  1421. segments.push({
  1422. start: waypoint,
  1423. end: waypoint2
  1424. });
  1425. // Finally add the endSegment
  1426. segments.push(endSegment);
  1427. return {
  1428. path: pathFromSegments(segments),
  1429. obstacles: segments
  1430. };
  1431. }, {
  1432. requiresObstacles: true
  1433. });
  1434. /**
  1435. * Find a path from a starting coordinate to an ending coordinate, taking
  1436. * obstacles into consideration. Might not always find the optimal path,
  1437. * but is fast, and usually good enough.
  1438. *
  1439. * @function Highcharts.Pathfinder.algorithms.fastAvoid
  1440. *
  1441. * @param {Highcharts.PositionObject} start
  1442. * Starting coordinate, object with x/y props.
  1443. *
  1444. * @param {Highcharts.PositionObject} end
  1445. * Ending coordinate, object with x/y props.
  1446. *
  1447. * @param {object} options
  1448. * Options for the algorithm.
  1449. * - chartObstacles: Array of chart obstacles to avoid
  1450. * - lineObstacles: Array of line obstacles to jump over
  1451. * - obstacleMetrics: Object with metrics of chartObstacles cached
  1452. * - hardBounds: Hard boundaries to not cross
  1453. * - obstacleOptions: Options for the obstacles, including margin
  1454. * - startDirectionX: Optional. True if starting in the X direction.
  1455. * If not provided, the algorithm starts in the
  1456. * direction that is the furthest between
  1457. * start/end.
  1458. *
  1459. * @return {object}
  1460. * An object with the SVG path in Array form as accepted by the SVG
  1461. * renderer, as well as an array of new obstacles making up this
  1462. * path.
  1463. */
  1464. var fastAvoid = extend(function (start,
  1465. end,
  1466. options) {
  1467. /*
  1468. Algorithm rules/description
  1469. - Find initial direction
  1470. - Determine soft/hard max for each direction.
  1471. - Move along initial direction until obstacle.
  1472. - Change direction.
  1473. - If hitting obstacle,
  1474. first try to change length of previous line
  1475. before changing direction again.
  1476. Soft min/max x = start/destination x +/- widest obstacle + margin
  1477. Soft min/max y = start/destination y +/- tallest obstacle + margin
  1478. @todo:
  1479. - Make retrospective,
  1480. try changing prev segment to reduce
  1481. corners
  1482. - Fix logic for breaking out of end-points - not always picking
  1483. the best direction currently
  1484. - When going around the end obstacle we should not always go the
  1485. shortest route,
  1486. rather pick the one closer to the end point
  1487. */
  1488. var dirIsX = pick(options.startDirectionX,
  1489. abs(end.x - start.x) > abs(end.y - start.y)),
  1490. dir = dirIsX ? 'x' : 'y',
  1491. segments,
  1492. useMax,
  1493. extractedEndPoint,
  1494. endSegments = [],
  1495. forceObstacleBreak = false, // Used in clearPathTo to keep track of
  1496. // when to force break through an obstacle.
  1497. // Boundaries to stay within. If beyond soft boundary, prefer to
  1498. // change direction ASAP. If at hard max, always change immediately.
  1499. metrics = options.obstacleMetrics,
  1500. softMinX = min(start.x,
  1501. end.x) - metrics.maxWidth - 10,
  1502. softMaxX = max(start.x,
  1503. end.x) + metrics.maxWidth + 10,
  1504. softMinY = min(start.y,
  1505. end.y) - metrics.maxHeight - 10,
  1506. softMaxY = max(start.y,
  1507. end.y) + metrics.maxHeight + 10,
  1508. // Obstacles
  1509. chartObstacles = options.chartObstacles,
  1510. startObstacleIx = findLastObstacleBefore(chartObstacles,
  1511. softMinX),
  1512. endObstacleIx = findLastObstacleBefore(chartObstacles,
  1513. softMaxX);
  1514. // eslint-disable-next-line valid-jsdoc
  1515. /**
  1516. * How far can you go between two points before hitting an obstacle?
  1517. * Does not work for diagonal lines (because it doesn't have to).
  1518. * @private
  1519. */
  1520. function pivotPoint(fromPoint, toPoint, directionIsX) {
  1521. var firstPoint,
  1522. lastPoint,
  1523. highestPoint,
  1524. lowestPoint,
  1525. i,
  1526. searchDirection = fromPoint.x < toPoint.x ? 1 : -1;
  1527. if (fromPoint.x < toPoint.x) {
  1528. firstPoint = fromPoint;
  1529. lastPoint = toPoint;
  1530. }
  1531. else {
  1532. firstPoint = toPoint;
  1533. lastPoint = fromPoint;
  1534. }
  1535. if (fromPoint.y < toPoint.y) {
  1536. lowestPoint = fromPoint;
  1537. highestPoint = toPoint;
  1538. }
  1539. else {
  1540. lowestPoint = toPoint;
  1541. highestPoint = fromPoint;
  1542. }
  1543. // Go through obstacle range in reverse if toPoint is before
  1544. // fromPoint in the X-dimension.
  1545. i = searchDirection < 0 ?
  1546. // Searching backwards, start at last obstacle before last point
  1547. min(findLastObstacleBefore(chartObstacles, lastPoint.x), chartObstacles.length - 1) :
  1548. // Forwards. Since we're not sorted by xMax, we have to look
  1549. // at all obstacles.
  1550. 0;
  1551. // Go through obstacles in this X range
  1552. while (chartObstacles[i] && (searchDirection > 0 && chartObstacles[i].xMin <= lastPoint.x ||
  1553. searchDirection < 0 && chartObstacles[i].xMax >= firstPoint.x)) {
  1554. // If this obstacle is between from and to points in a straight
  1555. // line, pivot at the intersection.
  1556. if (chartObstacles[i].xMin <= lastPoint.x &&
  1557. chartObstacles[i].xMax >= firstPoint.x &&
  1558. chartObstacles[i].yMin <= highestPoint.y &&
  1559. chartObstacles[i].yMax >= lowestPoint.y) {
  1560. if (directionIsX) {
  1561. return {
  1562. y: fromPoint.y,
  1563. x: fromPoint.x < toPoint.x ?
  1564. chartObstacles[i].xMin - 1 :
  1565. chartObstacles[i].xMax + 1,
  1566. obstacle: chartObstacles[i]
  1567. };
  1568. }
  1569. // else ...
  1570. return {
  1571. x: fromPoint.x,
  1572. y: fromPoint.y < toPoint.y ?
  1573. chartObstacles[i].yMin - 1 :
  1574. chartObstacles[i].yMax + 1,
  1575. obstacle: chartObstacles[i]
  1576. };
  1577. }
  1578. i += searchDirection;
  1579. }
  1580. return toPoint;
  1581. }
  1582. /**
  1583. * Decide in which direction to dodge or get out of an obstacle.
  1584. * Considers desired direction, which way is shortest, soft and hard
  1585. * bounds.
  1586. *
  1587. * (? Returns a string, either xMin, xMax, yMin or yMax.)
  1588. *
  1589. * @private
  1590. * @function
  1591. *
  1592. * @param {object} obstacle
  1593. * Obstacle to dodge/escape.
  1594. *
  1595. * @param {object} fromPoint
  1596. * Point with x/y props that's dodging/escaping.
  1597. *
  1598. * @param {object} toPoint
  1599. * Goal point.
  1600. *
  1601. * @param {boolean} dirIsX
  1602. * Dodge in X dimension.
  1603. *
  1604. * @param {object} bounds
  1605. * Hard and soft boundaries.
  1606. *
  1607. * @return {boolean}
  1608. * Use max or not.
  1609. */
  1610. function getDodgeDirection(obstacle, fromPoint, toPoint, dirIsX, bounds) {
  1611. var softBounds = bounds.soft, hardBounds = bounds.hard, dir = dirIsX ? 'x' : 'y', toPointMax = { x: fromPoint.x, y: fromPoint.y }, toPointMin = { x: fromPoint.x, y: fromPoint.y }, minPivot, maxPivot, maxOutOfSoftBounds = obstacle[dir + 'Max'] >=
  1612. softBounds[dir + 'Max'], minOutOfSoftBounds = obstacle[dir + 'Min'] <=
  1613. softBounds[dir + 'Min'], maxOutOfHardBounds = obstacle[dir + 'Max'] >=
  1614. hardBounds[dir + 'Max'], minOutOfHardBounds = obstacle[dir + 'Min'] <=
  1615. hardBounds[dir + 'Min'],
  1616. // Find out if we should prefer one direction over the other if
  1617. // we can choose freely
  1618. minDistance = abs(obstacle[dir + 'Min'] - fromPoint[dir]), maxDistance = abs(obstacle[dir + 'Max'] - fromPoint[dir]),
  1619. // If it's a small difference, pick the one leading towards dest
  1620. // point. Otherwise pick the shortest distance
  1621. useMax = abs(minDistance - maxDistance) < 10 ?
  1622. fromPoint[dir] < toPoint[dir] :
  1623. maxDistance < minDistance;
  1624. // Check if we hit any obstacles trying to go around in either
  1625. // direction.
  1626. toPointMin[dir] = obstacle[dir + 'Min'];
  1627. toPointMax[dir] = obstacle[dir + 'Max'];
  1628. minPivot = pivotPoint(fromPoint, toPointMin, dirIsX)[dir] !==
  1629. toPointMin[dir];
  1630. maxPivot = pivotPoint(fromPoint, toPointMax, dirIsX)[dir] !==
  1631. toPointMax[dir];
  1632. useMax = minPivot ?
  1633. (maxPivot ? useMax : true) :
  1634. (maxPivot ? false : useMax);
  1635. // useMax now contains our preferred choice, bounds not taken into
  1636. // account. If both or neither direction is out of bounds we want to
  1637. // use this.
  1638. // Deal with soft bounds
  1639. useMax = minOutOfSoftBounds ?
  1640. (maxOutOfSoftBounds ? useMax : true) : // Out on min
  1641. (maxOutOfSoftBounds ? false : useMax); // Not out on min
  1642. // Deal with hard bounds
  1643. useMax = minOutOfHardBounds ?
  1644. (maxOutOfHardBounds ? useMax : true) : // Out on min
  1645. (maxOutOfHardBounds ? false : useMax); // Not out on min
  1646. return useMax;
  1647. }
  1648. // eslint-disable-next-line valid-jsdoc
  1649. /**
  1650. * Find a clear path between point.
  1651. * @private
  1652. */
  1653. function clearPathTo(fromPoint, toPoint, dirIsX) {
  1654. // Don't waste time if we've hit goal
  1655. if (fromPoint.x === toPoint.x && fromPoint.y === toPoint.y) {
  1656. return [];
  1657. }
  1658. var dir = dirIsX ? 'x' : 'y',
  1659. pivot,
  1660. segments,
  1661. waypoint,
  1662. waypointUseMax,
  1663. envelopingObstacle,
  1664. secondEnvelopingObstacle,
  1665. envelopWaypoint,
  1666. obstacleMargin = options.obstacleOptions.margin,
  1667. bounds = {
  1668. soft: {
  1669. xMin: softMinX,
  1670. xMax: softMaxX,
  1671. yMin: softMinY,
  1672. yMax: softMaxY
  1673. },
  1674. hard: options.hardBounds
  1675. };
  1676. // If fromPoint is inside an obstacle we have a problem. Break out
  1677. // by just going to the outside of this obstacle. We prefer to go to
  1678. // the nearest edge in the chosen direction.
  1679. envelopingObstacle =
  1680. findObstacleFromPoint(chartObstacles, fromPoint);
  1681. if (envelopingObstacle > -1) {
  1682. envelopingObstacle = chartObstacles[envelopingObstacle];
  1683. waypointUseMax = getDodgeDirection(envelopingObstacle, fromPoint, toPoint, dirIsX, bounds);
  1684. // Cut obstacle to hard bounds to make sure we stay within
  1685. limitObstacleToBounds(envelopingObstacle, options.hardBounds);
  1686. envelopWaypoint = dirIsX ? {
  1687. y: fromPoint.y,
  1688. x: envelopingObstacle[waypointUseMax ? 'xMax' : 'xMin'] +
  1689. (waypointUseMax ? 1 : -1)
  1690. } : {
  1691. x: fromPoint.x,
  1692. y: envelopingObstacle[waypointUseMax ? 'yMax' : 'yMin'] +
  1693. (waypointUseMax ? 1 : -1)
  1694. };
  1695. // If we crashed into another obstacle doing this, we put the
  1696. // waypoint between them instead
  1697. secondEnvelopingObstacle = findObstacleFromPoint(chartObstacles, envelopWaypoint);
  1698. if (secondEnvelopingObstacle > -1) {
  1699. secondEnvelopingObstacle = chartObstacles[secondEnvelopingObstacle];
  1700. // Cut obstacle to hard bounds
  1701. limitObstacleToBounds(secondEnvelopingObstacle, options.hardBounds);
  1702. // Modify waypoint to lay between obstacles
  1703. envelopWaypoint[dir] = waypointUseMax ? max(envelopingObstacle[dir + 'Max'] - obstacleMargin + 1, (secondEnvelopingObstacle[dir + 'Min'] +
  1704. envelopingObstacle[dir + 'Max']) / 2) :
  1705. min((envelopingObstacle[dir + 'Min'] + obstacleMargin - 1), ((secondEnvelopingObstacle[dir + 'Max'] +
  1706. envelopingObstacle[dir + 'Min']) / 2));
  1707. // We are not going anywhere. If this happens for the first
  1708. // time, do nothing. Otherwise, try to go to the extreme of
  1709. // the obstacle pair in the current direction.
  1710. if (fromPoint.x === envelopWaypoint.x &&
  1711. fromPoint.y === envelopWaypoint.y) {
  1712. if (forceObstacleBreak) {
  1713. envelopWaypoint[dir] = waypointUseMax ?
  1714. max(envelopingObstacle[dir + 'Max'], secondEnvelopingObstacle[dir + 'Max']) + 1 :
  1715. min(envelopingObstacle[dir + 'Min'], secondEnvelopingObstacle[dir + 'Min']) - 1;
  1716. }
  1717. // Toggle on if off, and the opposite
  1718. forceObstacleBreak = !forceObstacleBreak;
  1719. }
  1720. else {
  1721. // This point is not identical to previous.
  1722. // Clear break trigger.
  1723. forceObstacleBreak = false;
  1724. }
  1725. }
  1726. segments = [{
  1727. start: fromPoint,
  1728. end: envelopWaypoint
  1729. }];
  1730. }
  1731. else { // If not enveloping, use standard pivot calculation
  1732. pivot = pivotPoint(fromPoint, {
  1733. x: dirIsX ? toPoint.x : fromPoint.x,
  1734. y: dirIsX ? fromPoint.y : toPoint.y
  1735. }, dirIsX);
  1736. segments = [{
  1737. start: fromPoint,
  1738. end: {
  1739. x: pivot.x,
  1740. y: pivot.y
  1741. }
  1742. }];
  1743. // Pivot before goal, use a waypoint to dodge obstacle
  1744. if (pivot[dirIsX ? 'x' : 'y'] !== toPoint[dirIsX ? 'x' : 'y']) {
  1745. // Find direction of waypoint
  1746. waypointUseMax = getDodgeDirection(pivot.obstacle, pivot, toPoint, !dirIsX, bounds);
  1747. // Cut waypoint to hard bounds
  1748. limitObstacleToBounds(pivot.obstacle, options.hardBounds);
  1749. waypoint = {
  1750. x: dirIsX ?
  1751. pivot.x :
  1752. pivot.obstacle[waypointUseMax ? 'xMax' : 'xMin'] +
  1753. (waypointUseMax ? 1 : -1),
  1754. y: dirIsX ?
  1755. pivot.obstacle[waypointUseMax ? 'yMax' : 'yMin'] +
  1756. (waypointUseMax ? 1 : -1) :
  1757. pivot.y
  1758. };
  1759. // We're changing direction here, store that to make sure we
  1760. // also change direction when adding the last segment array
  1761. // after handling waypoint.
  1762. dirIsX = !dirIsX;
  1763. segments = segments.concat(clearPathTo({
  1764. x: pivot.x,
  1765. y: pivot.y
  1766. }, waypoint, dirIsX));
  1767. }
  1768. }
  1769. // Get segments for the other direction too
  1770. // Recursion is our friend
  1771. segments = segments.concat(clearPathTo(segments[segments.length - 1].end, toPoint, !dirIsX));
  1772. return segments;
  1773. }
  1774. // eslint-disable-next-line valid-jsdoc
  1775. /**
  1776. * Extract point to outside of obstacle in whichever direction is
  1777. * closest. Returns new point outside obstacle.
  1778. * @private
  1779. */
  1780. function extractFromObstacle(obstacle, point, goalPoint) {
  1781. var dirIsX = min(obstacle.xMax - point.x,
  1782. point.x - obstacle.xMin) <
  1783. min(obstacle.yMax - point.y,
  1784. point.y - obstacle.yMin),
  1785. bounds = {
  1786. soft: options.hardBounds,
  1787. hard: options.hardBounds
  1788. },
  1789. useMax = getDodgeDirection(obstacle,
  1790. point,
  1791. goalPoint,
  1792. dirIsX,
  1793. bounds);
  1794. return dirIsX ? {
  1795. y: point.y,
  1796. x: obstacle[useMax ? 'xMax' : 'xMin'] + (useMax ? 1 : -1)
  1797. } : {
  1798. x: point.x,
  1799. y: obstacle[useMax ? 'yMax' : 'yMin'] + (useMax ? 1 : -1)
  1800. };
  1801. }
  1802. // Cut the obstacle array to soft bounds for optimization in large
  1803. // datasets.
  1804. chartObstacles =
  1805. chartObstacles.slice(startObstacleIx, endObstacleIx + 1);
  1806. // If an obstacle envelops the end point, move it out of there and add
  1807. // a little segment to where it was.
  1808. if ((endObstacleIx = findObstacleFromPoint(chartObstacles, end)) > -1) {
  1809. extractedEndPoint = extractFromObstacle(chartObstacles[endObstacleIx], end, start);
  1810. endSegments.push({
  1811. end: end,
  1812. start: extractedEndPoint
  1813. });
  1814. end = extractedEndPoint;
  1815. }
  1816. // If it's still inside one or more obstacles, get out of there by
  1817. // force-moving towards the start point.
  1818. while ((endObstacleIx = findObstacleFromPoint(chartObstacles, end)) > -1) {
  1819. useMax = end[dir] - start[dir] < 0;
  1820. extractedEndPoint = {
  1821. x: end.x,
  1822. y: end.y
  1823. };
  1824. extractedEndPoint[dir] = chartObstacles[endObstacleIx][useMax ? dir + 'Max' : dir + 'Min'] + (useMax ? 1 : -1);
  1825. endSegments.push({
  1826. end: end,
  1827. start: extractedEndPoint
  1828. });
  1829. end = extractedEndPoint;
  1830. }
  1831. // Find the path
  1832. segments = clearPathTo(start, end, dirIsX);
  1833. // Add the end-point segments
  1834. segments = segments.concat(endSegments.reverse());
  1835. return {
  1836. path: pathFromSegments(segments),
  1837. obstacles: segments
  1838. };
  1839. }, {
  1840. requiresObstacles: true
  1841. });
  1842. // Define the available pathfinding algorithms.
  1843. // Algorithms take up to 3 arguments: starting point, ending point, and an
  1844. // options object.
  1845. var algorithms = {
  1846. fastAvoid: fastAvoid,
  1847. straight: straight,
  1848. simpleConnect: simpleConnect
  1849. };
  1850. return algorithms;
  1851. });
  1852. _registerModule(_modules, 'Gantt/Pathfinder.js', [_modules['Gantt/Connection.js'], _modules['Core/Chart/Chart.js'], _modules['Core/Globals.js'], _modules['Core/Options.js'], _modules['Core/Series/Point.js'], _modules['Core/Utilities.js'], _modules['Gantt/PathfinderAlgorithms.js']], function (Connection, Chart, H, O, Point, U, pathfinderAlgorithms) {
  1853. /* *
  1854. *
  1855. * (c) 2016 Highsoft AS
  1856. * Authors: Øystein Moseng, Lars A. V. Cabrera
  1857. *
  1858. * License: www.highcharts.com/license
  1859. *
  1860. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  1861. *
  1862. * */
  1863. /**
  1864. * The default pathfinder algorithm to use for a chart. It is possible to define
  1865. * your own algorithms by adding them to the
  1866. * `Highcharts.Pathfinder.prototype.algorithms`
  1867. * object before the chart has been created.
  1868. *
  1869. * The default algorithms are as follows:
  1870. *
  1871. * `straight`: Draws a straight line between the connecting
  1872. * points. Does not avoid other points when drawing.
  1873. *
  1874. * `simpleConnect`: Finds a path between the points using right angles
  1875. * only. Takes only starting/ending points into
  1876. * account, and will not avoid other points.
  1877. *
  1878. * `fastAvoid`: Finds a path between the points using right angles
  1879. * only. Will attempt to avoid other points, but its
  1880. * focus is performance over accuracy. Works well with
  1881. * less dense datasets.
  1882. *
  1883. * @typedef {"fastAvoid"|"simpleConnect"|"straight"|string} Highcharts.PathfinderTypeValue
  1884. */
  1885. ''; // detach doclets above
  1886. var defaultOptions = O.defaultOptions;
  1887. var addEvent = U.addEvent,
  1888. defined = U.defined,
  1889. error = U.error,
  1890. extend = U.extend,
  1891. merge = U.merge,
  1892. objectEach = U.objectEach,
  1893. pick = U.pick,
  1894. splat = U.splat;
  1895. var deg2rad = H.deg2rad,
  1896. max = Math.max,
  1897. min = Math.min;
  1898. /*
  1899. @todo:
  1900. - Document how to write your own algorithms
  1901. - Consider adding a Point.pathTo method that wraps creating a connection
  1902. and rendering it
  1903. */
  1904. // Set default Pathfinder options
  1905. extend(defaultOptions, {
  1906. /**
  1907. * The Pathfinder module allows you to define connections between any two
  1908. * points, represented as lines - optionally with markers for the start
  1909. * and/or end points. Multiple algorithms are available for calculating how
  1910. * the connecting lines are drawn.
  1911. *
  1912. * Connector functionality requires Highcharts Gantt to be loaded. In Gantt
  1913. * charts, the connectors are used to draw dependencies between tasks.
  1914. *
  1915. * @see [dependency](series.gantt.data.dependency)
  1916. *
  1917. * @sample gantt/pathfinder/demo
  1918. * Pathfinder connections
  1919. *
  1920. * @declare Highcharts.ConnectorsOptions
  1921. * @product gantt
  1922. * @optionparent connectors
  1923. */
  1924. connectors: {
  1925. /**
  1926. * Enable connectors for this chart. Requires Highcharts Gantt.
  1927. *
  1928. * @type {boolean}
  1929. * @default true
  1930. * @since 6.2.0
  1931. * @apioption connectors.enabled
  1932. */
  1933. /**
  1934. * Set the default dash style for this chart's connecting lines.
  1935. *
  1936. * @type {string}
  1937. * @default solid
  1938. * @since 6.2.0
  1939. * @apioption connectors.dashStyle
  1940. */
  1941. /**
  1942. * Set the default color for this chart's Pathfinder connecting lines.
  1943. * Defaults to the color of the point being connected.
  1944. *
  1945. * @type {Highcharts.ColorString}
  1946. * @since 6.2.0
  1947. * @apioption connectors.lineColor
  1948. */
  1949. /**
  1950. * Set the default pathfinder margin to use, in pixels. Some Pathfinder
  1951. * algorithms attempt to avoid obstacles, such as other points in the
  1952. * chart. These algorithms use this margin to determine how close lines
  1953. * can be to an obstacle. The default is to compute this automatically
  1954. * from the size of the obstacles in the chart.
  1955. *
  1956. * To draw connecting lines close to existing points, set this to a low
  1957. * number. For more space around existing points, set this number
  1958. * higher.
  1959. *
  1960. * @sample gantt/pathfinder/algorithm-margin
  1961. * Small algorithmMargin
  1962. *
  1963. * @type {number}
  1964. * @since 6.2.0
  1965. * @apioption connectors.algorithmMargin
  1966. */
  1967. /**
  1968. * Set the default pathfinder algorithm to use for this chart. It is
  1969. * possible to define your own algorithms by adding them to the
  1970. * Highcharts.Pathfinder.prototype.algorithms object before the chart
  1971. * has been created.
  1972. *
  1973. * The default algorithms are as follows:
  1974. *
  1975. * `straight`: Draws a straight line between the connecting
  1976. * points. Does not avoid other points when drawing.
  1977. *
  1978. * `simpleConnect`: Finds a path between the points using right angles
  1979. * only. Takes only starting/ending points into
  1980. * account, and will not avoid other points.
  1981. *
  1982. * `fastAvoid`: Finds a path between the points using right angles
  1983. * only. Will attempt to avoid other points, but its
  1984. * focus is performance over accuracy. Works well with
  1985. * less dense datasets.
  1986. *
  1987. * Default value: `straight` is used as default for most series types,
  1988. * while `simpleConnect` is used as default for Gantt series, to show
  1989. * dependencies between points.
  1990. *
  1991. * @sample gantt/pathfinder/demo
  1992. * Different types used
  1993. *
  1994. * @type {Highcharts.PathfinderTypeValue}
  1995. * @default undefined
  1996. * @since 6.2.0
  1997. */
  1998. type: 'straight',
  1999. /**
  2000. * Set the default pixel width for this chart's Pathfinder connecting
  2001. * lines.
  2002. *
  2003. * @since 6.2.0
  2004. */
  2005. lineWidth: 1,
  2006. /**
  2007. * Marker options for this chart's Pathfinder connectors. Note that
  2008. * this option is overridden by the `startMarker` and `endMarker`
  2009. * options.
  2010. *
  2011. * @declare Highcharts.ConnectorsMarkerOptions
  2012. * @since 6.2.0
  2013. */
  2014. marker: {
  2015. /**
  2016. * Set the radius of the connector markers. The default is
  2017. * automatically computed based on the algorithmMargin setting.
  2018. *
  2019. * Setting marker.width and marker.height will override this
  2020. * setting.
  2021. *
  2022. * @type {number}
  2023. * @since 6.2.0
  2024. * @apioption connectors.marker.radius
  2025. */
  2026. /**
  2027. * Set the width of the connector markers. If not supplied, this
  2028. * is inferred from the marker radius.
  2029. *
  2030. * @type {number}
  2031. * @since 6.2.0
  2032. * @apioption connectors.marker.width
  2033. */
  2034. /**
  2035. * Set the height of the connector markers. If not supplied, this
  2036. * is inferred from the marker radius.
  2037. *
  2038. * @type {number}
  2039. * @since 6.2.0
  2040. * @apioption connectors.marker.height
  2041. */
  2042. /**
  2043. * Set the color of the connector markers. By default this is the
  2044. * same as the connector color.
  2045. *
  2046. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  2047. * @since 6.2.0
  2048. * @apioption connectors.marker.color
  2049. */
  2050. /**
  2051. * Set the line/border color of the connector markers. By default
  2052. * this is the same as the marker color.
  2053. *
  2054. * @type {Highcharts.ColorString}
  2055. * @since 6.2.0
  2056. * @apioption connectors.marker.lineColor
  2057. */
  2058. /**
  2059. * Enable markers for the connectors.
  2060. */
  2061. enabled: false,
  2062. /**
  2063. * Horizontal alignment of the markers relative to the points.
  2064. *
  2065. * @type {Highcharts.AlignValue}
  2066. */
  2067. align: 'center',
  2068. /**
  2069. * Vertical alignment of the markers relative to the points.
  2070. *
  2071. * @type {Highcharts.VerticalAlignValue}
  2072. */
  2073. verticalAlign: 'middle',
  2074. /**
  2075. * Whether or not to draw the markers inside the points.
  2076. */
  2077. inside: false,
  2078. /**
  2079. * Set the line/border width of the pathfinder markers.
  2080. */
  2081. lineWidth: 1
  2082. },
  2083. /**
  2084. * Marker options specific to the start markers for this chart's
  2085. * Pathfinder connectors. Overrides the generic marker options.
  2086. *
  2087. * @declare Highcharts.ConnectorsStartMarkerOptions
  2088. * @extends connectors.marker
  2089. * @since 6.2.0
  2090. */
  2091. startMarker: {
  2092. /**
  2093. * Set the symbol of the connector start markers.
  2094. */
  2095. symbol: 'diamond'
  2096. },
  2097. /**
  2098. * Marker options specific to the end markers for this chart's
  2099. * Pathfinder connectors. Overrides the generic marker options.
  2100. *
  2101. * @declare Highcharts.ConnectorsEndMarkerOptions
  2102. * @extends connectors.marker
  2103. * @since 6.2.0
  2104. */
  2105. endMarker: {
  2106. /**
  2107. * Set the symbol of the connector end markers.
  2108. */
  2109. symbol: 'arrow-filled'
  2110. }
  2111. }
  2112. });
  2113. /**
  2114. * Override Pathfinder connector options for a series. Requires Highcharts Gantt
  2115. * to be loaded.
  2116. *
  2117. * @declare Highcharts.SeriesConnectorsOptionsObject
  2118. * @extends connectors
  2119. * @since 6.2.0
  2120. * @excluding enabled, algorithmMargin
  2121. * @product gantt
  2122. * @apioption plotOptions.series.connectors
  2123. */
  2124. /**
  2125. * Connect to a point. This option can be either a string, referring to the ID
  2126. * of another point, or an object, or an array of either. If the option is an
  2127. * array, each element defines a connection.
  2128. *
  2129. * @sample gantt/pathfinder/demo
  2130. * Different connection types
  2131. *
  2132. * @declare Highcharts.XrangePointConnectorsOptionsObject
  2133. * @type {string|Array<string|*>|*}
  2134. * @extends plotOptions.series.connectors
  2135. * @since 6.2.0
  2136. * @excluding enabled
  2137. * @product gantt
  2138. * @requires highcharts-gantt
  2139. * @apioption series.xrange.data.connect
  2140. */
  2141. /**
  2142. * The ID of the point to connect to.
  2143. *
  2144. * @type {string}
  2145. * @since 6.2.0
  2146. * @product gantt
  2147. * @apioption series.xrange.data.connect.to
  2148. */
  2149. /**
  2150. * Get point bounding box using plotX/plotY and shapeArgs. If using
  2151. * graphic.getBBox() directly, the bbox will be affected by animation.
  2152. *
  2153. * @private
  2154. * @function
  2155. *
  2156. * @param {Highcharts.Point} point
  2157. * The point to get BB of.
  2158. *
  2159. * @return {Highcharts.Dictionary<number>|null}
  2160. * Result xMax, xMin, yMax, yMin.
  2161. */
  2162. function getPointBB(point) {
  2163. var shapeArgs = point.shapeArgs,
  2164. bb;
  2165. // Prefer using shapeArgs (columns)
  2166. if (shapeArgs) {
  2167. return {
  2168. xMin: shapeArgs.x,
  2169. xMax: shapeArgs.x + shapeArgs.width,
  2170. yMin: shapeArgs.y,
  2171. yMax: shapeArgs.y + shapeArgs.height
  2172. };
  2173. }
  2174. // Otherwise use plotX/plotY and bb
  2175. bb = point.graphic && point.graphic.getBBox();
  2176. return bb ? {
  2177. xMin: point.plotX - bb.width / 2,
  2178. xMax: point.plotX + bb.width / 2,
  2179. yMin: point.plotY - bb.height / 2,
  2180. yMax: point.plotY + bb.height / 2
  2181. } : null;
  2182. }
  2183. /**
  2184. * Calculate margin to place around obstacles for the pathfinder in pixels.
  2185. * Returns a minimum of 1 pixel margin.
  2186. *
  2187. * @private
  2188. * @function
  2189. *
  2190. * @param {Array<object>} obstacles
  2191. * Obstacles to calculate margin from.
  2192. *
  2193. * @return {number}
  2194. * The calculated margin in pixels. At least 1.
  2195. */
  2196. function calculateObstacleMargin(obstacles) {
  2197. var len = obstacles.length,
  2198. i = 0,
  2199. j,
  2200. obstacleDistance,
  2201. distances = [],
  2202. // Compute smallest distance between two rectangles
  2203. distance = function (a,
  2204. b,
  2205. bbMargin) {
  2206. // Count the distance even if we are slightly off
  2207. var margin = pick(bbMargin, 10),
  2208. yOverlap = a.yMax + margin > b.yMin - margin &&
  2209. a.yMin - margin < b.yMax + margin,
  2210. xOverlap = a.xMax + margin > b.xMin - margin &&
  2211. a.xMin - margin < b.xMax + margin,
  2212. xDistance = yOverlap ? (a.xMin > b.xMax ? a.xMin - b.xMax : b.xMin - a.xMax) : Infinity,
  2213. yDistance = xOverlap ? (a.yMin > b.yMax ? a.yMin - b.yMax : b.yMin - a.yMax) : Infinity;
  2214. // If the rectangles collide, try recomputing with smaller margin.
  2215. // If they collide anyway, discard the obstacle.
  2216. if (xOverlap && yOverlap) {
  2217. return (margin ?
  2218. distance(a, b, Math.floor(margin / 2)) :
  2219. Infinity);
  2220. }
  2221. return min(xDistance, yDistance);
  2222. };
  2223. // Go over all obstacles and compare them to the others.
  2224. for (; i < len; ++i) {
  2225. // Compare to all obstacles ahead. We will already have compared this
  2226. // obstacle to the ones before.
  2227. for (j = i + 1; j < len; ++j) {
  2228. obstacleDistance = distance(obstacles[i], obstacles[j]);
  2229. // TODO: Magic number 80
  2230. if (obstacleDistance < 80) { // Ignore large distances
  2231. distances.push(obstacleDistance);
  2232. }
  2233. }
  2234. }
  2235. // Ensure we always have at least one value, even in very spaceous charts
  2236. distances.push(80);
  2237. return max(Math.floor(distances.sort(function (a, b) {
  2238. return (a - b);
  2239. })[
  2240. // Discard first 10% of the relevant distances, and then grab
  2241. // the smallest one.
  2242. Math.floor(distances.length / 10)] / 2 - 1 // Divide the distance by 2 and subtract 1.
  2243. ), 1 // 1 is the minimum margin
  2244. );
  2245. }
  2246. /* eslint-disable no-invalid-this, valid-jsdoc */
  2247. /**
  2248. * The Pathfinder class.
  2249. *
  2250. * @private
  2251. * @class
  2252. * @name Highcharts.Pathfinder
  2253. *
  2254. * @param {Highcharts.Chart} chart
  2255. * The chart to operate on.
  2256. */
  2257. var Pathfinder = /** @class */ (function () {
  2258. function Pathfinder(chart) {
  2259. /* *
  2260. *
  2261. * Properties
  2262. *
  2263. * */
  2264. this.chart = void 0;
  2265. this.chartObstacles = void 0;
  2266. this.chartObstacleMetrics = void 0;
  2267. this.connections = void 0;
  2268. this.group = void 0;
  2269. this.lineObstacles = void 0;
  2270. this.init(chart);
  2271. }
  2272. /**
  2273. * @name Highcharts.Pathfinder#algorithms
  2274. * @type {Highcharts.Dictionary<Function>}
  2275. */
  2276. /**
  2277. * Initialize the Pathfinder object.
  2278. *
  2279. * @function Highcharts.Pathfinder#init
  2280. *
  2281. * @param {Highcharts.Chart} chart
  2282. * The chart context.
  2283. */
  2284. Pathfinder.prototype.init = function (chart) {
  2285. // Initialize pathfinder with chart context
  2286. this.chart = chart;
  2287. // Init connection reference list
  2288. this.connections = [];
  2289. // Recalculate paths/obstacles on chart redraw
  2290. addEvent(chart, 'redraw', function () {
  2291. this.pathfinder.update();
  2292. });
  2293. };
  2294. /**
  2295. * Update Pathfinder connections from scratch.
  2296. *
  2297. * @function Highcharts.Pathfinder#update
  2298. *
  2299. * @param {boolean} [deferRender]
  2300. * Whether or not to defer rendering of connections until
  2301. * series.afterAnimate event has fired. Used on first render.
  2302. */
  2303. Pathfinder.prototype.update = function (deferRender) {
  2304. var chart = this.chart,
  2305. pathfinder = this,
  2306. oldConnections = pathfinder.connections;
  2307. // Rebuild pathfinder connections from options
  2308. pathfinder.connections = [];
  2309. chart.series.forEach(function (series) {
  2310. if (series.visible && !series.options.isInternal) {
  2311. series.points.forEach(function (point) {
  2312. var to,
  2313. connects = (point.options &&
  2314. point.options.connect &&
  2315. splat(point.options.connect));
  2316. if (point.visible && point.isInside !== false && connects) {
  2317. connects.forEach(function (connect) {
  2318. to = chart.get(typeof connect === 'string' ?
  2319. connect : connect.to);
  2320. if (to instanceof Point &&
  2321. to.series.visible &&
  2322. to.visible &&
  2323. to.isInside !== false) {
  2324. // Add new connection
  2325. pathfinder.connections.push(new Connection(point, // from
  2326. to, typeof connect === 'string' ?
  2327. {} :
  2328. connect));
  2329. }
  2330. });
  2331. }
  2332. });
  2333. }
  2334. });
  2335. // Clear connections that should not be updated, and move old info over
  2336. // to new connections.
  2337. for (var j = 0, k, found, lenOld = oldConnections.length, lenNew = pathfinder.connections.length; j < lenOld; ++j) {
  2338. found = false;
  2339. for (k = 0; k < lenNew; ++k) {
  2340. if (oldConnections[j].fromPoint ===
  2341. pathfinder.connections[k].fromPoint &&
  2342. oldConnections[j].toPoint ===
  2343. pathfinder.connections[k].toPoint) {
  2344. pathfinder.connections[k].graphics =
  2345. oldConnections[j].graphics;
  2346. found = true;
  2347. break;
  2348. }
  2349. }
  2350. if (!found) {
  2351. oldConnections[j].destroy();
  2352. }
  2353. }
  2354. // Clear obstacles to force recalculation. This must be done on every
  2355. // redraw in case positions have changed. Recalculation is handled in
  2356. // Connection.getPath on demand.
  2357. delete this.chartObstacles;
  2358. delete this.lineObstacles;
  2359. // Draw the pending connections
  2360. pathfinder.renderConnections(deferRender);
  2361. };
  2362. /**
  2363. * Draw the chart's connecting paths.
  2364. *
  2365. * @function Highcharts.Pathfinder#renderConnections
  2366. *
  2367. * @param {boolean} [deferRender]
  2368. * Whether or not to defer render until series animation is finished.
  2369. * Used on first render.
  2370. */
  2371. Pathfinder.prototype.renderConnections = function (deferRender) {
  2372. if (deferRender) {
  2373. // Render after series are done animating
  2374. this.chart.series.forEach(function (series) {
  2375. var render = function () {
  2376. // Find pathfinder connections belonging to this series
  2377. // that haven't rendered, and render them now.
  2378. var pathfinder = series.chart.pathfinder,
  2379. conns = pathfinder && pathfinder.connections || [];
  2380. conns.forEach(function (connection) {
  2381. if (connection.fromPoint &&
  2382. connection.fromPoint.series === series) {
  2383. connection.render();
  2384. }
  2385. });
  2386. if (series.pathfinderRemoveRenderEvent) {
  2387. series.pathfinderRemoveRenderEvent();
  2388. delete series.pathfinderRemoveRenderEvent;
  2389. }
  2390. };
  2391. if (series.options.animation === false) {
  2392. render();
  2393. }
  2394. else {
  2395. series.pathfinderRemoveRenderEvent = addEvent(series, 'afterAnimate', render);
  2396. }
  2397. });
  2398. }
  2399. else {
  2400. // Go through connections and render them
  2401. this.connections.forEach(function (connection) {
  2402. connection.render();
  2403. });
  2404. }
  2405. };
  2406. /**
  2407. * Get obstacles for the points in the chart. Does not include connecting
  2408. * lines from Pathfinder. Applies algorithmMargin to the obstacles.
  2409. *
  2410. * @function Highcharts.Pathfinder#getChartObstacles
  2411. *
  2412. * @param {object} options
  2413. * Options for the calculation. Currenlty only
  2414. * options.algorithmMargin.
  2415. *
  2416. * @return {Array<object>}
  2417. * An array of calculated obstacles. Each obstacle is defined as an
  2418. * object with xMin, xMax, yMin and yMax properties.
  2419. */
  2420. Pathfinder.prototype.getChartObstacles = function (options) {
  2421. var obstacles = [],
  2422. series = this.chart.series,
  2423. margin = pick(options.algorithmMargin, 0),
  2424. calculatedMargin;
  2425. for (var i = 0, sLen = series.length; i < sLen; ++i) {
  2426. if (series[i].visible && !series[i].options.isInternal) {
  2427. for (var j = 0, pLen = series[i].points.length, bb, point; j < pLen; ++j) {
  2428. point = series[i].points[j];
  2429. if (point.visible) {
  2430. bb = getPointBB(point);
  2431. if (bb) {
  2432. obstacles.push({
  2433. xMin: bb.xMin - margin,
  2434. xMax: bb.xMax + margin,
  2435. yMin: bb.yMin - margin,
  2436. yMax: bb.yMax + margin
  2437. });
  2438. }
  2439. }
  2440. }
  2441. }
  2442. }
  2443. // Sort obstacles by xMin for optimization
  2444. obstacles = obstacles.sort(function (a, b) {
  2445. return a.xMin - b.xMin;
  2446. });
  2447. // Add auto-calculated margin if the option is not defined
  2448. if (!defined(options.algorithmMargin)) {
  2449. calculatedMargin =
  2450. options.algorithmMargin =
  2451. calculateObstacleMargin(obstacles);
  2452. obstacles.forEach(function (obstacle) {
  2453. obstacle.xMin -= calculatedMargin;
  2454. obstacle.xMax += calculatedMargin;
  2455. obstacle.yMin -= calculatedMargin;
  2456. obstacle.yMax += calculatedMargin;
  2457. });
  2458. }
  2459. return obstacles;
  2460. };
  2461. /**
  2462. * Utility function to get metrics for obstacles:
  2463. * - Widest obstacle width
  2464. * - Tallest obstacle height
  2465. *
  2466. * @function Highcharts.Pathfinder#getObstacleMetrics
  2467. *
  2468. * @param {Array<object>} obstacles
  2469. * An array of obstacles to inspect.
  2470. *
  2471. * @return {object}
  2472. * The calculated metrics, as an object with maxHeight and maxWidth
  2473. * properties.
  2474. */
  2475. Pathfinder.prototype.getObstacleMetrics = function (obstacles) {
  2476. var maxWidth = 0,
  2477. maxHeight = 0,
  2478. width,
  2479. height,
  2480. i = obstacles.length;
  2481. while (i--) {
  2482. width = obstacles[i].xMax - obstacles[i].xMin;
  2483. height = obstacles[i].yMax - obstacles[i].yMin;
  2484. if (maxWidth < width) {
  2485. maxWidth = width;
  2486. }
  2487. if (maxHeight < height) {
  2488. maxHeight = height;
  2489. }
  2490. }
  2491. return {
  2492. maxHeight: maxHeight,
  2493. maxWidth: maxWidth
  2494. };
  2495. };
  2496. /**
  2497. * Utility to get which direction to start the pathfinding algorithm
  2498. * (X vs Y), calculated from a set of marker options.
  2499. *
  2500. * @function Highcharts.Pathfinder#getAlgorithmStartDirection
  2501. *
  2502. * @param {Highcharts.ConnectorsMarkerOptions} markerOptions
  2503. * Marker options to calculate from.
  2504. *
  2505. * @return {boolean}
  2506. * Returns true for X, false for Y, and undefined for autocalculate.
  2507. */
  2508. Pathfinder.prototype.getAlgorithmStartDirection = function (markerOptions) {
  2509. var xCenter = markerOptions.align !== 'left' &&
  2510. markerOptions.align !== 'right', yCenter = markerOptions.verticalAlign !== 'top' &&
  2511. markerOptions.verticalAlign !== 'bottom', undef;
  2512. return xCenter ?
  2513. (yCenter ? undef : false) : // x is centered
  2514. (yCenter ? true : undef); // x is off-center
  2515. };
  2516. return Pathfinder;
  2517. }());
  2518. Pathfinder.prototype.algorithms = pathfinderAlgorithms;
  2519. // Add to Highcharts namespace
  2520. H.Pathfinder = Pathfinder;
  2521. // Add pathfinding capabilities to Points
  2522. extend(Point.prototype, /** @lends Point.prototype */ {
  2523. /**
  2524. * Get coordinates of anchor point for pathfinder connection.
  2525. *
  2526. * @private
  2527. * @function Highcharts.Point#getPathfinderAnchorPoint
  2528. *
  2529. * @param {Highcharts.ConnectorsMarkerOptions} markerOptions
  2530. * Connection options for position on point.
  2531. *
  2532. * @return {Highcharts.PositionObject}
  2533. * An object with x/y properties for the position. Coordinates are
  2534. * in plot values, not relative to point.
  2535. */
  2536. getPathfinderAnchorPoint: function (markerOptions) {
  2537. var bb = getPointBB(this),
  2538. x,
  2539. y;
  2540. switch (markerOptions.align) { // eslint-disable-line default-case
  2541. case 'right':
  2542. x = 'xMax';
  2543. break;
  2544. case 'left':
  2545. x = 'xMin';
  2546. }
  2547. switch (markerOptions.verticalAlign) { // eslint-disable-line default-case
  2548. case 'top':
  2549. y = 'yMin';
  2550. break;
  2551. case 'bottom':
  2552. y = 'yMax';
  2553. }
  2554. return {
  2555. x: x ? bb[x] : (bb.xMin + bb.xMax) / 2,
  2556. y: y ? bb[y] : (bb.yMin + bb.yMax) / 2
  2557. };
  2558. },
  2559. /**
  2560. * Utility to get the angle from one point to another.
  2561. *
  2562. * @private
  2563. * @function Highcharts.Point#getRadiansToVector
  2564. *
  2565. * @param {Highcharts.PositionObject} v1
  2566. * The first vector, as an object with x/y properties.
  2567. *
  2568. * @param {Highcharts.PositionObject} v2
  2569. * The second vector, as an object with x/y properties.
  2570. *
  2571. * @return {number}
  2572. * The angle in degrees
  2573. */
  2574. getRadiansToVector: function (v1, v2) {
  2575. var box;
  2576. if (!defined(v2)) {
  2577. box = getPointBB(this);
  2578. if (box) {
  2579. v2 = {
  2580. x: (box.xMin + box.xMax) / 2,
  2581. y: (box.yMin + box.yMax) / 2
  2582. };
  2583. }
  2584. }
  2585. return Math.atan2(v2.y - v1.y, v1.x - v2.x);
  2586. },
  2587. /**
  2588. * Utility to get the position of the marker, based on the path angle and
  2589. * the marker's radius.
  2590. *
  2591. * @private
  2592. * @function Highcharts.Point#getMarkerVector
  2593. *
  2594. * @param {number} radians
  2595. * The angle in radians from the point center to another vector.
  2596. *
  2597. * @param {number} markerRadius
  2598. * The radius of the marker, to calculate the additional distance to
  2599. * the center of the marker.
  2600. *
  2601. * @param {object} anchor
  2602. * The anchor point of the path and marker as an object with x/y
  2603. * properties.
  2604. *
  2605. * @return {object}
  2606. * The marker vector as an object with x/y properties.
  2607. */
  2608. getMarkerVector: function (radians, markerRadius, anchor) {
  2609. var twoPI = Math.PI * 2.0,
  2610. theta = radians,
  2611. bb = getPointBB(this),
  2612. rectWidth = bb.xMax - bb.xMin,
  2613. rectHeight = bb.yMax - bb.yMin,
  2614. rAtan = Math.atan2(rectHeight,
  2615. rectWidth),
  2616. tanTheta = 1,
  2617. leftOrRightRegion = false,
  2618. rectHalfWidth = rectWidth / 2.0,
  2619. rectHalfHeight = rectHeight / 2.0,
  2620. rectHorizontalCenter = bb.xMin + rectHalfWidth,
  2621. rectVerticalCenter = bb.yMin + rectHalfHeight,
  2622. edgePoint = {
  2623. x: rectHorizontalCenter,
  2624. y: rectVerticalCenter
  2625. },
  2626. markerPoint = {},
  2627. xFactor = 1,
  2628. yFactor = 1;
  2629. while (theta < -Math.PI) {
  2630. theta += twoPI;
  2631. }
  2632. while (theta > Math.PI) {
  2633. theta -= twoPI;
  2634. }
  2635. tanTheta = Math.tan(theta);
  2636. if ((theta > -rAtan) && (theta <= rAtan)) {
  2637. // Right side
  2638. yFactor = -1;
  2639. leftOrRightRegion = true;
  2640. }
  2641. else if (theta > rAtan && theta <= (Math.PI - rAtan)) {
  2642. // Top side
  2643. yFactor = -1;
  2644. }
  2645. else if (theta > (Math.PI - rAtan) || theta <= -(Math.PI - rAtan)) {
  2646. // Left side
  2647. xFactor = -1;
  2648. leftOrRightRegion = true;
  2649. }
  2650. else {
  2651. // Bottom side
  2652. xFactor = -1;
  2653. }
  2654. // Correct the edgePoint according to the placement of the marker
  2655. if (leftOrRightRegion) {
  2656. edgePoint.x += xFactor * (rectHalfWidth);
  2657. edgePoint.y += yFactor * (rectHalfWidth) * tanTheta;
  2658. }
  2659. else {
  2660. edgePoint.x += xFactor * (rectHeight / (2.0 * tanTheta));
  2661. edgePoint.y += yFactor * (rectHalfHeight);
  2662. }
  2663. if (anchor.x !== rectHorizontalCenter) {
  2664. edgePoint.x = anchor.x;
  2665. }
  2666. if (anchor.y !== rectVerticalCenter) {
  2667. edgePoint.y = anchor.y;
  2668. }
  2669. markerPoint.x = edgePoint.x + (markerRadius * Math.cos(theta));
  2670. markerPoint.y = edgePoint.y - (markerRadius * Math.sin(theta));
  2671. return markerPoint;
  2672. }
  2673. });
  2674. /**
  2675. * Warn if using legacy options. Copy the options over. Note that this will
  2676. * still break if using the legacy options in chart.update, addSeries etc.
  2677. * @private
  2678. */
  2679. function warnLegacy(chart) {
  2680. if (chart.options.pathfinder ||
  2681. chart.series.reduce(function (acc, series) {
  2682. if (series.options) {
  2683. merge(true, (series.options.connectors = series.options.connectors ||
  2684. {}), series.options.pathfinder);
  2685. }
  2686. return acc || series.options && series.options.pathfinder;
  2687. }, false)) {
  2688. merge(true, (chart.options.connectors = chart.options.connectors || {}), chart.options.pathfinder);
  2689. error('WARNING: Pathfinder options have been renamed. ' +
  2690. 'Use "chart.connectors" or "series.connectors" instead.');
  2691. }
  2692. }
  2693. // Initialize Pathfinder for charts
  2694. Chart.prototype.callbacks.push(function (chart) {
  2695. var options = chart.options;
  2696. if (options.connectors.enabled !== false) {
  2697. warnLegacy(chart);
  2698. this.pathfinder = new Pathfinder(this);
  2699. this.pathfinder.update(true); // First draw, defer render
  2700. }
  2701. });
  2702. return Pathfinder;
  2703. });
  2704. _registerModule(_modules, 'masters/modules/pathfinder.src.js', [], function () {
  2705. });
  2706. }));