pathfinder.src.js 114 KB

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