draggable-points.src.js 75 KB

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