DraggablePoints.js 75 KB

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