DraggablePoints.js 76 KB

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