draggable-points.src.js 75 KB

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