DraggablePoints.js 75 KB

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