draggable-points.src.js 75 KB

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