pathfinder.src.js 113 KB

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