draggable-points.src.js 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  1. /**
  2. * @license Highcharts JS v8.1.2 (2020-06-16)
  3. *
  4. * (c) 2009-2019 Torstein Honsi
  5. *
  6. * License: www.highcharts.com/license
  7. */
  8. 'use strict';
  9. (function (factory) {
  10. if (typeof module === 'object' && module.exports) {
  11. factory['default'] = factory;
  12. module.exports = factory;
  13. } else if (typeof define === 'function' && define.amd) {
  14. define('highcharts/modules/draggable-points', ['highcharts'], function (Highcharts) {
  15. factory(Highcharts);
  16. factory.Highcharts = Highcharts;
  17. return factory;
  18. });
  19. } else {
  20. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  21. }
  22. }(function (Highcharts) {
  23. var _modules = Highcharts ? Highcharts._modules : {};
  24. function _registerModule(obj, path, args, fn) {
  25. if (!obj.hasOwnProperty(path)) {
  26. obj[path] = fn.apply(null, args);
  27. }
  28. }
  29. _registerModule(_modules, 'modules/draggable-points.src.js', [_modules['parts/Globals.js'], _modules['parts/Point.js'], _modules['parts/Utilities.js']], function (H, Point, U) {
  30. /* *
  31. *
  32. * (c) 2009-2020 Highsoft AS
  33. *
  34. * Authors: Øystein Moseng, Torstein Hønsi, Jon A. Nygård
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var addEvent = U.addEvent, clamp = U.clamp, merge = U.merge, objectEach = U.objectEach, pick = U.pick;
  42. /**
  43. * Current drag and drop position.
  44. *
  45. * @interface Highcharts.DragDropPositionObject
  46. */ /**
  47. * Chart x position
  48. * @name Highcharts.DragDropPositionObject#chartX
  49. * @type {number}
  50. */ /**
  51. * Chart y position
  52. * @name Highcharts.DragDropPositionObject#chartY
  53. * @type {number}
  54. */ /**
  55. * Drag and drop guide box.
  56. * @name Highcharts.DragDropPositionObject#guideBox
  57. * @type {Highcharts.BBoxObject|undefined}
  58. */ /**
  59. * Updated point data.
  60. * @name Highcharts.DragDropPositionObject#points
  61. * @type {Highcharts.Dictionary<Highcharts.Dictionary<number>>}
  62. */ /**
  63. * Delta of previous x position.
  64. * @name Highcharts.DragDropPositionObject#prevdX
  65. * @type {number|undefined}
  66. */ /**
  67. * Delta of previous y position.
  68. * @name Highcharts.DragDropPositionObject#prevdY
  69. * @type {number|undefined}
  70. */
  71. /**
  72. * Function callback to execute while series points are dragged. Return false to
  73. * stop the default drag action.
  74. *
  75. * @callback Highcharts.PointDragCallbackFunction
  76. *
  77. * @param {Highcharts.Point} this
  78. * Point where the event occured.
  79. *
  80. * @param {Highcharts.PointDragEventObject} event
  81. * Event arguments.
  82. */
  83. /**
  84. * Contains information about a points new values.
  85. *
  86. * @interface Highcharts.PointDragDropObject
  87. */ /**
  88. * New values.
  89. * @name Highcharts.PointDragDropObject#newValues
  90. * @type {Highcharts.Dictionary<number>}
  91. */ /**
  92. * Updated point.
  93. * @name Highcharts.PointDragDropObject#point
  94. * @type {Highcharts.Point}
  95. */
  96. /**
  97. * Contains common information for a drag event on series points.
  98. *
  99. * @interface Highcharts.PointDragEventObject
  100. */ /**
  101. * New point after drag if only a single one.
  102. * @name Highcharts.PointDropEventObject#newPoint
  103. * @type {Highcharts.PointDragDropObject|undefined}
  104. */ /**
  105. * New point id after drag if only a single one.
  106. * @name Highcharts.PointDropEventObject#newPointId
  107. * @type {string|undefined}
  108. */ /**
  109. * New points during drag.
  110. * @name Highcharts.PointDragEventObject#newPoints
  111. * @type {Highcharts.Dictionary<Highcharts.PointDragDropObject>}
  112. */ /**
  113. * Original data.
  114. * @name Highcharts.PointDragEventObject#origin
  115. * @type {Highcharts.DragDropPositionObject}
  116. */ /**
  117. * Prevent default drag action.
  118. * @name Highcharts.PointDragEventObject#preventDefault
  119. * @type {Function}
  120. */ /**
  121. * Target point that caused the event.
  122. * @name Highcharts.PointDragEventObject#target
  123. * @type {Highcharts.Point}
  124. */ /**
  125. * Event type.
  126. * @name Highcharts.PointDragEventObject#type
  127. * @type {"drag"}
  128. */
  129. /**
  130. * Function callback to execute when a series point is dragged.
  131. *
  132. * @callback Highcharts.PointDragStartCallbackFunction
  133. *
  134. * @param {Highcharts.Point} this
  135. * Point where the event occured.
  136. *
  137. * @param {Highcharts.PointDragStartEventObject} event
  138. * Event arguments.
  139. */
  140. /**
  141. * Contains common information for a drag event on series point.
  142. *
  143. * @interface Highcharts.PointDragStartEventObject
  144. * @extends global.MouseEvent
  145. */ /**
  146. * Data property being dragged.
  147. * @name Highcharts.PointDragStartEventObject#updateProp
  148. * @type {string|undefined}
  149. */
  150. /**
  151. * Function callback to execute when series points are dropped.
  152. *
  153. * @callback Highcharts.PointDropCallbackFunction
  154. *
  155. * @param {Highcharts.Point} this
  156. * Point where the event occured.
  157. *
  158. * @param {Highcharts.PointDropEventObject} event
  159. * Event arguments.
  160. */
  161. /**
  162. * Contains common information for a drop event on series points.
  163. *
  164. * @interface Highcharts.PointDropEventObject
  165. */ /**
  166. * New point after drop if only a single one.
  167. * @name Highcharts.PointDropEventObject#newPoint
  168. * @type {Highcharts.PointDragDropObject|undefined}
  169. */ /**
  170. * New point id after drop if only a single one.
  171. * @name Highcharts.PointDropEventObject#newPointId
  172. * @type {string|undefined}
  173. */ /**
  174. * New points after drop.
  175. * @name Highcharts.PointDropEventObject#newPoints
  176. * @type {Highcharts.Dictionary<Highcharts.PointDragDropObject>}
  177. */ /**
  178. * Number of new points.
  179. * @name Highcharts.PointDropEventObject#numNewPoints
  180. * @type {number}
  181. */ /**
  182. * Original data.
  183. * @name Highcharts.PointDropEventObject#origin
  184. * @type {Highcharts.DragDropPositionObject}
  185. */ /**
  186. * Prevent default drop action.
  187. * @name Highcharts.PointDropEventObject#preventDefault
  188. * @type {Function}
  189. */ /**
  190. * Target point that caused the event.
  191. * @name Highcharts.PointDropEventObject#target
  192. * @type {Highcharts.Point}
  193. */ /**
  194. * Event type.
  195. * @name Highcharts.PointDropEventObject#type
  196. * @type {"drop"}
  197. */
  198. ''; // detaches doclets above
  199. var seriesTypes = H.seriesTypes;
  200. /**
  201. * Flip a side property, used with resizeRect. If input side is "left", return
  202. * "right" etc.
  203. *
  204. * @private
  205. * @function flipResizeSide
  206. *
  207. * @param {string} side
  208. * Side prop to flip. Can be `left`, `right`, `top` or `bottom`.
  209. *
  210. * @return {"bottom"|"left"|"right"|"top"|undefined}
  211. * The flipped side.
  212. */
  213. function flipResizeSide(side) {
  214. return {
  215. left: 'right',
  216. right: 'left',
  217. top: 'bottom',
  218. bottom: 'top'
  219. }[side];
  220. }
  221. /* @todo
  222. Add drag/drop support to specific data props for different series types.
  223. The dragDrop.draggableX/Y user options on series enable/disable all of these per
  224. irection unless they are specifically set in options using
  225. dragDrop.{optionName}. If the prop does not specify an optionName here, it can
  226. only be enabled/disabled by the user with draggableX/Y.
  227. Supported options for each prop:
  228. optionName: User option in series.dragDrop that enables/disables
  229. dragging this prop.
  230. axis: Can be 'x' or 'y'. Whether this prop is linked to x or y axis.
  231. move: Whether or not this prop should be updated when moving points.
  232. resize: Whether or not to draw a drag handle and allow user to drag and
  233. update this prop by itself.
  234. beforeResize: Hook to perform tasks before a resize is made. Gets
  235. the guide box, the new points values, and the point as args.
  236. resizeSide: Which side of the guide box to resize when dragging the
  237. handle. Can be "left", "right", "top", "bottom". Chart.inverted is
  238. handled automatically. Can also be a function, taking the new point
  239. values as parameter, as well as the point, and returning a string
  240. with the side.
  241. propValidate: Function that takes the prop value and the point as
  242. arguments, and returns true if the prop value is valid, false if
  243. not. It is used to prevent e.g. resizing "low" above "high".
  244. handlePositioner: For resizeable props, return 0,0 in SVG plot coords of
  245. where to place the dragHandle. Gets point as argument. Should return
  246. object with x and y properties.
  247. handleFormatter: For resizeable props, return the path of the drag
  248. handle as an SVG path array. Gets the point as argument. The handle
  249. is translated according to handlePositioner.
  250. handleOptions: Options to merge with the default handle options.
  251. TODO:
  252. - It makes sense to have support for resizing the size of bubbles and
  253. e.g variwide columns. This requires us to support dragging along a
  254. z-axis, somehow computing a relative value from old to new pixel
  255. size.
  256. - Moving maps could be useful, although we would have to compute new
  257. point.path values in order to do it properly (using SVG translate
  258. is easier, but won't update the data).
  259. */
  260. // 90deg rotated column handle path, used in multiple series types
  261. var horizHandleFormatter = function (point) {
  262. var shapeArgs = point.shapeArgs || point.graphic.getBBox(), top = shapeArgs.r || 0, // Rounding of bar corners
  263. bottom = shapeArgs.height - top, centerY = shapeArgs.height / 2;
  264. return [
  265. // Top wick
  266. ['M', 0, top],
  267. ['L', 0, centerY - 5],
  268. // Circle
  269. ['A', 1, 1, 0, 0, 0, 0, centerY + 5],
  270. ['A', 1, 1, 0, 0, 0, 0, centerY - 5],
  271. // Bottom wick
  272. ['M', 0, centerY + 5],
  273. ['L', 0, bottom]
  274. ];
  275. };
  276. // Line series - only draggableX/Y, no drag handles
  277. var lineDragDropProps = seriesTypes.line.prototype.dragDropProps = {
  278. x: {
  279. axis: 'x',
  280. move: true
  281. },
  282. y: {
  283. axis: 'y',
  284. move: true
  285. }
  286. };
  287. // Flag series - same as line/scatter
  288. if (seriesTypes.flags) {
  289. seriesTypes.flags.prototype.dragDropProps = lineDragDropProps;
  290. }
  291. // Column series - x can be moved, y can only be resized. Note extra
  292. // functionality for handling upside down columns (below threshold).
  293. var columnDragDropProps = seriesTypes.column.prototype.dragDropProps = {
  294. x: {
  295. axis: 'x',
  296. move: true
  297. },
  298. y: {
  299. axis: 'y',
  300. move: false,
  301. resize: true,
  302. // Force guideBox start coordinates
  303. beforeResize: function (guideBox, pointVals, point) {
  304. // We need to ensure that guideBox always starts at threshold.
  305. // We flip whether or not we update the top or bottom of the guide
  306. // box at threshold, but if we drag the mouse fast, the top has not
  307. // reached threshold before we cross over and update the bottom.
  308. var threshold = point.series.translatedThreshold, y = guideBox.attr('y'), height, diff;
  309. if (pointVals.y >= point.series.options.threshold || 0) {
  310. // Above threshold - always set height to hit the threshold
  311. height = guideBox.attr('height');
  312. diff = threshold ?
  313. threshold - (y + height) :
  314. 0;
  315. guideBox.attr({
  316. height: Math.max(0, Math.round(height + diff))
  317. });
  318. }
  319. else {
  320. // Below - always set y to start at threshold
  321. guideBox.attr({
  322. y: Math.round(y + (threshold ? threshold - y : 0))
  323. });
  324. }
  325. },
  326. // Flip the side of the resize handle if column is below threshold.
  327. // Make sure we remove the handle on the other side.
  328. resizeSide: function (pointVals, point) {
  329. var chart = point.series.chart, dragHandles = chart.dragHandles, side = pointVals.y >= (point.series.options.threshold || 0) ?
  330. 'top' : 'bottom', flipSide = flipResizeSide(side);
  331. // Force remove handle on other side
  332. if (dragHandles[flipSide]) {
  333. dragHandles[flipSide].destroy();
  334. delete dragHandles[flipSide];
  335. }
  336. return side;
  337. },
  338. // Position handle at bottom if column is below threshold
  339. handlePositioner: function (point) {
  340. var bBox = point.shapeArgs || point.graphic.getBBox();
  341. return {
  342. x: bBox.x,
  343. y: point.y >= (point.series.options.threshold || 0) ?
  344. bBox.y : bBox.y + bBox.height
  345. };
  346. },
  347. // Horizontal handle
  348. handleFormatter: function (point) {
  349. var shapeArgs = point.shapeArgs || {}, radius = shapeArgs.r || 0, // Rounding of bar corners
  350. width = shapeArgs.width || 0, centerX = width / 2;
  351. return [
  352. // Left wick
  353. ['M', radius, 0],
  354. ['L', centerX - 5, 0],
  355. // Circle
  356. ['A', 1, 1, 0, 0, 0, centerX + 5, 0],
  357. ['A', 1, 1, 0, 0, 0, centerX - 5, 0],
  358. // Right wick
  359. ['M', centerX + 5, 0],
  360. ['L', width - radius, 0]
  361. ];
  362. }
  363. }
  364. };
  365. // Bullet graph, x/y same as column, but also allow target to be dragged.
  366. if (seriesTypes.bullet) {
  367. seriesTypes.bullet.prototype.dragDropProps = {
  368. x: columnDragDropProps.x,
  369. y: columnDragDropProps.y,
  370. /**
  371. * Allow target value to be dragged individually.
  372. *
  373. * @type {boolean}
  374. * @default true
  375. * @requires modules/draggable-points
  376. * @apioption plotOptions.bullet.dragDrop.draggableTarget
  377. */
  378. target: {
  379. optionName: 'draggableTarget',
  380. axis: 'y',
  381. move: true,
  382. resize: true,
  383. resizeSide: 'top',
  384. handlePositioner: function (point) {
  385. var bBox = point.targetGraphic.getBBox();
  386. return {
  387. x: point.barX,
  388. y: bBox.y + bBox.height / 2
  389. };
  390. },
  391. handleFormatter: columnDragDropProps.y.handleFormatter
  392. }
  393. };
  394. }
  395. // Columnrange series - move x, resize or move low/high
  396. if (seriesTypes.columnrange) {
  397. seriesTypes.columnrange.prototype.dragDropProps = {
  398. x: {
  399. axis: 'x',
  400. move: true
  401. },
  402. /**
  403. * Allow low value to be dragged individually.
  404. *
  405. * @type {boolean}
  406. * @default true
  407. * @requires modules/draggable-points
  408. * @apioption plotOptions.columnrange.dragDrop.draggableLow
  409. */
  410. low: {
  411. optionName: 'draggableLow',
  412. axis: 'y',
  413. move: true,
  414. resize: true,
  415. resizeSide: 'bottom',
  416. handlePositioner: function (point) {
  417. var bBox = point.shapeArgs || point.graphic.getBBox();
  418. return {
  419. x: bBox.x,
  420. y: bBox.y + bBox.height
  421. };
  422. },
  423. handleFormatter: columnDragDropProps.y.handleFormatter,
  424. propValidate: function (val, point) {
  425. return val <= point.high;
  426. }
  427. },
  428. /**
  429. * Allow high value to be dragged individually.
  430. *
  431. * @type {boolean}
  432. * @default true
  433. * @requires modules/draggable-points
  434. * @apioption plotOptions.columnrange.dragDrop.draggableHigh
  435. */
  436. high: {
  437. optionName: 'draggableHigh',
  438. axis: 'y',
  439. move: true,
  440. resize: true,
  441. resizeSide: 'top',
  442. handlePositioner: function (point) {
  443. var bBox = point.shapeArgs || point.graphic.getBBox();
  444. return {
  445. x: bBox.x,
  446. y: bBox.y
  447. };
  448. },
  449. handleFormatter: columnDragDropProps.y.handleFormatter,
  450. propValidate: function (val, point) {
  451. return val >= point.low;
  452. }
  453. }
  454. };
  455. }
  456. // Boxplot series - move x, resize or move low/q1/q3/high
  457. if (seriesTypes.boxplot) {
  458. seriesTypes.boxplot.prototype.dragDropProps = {
  459. x: columnDragDropProps.x,
  460. /**
  461. * Allow low value to be dragged individually.
  462. *
  463. * @type {boolean}
  464. * @default true
  465. * @requires modules/draggable-points
  466. * @apioption plotOptions.boxplot.dragDrop.draggableLow
  467. */
  468. low: {
  469. optionName: 'draggableLow',
  470. axis: 'y',
  471. move: true,
  472. resize: true,
  473. resizeSide: 'bottom',
  474. handlePositioner: function (point) {
  475. return {
  476. x: point.shapeArgs.x,
  477. y: point.lowPlot
  478. };
  479. },
  480. handleFormatter: columnDragDropProps.y.handleFormatter,
  481. propValidate: function (val, point) {
  482. return val <= point.q1;
  483. }
  484. },
  485. /**
  486. * Allow Q1 value to be dragged individually.
  487. *
  488. * @type {boolean}
  489. * @default true
  490. * @requires modules/draggable-points
  491. * @apioption plotOptions.boxplot.dragDrop.draggableQ1
  492. */
  493. q1: {
  494. optionName: 'draggableQ1',
  495. axis: 'y',
  496. move: true,
  497. resize: true,
  498. resizeSide: 'bottom',
  499. handlePositioner: function (point) {
  500. return {
  501. x: point.shapeArgs.x,
  502. y: point.q1Plot
  503. };
  504. },
  505. handleFormatter: columnDragDropProps.y.handleFormatter,
  506. propValidate: function (val, point) {
  507. return val <= point.median && val >= point.low;
  508. }
  509. },
  510. median: {
  511. // Median can not be dragged individually, just move the whole
  512. // point for this.
  513. axis: 'y',
  514. move: true
  515. },
  516. /**
  517. * Allow Q3 value to be dragged individually.
  518. *
  519. * @type {boolean}
  520. * @default true
  521. * @requires modules/draggable-points
  522. * @apioption plotOptions.boxplot.dragDrop.draggableQ3
  523. */
  524. q3: {
  525. optionName: 'draggableQ3',
  526. axis: 'y',
  527. move: true,
  528. resize: true,
  529. resizeSide: 'top',
  530. handlePositioner: function (point) {
  531. return {
  532. x: point.shapeArgs.x,
  533. y: point.q3Plot
  534. };
  535. },
  536. handleFormatter: columnDragDropProps.y.handleFormatter,
  537. propValidate: function (val, point) {
  538. return val <= point.high && val >= point.median;
  539. }
  540. },
  541. /**
  542. * Allow high value to be dragged individually.
  543. *
  544. * @type {boolean}
  545. * @default true
  546. * @requires modules/draggable-points
  547. * @apioption plotOptions.boxplot.dragDrop.draggableHigh
  548. */
  549. high: {
  550. optionName: 'draggableHigh',
  551. axis: 'y',
  552. move: true,
  553. resize: true,
  554. resizeSide: 'top',
  555. handlePositioner: function (point) {
  556. return {
  557. x: point.shapeArgs.x,
  558. y: point.highPlot
  559. };
  560. },
  561. handleFormatter: columnDragDropProps.y.handleFormatter,
  562. propValidate: function (val, point) {
  563. return val >= point.q3;
  564. }
  565. }
  566. };
  567. }
  568. // OHLC series - move x, resize or move open/high/low/close
  569. if (seriesTypes.ohlc) {
  570. seriesTypes.ohlc.prototype.dragDropProps = {
  571. x: columnDragDropProps.x,
  572. /**
  573. * Allow low value to be dragged individually.
  574. *
  575. * @type {boolean}
  576. * @default true
  577. * @requires modules/draggable-points
  578. * @apioption plotOptions.ohlc.dragDrop.draggableLow
  579. */
  580. low: {
  581. optionName: 'draggableLow',
  582. axis: 'y',
  583. move: true,
  584. resize: true,
  585. resizeSide: 'bottom',
  586. handlePositioner: function (point) {
  587. return {
  588. x: point.shapeArgs.x,
  589. y: point.plotLow
  590. };
  591. },
  592. handleFormatter: columnDragDropProps.y.handleFormatter,
  593. propValidate: function (val, point) {
  594. return val <= point.open && val <= point.close;
  595. }
  596. },
  597. /**
  598. * Allow high value to be dragged individually.
  599. *
  600. * @type {boolean}
  601. * @default true
  602. * @requires modules/draggable-points
  603. * @apioption plotOptions.ohlc.dragDrop.draggableHigh
  604. */
  605. high: {
  606. optionName: 'draggableHigh',
  607. axis: 'y',
  608. move: true,
  609. resize: true,
  610. resizeSide: 'top',
  611. handlePositioner: function (point) {
  612. return {
  613. x: point.shapeArgs.x,
  614. y: point.plotHigh
  615. };
  616. },
  617. handleFormatter: columnDragDropProps.y.handleFormatter,
  618. propValidate: function (val, point) {
  619. return val >= point.open && val >= point.close;
  620. }
  621. },
  622. /**
  623. * Allow open value to be dragged individually.
  624. *
  625. * @type {boolean}
  626. * @default true
  627. * @requires modules/draggable-points
  628. * @apioption plotOptions.ohlc.dragDrop.draggableOpen
  629. */
  630. open: {
  631. optionName: 'draggableOpen',
  632. axis: 'y',
  633. move: true,
  634. resize: true,
  635. resizeSide: function (point) {
  636. return point.open >= point.close ? 'top' : 'bottom';
  637. },
  638. handlePositioner: function (point) {
  639. return {
  640. x: point.shapeArgs.x,
  641. y: point.plotOpen
  642. };
  643. },
  644. handleFormatter: columnDragDropProps.y.handleFormatter,
  645. propValidate: function (val, point) {
  646. return val <= point.high && val >= point.low;
  647. }
  648. },
  649. /**
  650. * Allow close value to be dragged individually.
  651. *
  652. * @type {boolean}
  653. * @default true
  654. * @requires modules/draggable-points
  655. * @apioption plotOptions.ohlc.dragDrop.draggableClose
  656. */
  657. close: {
  658. optionName: 'draggableClose',
  659. axis: 'y',
  660. move: true,
  661. resize: true,
  662. resizeSide: function (point) {
  663. return point.open >= point.close ? 'bottom' : 'top';
  664. },
  665. handlePositioner: function (point) {
  666. return {
  667. x: point.shapeArgs.x,
  668. y: point.plotClose
  669. };
  670. },
  671. handleFormatter: columnDragDropProps.y.handleFormatter,
  672. propValidate: function (val, point) {
  673. return val <= point.high && val >= point.low;
  674. }
  675. }
  676. };
  677. }
  678. // Arearange series - move x, resize or move low/high
  679. if (seriesTypes.arearange) {
  680. var columnrangeDragDropProps = seriesTypes.columnrange.prototype.dragDropProps,
  681. // Use a circle covering the marker as drag handle
  682. arearangeHandleFormatter = function (point) {
  683. var radius = point.graphic ?
  684. point.graphic.getBBox().width / 2 + 1 :
  685. 4;
  686. return [
  687. ['M', 0 - radius, 0],
  688. ['a', radius, radius, 0, 1, 0, radius * 2, 0],
  689. ['a', radius, radius, 0, 1, 0, radius * -2, 0]
  690. ];
  691. };
  692. seriesTypes.arearange.prototype.dragDropProps = {
  693. x: columnrangeDragDropProps.x,
  694. /**
  695. * Allow low value to be dragged individually.
  696. *
  697. * @type {boolean}
  698. * @default true
  699. * @requires modules/draggable-points
  700. * @apioption plotOptions.arearange.dragDrop.draggableLow
  701. */
  702. low: {
  703. optionName: 'draggableLow',
  704. axis: 'y',
  705. move: true,
  706. resize: true,
  707. resizeSide: 'bottom',
  708. handlePositioner: function (point) {
  709. var bBox = point.lowerGraphic && point.lowerGraphic.getBBox();
  710. return bBox ? {
  711. x: bBox.x + bBox.width / 2,
  712. y: bBox.y + bBox.height / 2
  713. } : { x: -999, y: -999 };
  714. },
  715. handleFormatter: arearangeHandleFormatter,
  716. propValidate: columnrangeDragDropProps.low.propValidate
  717. },
  718. /**
  719. * Allow high value to be dragged individually.
  720. *
  721. * @type {boolean}
  722. * @default true
  723. * @requires modules/draggable-points
  724. * @apioption plotOptions.arearange.dragDrop.draggableHigh
  725. */
  726. high: {
  727. optionName: 'draggableHigh',
  728. axis: 'y',
  729. move: true,
  730. resize: true,
  731. resizeSide: 'top',
  732. handlePositioner: function (point) {
  733. var bBox = point.upperGraphic && point.upperGraphic.getBBox();
  734. return bBox ? {
  735. x: bBox.x + bBox.width / 2,
  736. y: bBox.y + bBox.height / 2
  737. } : { x: -999, y: -999 };
  738. },
  739. handleFormatter: arearangeHandleFormatter,
  740. propValidate: columnrangeDragDropProps.high.propValidate
  741. }
  742. };
  743. }
  744. // Waterfall - mostly as column, but don't show drag handles for sum points
  745. if (seriesTypes.waterfall) {
  746. seriesTypes.waterfall.prototype.dragDropProps = {
  747. x: columnDragDropProps.x,
  748. y: merge(columnDragDropProps.y, {
  749. handleFormatter: function (point) {
  750. return point.isSum || point.isIntermediateSum ? null :
  751. columnDragDropProps.y.handleFormatter(point);
  752. }
  753. })
  754. };
  755. }
  756. // Xrange - resize/move x/x2, and move y
  757. if (seriesTypes.xrange) {
  758. // Handle positioner logic is the same for x and x2 apart from the
  759. // x value. shapeArgs does not take yAxis reversed etc into account, so we
  760. // use axis.toPixels to handle positioning.
  761. var xrangeHandlePositioner = function (point, xProp) {
  762. var series = point.series, xAxis = series.xAxis, yAxis = series.yAxis, inverted = series.chart.inverted,
  763. // Using toPixels handles axis.reversed, but doesn't take
  764. // chart.inverted into account.
  765. newX = xAxis.toPixels(point[xProp], true), newY = yAxis.toPixels(point.y, true), offsetY = series.columnMetrics ? series.columnMetrics.offset :
  766. -point.shapeArgs.height / 2;
  767. // Handle chart inverted
  768. if (inverted) {
  769. newX = xAxis.len - newX;
  770. newY = yAxis.len - newY;
  771. }
  772. newY += offsetY; // (#12872)
  773. return {
  774. x: Math.round(newX),
  775. y: Math.round(newY)
  776. };
  777. }, xrangeDragDropProps = seriesTypes.xrange.prototype.dragDropProps = {
  778. y: {
  779. axis: 'y',
  780. move: true
  781. },
  782. /**
  783. * Allow x value to be dragged individually.
  784. *
  785. * @type {boolean}
  786. * @default true
  787. * @requires modules/draggable-points
  788. * @apioption plotOptions.xrange.dragDrop.draggableX1
  789. */
  790. x: {
  791. optionName: 'draggableX1',
  792. axis: 'x',
  793. move: true,
  794. resize: true,
  795. resizeSide: 'left',
  796. handlePositioner: function (point) {
  797. return xrangeHandlePositioner(point, 'x');
  798. },
  799. handleFormatter: horizHandleFormatter,
  800. propValidate: function (val, point) {
  801. return val <= point.x2;
  802. }
  803. },
  804. /**
  805. * Allow x2 value to be dragged individually.
  806. *
  807. * @type {boolean}
  808. * @default true
  809. * @requires modules/draggable-points
  810. * @apioption plotOptions.xrange.dragDrop.draggableX2
  811. */
  812. x2: {
  813. optionName: 'draggableX2',
  814. axis: 'x',
  815. move: true,
  816. resize: true,
  817. resizeSide: 'right',
  818. handlePositioner: function (point) {
  819. return xrangeHandlePositioner(point, 'x2');
  820. },
  821. handleFormatter: horizHandleFormatter,
  822. propValidate: function (val, point) {
  823. return val >= point.x;
  824. }
  825. }
  826. };
  827. // Gantt - same as xrange, but with aliases
  828. if (seriesTypes.gantt) {
  829. seriesTypes.gantt.prototype.dragDropProps = {
  830. y: xrangeDragDropProps.y,
  831. /**
  832. * Allow start value to be dragged individually.
  833. *
  834. * @type {boolean}
  835. * @default true
  836. * @requires modules/draggable-points
  837. * @apioption plotOptions.gantt.dragDrop.draggableStart
  838. */
  839. start: merge(xrangeDragDropProps.x, {
  840. optionName: 'draggableStart',
  841. // Do not allow individual drag handles for milestones
  842. validateIndividualDrag: function (point) {
  843. return !point.milestone;
  844. }
  845. }),
  846. /**
  847. * Allow end value to be dragged individually.
  848. *
  849. * @type {boolean}
  850. * @default true
  851. * @requires modules/draggable-points
  852. * @apioption plotOptions.gantt.dragDrop.draggableEnd
  853. */
  854. end: merge(xrangeDragDropProps.x2, {
  855. optionName: 'draggableEnd',
  856. // Do not allow individual drag handles for milestones
  857. validateIndividualDrag: function (point) {
  858. return !point.milestone;
  859. }
  860. })
  861. };
  862. }
  863. }
  864. // Don't support certain series types
  865. [
  866. 'gauge',
  867. 'pie',
  868. 'sunburst',
  869. 'wordcloud',
  870. 'sankey',
  871. 'histogram',
  872. 'pareto',
  873. 'vector',
  874. 'windbarb',
  875. 'treemap',
  876. 'bellcurve',
  877. 'sma',
  878. 'map',
  879. 'mapline'
  880. ].forEach(
  881. /**
  882. * @private
  883. * @param {string} type
  884. * Unsupported series type
  885. * @return {void}
  886. */
  887. function (type) {
  888. if (seriesTypes[type]) {
  889. seriesTypes[type].prototype.dragDropProps = null;
  890. }
  891. });
  892. /**
  893. * The draggable-points module allows points to be moved around or modified in
  894. * the chart. In addition to the options mentioned under the `dragDrop` API
  895. * structure, the module fires three events,
  896. * [point.dragStart](plotOptions.series.point.events.dragStart),
  897. * [point.drag](plotOptions.series.point.events.drag) and
  898. * [point.drop](plotOptions.series.point.events.drop).
  899. *
  900. * @sample highcharts/dragdrop/resize-column
  901. * Draggable column and line series
  902. * @sample highcharts/dragdrop/bar-series
  903. * Draggable bar
  904. * @sample highcharts/dragdrop/drag-bubble
  905. * Draggable bubbles
  906. * @sample highcharts/dragdrop/drag-xrange
  907. * Draggable X range series
  908. *
  909. * @declare Highcharts.SeriesDragDropOptionsObject
  910. * @since 6.2.0
  911. * @requires modules/draggable-points
  912. * @apioption plotOptions.series.dragDrop
  913. */
  914. /**
  915. * The amount of pixels to drag the pointer before it counts as a drag
  916. * operation. This prevents drag/drop to fire when just clicking or selecting
  917. * points.
  918. *
  919. * @type {number}
  920. * @default 2
  921. * @since 6.2.0
  922. * @apioption plotOptions.series.dragDrop.dragSensitivity
  923. *
  924. * @private
  925. */
  926. var defaultDragSensitivity = 2;
  927. /**
  928. * Style options for the guide box. The guide box has one state by default, the
  929. * `default` state.
  930. *
  931. * @type {Highcharts.Dictionary<Highcharts.DragDropGuideBoxOptionsObject>}
  932. * @since 6.2.0
  933. * @optionparent plotOptions.series.dragDrop.guideBox
  934. *
  935. * @private
  936. */
  937. var defaultGuideBoxOptions = {
  938. /**
  939. * Style options for the guide box default state.
  940. *
  941. * @declare Highcharts.DragDropGuideBoxOptionsObject
  942. * @since 6.2.0
  943. */
  944. 'default': {
  945. /**
  946. * CSS class name of the guide box in this state. Defaults to
  947. * `highcharts-drag-box-default`.
  948. *
  949. * @since 6.2.0
  950. */
  951. className: 'highcharts-drag-box-default',
  952. /**
  953. * Width of the line around the guide box.
  954. *
  955. * @since 6.2.0
  956. */
  957. lineWidth: 1,
  958. /**
  959. * Color of the border around the guide box.
  960. *
  961. * @type {Highcharts.ColorString}
  962. * @since 6.2.0
  963. */
  964. lineColor: '#888',
  965. /**
  966. * Guide box fill color.
  967. *
  968. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  969. * @since 6.2.0
  970. */
  971. color: 'rgba(0, 0, 0, 0.1)',
  972. /**
  973. * Guide box cursor.
  974. *
  975. * @since 6.2.0
  976. */
  977. cursor: 'move',
  978. /**
  979. * Guide box zIndex.
  980. *
  981. * @since 6.2.0
  982. */
  983. zIndex: 900
  984. }
  985. };
  986. /**
  987. * Options for the drag handles.
  988. *
  989. * @declare Highcharts.DragDropHandleOptionsObject
  990. * @since 6.2.0
  991. * @optionparent plotOptions.series.dragDrop.dragHandle
  992. *
  993. * @private
  994. */
  995. var defaultDragHandleOptions = {
  996. /**
  997. * Function to define the SVG path to use for the drag handles. Takes the
  998. * point as argument. Should return an SVG path in array format. The SVG
  999. * path is automatically positioned on the point.
  1000. *
  1001. * @type {Function}
  1002. * @since 6.2.0
  1003. * @apioption plotOptions.series.dragDrop.dragHandle.pathFormatter
  1004. */
  1005. // pathFormatter: null,
  1006. /**
  1007. * The mouse cursor to use for the drag handles. By default this is
  1008. * intelligently switching between `ew-resize` and `ns-resize` depending on
  1009. * the direction the point is being dragged.
  1010. *
  1011. * @type {string}
  1012. * @since 6.2.0
  1013. * @apioption plotOptions.series.dragDrop.dragHandle.cursor
  1014. */
  1015. // cursor: null,
  1016. /**
  1017. * The class name of the drag handles. Defaults to `highcharts-drag-handle`.
  1018. *
  1019. * @since 6.2.0
  1020. */
  1021. className: 'highcharts-drag-handle',
  1022. /**
  1023. * The fill color of the drag handles.
  1024. *
  1025. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  1026. * @since 6.2.0
  1027. */
  1028. color: '#fff',
  1029. /**
  1030. * The line color of the drag handles.
  1031. *
  1032. * @type {Highcharts.ColorString}
  1033. * @since 6.2.0
  1034. */
  1035. lineColor: 'rgba(0, 0, 0, 0.6)',
  1036. /**
  1037. * The line width for the drag handles.
  1038. *
  1039. * @since 6.2.0
  1040. */
  1041. lineWidth: 1,
  1042. /**
  1043. * The z index for the drag handles.
  1044. *
  1045. * @since 6.2.0
  1046. */
  1047. zIndex: 901
  1048. };
  1049. /**
  1050. * Set the minimum X value the points can be moved to.
  1051. *
  1052. * @sample {gantt} gantt/dragdrop/drag-gantt
  1053. * Limit dragging
  1054. * @sample {highcharts} highcharts/dragdrop/drag-xrange
  1055. * Limit dragging
  1056. *
  1057. * @type {number}
  1058. * @since 6.2.0
  1059. * @apioption plotOptions.series.dragDrop.dragMinX
  1060. */
  1061. /**
  1062. * Set the maximum X value the points can be moved to.
  1063. *
  1064. * @sample {gantt} gantt/dragdrop/drag-gantt
  1065. * Limit dragging
  1066. * @sample {highcharts} highcharts/dragdrop/drag-xrange
  1067. * Limit dragging
  1068. *
  1069. * @type {number}
  1070. * @since 6.2.0
  1071. * @apioption plotOptions.series.dragDrop.dragMaxX
  1072. */
  1073. /**
  1074. * Set the minimum Y value the points can be moved to.
  1075. *
  1076. * @sample {gantt} gantt/dragdrop/drag-gantt
  1077. * Limit dragging
  1078. * @sample {highcharts} highcharts/dragdrop/drag-xrange
  1079. * Limit dragging
  1080. *
  1081. * @type {number}
  1082. * @since 6.2.0
  1083. * @apioption plotOptions.series.dragDrop.dragMinY
  1084. */
  1085. /**
  1086. * Set the maximum Y value the points can be moved to.
  1087. *
  1088. * @sample {gantt} gantt/dragdrop/drag-gantt
  1089. * Limit dragging
  1090. * @sample {highcharts} highcharts/dragdrop/drag-xrange
  1091. * Limit dragging
  1092. *
  1093. * @type {number}
  1094. * @since 6.2.0
  1095. * @apioption plotOptions.series.dragDrop.dragMaxY
  1096. */
  1097. /**
  1098. * The X precision value to drag to for this series. Set to 0 to disable. By
  1099. * default this is disabled, except for category axes, where the default is 1.
  1100. *
  1101. * @type {number}
  1102. * @default 0
  1103. * @since 6.2.0
  1104. * @apioption plotOptions.series.dragDrop.dragPrecisionX
  1105. */
  1106. /**
  1107. * The Y precision value to drag to for this series. Set to 0 to disable. By
  1108. * default this is disabled, except for category axes, where the default is 1.
  1109. *
  1110. * @type {number}
  1111. * @default 0
  1112. * @since 6.2.0
  1113. * @apioption plotOptions.series.dragDrop.dragPrecisionY
  1114. */
  1115. /**
  1116. * Enable dragging in the X dimension.
  1117. *
  1118. * @type {boolean}
  1119. * @since 6.2.0
  1120. * @apioption plotOptions.series.dragDrop.draggableX
  1121. */
  1122. /**
  1123. * Enable dragging in the Y dimension. Note that this is not supported for
  1124. * TreeGrid axes (the default axis type in Gantt charts).
  1125. *
  1126. * @type {boolean}
  1127. * @since 6.2.0
  1128. * @apioption plotOptions.series.dragDrop.draggableY
  1129. */
  1130. /**
  1131. * Group the points by a property. Points with the same property value will be
  1132. * grouped together when moving.
  1133. *
  1134. * @sample {gantt} gantt/dragdrop/drag-gantt
  1135. * Drag grouped points
  1136. * @sample {highcharts} highcharts/dragdrop/drag-xrange
  1137. * Drag grouped points
  1138. *
  1139. * @type {string}
  1140. * @since 6.2.0
  1141. * @apioption plotOptions.series.dragDrop.groupBy
  1142. */
  1143. /**
  1144. * Update points as they are dragged. If false, a guide box is drawn to
  1145. * illustrate the new point size.
  1146. *
  1147. * @sample {gantt} gantt/dragdrop/drag-gantt
  1148. * liveRedraw disabled
  1149. * @sample {highcharts} highcharts/dragdrop/drag-xrange
  1150. * liveRedraw disabled
  1151. *
  1152. * @type {boolean}
  1153. * @default true
  1154. * @since 6.2.0
  1155. * @apioption plotOptions.series.dragDrop.liveRedraw
  1156. */
  1157. /**
  1158. * Set a key to hold when dragging to zoom the chart. This is useful to avoid
  1159. * zooming while moving points. Should be set different than
  1160. * [chart.panKey](#chart.panKey).
  1161. *
  1162. * @type {string}
  1163. * @since 6.2.0
  1164. * @validvalue ["alt", "ctrl", "meta", "shift"]
  1165. * @requires modules/draggable-points
  1166. * @apioption chart.zoomKey
  1167. */
  1168. /**
  1169. * Callback that fires when starting to drag a point. The mouse event object is
  1170. * passed in as an argument. If a drag handle is used, `e.updateProp` is set to
  1171. * the data property being dragged. The `this` context is the point. See
  1172. * [drag and drop options](plotOptions.series.dragDrop).
  1173. *
  1174. * @sample {highcharts} highcharts/dragdrop/drag-xrange
  1175. * Drag events
  1176. *
  1177. * @type {Highcharts.PointDragStartCallbackFunction}
  1178. * @since 6.2.0
  1179. * @requires modules/draggable-points
  1180. * @apioption plotOptions.series.point.events.dragStart
  1181. */
  1182. /**
  1183. * Callback that fires while dragging a point. The mouse event is passed in as
  1184. * parameter. The original data can be accessed from `e.origin`, and the new
  1185. * point values can be accessed from `e.newPoints`. If there is only a single
  1186. * point being updated, it can be accessed from `e.newPoint` for simplicity, and
  1187. * its ID can be accessed from `e.newPointId`. The `this` context is the point
  1188. * being dragged. To stop the default drag action, return false. See
  1189. * [drag and drop options](plotOptions.series.dragDrop).
  1190. *
  1191. * @sample {highcharts} highcharts/dragdrop/drag-xrange
  1192. * Drag events
  1193. *
  1194. * @type {Highcharts.PointDragCallbackFunction}
  1195. * @since 6.2.0
  1196. * @requires modules/draggable-points
  1197. * @apioption plotOptions.series.point.events.drag
  1198. */
  1199. /**
  1200. * Callback that fires when the point is dropped. The parameters passed are the
  1201. * same as for [drag](#plotOptions.series.point.events.drag). To stop the
  1202. * default drop action, return false. See
  1203. * [drag and drop options](plotOptions.series.dragDrop).
  1204. *
  1205. * @sample {highcharts} highcharts/dragdrop/drag-xrange
  1206. * Drag events
  1207. *
  1208. * @type {Highcharts.PointDropCallbackFunction}
  1209. * @since 6.2.0
  1210. * @requires modules/draggable-points
  1211. * @apioption plotOptions.series.point.events.drop
  1212. */
  1213. /**
  1214. * Point specific options for the draggable-points module. Overrides options on
  1215. * `series.dragDrop`.
  1216. *
  1217. * @declare Highcharts.SeriesLineDataDragDropOptions
  1218. * @extends plotOptions.series.dragDrop
  1219. * @since 6.2.0
  1220. * @requires modules/draggable-points
  1221. * @apioption series.line.data.dragDrop
  1222. */
  1223. /**
  1224. * Utility function to test if a series is using drag/drop, looking at its
  1225. * options.
  1226. *
  1227. * @private
  1228. * @function isSeriesDraggable
  1229. * @param {Highcharts.Series} series
  1230. * The series to test.
  1231. * @return {boolean}
  1232. * True if the series is using drag/drop.
  1233. */
  1234. function isSeriesDraggable(series) {
  1235. var props = ['draggableX', 'draggableY'], i;
  1236. // Add optionNames from dragDropProps to the array of props to check for
  1237. objectEach(series.dragDropProps, function (val) {
  1238. if (val.optionName) {
  1239. props.push(val.optionName);
  1240. }
  1241. });
  1242. // Loop over all options we have that could enable dragDrop for this
  1243. // series. If any of them are truthy, this series is draggable.
  1244. i = props.length;
  1245. while (i--) {
  1246. if (series.options.dragDrop[props[i]]) {
  1247. return true;
  1248. }
  1249. }
  1250. }
  1251. /**
  1252. * Utility function to test if a chart should have drag/drop enabled, looking at
  1253. * its options.
  1254. *
  1255. * @private
  1256. * @function isChartDraggable
  1257. * @param {Highcharts.Chart} chart
  1258. * The chart to test.
  1259. * @return {boolean}
  1260. * True if the chart is drag/droppable.
  1261. */
  1262. function isChartDraggable(chart) {
  1263. var i = chart.series ? chart.series.length : 0;
  1264. if (chart.hasCartesianSeries && !chart.polar) {
  1265. while (i--) {
  1266. if (chart.series[i].options.dragDrop &&
  1267. isSeriesDraggable(chart.series[i])) {
  1268. return true;
  1269. }
  1270. }
  1271. }
  1272. }
  1273. /**
  1274. * Utility function to test if a point is movable (any of its props can be
  1275. * dragged by a move, not just individually).
  1276. *
  1277. * @private
  1278. * @function isPointMovable
  1279. * @param {Highcharts.Point} point
  1280. * The point to test.
  1281. * @return {boolean}
  1282. * True if the point is movable.
  1283. */
  1284. function isPointMovable(point) {
  1285. var series = point.series, seriesDragDropOptions = series.options.dragDrop || {}, pointDragDropOptions = point.options && point.options.dragDrop, updateProps = series.dragDropProps, hasMovableX, hasMovableY;
  1286. objectEach(updateProps, function (p) {
  1287. if (p.axis === 'x' && p.move) {
  1288. hasMovableX = true;
  1289. }
  1290. else if (p.axis === 'y' && p.move) {
  1291. hasMovableY = true;
  1292. }
  1293. });
  1294. // We can only move the point if draggableX/Y is set, even if all the
  1295. // individual prop options are set.
  1296. return ((seriesDragDropOptions.draggableX && hasMovableX ||
  1297. seriesDragDropOptions.draggableY && hasMovableY) &&
  1298. !(pointDragDropOptions &&
  1299. pointDragDropOptions.draggableX === false &&
  1300. pointDragDropOptions.draggableY === false) &&
  1301. series.yAxis &&
  1302. series.xAxis);
  1303. }
  1304. /**
  1305. * Take a mouse/touch event and return the event object with chartX/chartY.
  1306. *
  1307. * @private
  1308. * @function getNormalizedEvent
  1309. * @param {global.PointerEvent} e
  1310. * The event to normalize.
  1311. * @param {Highcharts.Chart} chart
  1312. * The related chart.
  1313. * @return {Highcharts.PointerEventLObject}
  1314. * The normalized event.
  1315. */
  1316. function getNormalizedEvent(e, chart) {
  1317. return (typeof e.chartX === 'undefined' ||
  1318. typeof e.chartY === 'undefined' ?
  1319. chart.pointer.normalize(e) :
  1320. e);
  1321. }
  1322. /**
  1323. * Add multiple event listeners with the same handler to the same element.
  1324. *
  1325. * @private
  1326. * @function addEvents
  1327. * @param {T} el
  1328. * The element or object to add listeners to.
  1329. * @param {Array<string>} types
  1330. * Array with the event types this handler should apply to.
  1331. * @param {Function|Highcharts.EventCallbackFunction<T>} fn
  1332. * The function callback to execute when the events are fired.
  1333. * @param {Highcharts.EventOptionsObject} [options]
  1334. * Event options:
  1335. * - `order`: The order the event handler should be called. This opens
  1336. * for having one handler be called before another, independent of in
  1337. * which order they were added.
  1338. * @return {Function}
  1339. * A callback function to remove the added events.
  1340. * @template T
  1341. */
  1342. function addEvents(el, types, fn, options) {
  1343. var removeFuncs = types.map(function (type) {
  1344. return addEvent(el, type, fn, options);
  1345. });
  1346. return function () {
  1347. removeFuncs.forEach(function (fn) {
  1348. fn();
  1349. });
  1350. };
  1351. }
  1352. /**
  1353. * In mousemove events, check that we have dragged mouse further than the
  1354. * dragSensitivity before we call mouseMove handler.
  1355. *
  1356. * @private
  1357. * @function hasDraggedPastSensitivity
  1358. *
  1359. * @param {Highcharts.PointerEventObject} e
  1360. * Mouse move event to test.
  1361. *
  1362. * @param {Highcharts.Chart} chart
  1363. * Chart that has started dragging.
  1364. *
  1365. * @param {number} sensitivity
  1366. * Pixel sensitivity to test against.
  1367. *
  1368. * @return {boolean}
  1369. * True if the event is moved past sensitivity relative to the chart's
  1370. * drag origin.
  1371. */
  1372. function hasDraggedPastSensitivity(e, chart, sensitivity) {
  1373. var orig = chart.dragDropData.origin, oldX = orig.chartX, oldY = orig.chartY, newX = e.chartX, newY = e.chartY, distance = Math.sqrt((newX - oldX) * (newX - oldX) +
  1374. (newY - oldY) * (newY - oldY));
  1375. return distance > sensitivity;
  1376. }
  1377. /**
  1378. * Get a snapshot of points, mouse position, and guide box dimensions
  1379. *
  1380. * @private
  1381. * @function getPositionSnapshot
  1382. *
  1383. * @param {Highcharts.PointerEventObject} e
  1384. * Mouse event with mouse position to snapshot.
  1385. *
  1386. * @param {Array<Highcharts.Point>} points
  1387. * Points to take snapshot of. We store the value of the data properties
  1388. * defined in each series' dragDropProps.
  1389. *
  1390. * @param {Highcharts.SVGElement} [guideBox]
  1391. * The guide box to take snapshot of.
  1392. *
  1393. * @return {object}
  1394. * Snapshot object. Point properties are placed in a hashmap with IDs as
  1395. * keys.
  1396. */
  1397. function getPositionSnapshot(e, points, guideBox) {
  1398. var res = {
  1399. chartX: e.chartX,
  1400. chartY: e.chartY,
  1401. guideBox: guideBox && {
  1402. x: guideBox.attr('x'),
  1403. y: guideBox.attr('y'),
  1404. width: guideBox.attr('width'),
  1405. height: guideBox.attr('height')
  1406. },
  1407. points: {}
  1408. };
  1409. // Loop over the points and add their props
  1410. points.forEach(function (point) {
  1411. var pointProps = {};
  1412. // Add all of the props defined in the series' dragDropProps to the
  1413. // snapshot
  1414. objectEach(point.series.dragDropProps, function (val, key) {
  1415. var axis = point.series[val.axis + 'Axis'];
  1416. pointProps[key] = point[key];
  1417. // Record how far cursor was from the point when drag started.
  1418. // This later will be used to calculate new value according to the
  1419. // current position of the cursor.
  1420. // e.g. `high` value is translated to `highOffset`
  1421. pointProps[key + 'Offset'] =
  1422. // e.g. yAxis.toPixels(point.high), xAxis.toPixels(point.end)
  1423. axis.toPixels(point[key]) -
  1424. (axis.horiz ? e.chartX : e.chartY);
  1425. });
  1426. pointProps.point = point; // Store reference to point
  1427. res.points[point.id] = pointProps;
  1428. });
  1429. return res;
  1430. }
  1431. /**
  1432. * Get a list of points that are grouped with this point. If only one point is
  1433. * in the group, that point is returned by itself in an array.
  1434. *
  1435. * @private
  1436. * @function getGroupedPoints
  1437. * @param {Highcharts.Point} point
  1438. * Point to find group from.
  1439. * @return {Array<Highcharts.Point>}
  1440. * Array of points in this group.
  1441. */
  1442. function getGroupedPoints(point) {
  1443. var series = point.series, points = [], groupKey = series.options.dragDrop.groupBy;
  1444. if (series.isSeriesBoosting) { // #11156
  1445. series.options.data.forEach(function (pointOptions, i) {
  1446. points.push((new series.pointClass()).init(// eslint-disable-line new-cap
  1447. series, pointOptions));
  1448. points[points.length - 1].index = i;
  1449. });
  1450. }
  1451. else {
  1452. points = series.points;
  1453. }
  1454. return point.options[groupKey] ?
  1455. // If we have a grouping option, filter the points by that
  1456. points.filter(function (comparePoint) {
  1457. return comparePoint.options[groupKey] ===
  1458. point.options[groupKey];
  1459. }) :
  1460. // Otherwise return the point by itself only
  1461. [point];
  1462. }
  1463. /**
  1464. * Resize a rect element on one side. The element is modified.
  1465. *
  1466. * @private
  1467. * @function resizeRect
  1468. * @param {Highcharts.SVGElement} rect
  1469. * Rect element to resize.
  1470. * @param {string} updateSide
  1471. * Which side of the rect to update. Can be `left`, `right`, `top` or
  1472. * `bottom`.
  1473. * @param {Highcharts.PositionObject} update
  1474. * Object with x and y properties, detailing how much to resize each
  1475. * dimension.
  1476. * @return {void}
  1477. */
  1478. function resizeRect(rect, updateSide, update) {
  1479. var resizeAttrs;
  1480. switch (updateSide) {
  1481. case 'left':
  1482. resizeAttrs = {
  1483. x: rect.attr('x') + update.x,
  1484. width: Math.max(1, rect.attr('width') - update.x)
  1485. };
  1486. break;
  1487. case 'right':
  1488. resizeAttrs = {
  1489. width: Math.max(1, rect.attr('width') + update.x)
  1490. };
  1491. break;
  1492. case 'top':
  1493. resizeAttrs = {
  1494. y: rect.attr('y') + update.y,
  1495. height: Math.max(1, rect.attr('height') - update.y)
  1496. };
  1497. break;
  1498. case 'bottom':
  1499. resizeAttrs = {
  1500. height: Math.max(1, rect.attr('height') + update.y)
  1501. };
  1502. break;
  1503. default:
  1504. }
  1505. rect.attr(resizeAttrs);
  1506. }
  1507. /**
  1508. * Prepare chart.dragDropData with origin info, and show the guide box.
  1509. *
  1510. * @private
  1511. * @function initDragDrop
  1512. * @param {Highcharts.PointerEventObject} e
  1513. * Mouse event with original mouse position.
  1514. * @param {Highcharts.Point} point
  1515. * The point the dragging started on.
  1516. * @return {void}
  1517. */
  1518. function initDragDrop(e, point) {
  1519. var groupedPoints = getGroupedPoints(point), series = point.series, chart = series.chart, guideBox;
  1520. // If liveRedraw is disabled, show the guide box with the default state
  1521. if (!pick(series.options.dragDrop && series.options.dragDrop.liveRedraw, true)) {
  1522. chart.dragGuideBox = guideBox = series.getGuideBox(groupedPoints);
  1523. chart
  1524. .setGuideBoxState('default', series.options.dragDrop.guideBox)
  1525. .add(series.group);
  1526. }
  1527. // Store some data on the chart to pick up later
  1528. chart.dragDropData = {
  1529. origin: getPositionSnapshot(e, groupedPoints, guideBox),
  1530. point: point,
  1531. groupedPoints: groupedPoints,
  1532. isDragging: true
  1533. };
  1534. }
  1535. /**
  1536. * Calculate new point options from points being dragged.
  1537. *
  1538. * @private
  1539. * @function getNewPoints
  1540. *
  1541. * @param {object} dragDropData
  1542. * A chart's dragDropData with drag/drop origin information, and info on
  1543. * which points are being dragged.
  1544. *
  1545. * @param {Highcharts.PointerEventObject} newPos
  1546. * Event with the new position of the mouse (chartX/Y properties).
  1547. *
  1548. * @return {Highchats.Dictionary<object>}
  1549. * Hashmap with point.id mapped to an object with the original point
  1550. * reference, as well as the new data values.
  1551. */
  1552. function getNewPoints(dragDropData, newPos) {
  1553. var point = dragDropData.point, series = point.series, options = merge(series.options.dragDrop, point.options.dragDrop), updateProps = {}, resizeProp = dragDropData.updateProp, hashmap = {};
  1554. // Go through the data props that can be updated on this series and find out
  1555. // which ones we want to update.
  1556. objectEach(point.series.dragDropProps, function (val, key) {
  1557. // If we are resizing, skip if this key is not the correct one or it
  1558. // is not resizable.
  1559. if (resizeProp && (resizeProp !== key ||
  1560. !val.resize ||
  1561. val.optionName && options[val.optionName] === false)) {
  1562. return;
  1563. }
  1564. // If we are resizing, we now know it is good. If we are moving, check
  1565. // that moving along this axis is enabled, and the prop is movable.
  1566. // If this prop is enabled, add it to be updated.
  1567. if (resizeProp || (val.move &&
  1568. (val.axis === 'x' && options.draggableX ||
  1569. val.axis === 'y' && options.draggableY))) {
  1570. updateProps[key] = val;
  1571. }
  1572. });
  1573. // Go through the points to be updated and get new options for each of them
  1574. (
  1575. // If resizing).forEach(only update the point we are resizing
  1576. resizeProp ?
  1577. [point] :
  1578. dragDropData.groupedPoints).forEach(function (p) {
  1579. hashmap[p.id] = {
  1580. point: p,
  1581. newValues: p.getDropValues(dragDropData.origin, newPos, updateProps)
  1582. };
  1583. });
  1584. return hashmap;
  1585. }
  1586. /**
  1587. * Update the points in a chart from dragDropData.newPoints.
  1588. *
  1589. * @private
  1590. * @function updatePoints
  1591. * @param {Highcharts.Chart} chart
  1592. * A chart with dragDropData.newPoints.
  1593. * @param {boolean} [animate=true]
  1594. * Animate updating points?
  1595. */
  1596. function updatePoints(chart, animate) {
  1597. var newPoints = chart.dragDropData.newPoints, animOptions = animate === false ? false : merge({
  1598. duration: 400 // 400 is the default in animate
  1599. }, chart.options.chart.animation);
  1600. chart.isDragDropAnimating = true;
  1601. // Update the points
  1602. objectEach(newPoints, function (newPoint) {
  1603. newPoint.point.update(newPoint.newValues, false);
  1604. });
  1605. chart.redraw(animOptions);
  1606. // Clear the isAnimating flag after animation duration is complete.
  1607. // The complete handler for animation seems to have bugs at this time, so
  1608. // we have to use a timeout instead.
  1609. setTimeout(function () {
  1610. delete chart.isDragDropAnimating;
  1611. if (chart.hoverPoint && !chart.dragHandles) {
  1612. chart.hoverPoint.showDragHandles();
  1613. }
  1614. }, animOptions.duration);
  1615. }
  1616. /**
  1617. * Resize the guide box according to point options and a difference in mouse
  1618. * positions. Handles reversed axes.
  1619. *
  1620. * @private
  1621. * @function resizeGuideBox
  1622. * @param {Highcharts.Point} point
  1623. * The point that is being resized.
  1624. * @param {number} dX
  1625. * Difference in X position.
  1626. * @param {number} dY
  1627. * Difference in Y position.
  1628. */
  1629. function resizeGuideBox(point, dX, dY) {
  1630. var series = point.series, chart = series.chart, dragDropData = chart.dragDropData, resizeSide, newPoint, resizeProp = series.dragDropProps[dragDropData.updateProp];
  1631. // dragDropProp.resizeSide holds info on which side to resize.
  1632. newPoint = dragDropData.newPoints[point.id].newValues;
  1633. resizeSide = typeof resizeProp.resizeSide === 'function' ?
  1634. resizeProp.resizeSide(newPoint, point) : resizeProp.resizeSide;
  1635. // Call resize hook if it is defined
  1636. if (resizeProp.beforeResize) {
  1637. resizeProp.beforeResize(chart.dragGuideBox, newPoint, point);
  1638. }
  1639. // Do the resize
  1640. resizeRect(chart.dragGuideBox, resizeProp.axis === 'x' && series.xAxis.reversed ||
  1641. resizeProp.axis === 'y' && series.yAxis.reversed ?
  1642. flipResizeSide(resizeSide) : resizeSide, {
  1643. x: resizeProp.axis === 'x' ?
  1644. dX - (dragDropData.origin.prevdX || 0) : 0,
  1645. y: resizeProp.axis === 'y' ?
  1646. dY - (dragDropData.origin.prevdY || 0) : 0
  1647. });
  1648. }
  1649. /**
  1650. * Default mouse move handler while dragging. Handles updating points or guide
  1651. * box.
  1652. *
  1653. * @private
  1654. * @function dragMove
  1655. * @param {Highcharts.PointerEventObject} e
  1656. * The mouse move event.
  1657. * @param {Highcharts.Point} point
  1658. * The point that is dragged.
  1659. */
  1660. function dragMove(e, point) {
  1661. var series = point.series, chart = series.chart, data = chart.dragDropData, options = merge(series.options.dragDrop, point.options.dragDrop), draggableX = options.draggableX, draggableY = options.draggableY, origin = data.origin, dX = e.chartX - origin.chartX, dY = e.chartY - origin.chartY, oldDx = dX, updateProp = data.updateProp;
  1662. // Handle inverted
  1663. if (chart.inverted) {
  1664. dX = -dY;
  1665. dY = -oldDx;
  1666. }
  1667. // If we have liveRedraw enabled, update the points immediately. Otherwise
  1668. // update the guideBox.
  1669. if (pick(options.liveRedraw, true)) {
  1670. updatePoints(chart, false);
  1671. // Update drag handles
  1672. point.showDragHandles();
  1673. }
  1674. else {
  1675. // No live redraw, update guide box
  1676. if (updateProp) {
  1677. // We are resizing, so resize the guide box
  1678. resizeGuideBox(point, dX, dY);
  1679. }
  1680. else {
  1681. // We are moving, so move the guide box
  1682. chart.dragGuideBox.translate(draggableX ? dX : 0, draggableY ? dY : 0);
  1683. }
  1684. }
  1685. // Update stored previous dX/Y
  1686. origin.prevdX = dX;
  1687. origin.prevdY = dY;
  1688. }
  1689. /**
  1690. * Set the state of the guide box.
  1691. *
  1692. * @private
  1693. * @function Highcharts.Chart#setGuideBoxState
  1694. * @param {string} state
  1695. * The state to set the guide box to.
  1696. * @param {Highcharts.Dictionary<Highcharts.DragDropGuideBoxOptionsObject>} [options]
  1697. * Additional overall guideBox options to consider.
  1698. * @return {Highcharts.SVGElement}
  1699. * The modified guide box.
  1700. */
  1701. H.Chart.prototype.setGuideBoxState = function (state, options) {
  1702. var guideBox = this.dragGuideBox, guideBoxOptions = merge(defaultGuideBoxOptions, options), stateOptions = merge(guideBoxOptions['default'], // eslint-disable-line dot-notation
  1703. guideBoxOptions[state]);
  1704. return guideBox
  1705. .attr({
  1706. className: stateOptions.className,
  1707. stroke: stateOptions.lineColor,
  1708. strokeWidth: stateOptions.lineWidth,
  1709. fill: stateOptions.color,
  1710. cursor: stateOptions.cursor,
  1711. zIndex: stateOptions.zIndex
  1712. })
  1713. // Use pointerEvents 'none' to avoid capturing the click event
  1714. .css({ pointerEvents: 'none' });
  1715. };
  1716. /**
  1717. * Get updated point values when dragging a point.
  1718. *
  1719. * @private
  1720. * @function Highcharts.Point#getDropValues
  1721. *
  1722. * @param {object} origin
  1723. * Mouse position (chartX/Y) and point props at current data values.
  1724. * Point props should be organized per point.id in a hashmap.
  1725. *
  1726. * @param {Highcharts.PointerEventObject} newPos
  1727. * New mouse position (chartX/Y).
  1728. *
  1729. * @param {Highcharts.Dictionary<Highcharts.Dictionary<Highcharts.Dictionary<string>>>} updateProps
  1730. * Point props to modify. Map of prop objects where each key refers to
  1731. * the prop, and the value is an object with an axis property. Example:
  1732. * {
  1733. * x: {
  1734. * axis: 'x'
  1735. * },
  1736. * x2: {
  1737. * axis: 'x'
  1738. * }
  1739. * }
  1740. *
  1741. * @return {Highcharts.Dictionary<number>}
  1742. * An object with updated data values.
  1743. */
  1744. Point.prototype.getDropValues = function (origin, newPos, updateProps) {
  1745. var point = this, series = point.series, options = merge(series.options.dragDrop, point.options.dragDrop), result = {}, updateSingleProp, pointOrigin = origin.points[point.id];
  1746. // Find out if we only have one prop to update
  1747. for (var key in updateProps) {
  1748. if (Object.hasOwnProperty.call(updateProps, key)) {
  1749. if (typeof updateSingleProp !== 'undefined') {
  1750. updateSingleProp = false;
  1751. break;
  1752. }
  1753. updateSingleProp = true;
  1754. }
  1755. }
  1756. /**
  1757. * Utility function to apply precision and limit a value within the
  1758. * draggable range.
  1759. * @private
  1760. * @param {number} val
  1761. * Value to limit
  1762. * @param {string} direction
  1763. * Axis direction
  1764. * @return {number}
  1765. * Limited value
  1766. */
  1767. var limitToRange = function (val, direction) {
  1768. var defaultPrecision = series[direction.toLowerCase() + 'Axis']
  1769. .categories ? 1 : 0, precision = pick(options['dragPrecision' + direction], defaultPrecision), min = pick(options['dragMin' + direction], -Infinity), max = pick(options['dragMax' + direction], Infinity), res = val;
  1770. if (precision) {
  1771. res = Math.round(res / precision) * precision;
  1772. }
  1773. return clamp(res, min, max);
  1774. };
  1775. // Assign new value to property. Adds dX/YValue to the old value, limiting
  1776. // it within min/max ranges.
  1777. objectEach(updateProps, function (val, key) {
  1778. var oldVal = pointOrigin[key], axis = series[val.axis + 'Axis'], newVal = limitToRange(axis.toValue((axis.horiz ? newPos.chartX : newPos.chartY) +
  1779. pointOrigin[key + 'Offset']), val.axis.toUpperCase());
  1780. // If we are updating a single prop, and it has a validation function
  1781. // for the prop, run it. If it fails, don't update the value.
  1782. if (!(updateSingleProp &&
  1783. val.propValidate &&
  1784. !val.propValidate(newVal, point)) &&
  1785. typeof oldVal !== 'undefined') {
  1786. result[key] = newVal;
  1787. }
  1788. });
  1789. return result;
  1790. };
  1791. /**
  1792. * Returns an SVGElement to use as the guide box for a set of points.
  1793. *
  1794. * @private
  1795. * @function Highcharts.Series#getGuideBox
  1796. *
  1797. * @param {Array<Highcharts.Point>} points
  1798. * The state to set the guide box to.
  1799. *
  1800. * @return {Highcharts.SVGElement}
  1801. * An SVG element for the guide box, not added to DOM.
  1802. */
  1803. H.Series.prototype.getGuideBox = function (points) {
  1804. var chart = this.chart, minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity, changed;
  1805. // Find bounding box of all points
  1806. points.forEach(function (point) {
  1807. var bBox = point.graphic && point.graphic.getBBox() || point.shapeArgs;
  1808. if (bBox && (bBox.width || bBox.height || bBox.x || bBox.y)) {
  1809. changed = true;
  1810. minX = Math.min(bBox.x, minX);
  1811. maxX = Math.max(bBox.x + bBox.width, maxX);
  1812. minY = Math.min(bBox.y, minY);
  1813. maxY = Math.max(bBox.y + bBox.height, maxY);
  1814. }
  1815. });
  1816. return changed ? chart.renderer.rect(minX, minY, maxX - minX, maxY - minY) : chart.renderer.g();
  1817. };
  1818. /**
  1819. * On point mouse out. Hide drag handles, depending on state.
  1820. *
  1821. * @private
  1822. * @function mouseOut
  1823. * @param {Highcharts.Point} point
  1824. * The point mousing out of.
  1825. */
  1826. function mouseOut(point) {
  1827. var chart = point.series && point.series.chart, dragDropData = chart && chart.dragDropData;
  1828. if (chart &&
  1829. chart.dragHandles &&
  1830. !(dragDropData &&
  1831. (dragDropData.isDragging &&
  1832. dragDropData.draggedPastSensitivity ||
  1833. dragDropData.isHoveringHandle === point.id))) {
  1834. chart.hideDragHandles();
  1835. }
  1836. }
  1837. /**
  1838. * Mouseout on resize handle. Handle states, and possibly run mouseOut on point.
  1839. *
  1840. * @private
  1841. * @function onResizeHandleMouseOut
  1842. * @param {Highcharts.Point} point
  1843. * The point mousing out of.
  1844. */
  1845. function onResizeHandleMouseOut(point) {
  1846. var chart = point.series.chart;
  1847. if (chart.dragDropData &&
  1848. point.id === chart.dragDropData.isHoveringHandle) {
  1849. delete chart.dragDropData.isHoveringHandle;
  1850. }
  1851. if (!chart.hoverPoint) {
  1852. mouseOut(point);
  1853. }
  1854. }
  1855. /**
  1856. * Mousedown on resize handle. Init a drag if the conditions are right.
  1857. *
  1858. * @private
  1859. * @function onResizeHandleMouseDown
  1860. * @param {Highcharts.PointerEventObject} e
  1861. * The mousedown event.
  1862. * @param {Highcharts.Point} point
  1863. * The point mousing down on.
  1864. * @param {string} updateProp
  1865. * The data property this resize handle is attached to for this point.
  1866. */
  1867. function onResizeHandleMouseDown(e, point, updateProp) {
  1868. var chart = point.series.chart;
  1869. // Ignore if zoom/pan key is pressed
  1870. if (chart.zoomOrPanKeyPressed(e)) {
  1871. return;
  1872. }
  1873. // Prevent zooming
  1874. chart.mouseIsDown = false;
  1875. // We started a drag
  1876. initDragDrop(e, point);
  1877. chart.dragDropData.updateProp =
  1878. e.updateProp = updateProp;
  1879. point.firePointEvent('dragStart', e);
  1880. // Prevent default to avoid point click for dragging too
  1881. e.stopPropagation();
  1882. e.preventDefault();
  1883. }
  1884. /**
  1885. * Render drag handles on a point - depending on which handles are enabled - and
  1886. * attach events to them.
  1887. *
  1888. * @private
  1889. * @function Highcharts.Point#showDragHandles
  1890. * @return {void}
  1891. */
  1892. Point.prototype.showDragHandles = function () {
  1893. var point = this, series = point.series, chart = series.chart, renderer = chart.renderer, options = merge(series.options.dragDrop, point.options.dragDrop);
  1894. // Go through each updateProp and see if we are supposed to create a handle
  1895. // for it.
  1896. objectEach(series.dragDropProps, function (val, key) {
  1897. var handleOptions = merge(defaultDragHandleOptions, val.handleOptions, options.dragHandle), handleAttrs = {
  1898. className: handleOptions.className,
  1899. 'stroke-width': handleOptions.lineWidth,
  1900. fill: handleOptions.color,
  1901. stroke: handleOptions.lineColor
  1902. }, pathFormatter = handleOptions.pathFormatter || val.handleFormatter, positioner = val.handlePositioner, pos, handle, path,
  1903. // Run validation function on whether or not we allow individual
  1904. // updating of this prop.
  1905. validate = val.validateIndividualDrag ?
  1906. val.validateIndividualDrag(point) : true;
  1907. if (val.resize &&
  1908. validate &&
  1909. val.resizeSide &&
  1910. pathFormatter &&
  1911. (options['draggable' + val.axis.toUpperCase()] ||
  1912. options[val.optionName]) &&
  1913. options[val.optionName] !== false) {
  1914. // Create group if it doesn't exist
  1915. if (!chart.dragHandles) {
  1916. chart.dragHandles = {
  1917. group: renderer
  1918. .g('drag-drop-handles')
  1919. .add(series.markerGroup || series.group)
  1920. };
  1921. }
  1922. // Store which point this is
  1923. chart.dragHandles.point = point.id;
  1924. // Find position and path of handle
  1925. pos = positioner(point);
  1926. handleAttrs.d = path = pathFormatter(point);
  1927. if (!path || pos.x < 0 || pos.y < 0) {
  1928. return;
  1929. }
  1930. // If cursor is not set explicitly, use axis direction
  1931. handleAttrs.cursor = handleOptions.cursor ||
  1932. (val.axis === 'x') !== !!chart.inverted ?
  1933. 'ew-resize' : 'ns-resize';
  1934. // Create and add the handle element if it doesn't exist
  1935. handle = chart.dragHandles[val.optionName];
  1936. if (!handle) {
  1937. handle = chart.dragHandles[val.optionName] = renderer
  1938. .path()
  1939. .add(chart.dragHandles.group);
  1940. }
  1941. // Move and update handle
  1942. handle.translate(pos.x, pos.y).attr(handleAttrs);
  1943. // Add events
  1944. addEvents(handle.element, ['touchstart', 'mousedown'], function (e) {
  1945. onResizeHandleMouseDown(getNormalizedEvent(e, chart), point, key);
  1946. });
  1947. addEvent(chart.dragHandles.group.element, 'mouseover', function () {
  1948. chart.dragDropData = chart.dragDropData || {};
  1949. chart.dragDropData.isHoveringHandle = point.id;
  1950. });
  1951. addEvents(chart.dragHandles.group.element, ['touchend', 'mouseout'], function () {
  1952. onResizeHandleMouseOut(point);
  1953. });
  1954. }
  1955. });
  1956. };
  1957. /**
  1958. * Remove the chart's drag handles if they exist.
  1959. *
  1960. * @private
  1961. * @function Highcharts.Chart#hideDragHandles
  1962. * @return {void}
  1963. */
  1964. H.Chart.prototype.hideDragHandles = function () {
  1965. var chart = this;
  1966. if (chart.dragHandles) {
  1967. objectEach(chart.dragHandles, function (val, key) {
  1968. if (key !== 'group' && val.destroy) {
  1969. val.destroy();
  1970. }
  1971. });
  1972. if (chart.dragHandles.group && chart.dragHandles.group.destroy) {
  1973. chart.dragHandles.group.destroy();
  1974. }
  1975. delete chart.dragHandles;
  1976. }
  1977. };
  1978. /**
  1979. * Utility function to count the number of props in an object.
  1980. *
  1981. * @private
  1982. * @function countProps
  1983. *
  1984. * @param {object} obj
  1985. * The object to count.
  1986. *
  1987. * @return {number}
  1988. * Number of own properties on the object.
  1989. */
  1990. function countProps(obj) {
  1991. var count = 0;
  1992. for (var p in obj) {
  1993. if (Object.hasOwnProperty.call(obj, p)) {
  1994. count++;
  1995. }
  1996. }
  1997. return count;
  1998. }
  1999. /**
  2000. * Utility function to get the value of the first prop of an object. (Note that
  2001. * the order of keys in an object is usually not guaranteed.)
  2002. *
  2003. * @private
  2004. * @function getFirstProp
  2005. * @param {Highcharts.Dictionary<T>} obj
  2006. * The object to count.
  2007. * @return {T}
  2008. * Value of the first prop in the object.
  2009. * @template T
  2010. */
  2011. function getFirstProp(obj) {
  2012. for (var p in obj) {
  2013. if (Object.hasOwnProperty.call(obj, p)) {
  2014. return obj[p];
  2015. }
  2016. }
  2017. }
  2018. /**
  2019. * Mouseover on a point. Show drag handles if the conditions are right.
  2020. *
  2021. * @private
  2022. * @function mouseOver
  2023. * @param {Highcharts.Point} point
  2024. * The point mousing over.
  2025. */
  2026. function mouseOver(point) {
  2027. var series = point.series, chart = series && series.chart, dragDropData = chart && chart.dragDropData, is3d = chart && chart.is3d && chart.is3d();
  2028. if (chart &&
  2029. !(dragDropData &&
  2030. dragDropData.isDragging && // Ignore if dragging a point
  2031. dragDropData.draggedPastSensitivity) &&
  2032. !chart.isDragDropAnimating && // Ignore if animating
  2033. series.options.dragDrop && // No need to compute handles without this
  2034. !is3d // No 3D support
  2035. ) {
  2036. // Hide the handles if they exist on another point already
  2037. if (chart.dragHandles) {
  2038. chart.hideDragHandles();
  2039. }
  2040. point.showDragHandles();
  2041. }
  2042. }
  2043. /**
  2044. * On container mouse move. Handle drag sensitivity and fire drag event.
  2045. *
  2046. * @private
  2047. * @function mouseMove
  2048. * @param {Highcharts.PointerEventObject} e
  2049. * The mouse move event.
  2050. * @param {Highcharts.Chart} chart
  2051. * The chart we are moving across.
  2052. */
  2053. function mouseMove(e, chart) {
  2054. // Ignore if zoom/pan key is pressed
  2055. if (chart.zoomOrPanKeyPressed(e)) {
  2056. return;
  2057. }
  2058. var dragDropData = chart.dragDropData, point, seriesDragDropOpts, newPoints, numNewPoints = 0, newPoint;
  2059. if (dragDropData && dragDropData.isDragging) {
  2060. point = dragDropData.point;
  2061. seriesDragDropOpts = point.series.options.dragDrop;
  2062. // No tooltip for dragging
  2063. e.preventDefault();
  2064. // Update sensitivity test if not passed yet
  2065. if (!dragDropData.draggedPastSensitivity) {
  2066. dragDropData.draggedPastSensitivity = hasDraggedPastSensitivity(e, chart, pick(point.options.dragDrop &&
  2067. point.options.dragDrop.dragSensitivity, seriesDragDropOpts &&
  2068. seriesDragDropOpts.dragSensitivity, defaultDragSensitivity));
  2069. }
  2070. // If we have dragged past dragSensitivity, run the mousemove handler
  2071. // for dragging
  2072. if (dragDropData.draggedPastSensitivity) {
  2073. // Find the new point values from the moving
  2074. dragDropData.newPoints = getNewPoints(dragDropData, e);
  2075. // If we are only dragging one point, add it to the event
  2076. newPoints = dragDropData.newPoints;
  2077. numNewPoints = countProps(newPoints);
  2078. newPoint = numNewPoints === 1 ?
  2079. getFirstProp(newPoints) :
  2080. null;
  2081. // Run the handler
  2082. point.firePointEvent('drag', {
  2083. origin: dragDropData.origin,
  2084. newPoints: dragDropData.newPoints,
  2085. newPoint: newPoint && newPoint.newValues,
  2086. newPointId: newPoint && newPoint.point.id,
  2087. numNewPoints: numNewPoints,
  2088. chartX: e.chartX,
  2089. chartY: e.chartY
  2090. }, function () {
  2091. dragMove(e, point);
  2092. });
  2093. }
  2094. }
  2095. }
  2096. /**
  2097. * On container mouse up. Fire drop event and reset state.
  2098. *
  2099. * @private
  2100. * @function mouseUp
  2101. * @param {Highcharts.PointerEventObject} e
  2102. * The mouse up event.
  2103. * @param {Highcharts.Chart} chart
  2104. * The chart we were dragging in.
  2105. */
  2106. function mouseUp(e, chart) {
  2107. var dragDropData = chart.dragDropData;
  2108. if (dragDropData &&
  2109. dragDropData.isDragging &&
  2110. dragDropData.draggedPastSensitivity) {
  2111. var point = dragDropData.point, newPoints = dragDropData.newPoints, numNewPoints = countProps(newPoints), newPoint = numNewPoints === 1 ?
  2112. getFirstProp(newPoints) :
  2113. null;
  2114. // Hide the drag handles
  2115. if (chart.dragHandles) {
  2116. chart.hideDragHandles();
  2117. }
  2118. // Prevent default action
  2119. e.preventDefault();
  2120. chart.cancelClick = true;
  2121. // Fire the event, with a default handler that updates the points
  2122. point.firePointEvent('drop', {
  2123. origin: dragDropData.origin,
  2124. chartX: e.chartX,
  2125. chartY: e.chartY,
  2126. newPoints: newPoints,
  2127. numNewPoints: numNewPoints,
  2128. newPoint: newPoint && newPoint.newValues,
  2129. newPointId: newPoint && newPoint.point.id
  2130. }, function () {
  2131. updatePoints(chart);
  2132. });
  2133. }
  2134. // Reset
  2135. delete chart.dragDropData;
  2136. // Clean up the drag guide box if it exists. This is always added on
  2137. // drag start, even if user is overriding events.
  2138. if (chart.dragGuideBox) {
  2139. chart.dragGuideBox.destroy();
  2140. delete chart.dragGuideBox;
  2141. }
  2142. }
  2143. /**
  2144. * On container mouse down. Init dragdrop if conditions are right.
  2145. *
  2146. * @private
  2147. * @function mouseDown
  2148. * @param {Highcharts.PointerEventObject} e
  2149. * The mouse down event.
  2150. * @param {Highcharts.Chart} chart
  2151. * The chart we are clicking.
  2152. */
  2153. function mouseDown(e, chart) {
  2154. var dragPoint = chart.hoverPoint, dragDropOptions = merge(dragPoint && dragPoint.series.options.dragDrop, dragPoint && dragPoint.options.dragDrop), draggableX = dragDropOptions.draggableX || false, draggableY = dragDropOptions.draggableY || false;
  2155. // Reset cancel click
  2156. chart.cancelClick = false;
  2157. // Ignore if:
  2158. if (
  2159. // Option is disabled for the point
  2160. !(draggableX || draggableY) ||
  2161. // Zoom/pan key is pressed
  2162. chart.zoomOrPanKeyPressed(e) ||
  2163. // Dragging an annotation
  2164. chart.hasDraggedAnnotation) {
  2165. return;
  2166. }
  2167. // If we somehow get a mousedown event while we are dragging, cancel
  2168. if (chart.dragDropData && chart.dragDropData.isDragging) {
  2169. mouseUp(e, chart);
  2170. return;
  2171. }
  2172. // If this point is movable, start dragging it
  2173. if (dragPoint && isPointMovable(dragPoint)) {
  2174. chart.mouseIsDown = false; // Prevent zooming
  2175. initDragDrop(e, dragPoint);
  2176. dragPoint.firePointEvent('dragStart', e);
  2177. }
  2178. }
  2179. /* eslint-disable no-invalid-this */
  2180. // Point hover event. We use a short timeout due to issues with coordinating
  2181. // point mouseover/out events on dragHandles and points. Particularly arearange
  2182. // series are finicky since the markers are not individual points. This logic
  2183. // should preferably be improved in the future. Notice that the mouseOut event
  2184. // below must have a shorter timeout to ensure event order.
  2185. addEvent(Point, 'mouseOver', function () {
  2186. var point = this;
  2187. setTimeout(function () {
  2188. mouseOver(point);
  2189. }, 12);
  2190. });
  2191. // Point mouseleave event. See above function for explanation of the timeout.
  2192. addEvent(Point, 'mouseOut', function () {
  2193. var point = this;
  2194. setTimeout(function () {
  2195. if (point.series) {
  2196. mouseOut(point);
  2197. }
  2198. }, 10);
  2199. });
  2200. // Hide drag handles on a point if it is removed
  2201. addEvent(Point, 'remove', function () {
  2202. var chart = this.series.chart, dragHandles = chart.dragHandles;
  2203. if (dragHandles && dragHandles.point === this.id) {
  2204. chart.hideDragHandles();
  2205. }
  2206. });
  2207. /**
  2208. * Check whether the zoomKey or panKey is pressed.
  2209. *
  2210. * @private
  2211. * @function Highcharts.Chart#zoomOrPanKeyPressed
  2212. * @param {global.Event} e
  2213. * A mouse event.
  2214. * @return {boolean}
  2215. * True if the zoom or pan keys are pressed. False otherwise.
  2216. */
  2217. H.Chart.prototype.zoomOrPanKeyPressed = function (e) {
  2218. // Check whether the panKey and zoomKey are set in chart.userOptions
  2219. var chartOptions = this.userOptions.chart || {}, panKey = chartOptions.panKey && chartOptions.panKey + 'Key', zoomKey = chartOptions.zoomKey && chartOptions.zoomKey + 'Key';
  2220. return (e[zoomKey] || e[panKey]);
  2221. };
  2222. /**
  2223. * Add events to document and chart if the chart is draggable.
  2224. *
  2225. * @private
  2226. * @function addDragDropEvents
  2227. * @param {Highcharts.Chart} chart
  2228. * The chart to add events to.
  2229. */
  2230. function addDragDropEvents(chart) {
  2231. var container = chart.container, doc = H.doc;
  2232. // Only enable if we have a draggable chart
  2233. if (isChartDraggable(chart)) {
  2234. addEvents(container, ['mousedown', 'touchstart'], function (e) {
  2235. mouseDown(getNormalizedEvent(e, chart), chart);
  2236. });
  2237. addEvents(container, ['mousemove', 'touchmove'], function (e) {
  2238. mouseMove(getNormalizedEvent(e, chart), chart);
  2239. });
  2240. addEvent(container, 'mouseleave', function (e) {
  2241. mouseUp(getNormalizedEvent(e, chart), chart);
  2242. });
  2243. chart.unbindDragDropMouseUp = addEvents(doc, ['mouseup', 'touchend'], function (e) {
  2244. mouseUp(getNormalizedEvent(e, chart), chart);
  2245. });
  2246. // Add flag to avoid doing this again
  2247. chart.hasAddedDragDropEvents = true;
  2248. // Add cleanup to make sure we don't pollute document
  2249. addEvent(chart, 'destroy', function () {
  2250. if (chart.unbindDragDropMouseUp) {
  2251. chart.unbindDragDropMouseUp();
  2252. }
  2253. });
  2254. }
  2255. }
  2256. // Add event listener to Chart.render that checks whether or not we should add
  2257. // dragdrop.
  2258. addEvent(H.Chart, 'render', function () {
  2259. // If we don't have dragDrop events, see if we should add them
  2260. if (!this.hasAddedDragDropEvents) {
  2261. addDragDropEvents(this);
  2262. }
  2263. });
  2264. });
  2265. _registerModule(_modules, 'masters/modules/draggable-points.src.js', [], function () {
  2266. });
  2267. }));