pathfinder.src.js 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  1. /**
  2. * @license Highcharts Gantt JS v8.1.2 (2020-06-16)
  3. *
  4. * Pathfinder
  5. *
  6. * (c) 2016-2019 Øystein Moseng
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/pathfinder', ['highcharts'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'parts-gantt/PathfinderAlgorithms.js', [_modules['parts/Utilities.js']], function (U) {
  32. /* *
  33. *
  34. * (c) 2016 Highsoft AS
  35. * Author: Øystein Moseng
  36. *
  37. * License: www.highcharts.com/license
  38. *
  39. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  40. *
  41. * */
  42. var extend = U.extend, pick = U.pick;
  43. var min = Math.min, max = Math.max, abs = Math.abs;
  44. /**
  45. * Get index of last obstacle before xMin. Employs a type of binary search, and
  46. * thus requires that obstacles are sorted by xMin value.
  47. *
  48. * @private
  49. * @function findLastObstacleBefore
  50. *
  51. * @param {Array<object>} obstacles
  52. * Array of obstacles to search in.
  53. *
  54. * @param {number} xMin
  55. * The xMin threshold.
  56. *
  57. * @param {number} [startIx]
  58. * Starting index to search from. Must be within array range.
  59. *
  60. * @return {number}
  61. * The index of the last obstacle element before xMin.
  62. */
  63. function findLastObstacleBefore(obstacles, xMin, startIx) {
  64. var left = startIx || 0, // left limit
  65. right = obstacles.length - 1, // right limit
  66. min = xMin - 0.0000001, // Make sure we include all obstacles at xMin
  67. cursor, cmp;
  68. while (left <= right) {
  69. cursor = (right + left) >> 1;
  70. cmp = min - obstacles[cursor].xMin;
  71. if (cmp > 0) {
  72. left = cursor + 1;
  73. }
  74. else if (cmp < 0) {
  75. right = cursor - 1;
  76. }
  77. else {
  78. return cursor;
  79. }
  80. }
  81. return left > 0 ? left - 1 : 0;
  82. }
  83. /**
  84. * Test if a point lays within an obstacle.
  85. *
  86. * @private
  87. * @function pointWithinObstacle
  88. *
  89. * @param {object} obstacle
  90. * Obstacle to test.
  91. *
  92. * @param {Highcharts.Point} point
  93. * Point with x/y props.
  94. *
  95. * @return {boolean}
  96. * Whether point is within the obstacle or not.
  97. */
  98. function pointWithinObstacle(obstacle, point) {
  99. return (point.x <= obstacle.xMax &&
  100. point.x >= obstacle.xMin &&
  101. point.y <= obstacle.yMax &&
  102. point.y >= obstacle.yMin);
  103. }
  104. /**
  105. * Find the index of an obstacle that wraps around a point.
  106. * Returns -1 if not found.
  107. *
  108. * @private
  109. * @function findObstacleFromPoint
  110. *
  111. * @param {Array<object>} obstacles
  112. * Obstacles to test.
  113. *
  114. * @param {Highcharts.Point} point
  115. * Point with x/y props.
  116. *
  117. * @return {number}
  118. * Ix of the obstacle in the array, or -1 if not found.
  119. */
  120. function findObstacleFromPoint(obstacles, point) {
  121. var i = findLastObstacleBefore(obstacles, point.x + 1) + 1;
  122. while (i--) {
  123. if (obstacles[i].xMax >= point.x &&
  124. // optimization using lazy evaluation
  125. pointWithinObstacle(obstacles[i], point)) {
  126. return i;
  127. }
  128. }
  129. return -1;
  130. }
  131. /**
  132. * Get SVG path array from array of line segments.
  133. *
  134. * @private
  135. * @function pathFromSegments
  136. *
  137. * @param {Array<object>} segments
  138. * The segments to build the path from.
  139. *
  140. * @return {Highcharts.SVGPathArray}
  141. * SVG path array as accepted by the SVG Renderer.
  142. */
  143. function pathFromSegments(segments) {
  144. var path = [];
  145. if (segments.length) {
  146. path.push(['M', segments[0].start.x, segments[0].start.y]);
  147. for (var i = 0; i < segments.length; ++i) {
  148. path.push(['L', segments[i].end.x, segments[i].end.y]);
  149. }
  150. }
  151. return path;
  152. }
  153. /**
  154. * Limits obstacle max/mins in all directions to bounds. Modifies input
  155. * obstacle.
  156. *
  157. * @private
  158. * @function limitObstacleToBounds
  159. *
  160. * @param {object} obstacle
  161. * Obstacle to limit.
  162. *
  163. * @param {object} bounds
  164. * Bounds to use as limit.
  165. *
  166. * @return {void}
  167. */
  168. function limitObstacleToBounds(obstacle, bounds) {
  169. obstacle.yMin = max(obstacle.yMin, bounds.yMin);
  170. obstacle.yMax = min(obstacle.yMax, bounds.yMax);
  171. obstacle.xMin = max(obstacle.xMin, bounds.xMin);
  172. obstacle.xMax = min(obstacle.xMax, bounds.xMax);
  173. }
  174. // Define the available pathfinding algorithms.
  175. // Algorithms take up to 3 arguments: starting point, ending point, and an
  176. // options object.
  177. var algorithms = {
  178. /**
  179. * Get an SVG path from a starting coordinate to an ending coordinate.
  180. * Draws a straight line.
  181. *
  182. * @function Highcharts.Pathfinder.algorithms.straight
  183. *
  184. * @param {Highcharts.PositionObject} start
  185. * Starting coordinate, object with x/y props.
  186. *
  187. * @param {Highcharts.PositionObject} end
  188. * Ending coordinate, object with x/y props.
  189. *
  190. * @return {object}
  191. * An object with the SVG path in Array form as accepted by the SVG
  192. * renderer, as well as an array of new obstacles making up this
  193. * path.
  194. */
  195. straight: function (start, end) {
  196. return {
  197. path: [
  198. ['M', start.x, start.y],
  199. ['L', end.x, end.y]
  200. ],
  201. obstacles: [{ start: start, end: end }]
  202. };
  203. },
  204. /**
  205. * Find a path from a starting coordinate to an ending coordinate, using
  206. * right angles only, and taking only starting/ending obstacle into
  207. * consideration.
  208. *
  209. * @function Highcharts.Pathfinder.algorithms.simpleConnect
  210. *
  211. * @param {Highcharts.PositionObject} start
  212. * Starting coordinate, object with x/y props.
  213. *
  214. * @param {Highcharts.PositionObject} end
  215. * Ending coordinate, object with x/y props.
  216. *
  217. * @param {object} options
  218. * Options for the algorithm:
  219. * - chartObstacles: Array of chart obstacles to avoid
  220. * - startDirectionX: Optional. True if starting in the X direction.
  221. * If not provided, the algorithm starts in the direction that is
  222. * the furthest between start/end.
  223. *
  224. * @return {object}
  225. * An object with the SVG path in Array form as accepted by the SVG
  226. * renderer, as well as an array of new obstacles making up this
  227. * path.
  228. */
  229. simpleConnect: extend(function (start, end, options) {
  230. var segments = [], endSegment, dir = pick(options.startDirectionX, abs(end.x - start.x) > abs(end.y - start.y)) ? 'x' : 'y', chartObstacles = options.chartObstacles, startObstacleIx = findObstacleFromPoint(chartObstacles, start), endObstacleIx = findObstacleFromPoint(chartObstacles, end), startObstacle, endObstacle, prevWaypoint, waypoint, waypoint2, useMax, endPoint;
  231. // eslint-disable-next-line valid-jsdoc
  232. /**
  233. * Return a clone of a point with a property set from a target object,
  234. * optionally with an offset
  235. * @private
  236. */
  237. function copyFromPoint(from, fromKey, to, toKey, offset) {
  238. var point = {
  239. x: from.x,
  240. y: from.y
  241. };
  242. point[fromKey] = to[toKey || fromKey] + (offset || 0);
  243. return point;
  244. }
  245. // eslint-disable-next-line valid-jsdoc
  246. /**
  247. * Return waypoint outside obstacle.
  248. * @private
  249. */
  250. function getMeOut(obstacle, point, direction) {
  251. var useMax = abs(point[direction] - obstacle[direction + 'Min']) >
  252. abs(point[direction] - obstacle[direction + 'Max']);
  253. return copyFromPoint(point, direction, obstacle, direction + (useMax ? 'Max' : 'Min'), useMax ? 1 : -1);
  254. }
  255. // Pull out end point
  256. if (endObstacleIx > -1) {
  257. endObstacle = chartObstacles[endObstacleIx];
  258. waypoint = getMeOut(endObstacle, end, dir);
  259. endSegment = {
  260. start: waypoint,
  261. end: end
  262. };
  263. endPoint = waypoint;
  264. }
  265. else {
  266. endPoint = end;
  267. }
  268. // If an obstacle envelops the start point, add a segment to get out,
  269. // and around it.
  270. if (startObstacleIx > -1) {
  271. startObstacle = chartObstacles[startObstacleIx];
  272. waypoint = getMeOut(startObstacle, start, dir);
  273. segments.push({
  274. start: start,
  275. end: waypoint
  276. });
  277. // If we are going back again, switch direction to get around start
  278. // obstacle.
  279. if (
  280. // Going towards max from start:
  281. waypoint[dir] >= start[dir] ===
  282. // Going towards min to end:
  283. waypoint[dir] >= endPoint[dir]) {
  284. dir = dir === 'y' ? 'x' : 'y';
  285. useMax = start[dir] < end[dir];
  286. segments.push({
  287. start: waypoint,
  288. end: copyFromPoint(waypoint, dir, startObstacle, dir + (useMax ? 'Max' : 'Min'), useMax ? 1 : -1)
  289. });
  290. // Switch direction again
  291. dir = dir === 'y' ? 'x' : 'y';
  292. }
  293. }
  294. // We are around the start obstacle. Go towards the end in one
  295. // direction.
  296. prevWaypoint = segments.length ?
  297. segments[segments.length - 1].end :
  298. start;
  299. waypoint = copyFromPoint(prevWaypoint, dir, endPoint);
  300. segments.push({
  301. start: prevWaypoint,
  302. end: waypoint
  303. });
  304. // Final run to end point in the other direction
  305. dir = dir === 'y' ? 'x' : 'y';
  306. waypoint2 = copyFromPoint(waypoint, dir, endPoint);
  307. segments.push({
  308. start: waypoint,
  309. end: waypoint2
  310. });
  311. // Finally add the endSegment
  312. segments.push(endSegment);
  313. return {
  314. path: pathFromSegments(segments),
  315. obstacles: segments
  316. };
  317. }, {
  318. requiresObstacles: true
  319. }),
  320. /**
  321. * Find a path from a starting coordinate to an ending coordinate, taking
  322. * obstacles into consideration. Might not always find the optimal path,
  323. * but is fast, and usually good enough.
  324. *
  325. * @function Highcharts.Pathfinder.algorithms.fastAvoid
  326. *
  327. * @param {Highcharts.PositionObject} start
  328. * Starting coordinate, object with x/y props.
  329. *
  330. * @param {Highcharts.PositionObject} end
  331. * Ending coordinate, object with x/y props.
  332. *
  333. * @param {object} options
  334. * Options for the algorithm.
  335. * - chartObstacles: Array of chart obstacles to avoid
  336. * - lineObstacles: Array of line obstacles to jump over
  337. * - obstacleMetrics: Object with metrics of chartObstacles cached
  338. * - hardBounds: Hard boundaries to not cross
  339. * - obstacleOptions: Options for the obstacles, including margin
  340. * - startDirectionX: Optional. True if starting in the X direction.
  341. * If not provided, the algorithm starts in the
  342. * direction that is the furthest between
  343. * start/end.
  344. *
  345. * @return {object}
  346. * An object with the SVG path in Array form as accepted by the SVG
  347. * renderer, as well as an array of new obstacles making up this
  348. * path.
  349. */
  350. fastAvoid: extend(function (start, end, options) {
  351. /*
  352. Algorithm rules/description
  353. - Find initial direction
  354. - Determine soft/hard max for each direction.
  355. - Move along initial direction until obstacle.
  356. - Change direction.
  357. - If hitting obstacle, first try to change length of previous line
  358. before changing direction again.
  359. Soft min/max x = start/destination x +/- widest obstacle + margin
  360. Soft min/max y = start/destination y +/- tallest obstacle + margin
  361. @todo:
  362. - Make retrospective, try changing prev segment to reduce
  363. corners
  364. - Fix logic for breaking out of end-points - not always picking
  365. the best direction currently
  366. - When going around the end obstacle we should not always go the
  367. shortest route, rather pick the one closer to the end point
  368. */
  369. var dirIsX = pick(options.startDirectionX, abs(end.x - start.x) > abs(end.y - start.y)), dir = dirIsX ? 'x' : 'y', segments, useMax, extractedEndPoint, endSegments = [], forceObstacleBreak = false, // Used in clearPathTo to keep track of
  370. // when to force break through an obstacle.
  371. // Boundaries to stay within. If beyond soft boundary, prefer to
  372. // change direction ASAP. If at hard max, always change immediately.
  373. metrics = options.obstacleMetrics, softMinX = min(start.x, end.x) - metrics.maxWidth - 10, softMaxX = max(start.x, end.x) + metrics.maxWidth + 10, softMinY = min(start.y, end.y) - metrics.maxHeight - 10, softMaxY = max(start.y, end.y) + metrics.maxHeight + 10,
  374. // Obstacles
  375. chartObstacles = options.chartObstacles, startObstacleIx = findLastObstacleBefore(chartObstacles, softMinX), endObstacleIx = findLastObstacleBefore(chartObstacles, softMaxX);
  376. // eslint-disable-next-line valid-jsdoc
  377. /**
  378. * How far can you go between two points before hitting an obstacle?
  379. * Does not work for diagonal lines (because it doesn't have to).
  380. * @private
  381. */
  382. function pivotPoint(fromPoint, toPoint, directionIsX) {
  383. var firstPoint, lastPoint, highestPoint, lowestPoint, i, searchDirection = fromPoint.x < toPoint.x ? 1 : -1;
  384. if (fromPoint.x < toPoint.x) {
  385. firstPoint = fromPoint;
  386. lastPoint = toPoint;
  387. }
  388. else {
  389. firstPoint = toPoint;
  390. lastPoint = fromPoint;
  391. }
  392. if (fromPoint.y < toPoint.y) {
  393. lowestPoint = fromPoint;
  394. highestPoint = toPoint;
  395. }
  396. else {
  397. lowestPoint = toPoint;
  398. highestPoint = fromPoint;
  399. }
  400. // Go through obstacle range in reverse if toPoint is before
  401. // fromPoint in the X-dimension.
  402. i = searchDirection < 0 ?
  403. // Searching backwards, start at last obstacle before last point
  404. min(findLastObstacleBefore(chartObstacles, lastPoint.x), chartObstacles.length - 1) :
  405. // Forwards. Since we're not sorted by xMax, we have to look
  406. // at all obstacles.
  407. 0;
  408. // Go through obstacles in this X range
  409. while (chartObstacles[i] && (searchDirection > 0 && chartObstacles[i].xMin <= lastPoint.x ||
  410. searchDirection < 0 && chartObstacles[i].xMax >= firstPoint.x)) {
  411. // If this obstacle is between from and to points in a straight
  412. // line, pivot at the intersection.
  413. if (chartObstacles[i].xMin <= lastPoint.x &&
  414. chartObstacles[i].xMax >= firstPoint.x &&
  415. chartObstacles[i].yMin <= highestPoint.y &&
  416. chartObstacles[i].yMax >= lowestPoint.y) {
  417. if (directionIsX) {
  418. return {
  419. y: fromPoint.y,
  420. x: fromPoint.x < toPoint.x ?
  421. chartObstacles[i].xMin - 1 :
  422. chartObstacles[i].xMax + 1,
  423. obstacle: chartObstacles[i]
  424. };
  425. }
  426. // else ...
  427. return {
  428. x: fromPoint.x,
  429. y: fromPoint.y < toPoint.y ?
  430. chartObstacles[i].yMin - 1 :
  431. chartObstacles[i].yMax + 1,
  432. obstacle: chartObstacles[i]
  433. };
  434. }
  435. i += searchDirection;
  436. }
  437. return toPoint;
  438. }
  439. /**
  440. * Decide in which direction to dodge or get out of an obstacle.
  441. * Considers desired direction, which way is shortest, soft and hard
  442. * bounds.
  443. *
  444. * (? Returns a string, either xMin, xMax, yMin or yMax.)
  445. *
  446. * @private
  447. * @function
  448. *
  449. * @param {object} obstacle
  450. * Obstacle to dodge/escape.
  451. *
  452. * @param {object} fromPoint
  453. * Point with x/y props that's dodging/escaping.
  454. *
  455. * @param {object} toPoint
  456. * Goal point.
  457. *
  458. * @param {boolean} dirIsX
  459. * Dodge in X dimension.
  460. *
  461. * @param {object} bounds
  462. * Hard and soft boundaries.
  463. *
  464. * @return {boolean}
  465. * Use max or not.
  466. */
  467. function getDodgeDirection(obstacle, fromPoint, toPoint, dirIsX, bounds) {
  468. 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'] >=
  469. softBounds[dir + 'Max'], minOutOfSoftBounds = obstacle[dir + 'Min'] <=
  470. softBounds[dir + 'Min'], maxOutOfHardBounds = obstacle[dir + 'Max'] >=
  471. hardBounds[dir + 'Max'], minOutOfHardBounds = obstacle[dir + 'Min'] <=
  472. hardBounds[dir + 'Min'],
  473. // Find out if we should prefer one direction over the other if
  474. // we can choose freely
  475. minDistance = abs(obstacle[dir + 'Min'] - fromPoint[dir]), maxDistance = abs(obstacle[dir + 'Max'] - fromPoint[dir]),
  476. // If it's a small difference, pick the one leading towards dest
  477. // point. Otherwise pick the shortest distance
  478. useMax = abs(minDistance - maxDistance) < 10 ?
  479. fromPoint[dir] < toPoint[dir] :
  480. maxDistance < minDistance;
  481. // Check if we hit any obstacles trying to go around in either
  482. // direction.
  483. toPointMin[dir] = obstacle[dir + 'Min'];
  484. toPointMax[dir] = obstacle[dir + 'Max'];
  485. minPivot = pivotPoint(fromPoint, toPointMin, dirIsX)[dir] !==
  486. toPointMin[dir];
  487. maxPivot = pivotPoint(fromPoint, toPointMax, dirIsX)[dir] !==
  488. toPointMax[dir];
  489. useMax = minPivot ?
  490. (maxPivot ? useMax : true) :
  491. (maxPivot ? false : useMax);
  492. // useMax now contains our preferred choice, bounds not taken into
  493. // account. If both or neither direction is out of bounds we want to
  494. // use this.
  495. // Deal with soft bounds
  496. useMax = minOutOfSoftBounds ?
  497. (maxOutOfSoftBounds ? useMax : true) : // Out on min
  498. (maxOutOfSoftBounds ? false : useMax); // Not out on min
  499. // Deal with hard bounds
  500. useMax = minOutOfHardBounds ?
  501. (maxOutOfHardBounds ? useMax : true) : // Out on min
  502. (maxOutOfHardBounds ? false : useMax); // Not out on min
  503. return useMax;
  504. }
  505. // eslint-disable-next-line valid-jsdoc
  506. /**
  507. * Find a clear path between point.
  508. * @private
  509. */
  510. function clearPathTo(fromPoint, toPoint, dirIsX) {
  511. // Don't waste time if we've hit goal
  512. if (fromPoint.x === toPoint.x && fromPoint.y === toPoint.y) {
  513. return [];
  514. }
  515. var dir = dirIsX ? 'x' : 'y', pivot, segments, waypoint, waypointUseMax, envelopingObstacle, secondEnvelopingObstacle, envelopWaypoint, obstacleMargin = options.obstacleOptions.margin, bounds = {
  516. soft: {
  517. xMin: softMinX,
  518. xMax: softMaxX,
  519. yMin: softMinY,
  520. yMax: softMaxY
  521. },
  522. hard: options.hardBounds
  523. };
  524. // If fromPoint is inside an obstacle we have a problem. Break out
  525. // by just going to the outside of this obstacle. We prefer to go to
  526. // the nearest edge in the chosen direction.
  527. envelopingObstacle =
  528. findObstacleFromPoint(chartObstacles, fromPoint);
  529. if (envelopingObstacle > -1) {
  530. envelopingObstacle = chartObstacles[envelopingObstacle];
  531. waypointUseMax = getDodgeDirection(envelopingObstacle, fromPoint, toPoint, dirIsX, bounds);
  532. // Cut obstacle to hard bounds to make sure we stay within
  533. limitObstacleToBounds(envelopingObstacle, options.hardBounds);
  534. envelopWaypoint = dirIsX ? {
  535. y: fromPoint.y,
  536. x: envelopingObstacle[waypointUseMax ? 'xMax' : 'xMin'] +
  537. (waypointUseMax ? 1 : -1)
  538. } : {
  539. x: fromPoint.x,
  540. y: envelopingObstacle[waypointUseMax ? 'yMax' : 'yMin'] +
  541. (waypointUseMax ? 1 : -1)
  542. };
  543. // If we crashed into another obstacle doing this, we put the
  544. // waypoint between them instead
  545. secondEnvelopingObstacle = findObstacleFromPoint(chartObstacles, envelopWaypoint);
  546. if (secondEnvelopingObstacle > -1) {
  547. secondEnvelopingObstacle = chartObstacles[secondEnvelopingObstacle];
  548. // Cut obstacle to hard bounds
  549. limitObstacleToBounds(secondEnvelopingObstacle, options.hardBounds);
  550. // Modify waypoint to lay between obstacles
  551. envelopWaypoint[dir] = waypointUseMax ? max(envelopingObstacle[dir + 'Max'] - obstacleMargin + 1, (secondEnvelopingObstacle[dir + 'Min'] +
  552. envelopingObstacle[dir + 'Max']) / 2) :
  553. min((envelopingObstacle[dir + 'Min'] + obstacleMargin - 1), ((secondEnvelopingObstacle[dir + 'Max'] +
  554. envelopingObstacle[dir + 'Min']) / 2));
  555. // We are not going anywhere. If this happens for the first
  556. // time, do nothing. Otherwise, try to go to the extreme of
  557. // the obstacle pair in the current direction.
  558. if (fromPoint.x === envelopWaypoint.x &&
  559. fromPoint.y === envelopWaypoint.y) {
  560. if (forceObstacleBreak) {
  561. envelopWaypoint[dir] = waypointUseMax ?
  562. max(envelopingObstacle[dir + 'Max'], secondEnvelopingObstacle[dir + 'Max']) + 1 :
  563. min(envelopingObstacle[dir + 'Min'], secondEnvelopingObstacle[dir + 'Min']) - 1;
  564. }
  565. // Toggle on if off, and the opposite
  566. forceObstacleBreak = !forceObstacleBreak;
  567. }
  568. else {
  569. // This point is not identical to previous.
  570. // Clear break trigger.
  571. forceObstacleBreak = false;
  572. }
  573. }
  574. segments = [{
  575. start: fromPoint,
  576. end: envelopWaypoint
  577. }];
  578. }
  579. else { // If not enveloping, use standard pivot calculation
  580. pivot = pivotPoint(fromPoint, {
  581. x: dirIsX ? toPoint.x : fromPoint.x,
  582. y: dirIsX ? fromPoint.y : toPoint.y
  583. }, dirIsX);
  584. segments = [{
  585. start: fromPoint,
  586. end: {
  587. x: pivot.x,
  588. y: pivot.y
  589. }
  590. }];
  591. // Pivot before goal, use a waypoint to dodge obstacle
  592. if (pivot[dirIsX ? 'x' : 'y'] !== toPoint[dirIsX ? 'x' : 'y']) {
  593. // Find direction of waypoint
  594. waypointUseMax = getDodgeDirection(pivot.obstacle, pivot, toPoint, !dirIsX, bounds);
  595. // Cut waypoint to hard bounds
  596. limitObstacleToBounds(pivot.obstacle, options.hardBounds);
  597. waypoint = {
  598. x: dirIsX ?
  599. pivot.x :
  600. pivot.obstacle[waypointUseMax ? 'xMax' : 'xMin'] +
  601. (waypointUseMax ? 1 : -1),
  602. y: dirIsX ?
  603. pivot.obstacle[waypointUseMax ? 'yMax' : 'yMin'] +
  604. (waypointUseMax ? 1 : -1) :
  605. pivot.y
  606. };
  607. // We're changing direction here, store that to make sure we
  608. // also change direction when adding the last segment array
  609. // after handling waypoint.
  610. dirIsX = !dirIsX;
  611. segments = segments.concat(clearPathTo({
  612. x: pivot.x,
  613. y: pivot.y
  614. }, waypoint, dirIsX));
  615. }
  616. }
  617. // Get segments for the other direction too
  618. // Recursion is our friend
  619. segments = segments.concat(clearPathTo(segments[segments.length - 1].end, toPoint, !dirIsX));
  620. return segments;
  621. }
  622. // eslint-disable-next-line valid-jsdoc
  623. /**
  624. * Extract point to outside of obstacle in whichever direction is
  625. * closest. Returns new point outside obstacle.
  626. * @private
  627. */
  628. function extractFromObstacle(obstacle, point, goalPoint) {
  629. var dirIsX = min(obstacle.xMax - point.x, point.x - obstacle.xMin) <
  630. min(obstacle.yMax - point.y, point.y - obstacle.yMin), bounds = {
  631. soft: options.hardBounds,
  632. hard: options.hardBounds
  633. }, useMax = getDodgeDirection(obstacle, point, goalPoint, dirIsX, bounds);
  634. return dirIsX ? {
  635. y: point.y,
  636. x: obstacle[useMax ? 'xMax' : 'xMin'] + (useMax ? 1 : -1)
  637. } : {
  638. x: point.x,
  639. y: obstacle[useMax ? 'yMax' : 'yMin'] + (useMax ? 1 : -1)
  640. };
  641. }
  642. // Cut the obstacle array to soft bounds for optimization in large
  643. // datasets.
  644. chartObstacles =
  645. chartObstacles.slice(startObstacleIx, endObstacleIx + 1);
  646. // If an obstacle envelops the end point, move it out of there and add
  647. // a little segment to where it was.
  648. if ((endObstacleIx = findObstacleFromPoint(chartObstacles, end)) > -1) {
  649. extractedEndPoint = extractFromObstacle(chartObstacles[endObstacleIx], end, start);
  650. endSegments.push({
  651. end: end,
  652. start: extractedEndPoint
  653. });
  654. end = extractedEndPoint;
  655. }
  656. // If it's still inside one or more obstacles, get out of there by
  657. // force-moving towards the start point.
  658. while ((endObstacleIx = findObstacleFromPoint(chartObstacles, end)) > -1) {
  659. useMax = end[dir] - start[dir] < 0;
  660. extractedEndPoint = {
  661. x: end.x,
  662. y: end.y
  663. };
  664. extractedEndPoint[dir] = chartObstacles[endObstacleIx][useMax ? dir + 'Max' : dir + 'Min'] + (useMax ? 1 : -1);
  665. endSegments.push({
  666. end: end,
  667. start: extractedEndPoint
  668. });
  669. end = extractedEndPoint;
  670. }
  671. // Find the path
  672. segments = clearPathTo(start, end, dirIsX);
  673. // Add the end-point segments
  674. segments = segments.concat(endSegments.reverse());
  675. return {
  676. path: pathFromSegments(segments),
  677. obstacles: segments
  678. };
  679. }, {
  680. requiresObstacles: true
  681. })
  682. };
  683. return algorithms;
  684. });
  685. _registerModule(_modules, 'parts-gantt/ArrowSymbols.js', [_modules['parts/SVGRenderer.js']], function (SVGRenderer) {
  686. /* *
  687. *
  688. * (c) 2017 Highsoft AS
  689. * Authors: Lars A. V. Cabrera
  690. *
  691. * License: www.highcharts.com/license
  692. *
  693. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  694. *
  695. * */
  696. /**
  697. * Creates an arrow symbol. Like a triangle, except not filled.
  698. * ```
  699. * o
  700. * o
  701. * o
  702. * o
  703. * o
  704. * o
  705. * o
  706. * ```
  707. *
  708. * @private
  709. * @function
  710. *
  711. * @param {number} x
  712. * x position of the arrow
  713. *
  714. * @param {number} y
  715. * y position of the arrow
  716. *
  717. * @param {number} w
  718. * width of the arrow
  719. *
  720. * @param {number} h
  721. * height of the arrow
  722. *
  723. * @return {Highcharts.SVGPathArray}
  724. * Path array
  725. */
  726. SVGRenderer.prototype.symbols.arrow = function (x, y, w, h) {
  727. return [
  728. ['M', x, y + h / 2],
  729. ['L', x + w, y],
  730. ['L', x, y + h / 2],
  731. ['L', x + w, y + h]
  732. ];
  733. };
  734. /**
  735. * Creates a half-width arrow symbol. Like a triangle, except not filled.
  736. * ```
  737. * o
  738. * o
  739. * o
  740. * o
  741. * o
  742. * ```
  743. *
  744. * @private
  745. * @function
  746. *
  747. * @param {number} x
  748. * x position of the arrow
  749. *
  750. * @param {number} y
  751. * y position of the arrow
  752. *
  753. * @param {number} w
  754. * width of the arrow
  755. *
  756. * @param {number} h
  757. * height of the arrow
  758. *
  759. * @return {Highcharts.SVGPathArray}
  760. * Path array
  761. */
  762. SVGRenderer.prototype.symbols['arrow-half'] = function (x, y, w, h) {
  763. return SVGRenderer.prototype.symbols.arrow(x, y, w / 2, h);
  764. };
  765. /**
  766. * Creates a left-oriented triangle.
  767. * ```
  768. * o
  769. * ooooooo
  770. * ooooooooooooo
  771. * ooooooo
  772. * o
  773. * ```
  774. *
  775. * @private
  776. * @function
  777. *
  778. * @param {number} x
  779. * x position of the triangle
  780. *
  781. * @param {number} y
  782. * y position of the triangle
  783. *
  784. * @param {number} w
  785. * width of the triangle
  786. *
  787. * @param {number} h
  788. * height of the triangle
  789. *
  790. * @return {Highcharts.SVGPathArray}
  791. * Path array
  792. */
  793. SVGRenderer.prototype.symbols['triangle-left'] = function (x, y, w, h) {
  794. return [
  795. ['M', x + w, y],
  796. ['L', x, y + h / 2],
  797. ['L', x + w, y + h],
  798. ['Z']
  799. ];
  800. };
  801. /**
  802. * Alias function for triangle-left.
  803. *
  804. * @private
  805. * @function
  806. *
  807. * @param {number} x
  808. * x position of the arrow
  809. *
  810. * @param {number} y
  811. * y position of the arrow
  812. *
  813. * @param {number} w
  814. * width of the arrow
  815. *
  816. * @param {number} h
  817. * height of the arrow
  818. *
  819. * @return {Highcharts.SVGPathArray}
  820. * Path array
  821. */
  822. SVGRenderer.prototype.symbols['arrow-filled'] = SVGRenderer.prototype.symbols['triangle-left'];
  823. /**
  824. * Creates a half-width, left-oriented triangle.
  825. * ```
  826. * o
  827. * oooo
  828. * ooooooo
  829. * oooo
  830. * o
  831. * ```
  832. *
  833. * @private
  834. * @function
  835. *
  836. * @param {number} x
  837. * x position of the triangle
  838. *
  839. * @param {number} y
  840. * y position of the triangle
  841. *
  842. * @param {number} w
  843. * width of the triangle
  844. *
  845. * @param {number} h
  846. * height of the triangle
  847. *
  848. * @return {Highcharts.SVGPathArray}
  849. * Path array
  850. */
  851. SVGRenderer.prototype.symbols['triangle-left-half'] = function (x, y, w, h) {
  852. return SVGRenderer.prototype.symbols['triangle-left'](x, y, w / 2, h);
  853. };
  854. /**
  855. * Alias function for triangle-left-half.
  856. *
  857. * @private
  858. * @function
  859. *
  860. * @param {number} x
  861. * x position of the arrow
  862. *
  863. * @param {number} y
  864. * y position of the arrow
  865. *
  866. * @param {number} w
  867. * width of the arrow
  868. *
  869. * @param {number} h
  870. * height of the arrow
  871. *
  872. * @return {Highcharts.SVGPathArray}
  873. * Path array
  874. */
  875. SVGRenderer.prototype.symbols['arrow-filled-half'] = SVGRenderer.prototype.symbols['triangle-left-half'];
  876. });
  877. _registerModule(_modules, 'parts-gantt/Pathfinder.js', [_modules['parts/Chart.js'], _modules['parts/Globals.js'], _modules['parts/Options.js'], _modules['parts/Point.js'], _modules['parts/Utilities.js'], _modules['parts-gantt/PathfinderAlgorithms.js']], function (Chart, H, O, Point, U, pathfinderAlgorithms) {
  878. /* *
  879. *
  880. * (c) 2016 Highsoft AS
  881. * Authors: Øystein Moseng, Lars A. V. Cabrera
  882. *
  883. * License: www.highcharts.com/license
  884. *
  885. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  886. *
  887. * */
  888. /**
  889. * The default pathfinder algorithm to use for a chart. It is possible to define
  890. * your own algorithms by adding them to the
  891. * `Highcharts.Pathfinder.prototype.algorithms`
  892. * object before the chart has been created.
  893. *
  894. * The default algorithms are as follows:
  895. *
  896. * `straight`: Draws a straight line between the connecting
  897. * points. Does not avoid other points when drawing.
  898. *
  899. * `simpleConnect`: Finds a path between the points using right angles
  900. * only. Takes only starting/ending points into
  901. * account, and will not avoid other points.
  902. *
  903. * `fastAvoid`: Finds a path between the points using right angles
  904. * only. Will attempt to avoid other points, but its
  905. * focus is performance over accuracy. Works well with
  906. * less dense datasets.
  907. *
  908. * @typedef {"fastAvoid"|"simpleConnect"|"straight"|string} Highcharts.PathfinderTypeValue
  909. */
  910. ''; // detach doclets above
  911. var defaultOptions = O.defaultOptions;
  912. var addEvent = U.addEvent, defined = U.defined, error = U.error, extend = U.extend, merge = U.merge, objectEach = U.objectEach, pick = U.pick, splat = U.splat;
  913. var deg2rad = H.deg2rad, max = Math.max, min = Math.min;
  914. /*
  915. @todo:
  916. - Document how to write your own algorithms
  917. - Consider adding a Point.pathTo method that wraps creating a connection
  918. and rendering it
  919. */
  920. // Set default Pathfinder options
  921. extend(defaultOptions, {
  922. /**
  923. * The Pathfinder module allows you to define connections between any two
  924. * points, represented as lines - optionally with markers for the start
  925. * and/or end points. Multiple algorithms are available for calculating how
  926. * the connecting lines are drawn.
  927. *
  928. * Connector functionality requires Highcharts Gantt to be loaded. In Gantt
  929. * charts, the connectors are used to draw dependencies between tasks.
  930. *
  931. * @see [dependency](series.gantt.data.dependency)
  932. *
  933. * @sample gantt/pathfinder/demo
  934. * Pathfinder connections
  935. *
  936. * @declare Highcharts.ConnectorsOptions
  937. * @product gantt
  938. * @optionparent connectors
  939. */
  940. connectors: {
  941. /**
  942. * Enable connectors for this chart. Requires Highcharts Gantt.
  943. *
  944. * @type {boolean}
  945. * @default true
  946. * @since 6.2.0
  947. * @apioption connectors.enabled
  948. */
  949. /**
  950. * Set the default dash style for this chart's connecting lines.
  951. *
  952. * @type {string}
  953. * @default solid
  954. * @since 6.2.0
  955. * @apioption connectors.dashStyle
  956. */
  957. /**
  958. * Set the default color for this chart's Pathfinder connecting lines.
  959. * Defaults to the color of the point being connected.
  960. *
  961. * @type {Highcharts.ColorString}
  962. * @since 6.2.0
  963. * @apioption connectors.lineColor
  964. */
  965. /**
  966. * Set the default pathfinder margin to use, in pixels. Some Pathfinder
  967. * algorithms attempt to avoid obstacles, such as other points in the
  968. * chart. These algorithms use this margin to determine how close lines
  969. * can be to an obstacle. The default is to compute this automatically
  970. * from the size of the obstacles in the chart.
  971. *
  972. * To draw connecting lines close to existing points, set this to a low
  973. * number. For more space around existing points, set this number
  974. * higher.
  975. *
  976. * @sample gantt/pathfinder/algorithm-margin
  977. * Small algorithmMargin
  978. *
  979. * @type {number}
  980. * @since 6.2.0
  981. * @apioption connectors.algorithmMargin
  982. */
  983. /**
  984. * Set the default pathfinder algorithm to use for this chart. It is
  985. * possible to define your own algorithms by adding them to the
  986. * Highcharts.Pathfinder.prototype.algorithms object before the chart
  987. * has been created.
  988. *
  989. * The default algorithms are as follows:
  990. *
  991. * `straight`: Draws a straight line between the connecting
  992. * points. Does not avoid other points when drawing.
  993. *
  994. * `simpleConnect`: Finds a path between the points using right angles
  995. * only. Takes only starting/ending points into
  996. * account, and will not avoid other points.
  997. *
  998. * `fastAvoid`: Finds a path between the points using right angles
  999. * only. Will attempt to avoid other points, but its
  1000. * focus is performance over accuracy. Works well with
  1001. * less dense datasets.
  1002. *
  1003. * Default value: `straight` is used as default for most series types,
  1004. * while `simpleConnect` is used as default for Gantt series, to show
  1005. * dependencies between points.
  1006. *
  1007. * @sample gantt/pathfinder/demo
  1008. * Different types used
  1009. *
  1010. * @type {Highcharts.PathfinderTypeValue}
  1011. * @default undefined
  1012. * @since 6.2.0
  1013. */
  1014. type: 'straight',
  1015. /**
  1016. * Set the default pixel width for this chart's Pathfinder connecting
  1017. * lines.
  1018. *
  1019. * @since 6.2.0
  1020. */
  1021. lineWidth: 1,
  1022. /**
  1023. * Marker options for this chart's Pathfinder connectors. Note that
  1024. * this option is overridden by the `startMarker` and `endMarker`
  1025. * options.
  1026. *
  1027. * @declare Highcharts.ConnectorsMarkerOptions
  1028. * @since 6.2.0
  1029. */
  1030. marker: {
  1031. /**
  1032. * Set the radius of the connector markers. The default is
  1033. * automatically computed based on the algorithmMargin setting.
  1034. *
  1035. * Setting marker.width and marker.height will override this
  1036. * setting.
  1037. *
  1038. * @type {number}
  1039. * @since 6.2.0
  1040. * @apioption connectors.marker.radius
  1041. */
  1042. /**
  1043. * Set the width of the connector markers. If not supplied, this
  1044. * is inferred from the marker radius.
  1045. *
  1046. * @type {number}
  1047. * @since 6.2.0
  1048. * @apioption connectors.marker.width
  1049. */
  1050. /**
  1051. * Set the height of the connector markers. If not supplied, this
  1052. * is inferred from the marker radius.
  1053. *
  1054. * @type {number}
  1055. * @since 6.2.0
  1056. * @apioption connectors.marker.height
  1057. */
  1058. /**
  1059. * Set the color of the connector markers. By default this is the
  1060. * same as the connector color.
  1061. *
  1062. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  1063. * @since 6.2.0
  1064. * @apioption connectors.marker.color
  1065. */
  1066. /**
  1067. * Set the line/border color of the connector markers. By default
  1068. * this is the same as the marker color.
  1069. *
  1070. * @type {Highcharts.ColorString}
  1071. * @since 6.2.0
  1072. * @apioption connectors.marker.lineColor
  1073. */
  1074. /**
  1075. * Enable markers for the connectors.
  1076. */
  1077. enabled: false,
  1078. /**
  1079. * Horizontal alignment of the markers relative to the points.
  1080. *
  1081. * @type {Highcharts.AlignValue}
  1082. */
  1083. align: 'center',
  1084. /**
  1085. * Vertical alignment of the markers relative to the points.
  1086. *
  1087. * @type {Highcharts.VerticalAlignValue}
  1088. */
  1089. verticalAlign: 'middle',
  1090. /**
  1091. * Whether or not to draw the markers inside the points.
  1092. */
  1093. inside: false,
  1094. /**
  1095. * Set the line/border width of the pathfinder markers.
  1096. */
  1097. lineWidth: 1
  1098. },
  1099. /**
  1100. * Marker options specific to the start markers for this chart's
  1101. * Pathfinder connectors. Overrides the generic marker options.
  1102. *
  1103. * @declare Highcharts.ConnectorsStartMarkerOptions
  1104. * @extends connectors.marker
  1105. * @since 6.2.0
  1106. */
  1107. startMarker: {
  1108. /**
  1109. * Set the symbol of the connector start markers.
  1110. */
  1111. symbol: 'diamond'
  1112. },
  1113. /**
  1114. * Marker options specific to the end markers for this chart's
  1115. * Pathfinder connectors. Overrides the generic marker options.
  1116. *
  1117. * @declare Highcharts.ConnectorsEndMarkerOptions
  1118. * @extends connectors.marker
  1119. * @since 6.2.0
  1120. */
  1121. endMarker: {
  1122. /**
  1123. * Set the symbol of the connector end markers.
  1124. */
  1125. symbol: 'arrow-filled'
  1126. }
  1127. }
  1128. });
  1129. /**
  1130. * Override Pathfinder connector options for a series. Requires Highcharts Gantt
  1131. * to be loaded.
  1132. *
  1133. * @declare Highcharts.SeriesConnectorsOptionsObject
  1134. * @extends connectors
  1135. * @since 6.2.0
  1136. * @excluding enabled, algorithmMargin
  1137. * @product gantt
  1138. * @apioption plotOptions.series.connectors
  1139. */
  1140. /**
  1141. * Connect to a point. This option can be either a string, referring to the ID
  1142. * of another point, or an object, or an array of either. If the option is an
  1143. * array, each element defines a connection.
  1144. *
  1145. * @sample gantt/pathfinder/demo
  1146. * Different connection types
  1147. *
  1148. * @declare Highcharts.XrangePointConnectorsOptionsObject
  1149. * @type {string|Array<string|*>|*}
  1150. * @extends plotOptions.series.connectors
  1151. * @since 6.2.0
  1152. * @excluding enabled
  1153. * @product gantt
  1154. * @requires highcharts-gantt
  1155. * @apioption series.xrange.data.connect
  1156. */
  1157. /**
  1158. * The ID of the point to connect to.
  1159. *
  1160. * @type {string}
  1161. * @since 6.2.0
  1162. * @product gantt
  1163. * @apioption series.xrange.data.connect.to
  1164. */
  1165. /**
  1166. * Get point bounding box using plotX/plotY and shapeArgs. If using
  1167. * graphic.getBBox() directly, the bbox will be affected by animation.
  1168. *
  1169. * @private
  1170. * @function
  1171. *
  1172. * @param {Highcharts.Point} point
  1173. * The point to get BB of.
  1174. *
  1175. * @return {Highcharts.Dictionary<number>|null}
  1176. * Result xMax, xMin, yMax, yMin.
  1177. */
  1178. function getPointBB(point) {
  1179. var shapeArgs = point.shapeArgs, bb;
  1180. // Prefer using shapeArgs (columns)
  1181. if (shapeArgs) {
  1182. return {
  1183. xMin: shapeArgs.x,
  1184. xMax: shapeArgs.x + shapeArgs.width,
  1185. yMin: shapeArgs.y,
  1186. yMax: shapeArgs.y + shapeArgs.height
  1187. };
  1188. }
  1189. // Otherwise use plotX/plotY and bb
  1190. bb = point.graphic && point.graphic.getBBox();
  1191. return bb ? {
  1192. xMin: point.plotX - bb.width / 2,
  1193. xMax: point.plotX + bb.width / 2,
  1194. yMin: point.plotY - bb.height / 2,
  1195. yMax: point.plotY + bb.height / 2
  1196. } : null;
  1197. }
  1198. /**
  1199. * Calculate margin to place around obstacles for the pathfinder in pixels.
  1200. * Returns a minimum of 1 pixel margin.
  1201. *
  1202. * @private
  1203. * @function
  1204. *
  1205. * @param {Array<object>} obstacles
  1206. * Obstacles to calculate margin from.
  1207. *
  1208. * @return {number}
  1209. * The calculated margin in pixels. At least 1.
  1210. */
  1211. function calculateObstacleMargin(obstacles) {
  1212. var len = obstacles.length, i = 0, j, obstacleDistance, distances = [],
  1213. // Compute smallest distance between two rectangles
  1214. distance = function (a, b, bbMargin) {
  1215. // Count the distance even if we are slightly off
  1216. var margin = pick(bbMargin, 10), yOverlap = a.yMax + margin > b.yMin - margin &&
  1217. a.yMin - margin < b.yMax + margin, xOverlap = a.xMax + margin > b.xMin - margin &&
  1218. a.xMin - margin < b.xMax + margin, xDistance = yOverlap ? (a.xMin > b.xMax ? a.xMin - b.xMax : b.xMin - a.xMax) : Infinity, yDistance = xOverlap ? (a.yMin > b.yMax ? a.yMin - b.yMax : b.yMin - a.yMax) : Infinity;
  1219. // If the rectangles collide, try recomputing with smaller margin.
  1220. // If they collide anyway, discard the obstacle.
  1221. if (xOverlap && yOverlap) {
  1222. return (margin ?
  1223. distance(a, b, Math.floor(margin / 2)) :
  1224. Infinity);
  1225. }
  1226. return min(xDistance, yDistance);
  1227. };
  1228. // Go over all obstacles and compare them to the others.
  1229. for (; i < len; ++i) {
  1230. // Compare to all obstacles ahead. We will already have compared this
  1231. // obstacle to the ones before.
  1232. for (j = i + 1; j < len; ++j) {
  1233. obstacleDistance = distance(obstacles[i], obstacles[j]);
  1234. // TODO: Magic number 80
  1235. if (obstacleDistance < 80) { // Ignore large distances
  1236. distances.push(obstacleDistance);
  1237. }
  1238. }
  1239. }
  1240. // Ensure we always have at least one value, even in very spaceous charts
  1241. distances.push(80);
  1242. return max(Math.floor(distances.sort(function (a, b) {
  1243. return (a - b);
  1244. })[
  1245. // Discard first 10% of the relevant distances, and then grab
  1246. // the smallest one.
  1247. Math.floor(distances.length / 10)] / 2 - 1 // Divide the distance by 2 and subtract 1.
  1248. ), 1 // 1 is the minimum margin
  1249. );
  1250. }
  1251. /* eslint-disable no-invalid-this, valid-jsdoc */
  1252. /**
  1253. * The Connection class. Used internally to represent a connection between two
  1254. * points.
  1255. *
  1256. * @private
  1257. * @class
  1258. * @name Highcharts.Connection
  1259. *
  1260. * @param {Highcharts.Point} from
  1261. * Connection runs from this Point.
  1262. *
  1263. * @param {Highcharts.Point} to
  1264. * Connection runs to this Point.
  1265. *
  1266. * @param {Highcharts.ConnectorsOptions} [options]
  1267. * Connection options.
  1268. */
  1269. function Connection(from, to, options) {
  1270. this.init(from, to, options);
  1271. }
  1272. Connection.prototype = {
  1273. /**
  1274. * Initialize the Connection object. Used as constructor only.
  1275. *
  1276. * @function Highcharts.Connection#init
  1277. *
  1278. * @param {Highcharts.Point} from
  1279. * Connection runs from this Point.
  1280. *
  1281. * @param {Highcharts.Point} to
  1282. * Connection runs to this Point.
  1283. *
  1284. * @param {Highcharts.ConnectorsOptions} [options]
  1285. * Connection options.
  1286. */
  1287. init: function (from, to, options) {
  1288. this.fromPoint = from;
  1289. this.toPoint = to;
  1290. this.options = options;
  1291. this.chart = from.series.chart;
  1292. this.pathfinder = this.chart.pathfinder;
  1293. },
  1294. /**
  1295. * Add (or update) this connection's path on chart. Stores reference to the
  1296. * created element on this.graphics.path.
  1297. *
  1298. * @function Highcharts.Connection#renderPath
  1299. *
  1300. * @param {Highcharts.SVGPathArray} path
  1301. * Path to render, in array format. E.g. ['M', 0, 0, 'L', 10, 10]
  1302. *
  1303. * @param {Highcharts.SVGAttributes} [attribs]
  1304. * SVG attributes for the path.
  1305. *
  1306. * @param {Highcharts.AnimationOptionsObject} [animation]
  1307. * Animation options for the rendering.
  1308. */
  1309. renderPath: function (path, attribs, animation) {
  1310. var connection = this, chart = this.chart, styledMode = chart.styledMode, pathfinder = chart.pathfinder, animate = !chart.options.chart.forExport && animation !== false, pathGraphic = connection.graphics && connection.graphics.path, anim;
  1311. // Add the SVG element of the pathfinder group if it doesn't exist
  1312. if (!pathfinder.group) {
  1313. pathfinder.group = chart.renderer.g()
  1314. .addClass('highcharts-pathfinder-group')
  1315. .attr({ zIndex: -1 })
  1316. .add(chart.seriesGroup);
  1317. }
  1318. // Shift the group to compensate for plot area.
  1319. // Note: Do this always (even when redrawing a path) to avoid issues
  1320. // when updating chart in a way that changes plot metrics.
  1321. pathfinder.group.translate(chart.plotLeft, chart.plotTop);
  1322. // Create path if does not exist
  1323. if (!(pathGraphic && pathGraphic.renderer)) {
  1324. pathGraphic = chart.renderer.path()
  1325. .add(pathfinder.group);
  1326. if (!styledMode) {
  1327. pathGraphic.attr({
  1328. opacity: 0
  1329. });
  1330. }
  1331. }
  1332. // Set path attribs and animate to the new path
  1333. pathGraphic.attr(attribs);
  1334. anim = { d: path };
  1335. if (!styledMode) {
  1336. anim.opacity = 1;
  1337. }
  1338. pathGraphic[animate ? 'animate' : 'attr'](anim, animation);
  1339. // Store reference on connection
  1340. this.graphics = this.graphics || {};
  1341. this.graphics.path = pathGraphic;
  1342. },
  1343. /**
  1344. * Calculate and add marker graphics for connection to the chart. The
  1345. * created/updated elements are stored on this.graphics.start and
  1346. * this.graphics.end.
  1347. *
  1348. * @function Highcharts.Connection#addMarker
  1349. *
  1350. * @param {string} type
  1351. * Marker type, either 'start' or 'end'.
  1352. *
  1353. * @param {Highcharts.ConnectorsMarkerOptions} options
  1354. * All options for this marker. Not calculated or merged with other
  1355. * options.
  1356. *
  1357. * @param {Highcharts.SVGPathArray} path
  1358. * Connection path in array format. This is used to calculate the
  1359. * rotation angle of the markers.
  1360. */
  1361. addMarker: function (type, options, path) {
  1362. var connection = this, chart = connection.fromPoint.series.chart, pathfinder = chart.pathfinder, renderer = chart.renderer, point = (type === 'start' ?
  1363. connection.fromPoint :
  1364. connection.toPoint), anchor = point.getPathfinderAnchorPoint(options), markerVector, radians, rotation, box, width, height, pathVector, segment;
  1365. if (!options.enabled) {
  1366. return;
  1367. }
  1368. // Last vector before start/end of path, used to get angle
  1369. if (type === 'start') {
  1370. segment = path[1];
  1371. }
  1372. else { // 'end'
  1373. segment = path[path.length - 2];
  1374. }
  1375. if (segment && segment[0] === 'M' || segment[0] === 'L') {
  1376. pathVector = {
  1377. x: segment[1],
  1378. y: segment[2]
  1379. };
  1380. // Get angle between pathVector and anchor point and use it to
  1381. // create marker position.
  1382. radians = point.getRadiansToVector(pathVector, anchor);
  1383. markerVector = point.getMarkerVector(radians, options.radius, anchor);
  1384. // Rotation of marker is calculated from angle between pathVector
  1385. // and markerVector.
  1386. // (Note:
  1387. // Used to recalculate radians between markerVector and pathVector,
  1388. // but this should be the same as between pathVector and anchor.)
  1389. rotation = -radians / deg2rad;
  1390. if (options.width && options.height) {
  1391. width = options.width;
  1392. height = options.height;
  1393. }
  1394. else {
  1395. width = height = options.radius * 2;
  1396. }
  1397. // Add graphics object if it does not exist
  1398. connection.graphics = connection.graphics || {};
  1399. box = {
  1400. x: markerVector.x - (width / 2),
  1401. y: markerVector.y - (height / 2),
  1402. width: width,
  1403. height: height,
  1404. rotation: rotation,
  1405. rotationOriginX: markerVector.x,
  1406. rotationOriginY: markerVector.y
  1407. };
  1408. if (!connection.graphics[type]) {
  1409. // Create new marker element
  1410. connection.graphics[type] = renderer
  1411. .symbol(options.symbol)
  1412. .addClass('highcharts-point-connecting-path-' + type + '-marker')
  1413. .attr(box)
  1414. .add(pathfinder.group);
  1415. if (!renderer.styledMode) {
  1416. connection.graphics[type].attr({
  1417. fill: options.color || connection.fromPoint.color,
  1418. stroke: options.lineColor,
  1419. 'stroke-width': options.lineWidth,
  1420. opacity: 0
  1421. })
  1422. .animate({
  1423. opacity: 1
  1424. }, point.series.options.animation);
  1425. }
  1426. }
  1427. else {
  1428. connection.graphics[type].animate(box);
  1429. }
  1430. }
  1431. },
  1432. /**
  1433. * Calculate and return connection path.
  1434. * Note: Recalculates chart obstacles on demand if they aren't calculated.
  1435. *
  1436. * @function Highcharts.Connection#getPath
  1437. *
  1438. * @param {Highcharts.ConnectorsOptions} options
  1439. * Connector options. Not calculated or merged with other options.
  1440. *
  1441. * @return {object|undefined}
  1442. * Calculated SVG path data in array format.
  1443. */
  1444. getPath: function (options) {
  1445. var pathfinder = this.pathfinder, chart = this.chart, algorithm = pathfinder.algorithms[options.type], chartObstacles = pathfinder.chartObstacles;
  1446. if (typeof algorithm !== 'function') {
  1447. error('"' + options.type + '" is not a Pathfinder algorithm.');
  1448. return;
  1449. }
  1450. // This function calculates obstacles on demand if they don't exist
  1451. if (algorithm.requiresObstacles && !chartObstacles) {
  1452. chartObstacles =
  1453. pathfinder.chartObstacles =
  1454. pathfinder.getChartObstacles(options);
  1455. // If the algorithmMargin was computed, store the result in default
  1456. // options.
  1457. chart.options.connectors.algorithmMargin =
  1458. options.algorithmMargin;
  1459. // Cache some metrics too
  1460. pathfinder.chartObstacleMetrics =
  1461. pathfinder.getObstacleMetrics(chartObstacles);
  1462. }
  1463. // Get the SVG path
  1464. return algorithm(
  1465. // From
  1466. this.fromPoint.getPathfinderAnchorPoint(options.startMarker),
  1467. // To
  1468. this.toPoint.getPathfinderAnchorPoint(options.endMarker), merge({
  1469. chartObstacles: chartObstacles,
  1470. lineObstacles: pathfinder.lineObstacles || [],
  1471. obstacleMetrics: pathfinder.chartObstacleMetrics,
  1472. hardBounds: {
  1473. xMin: 0,
  1474. xMax: chart.plotWidth,
  1475. yMin: 0,
  1476. yMax: chart.plotHeight
  1477. },
  1478. obstacleOptions: {
  1479. margin: options.algorithmMargin
  1480. },
  1481. startDirectionX: pathfinder.getAlgorithmStartDirection(options.startMarker)
  1482. }, options));
  1483. },
  1484. /**
  1485. * (re)Calculate and (re)draw the connection.
  1486. *
  1487. * @function Highcharts.Connection#render
  1488. */
  1489. render: function () {
  1490. var connection = this, fromPoint = connection.fromPoint, series = fromPoint.series, chart = series.chart, pathfinder = chart.pathfinder, pathResult, path, options = merge(chart.options.connectors, series.options.connectors, fromPoint.options.connectors, connection.options), attribs = {};
  1491. // Set path attribs
  1492. if (!chart.styledMode) {
  1493. attribs.stroke = options.lineColor || fromPoint.color;
  1494. attribs['stroke-width'] = options.lineWidth;
  1495. if (options.dashStyle) {
  1496. attribs.dashstyle = options.dashStyle;
  1497. }
  1498. }
  1499. attribs['class'] = // eslint-disable-line dot-notation
  1500. 'highcharts-point-connecting-path ' +
  1501. 'highcharts-color-' + fromPoint.colorIndex;
  1502. options = merge(attribs, options);
  1503. // Set common marker options
  1504. if (!defined(options.marker.radius)) {
  1505. options.marker.radius = min(max(Math.ceil((options.algorithmMargin || 8) / 2) - 1, 1), 5);
  1506. }
  1507. // Get the path
  1508. pathResult = connection.getPath(options);
  1509. path = pathResult.path;
  1510. // Always update obstacle storage with obstacles from this path.
  1511. // We don't know if future calls will need this for their algorithm.
  1512. if (pathResult.obstacles) {
  1513. pathfinder.lineObstacles =
  1514. pathfinder.lineObstacles || [];
  1515. pathfinder.lineObstacles =
  1516. pathfinder.lineObstacles.concat(pathResult.obstacles);
  1517. }
  1518. // Add the calculated path to the pathfinder group
  1519. connection.renderPath(path, attribs, series.options.animation);
  1520. // Render the markers
  1521. connection.addMarker('start', merge(options.marker, options.startMarker), path);
  1522. connection.addMarker('end', merge(options.marker, options.endMarker), path);
  1523. },
  1524. /**
  1525. * Destroy connection by destroying the added graphics elements.
  1526. *
  1527. * @function Highcharts.Connection#destroy
  1528. */
  1529. destroy: function () {
  1530. if (this.graphics) {
  1531. objectEach(this.graphics, function (val) {
  1532. val.destroy();
  1533. });
  1534. delete this.graphics;
  1535. }
  1536. }
  1537. };
  1538. /**
  1539. * The Pathfinder class.
  1540. *
  1541. * @private
  1542. * @class
  1543. * @name Highcharts.Pathfinder
  1544. *
  1545. * @param {Highcharts.Chart} chart
  1546. * The chart to operate on.
  1547. */
  1548. function Pathfinder(chart) {
  1549. this.init(chart);
  1550. }
  1551. Pathfinder.prototype = {
  1552. /**
  1553. * @name Highcharts.Pathfinder#algorithms
  1554. * @type {Highcharts.Dictionary<Function>}
  1555. */
  1556. algorithms: pathfinderAlgorithms,
  1557. /**
  1558. * Initialize the Pathfinder object.
  1559. *
  1560. * @function Highcharts.Pathfinder#init
  1561. *
  1562. * @param {Highcharts.Chart} chart
  1563. * The chart context.
  1564. */
  1565. init: function (chart) {
  1566. // Initialize pathfinder with chart context
  1567. this.chart = chart;
  1568. // Init connection reference list
  1569. this.connections = [];
  1570. // Recalculate paths/obstacles on chart redraw
  1571. addEvent(chart, 'redraw', function () {
  1572. this.pathfinder.update();
  1573. });
  1574. },
  1575. /**
  1576. * Update Pathfinder connections from scratch.
  1577. *
  1578. * @function Highcharts.Pathfinder#update
  1579. *
  1580. * @param {boolean} [deferRender]
  1581. * Whether or not to defer rendering of connections until
  1582. * series.afterAnimate event has fired. Used on first render.
  1583. */
  1584. update: function (deferRender) {
  1585. var chart = this.chart, pathfinder = this, oldConnections = pathfinder.connections;
  1586. // Rebuild pathfinder connections from options
  1587. pathfinder.connections = [];
  1588. chart.series.forEach(function (series) {
  1589. if (series.visible && !series.options.isInternal) {
  1590. series.points.forEach(function (point) {
  1591. var to, connects = (point.options &&
  1592. point.options.connect &&
  1593. splat(point.options.connect));
  1594. if (point.visible && point.isInside !== false && connects) {
  1595. connects.forEach(function (connect) {
  1596. to = chart.get(typeof connect === 'string' ?
  1597. connect : connect.to);
  1598. if (to instanceof Point &&
  1599. to.series.visible &&
  1600. to.visible &&
  1601. to.isInside !== false) {
  1602. // Add new connection
  1603. pathfinder.connections.push(new Connection(point, // from
  1604. to, typeof connect === 'string' ?
  1605. {} :
  1606. connect));
  1607. }
  1608. });
  1609. }
  1610. });
  1611. }
  1612. });
  1613. // Clear connections that should not be updated, and move old info over
  1614. // to new connections.
  1615. for (var j = 0, k, found, lenOld = oldConnections.length, lenNew = pathfinder.connections.length; j < lenOld; ++j) {
  1616. found = false;
  1617. for (k = 0; k < lenNew; ++k) {
  1618. if (oldConnections[j].fromPoint ===
  1619. pathfinder.connections[k].fromPoint &&
  1620. oldConnections[j].toPoint ===
  1621. pathfinder.connections[k].toPoint) {
  1622. pathfinder.connections[k].graphics =
  1623. oldConnections[j].graphics;
  1624. found = true;
  1625. break;
  1626. }
  1627. }
  1628. if (!found) {
  1629. oldConnections[j].destroy();
  1630. }
  1631. }
  1632. // Clear obstacles to force recalculation. This must be done on every
  1633. // redraw in case positions have changed. Recalculation is handled in
  1634. // Connection.getPath on demand.
  1635. delete this.chartObstacles;
  1636. delete this.lineObstacles;
  1637. // Draw the pending connections
  1638. pathfinder.renderConnections(deferRender);
  1639. },
  1640. /**
  1641. * Draw the chart's connecting paths.
  1642. *
  1643. * @function Highcharts.Pathfinder#renderConnections
  1644. *
  1645. * @param {boolean} [deferRender]
  1646. * Whether or not to defer render until series animation is finished.
  1647. * Used on first render.
  1648. */
  1649. renderConnections: function (deferRender) {
  1650. if (deferRender) {
  1651. // Render after series are done animating
  1652. this.chart.series.forEach(function (series) {
  1653. var render = function () {
  1654. // Find pathfinder connections belonging to this series
  1655. // that haven't rendered, and render them now.
  1656. var pathfinder = series.chart.pathfinder, conns = pathfinder && pathfinder.connections || [];
  1657. conns.forEach(function (connection) {
  1658. if (connection.fromPoint &&
  1659. connection.fromPoint.series === series) {
  1660. connection.render();
  1661. }
  1662. });
  1663. if (series.pathfinderRemoveRenderEvent) {
  1664. series.pathfinderRemoveRenderEvent();
  1665. delete series.pathfinderRemoveRenderEvent;
  1666. }
  1667. };
  1668. if (series.options.animation === false) {
  1669. render();
  1670. }
  1671. else {
  1672. series.pathfinderRemoveRenderEvent = addEvent(series, 'afterAnimate', render);
  1673. }
  1674. });
  1675. }
  1676. else {
  1677. // Go through connections and render them
  1678. this.connections.forEach(function (connection) {
  1679. connection.render();
  1680. });
  1681. }
  1682. },
  1683. /**
  1684. * Get obstacles for the points in the chart. Does not include connecting
  1685. * lines from Pathfinder. Applies algorithmMargin to the obstacles.
  1686. *
  1687. * @function Highcharts.Pathfinder#getChartObstacles
  1688. *
  1689. * @param {object} options
  1690. * Options for the calculation. Currenlty only
  1691. * options.algorithmMargin.
  1692. *
  1693. * @return {Array<object>}
  1694. * An array of calculated obstacles. Each obstacle is defined as an
  1695. * object with xMin, xMax, yMin and yMax properties.
  1696. */
  1697. getChartObstacles: function (options) {
  1698. var obstacles = [], series = this.chart.series, margin = pick(options.algorithmMargin, 0), calculatedMargin;
  1699. for (var i = 0, sLen = series.length; i < sLen; ++i) {
  1700. if (series[i].visible && !series[i].options.isInternal) {
  1701. for (var j = 0, pLen = series[i].points.length, bb, point; j < pLen; ++j) {
  1702. point = series[i].points[j];
  1703. if (point.visible) {
  1704. bb = getPointBB(point);
  1705. if (bb) {
  1706. obstacles.push({
  1707. xMin: bb.xMin - margin,
  1708. xMax: bb.xMax + margin,
  1709. yMin: bb.yMin - margin,
  1710. yMax: bb.yMax + margin
  1711. });
  1712. }
  1713. }
  1714. }
  1715. }
  1716. }
  1717. // Sort obstacles by xMin for optimization
  1718. obstacles = obstacles.sort(function (a, b) {
  1719. return a.xMin - b.xMin;
  1720. });
  1721. // Add auto-calculated margin if the option is not defined
  1722. if (!defined(options.algorithmMargin)) {
  1723. calculatedMargin =
  1724. options.algorithmMargin =
  1725. calculateObstacleMargin(obstacles);
  1726. obstacles.forEach(function (obstacle) {
  1727. obstacle.xMin -= calculatedMargin;
  1728. obstacle.xMax += calculatedMargin;
  1729. obstacle.yMin -= calculatedMargin;
  1730. obstacle.yMax += calculatedMargin;
  1731. });
  1732. }
  1733. return obstacles;
  1734. },
  1735. /**
  1736. * Utility function to get metrics for obstacles:
  1737. * - Widest obstacle width
  1738. * - Tallest obstacle height
  1739. *
  1740. * @function Highcharts.Pathfinder#getObstacleMetrics
  1741. *
  1742. * @param {Array<object>} obstacles
  1743. * An array of obstacles to inspect.
  1744. *
  1745. * @return {object}
  1746. * The calculated metrics, as an object with maxHeight and maxWidth
  1747. * properties.
  1748. */
  1749. getObstacleMetrics: function (obstacles) {
  1750. var maxWidth = 0, maxHeight = 0, width, height, i = obstacles.length;
  1751. while (i--) {
  1752. width = obstacles[i].xMax - obstacles[i].xMin;
  1753. height = obstacles[i].yMax - obstacles[i].yMin;
  1754. if (maxWidth < width) {
  1755. maxWidth = width;
  1756. }
  1757. if (maxHeight < height) {
  1758. maxHeight = height;
  1759. }
  1760. }
  1761. return {
  1762. maxHeight: maxHeight,
  1763. maxWidth: maxWidth
  1764. };
  1765. },
  1766. /**
  1767. * Utility to get which direction to start the pathfinding algorithm
  1768. * (X vs Y), calculated from a set of marker options.
  1769. *
  1770. * @function Highcharts.Pathfinder#getAlgorithmStartDirection
  1771. *
  1772. * @param {Highcharts.ConnectorsMarkerOptions} markerOptions
  1773. * Marker options to calculate from.
  1774. *
  1775. * @return {boolean}
  1776. * Returns true for X, false for Y, and undefined for autocalculate.
  1777. */
  1778. getAlgorithmStartDirection: function (markerOptions) {
  1779. var xCenter = markerOptions.align !== 'left' &&
  1780. markerOptions.align !== 'right', yCenter = markerOptions.verticalAlign !== 'top' &&
  1781. markerOptions.verticalAlign !== 'bottom', undef;
  1782. return xCenter ?
  1783. (yCenter ? undef : false) : // x is centered
  1784. (yCenter ? true : undef); // x is off-center
  1785. }
  1786. };
  1787. // Add to Highcharts namespace
  1788. H.Connection = Connection;
  1789. H.Pathfinder = Pathfinder;
  1790. // Add pathfinding capabilities to Points
  1791. extend(Point.prototype, /** @lends Point.prototype */ {
  1792. /**
  1793. * Get coordinates of anchor point for pathfinder connection.
  1794. *
  1795. * @private
  1796. * @function Highcharts.Point#getPathfinderAnchorPoint
  1797. *
  1798. * @param {Highcharts.ConnectorsMarkerOptions} markerOptions
  1799. * Connection options for position on point.
  1800. *
  1801. * @return {Highcharts.PositionObject}
  1802. * An object with x/y properties for the position. Coordinates are
  1803. * in plot values, not relative to point.
  1804. */
  1805. getPathfinderAnchorPoint: function (markerOptions) {
  1806. var bb = getPointBB(this), x, y;
  1807. switch (markerOptions.align) { // eslint-disable-line default-case
  1808. case 'right':
  1809. x = 'xMax';
  1810. break;
  1811. case 'left':
  1812. x = 'xMin';
  1813. }
  1814. switch (markerOptions.verticalAlign) { // eslint-disable-line default-case
  1815. case 'top':
  1816. y = 'yMin';
  1817. break;
  1818. case 'bottom':
  1819. y = 'yMax';
  1820. }
  1821. return {
  1822. x: x ? bb[x] : (bb.xMin + bb.xMax) / 2,
  1823. y: y ? bb[y] : (bb.yMin + bb.yMax) / 2
  1824. };
  1825. },
  1826. /**
  1827. * Utility to get the angle from one point to another.
  1828. *
  1829. * @private
  1830. * @function Highcharts.Point#getRadiansToVector
  1831. *
  1832. * @param {Highcharts.PositionObject} v1
  1833. * The first vector, as an object with x/y properties.
  1834. *
  1835. * @param {Highcharts.PositionObject} v2
  1836. * The second vector, as an object with x/y properties.
  1837. *
  1838. * @return {number}
  1839. * The angle in degrees
  1840. */
  1841. getRadiansToVector: function (v1, v2) {
  1842. var box;
  1843. if (!defined(v2)) {
  1844. box = getPointBB(this);
  1845. if (box) {
  1846. v2 = {
  1847. x: (box.xMin + box.xMax) / 2,
  1848. y: (box.yMin + box.yMax) / 2
  1849. };
  1850. }
  1851. }
  1852. return Math.atan2(v2.y - v1.y, v1.x - v2.x);
  1853. },
  1854. /**
  1855. * Utility to get the position of the marker, based on the path angle and
  1856. * the marker's radius.
  1857. *
  1858. * @private
  1859. * @function Highcharts.Point#getMarkerVector
  1860. *
  1861. * @param {number} radians
  1862. * The angle in radians from the point center to another vector.
  1863. *
  1864. * @param {number} markerRadius
  1865. * The radius of the marker, to calculate the additional distance to
  1866. * the center of the marker.
  1867. *
  1868. * @param {object} anchor
  1869. * The anchor point of the path and marker as an object with x/y
  1870. * properties.
  1871. *
  1872. * @return {object}
  1873. * The marker vector as an object with x/y properties.
  1874. */
  1875. getMarkerVector: function (radians, markerRadius, anchor) {
  1876. var twoPI = Math.PI * 2.0, theta = radians, bb = getPointBB(this), rectWidth = bb.xMax - bb.xMin, rectHeight = bb.yMax - bb.yMin, rAtan = Math.atan2(rectHeight, rectWidth), tanTheta = 1, leftOrRightRegion = false, rectHalfWidth = rectWidth / 2.0, rectHalfHeight = rectHeight / 2.0, rectHorizontalCenter = bb.xMin + rectHalfWidth, rectVerticalCenter = bb.yMin + rectHalfHeight, edgePoint = {
  1877. x: rectHorizontalCenter,
  1878. y: rectVerticalCenter
  1879. }, markerPoint = {}, xFactor = 1, yFactor = 1;
  1880. while (theta < -Math.PI) {
  1881. theta += twoPI;
  1882. }
  1883. while (theta > Math.PI) {
  1884. theta -= twoPI;
  1885. }
  1886. tanTheta = Math.tan(theta);
  1887. if ((theta > -rAtan) && (theta <= rAtan)) {
  1888. // Right side
  1889. yFactor = -1;
  1890. leftOrRightRegion = true;
  1891. }
  1892. else if (theta > rAtan && theta <= (Math.PI - rAtan)) {
  1893. // Top side
  1894. yFactor = -1;
  1895. }
  1896. else if (theta > (Math.PI - rAtan) || theta <= -(Math.PI - rAtan)) {
  1897. // Left side
  1898. xFactor = -1;
  1899. leftOrRightRegion = true;
  1900. }
  1901. else {
  1902. // Bottom side
  1903. xFactor = -1;
  1904. }
  1905. // Correct the edgePoint according to the placement of the marker
  1906. if (leftOrRightRegion) {
  1907. edgePoint.x += xFactor * (rectHalfWidth);
  1908. edgePoint.y += yFactor * (rectHalfWidth) * tanTheta;
  1909. }
  1910. else {
  1911. edgePoint.x += xFactor * (rectHeight / (2.0 * tanTheta));
  1912. edgePoint.y += yFactor * (rectHalfHeight);
  1913. }
  1914. if (anchor.x !== rectHorizontalCenter) {
  1915. edgePoint.x = anchor.x;
  1916. }
  1917. if (anchor.y !== rectVerticalCenter) {
  1918. edgePoint.y = anchor.y;
  1919. }
  1920. markerPoint.x = edgePoint.x + (markerRadius * Math.cos(theta));
  1921. markerPoint.y = edgePoint.y - (markerRadius * Math.sin(theta));
  1922. return markerPoint;
  1923. }
  1924. });
  1925. /**
  1926. * Warn if using legacy options. Copy the options over. Note that this will
  1927. * still break if using the legacy options in chart.update, addSeries etc.
  1928. * @private
  1929. */
  1930. function warnLegacy(chart) {
  1931. if (chart.options.pathfinder ||
  1932. chart.series.reduce(function (acc, series) {
  1933. if (series.options) {
  1934. merge(true, (series.options.connectors = series.options.connectors ||
  1935. {}), series.options.pathfinder);
  1936. }
  1937. return acc || series.options && series.options.pathfinder;
  1938. }, false)) {
  1939. merge(true, (chart.options.connectors = chart.options.connectors || {}), chart.options.pathfinder);
  1940. error('WARNING: Pathfinder options have been renamed. ' +
  1941. 'Use "chart.connectors" or "series.connectors" instead.');
  1942. }
  1943. }
  1944. // Initialize Pathfinder for charts
  1945. Chart.prototype.callbacks.push(function (chart) {
  1946. var options = chart.options;
  1947. if (options.connectors.enabled !== false) {
  1948. warnLegacy(chart);
  1949. this.pathfinder = new Pathfinder(this);
  1950. this.pathfinder.update(true); // First draw, defer render
  1951. }
  1952. });
  1953. });
  1954. _registerModule(_modules, 'masters/modules/pathfinder.src.js', [], function () {
  1955. });
  1956. }));