DraggablePoints.js 75 KB

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