drag-panes.src.js 19 KB

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