drag-panes.src.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  3. *
  4. * Drag-panes module
  5. *
  6. * (c) 2010-2019 Highsoft AS
  7. * Author: Kacper Madej
  8. *
  9. * License: www.highcharts.com/license
  10. */
  11. 'use strict';
  12. (function (factory) {
  13. if (typeof module === 'object' && module.exports) {
  14. factory['default'] = factory;
  15. module.exports = factory;
  16. } else if (typeof define === 'function' && define.amd) {
  17. define('highcharts/modules/drag-panes', ['highcharts', 'highcharts/modules/stock'], function (Highcharts) {
  18. factory(Highcharts);
  19. factory.Highcharts = Highcharts;
  20. return factory;
  21. });
  22. } else {
  23. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  24. }
  25. }(function (Highcharts) {
  26. var _modules = Highcharts ? Highcharts._modules : {};
  27. function _registerModule(obj, path, args, fn) {
  28. if (!obj.hasOwnProperty(path)) {
  29. obj[path] = fn.apply(null, args);
  30. }
  31. }
  32. _registerModule(_modules, 'Extensions/DragPanes.js', [_modules['Core/Globals.js'], _modules['Core/Axis/Axis.js'], _modules['Core/Pointer.js'], _modules['Core/Utilities.js']], function (H, Axis, Pointer, U) {
  33. /* *
  34. *
  35. * Plugin for resizing axes / panes in a chart.
  36. *
  37. * (c) 2010-2017 Highsoft AS
  38. *
  39. * Author: Kacper Madej
  40. *
  41. * License: www.highcharts.com/license
  42. *
  43. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  44. *
  45. * */
  46. var hasTouch = H.hasTouch;
  47. var addEvent = U.addEvent,
  48. clamp = U.clamp,
  49. isNumber = U.isNumber,
  50. merge = U.merge,
  51. objectEach = U.objectEach,
  52. relativeLength = U.relativeLength,
  53. wrap = U.wrap;
  54. /* eslint-disable no-invalid-this, valid-jsdoc */
  55. /**
  56. * The AxisResizer class.
  57. *
  58. * @private
  59. * @class
  60. * @name Highcharts.AxisResizer
  61. *
  62. * @param {Highcharts.Axis} axis
  63. * Main axis for the AxisResizer.
  64. */
  65. var AxisResizer = /** @class */ (function () {
  66. function AxisResizer(axis) {
  67. /* eslint-enable no-invalid-this */
  68. this.axis = void 0;
  69. this.controlLine = void 0;
  70. this.lastPos = void 0;
  71. this.options = void 0;
  72. this.init(axis);
  73. }
  74. /**
  75. * Initialize the AxisResizer object.
  76. *
  77. * @function Highcharts.AxisResizer#init
  78. *
  79. * @param {Highcharts.Axis} axis
  80. * Main axis for the AxisResizer.
  81. */
  82. AxisResizer.prototype.init = function (axis, update) {
  83. this.axis = axis;
  84. this.options = axis.options.resize;
  85. this.render();
  86. if (!update) {
  87. // Add mouse events.
  88. this.addMouseEvents();
  89. }
  90. };
  91. /**
  92. * Render the AxisResizer
  93. *
  94. * @function Highcharts.AxisResizer#render
  95. */
  96. AxisResizer.prototype.render = function () {
  97. var resizer = this,
  98. axis = resizer.axis,
  99. chart = axis.chart,
  100. options = resizer.options,
  101. x = options.x || 0,
  102. y = options.y,
  103. // Normalize control line position according to the plot area
  104. pos = clamp(axis.top + axis.height + y,
  105. chart.plotTop,
  106. chart.plotTop + chart.plotHeight),
  107. attr = {},
  108. lineWidth;
  109. if (!chart.styledMode) {
  110. attr = {
  111. cursor: options.cursor,
  112. stroke: options.lineColor,
  113. 'stroke-width': options.lineWidth,
  114. dashstyle: options.lineDashStyle
  115. };
  116. }
  117. // Register current position for future reference.
  118. resizer.lastPos = pos - y;
  119. if (!resizer.controlLine) {
  120. resizer.controlLine = chart.renderer.path()
  121. .addClass('highcharts-axis-resizer');
  122. }
  123. // Add to axisGroup after axis update, because the group is recreated
  124. // Do .add() before path is calculated because strokeWidth() needs it.
  125. resizer.controlLine.add(axis.axisGroup);
  126. lineWidth = chart.styledMode ?
  127. resizer.controlLine.strokeWidth() :
  128. options.lineWidth;
  129. attr.d = chart.renderer.crispLine([
  130. ['M', axis.left + x, pos],
  131. ['L', axis.left + axis.width + x, pos]
  132. ], lineWidth);
  133. resizer.controlLine.attr(attr);
  134. };
  135. /**
  136. * Set up the mouse and touch events for the control line.
  137. *
  138. * @function Highcharts.AxisResizer#addMouseEvents
  139. */
  140. AxisResizer.prototype.addMouseEvents = function () {
  141. var resizer = this,
  142. ctrlLineElem = resizer.controlLine.element,
  143. container = resizer.axis.chart.container,
  144. eventsToUnbind = [],
  145. mouseMoveHandler,
  146. mouseUpHandler,
  147. mouseDownHandler;
  148. // Create mouse events' handlers.
  149. // Make them as separate functions to enable wrapping them:
  150. resizer.mouseMoveHandler = mouseMoveHandler = function (e) {
  151. resizer.onMouseMove(e);
  152. };
  153. resizer.mouseUpHandler = mouseUpHandler = function (e) {
  154. resizer.onMouseUp(e);
  155. };
  156. resizer.mouseDownHandler = mouseDownHandler = function (e) {
  157. resizer.onMouseDown(e);
  158. };
  159. // Add mouse move and mouseup events. These are bind to doc/container,
  160. // because resizer.grabbed flag is stored in mousedown events.
  161. eventsToUnbind.push(addEvent(container, 'mousemove', mouseMoveHandler), addEvent(container.ownerDocument, 'mouseup', mouseUpHandler), addEvent(ctrlLineElem, 'mousedown', mouseDownHandler));
  162. // Touch events.
  163. if (hasTouch) {
  164. eventsToUnbind.push(addEvent(container, 'touchmove', mouseMoveHandler), addEvent(container.ownerDocument, 'touchend', mouseUpHandler), addEvent(ctrlLineElem, 'touchstart', mouseDownHandler));
  165. }
  166. resizer.eventsToUnbind = eventsToUnbind;
  167. };
  168. /**
  169. * Mouse move event based on x/y mouse position.
  170. *
  171. * @function Highcharts.AxisResizer#onMouseMove
  172. *
  173. * @param {Highcharts.PointerEventObject} e
  174. * Mouse event.
  175. */
  176. AxisResizer.prototype.onMouseMove = function (e) {
  177. /*
  178. * In iOS, a mousemove event with e.pageX === 0 is fired when holding
  179. * the finger down in the center of the scrollbar. This should
  180. * be ignored. Borrowed from Navigator.
  181. */
  182. if (!e.touches || e.touches[0].pageX !== 0) {
  183. // Drag the control line
  184. if (this.grabbed) {
  185. this.hasDragged = true;
  186. this.updateAxes(this.axis.chart.pointer.normalize(e).chartY -
  187. this.options.y);
  188. }
  189. }
  190. };
  191. /**
  192. * Mouse up event based on x/y mouse position.
  193. *
  194. * @function Highcharts.AxisResizer#onMouseUp
  195. *
  196. * @param {Highcharts.PointerEventObject} e
  197. * Mouse event.
  198. */
  199. AxisResizer.prototype.onMouseUp = function (e) {
  200. if (this.hasDragged) {
  201. this.updateAxes(this.axis.chart.pointer.normalize(e).chartY -
  202. this.options.y);
  203. }
  204. // Restore runPointActions.
  205. this.grabbed = this.hasDragged = this.axis.chart.activeResizer =
  206. null;
  207. };
  208. /**
  209. * Mousedown on a control line.
  210. * Will store necessary information for drag&drop.
  211. *
  212. * @function Highcharts.AxisResizer#onMouseDown
  213. */
  214. AxisResizer.prototype.onMouseDown = function (e) {
  215. // Clear all hover effects.
  216. this.axis.chart.pointer.reset(false, 0);
  217. // Disable runPointActions.
  218. this.grabbed = this.axis.chart.activeResizer = true;
  219. };
  220. /**
  221. * Update all connected axes after a change of control line position
  222. *
  223. * @function Highcharts.AxisResizer#updateAxes
  224. *
  225. * @param {number} chartY
  226. */
  227. AxisResizer.prototype.updateAxes = function (chartY) {
  228. var resizer = this,
  229. chart = resizer.axis.chart,
  230. axes = resizer.options.controlledAxis,
  231. nextAxes = axes.next.length === 0 ?
  232. [chart.yAxis.indexOf(resizer.axis) + 1] : axes.next,
  233. // Main axis is included in the prev array by default
  234. prevAxes = [resizer.axis].concat(axes.prev),
  235. // prev and next configs
  236. axesConfigs = [],
  237. stopDrag = false,
  238. plotTop = chart.plotTop,
  239. plotHeight = chart.plotHeight,
  240. plotBottom = plotTop + plotHeight,
  241. yDelta,
  242. calculatePercent = function (value) {
  243. return value * 100 / plotHeight + '%';
  244. }, normalize = function (val, min, max) {
  245. return Math.round(clamp(val, min, max));
  246. };
  247. // Normalize chartY to plot area limits
  248. chartY = clamp(chartY, plotTop, plotBottom);
  249. yDelta = chartY - resizer.lastPos;
  250. // Update on changes of at least 1 pixel in the desired direction
  251. if (yDelta * yDelta < 1) {
  252. return;
  253. }
  254. // First gather info how axes should behave
  255. [prevAxes, nextAxes].forEach(function (axesGroup, isNext) {
  256. axesGroup.forEach(function (axisInfo, i) {
  257. // Axes given as array index, axis object or axis id
  258. var axis = isNumber(axisInfo) ?
  259. // If it's a number - it's an index
  260. chart.yAxis[axisInfo] :
  261. (
  262. // If it's first elem. in first group
  263. (!isNext && !i) ?
  264. // then it's an Axis object
  265. axisInfo :
  266. // else it should be an id
  267. chart.get(axisInfo)),
  268. axisOptions = axis && axis.options,
  269. optionsToUpdate = {},
  270. hDelta = 0,
  271. height,
  272. top,
  273. minLength,
  274. maxLength;
  275. // Skip if axis is not found
  276. // or it is navigator's yAxis (#7732)
  277. if (!axisOptions ||
  278. axisOptions.id === 'navigator-y-axis') {
  279. return;
  280. }
  281. top = axis.top;
  282. minLength = Math.round(relativeLength(axisOptions.minLength, plotHeight));
  283. maxLength = Math.round(relativeLength(axisOptions.maxLength, plotHeight));
  284. if (isNext) {
  285. // Try to change height first. yDelta could had changed
  286. yDelta = chartY - resizer.lastPos;
  287. // Normalize height to option limits
  288. height = normalize(axis.len - yDelta, minLength, maxLength);
  289. // Adjust top, so the axis looks like shrinked from top
  290. top = axis.top + yDelta;
  291. // Check for plot area limits
  292. if (top + height > plotBottom) {
  293. hDelta = plotBottom - height - top;
  294. chartY += hDelta;
  295. top += hDelta;
  296. }
  297. // Fit to plot - when overflowing on top
  298. if (top < plotTop) {
  299. top = plotTop;
  300. if (top + height > plotBottom) {
  301. height = plotHeight;
  302. }
  303. }
  304. // If next axis meets min length, stop dragging:
  305. if (height === minLength) {
  306. stopDrag = true;
  307. }
  308. axesConfigs.push({
  309. axis: axis,
  310. options: {
  311. top: calculatePercent(top - plotTop),
  312. height: calculatePercent(height)
  313. }
  314. });
  315. }
  316. else {
  317. // Normalize height to option limits
  318. height = normalize(chartY - top, minLength, maxLength);
  319. // If prev axis meets max length, stop dragging:
  320. if (height === maxLength) {
  321. stopDrag = true;
  322. }
  323. // Check axis size limits
  324. chartY = top + height;
  325. axesConfigs.push({
  326. axis: axis,
  327. options: {
  328. height: calculatePercent(height)
  329. }
  330. });
  331. }
  332. optionsToUpdate.height = height;
  333. });
  334. });
  335. // If we hit the min/maxLength with dragging, don't do anything:
  336. if (!stopDrag) {
  337. // Now update axes:
  338. axesConfigs.forEach(function (config) {
  339. config.axis.update(config.options, false);
  340. });
  341. chart.redraw(false);
  342. }
  343. };
  344. /**
  345. * Destroy AxisResizer. Clear outside references, clear events,
  346. * destroy elements, nullify properties.
  347. *
  348. * @function Highcharts.AxisResizer#destroy
  349. */
  350. AxisResizer.prototype.destroy = function () {
  351. var resizer = this,
  352. axis = resizer.axis;
  353. // Clear resizer in axis
  354. delete axis.resizer;
  355. // Clear control line events
  356. if (this.eventsToUnbind) {
  357. this.eventsToUnbind.forEach(function (unbind) {
  358. unbind();
  359. });
  360. }
  361. // Destroy AxisResizer elements
  362. resizer.controlLine.destroy();
  363. // Nullify properties
  364. objectEach(resizer, function (val, key) {
  365. resizer[key] = null;
  366. });
  367. };
  368. // Default options for AxisResizer.
  369. AxisResizer.resizerOptions = {
  370. /**
  371. * Minimal size of a resizable axis. Could be set as a percent
  372. * of plot area or pixel size.
  373. *
  374. * @sample {highstock} stock/yaxis/resize-min-max-length
  375. * minLength and maxLength
  376. *
  377. * @type {number|string}
  378. * @product highstock
  379. * @requires modules/drag-panes
  380. * @apioption yAxis.minLength
  381. */
  382. minLength: '10%',
  383. /**
  384. * Maximal size of a resizable axis. Could be set as a percent
  385. * of plot area or pixel size.
  386. *
  387. * @sample {highstock} stock/yaxis/resize-min-max-length
  388. * minLength and maxLength
  389. *
  390. * @type {number|string}
  391. * @product highstock
  392. * @requires modules/drag-panes
  393. * @apioption yAxis.maxLength
  394. */
  395. maxLength: '100%',
  396. /**
  397. * Options for axis resizing. It adds a thick line between panes which
  398. * the user can drag in order to resize the panes.
  399. *
  400. * @sample {highstock} stock/demo/candlestick-and-volume
  401. * Axis resizing enabled
  402. *
  403. * @product highstock
  404. * @requires modules/drag-panes
  405. * @optionparent yAxis.resize
  406. */
  407. resize: {
  408. /**
  409. * Contains two arrays of axes that are controlled by control line
  410. * of the axis.
  411. *
  412. * @requires modules/drag-panes
  413. */
  414. controlledAxis: {
  415. /**
  416. * Array of axes that should move out of the way of resizing
  417. * being done for the current axis. If not set, the next axis
  418. * will be used.
  419. *
  420. * @sample {highstock} stock/yaxis/multiple-resizers
  421. * Three panes with resizers
  422. * @sample {highstock} stock/yaxis/resize-multiple-axes
  423. * One resizer controlling multiple axes
  424. *
  425. * @type {Array<number|string>}
  426. * @default []
  427. * @requires modules/drag-panes
  428. */
  429. next: [],
  430. /**
  431. * Array of axes that should move with the current axis
  432. * while resizing.
  433. *
  434. * @sample {highstock} stock/yaxis/multiple-resizers
  435. * Three panes with resizers
  436. * @sample {highstock} stock/yaxis/resize-multiple-axes
  437. * One resizer controlling multiple axes
  438. *
  439. * @type {Array<number|string>}
  440. * @default []
  441. * @requires modules/drag-panes
  442. */
  443. prev: []
  444. },
  445. /**
  446. * Enable or disable resize by drag for the axis.
  447. *
  448. * @sample {highstock} stock/demo/candlestick-and-volume
  449. * Enabled resizer
  450. *
  451. * @requires modules/drag-panes
  452. */
  453. enabled: false,
  454. /**
  455. * Cursor style for the control line.
  456. *
  457. * In styled mode use class `highcharts-axis-resizer` instead.
  458. *
  459. * @requires modules/drag-panes
  460. */
  461. cursor: 'ns-resize',
  462. /**
  463. * Color of the control line.
  464. *
  465. * In styled mode use class `highcharts-axis-resizer` instead.
  466. *
  467. * @sample {highstock} stock/yaxis/styled-resizer
  468. * Styled resizer
  469. *
  470. * @type {Highcharts.ColorString}
  471. * @requires modules/drag-panes
  472. */
  473. lineColor: '#cccccc',
  474. /**
  475. * Dash style of the control line.
  476. *
  477. * In styled mode use class `highcharts-axis-resizer` instead.
  478. *
  479. * @see For supported options check [dashStyle](#plotOptions.series.dashStyle)
  480. *
  481. * @sample {highstock} stock/yaxis/styled-resizer
  482. * Styled resizer
  483. *
  484. * @requires modules/drag-panes
  485. */
  486. lineDashStyle: 'Solid',
  487. /**
  488. * Width of the control line.
  489. *
  490. * In styled mode use class `highcharts-axis-resizer` instead.
  491. *
  492. * @sample {highstock} stock/yaxis/styled-resizer
  493. * Styled resizer
  494. *
  495. * @requires modules/drag-panes
  496. */
  497. lineWidth: 4,
  498. /**
  499. * Horizontal offset of the control line.
  500. *
  501. * @sample {highstock} stock/yaxis/styled-resizer
  502. * Styled resizer
  503. *
  504. * @requires modules/drag-panes
  505. */
  506. x: 0,
  507. /**
  508. * Vertical offset of the control line.
  509. *
  510. * @sample {highstock} stock/yaxis/styled-resizer
  511. * Styled resizer
  512. *
  513. * @requires modules/drag-panes
  514. */
  515. y: 0
  516. }
  517. };
  518. return AxisResizer;
  519. }());
  520. // Keep resizer reference on axis update
  521. Axis.keepProps.push('resizer');
  522. /* eslint-disable no-invalid-this */
  523. // Add new AxisResizer, update or remove it
  524. addEvent(Axis, 'afterRender', function () {
  525. var axis = this,
  526. resizer = axis.resizer,
  527. resizerOptions = axis.options.resize,
  528. enabled;
  529. if (resizerOptions) {
  530. enabled = resizerOptions.enabled !== false;
  531. if (resizer) {
  532. // Resizer present and enabled
  533. if (enabled) {
  534. // Update options
  535. resizer.init(axis, true);
  536. // Resizer present, but disabled
  537. }
  538. else {
  539. // Destroy the resizer
  540. resizer.destroy();
  541. }
  542. }
  543. else {
  544. // Resizer not present and enabled
  545. if (enabled) {
  546. // Add new resizer
  547. axis.resizer = new H.AxisResizer(axis);
  548. }
  549. // Resizer not present and disabled, so do nothing
  550. }
  551. }
  552. });
  553. // Clear resizer on axis remove.
  554. addEvent(Axis, 'destroy', function (e) {
  555. if (!e.keepEvents && this.resizer) {
  556. this.resizer.destroy();
  557. }
  558. });
  559. // Prevent any hover effects while dragging a control line of AxisResizer.
  560. wrap(Pointer.prototype, 'runPointActions', function (proceed) {
  561. if (!this.chart.activeResizer) {
  562. proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  563. }
  564. });
  565. // Prevent default drag action detection while dragging a control line of
  566. // AxisResizer. (#7563)
  567. wrap(Pointer.prototype, 'drag', function (proceed) {
  568. if (!this.chart.activeResizer) {
  569. proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  570. }
  571. });
  572. merge(true, Axis.defaultYAxisOptions, AxisResizer.resizerOptions);
  573. H.AxisResizer = AxisResizer;
  574. return H.AxisResizer;
  575. });
  576. _registerModule(_modules, 'masters/modules/drag-panes.src.js', [], function () {
  577. });
  578. }));