ScrollablePlotArea.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * Highcharts feature to make the Y axis stay fixed when scrolling the chart
  10. * horizontally on mobile devices. Supports left and right side axes.
  11. */
  12. /*
  13. WIP on vertical scrollable plot area (#9378). To do:
  14. - Bottom axis positioning
  15. - Test with Gantt
  16. - Look for size optimizing the code
  17. - API and demos
  18. */
  19. 'use strict';
  20. import A from '../Core/Animation/AnimationUtilities.js';
  21. var stop = A.stop;
  22. import Chart from '../Core/Chart/Chart.js';
  23. import H from '../Core/Globals.js';
  24. import U from '../Core/Utilities.js';
  25. var addEvent = U.addEvent, createElement = U.createElement, pick = U.pick;
  26. /**
  27. * Options for a scrollable plot area. This feature provides a minimum size for
  28. * the plot area of the chart. If the size gets smaller than this, typically
  29. * on mobile devices, a native browser scrollbar is presented. This scrollbar
  30. * provides smooth scrolling for the contents of the plot area, whereas the
  31. * title, legend and unaffected axes are fixed.
  32. *
  33. * Since v7.1.2, a scrollable plot area can be defined for either horizontal or
  34. * vertical scrolling, depending on whether the `minWidth` or `minHeight`
  35. * option is set.
  36. *
  37. * @sample highcharts/chart/scrollable-plotarea
  38. * Scrollable plot area
  39. * @sample highcharts/chart/scrollable-plotarea-vertical
  40. * Vertically scrollable plot area
  41. * @sample {gantt} highcharts/chart/scrollable-plotarea-vertical
  42. * Gantt chart with vertically scrollable plot area
  43. *
  44. * @since 6.1.0
  45. * @product highcharts gantt
  46. * @apioption chart.scrollablePlotArea
  47. */
  48. /**
  49. * The minimum height for the plot area. If it gets smaller than this, the plot
  50. * area will become scrollable.
  51. *
  52. * @type {number}
  53. * @apioption chart.scrollablePlotArea.minHeight
  54. */
  55. /**
  56. * The minimum width for the plot area. If it gets smaller than this, the plot
  57. * area will become scrollable.
  58. *
  59. * @type {number}
  60. * @apioption chart.scrollablePlotArea.minWidth
  61. */
  62. /**
  63. * The initial scrolling position of the scrollable plot area. Ranges from 0 to
  64. * 1, where 0 aligns the plot area to the left and 1 aligns it to the right.
  65. * Typically we would use 1 if the chart has right aligned Y axes.
  66. *
  67. * @type {number}
  68. * @apioption chart.scrollablePlotArea.scrollPositionX
  69. */
  70. /**
  71. * The initial scrolling position of the scrollable plot area. Ranges from 0 to
  72. * 1, where 0 aligns the plot area to the top and 1 aligns it to the bottom.
  73. *
  74. * @type {number}
  75. * @apioption chart.scrollablePlotArea.scrollPositionY
  76. */
  77. /**
  78. * The opacity of mask applied on one of the sides of the plot
  79. * area.
  80. *
  81. * @sample {highcharts} highcharts/chart/scrollable-plotarea-opacity
  82. * Disabled opacity for the mask
  83. *
  84. * @type {number}
  85. * @default 0.85
  86. * @since 7.1.1
  87. * @apioption chart.scrollablePlotArea.opacity
  88. */
  89. ''; // detach API doclets
  90. /* eslint-disable no-invalid-this, valid-jsdoc */
  91. addEvent(Chart, 'afterSetChartSize', function (e) {
  92. var scrollablePlotArea = this.options.chart.scrollablePlotArea, scrollableMinWidth = scrollablePlotArea && scrollablePlotArea.minWidth, scrollableMinHeight = scrollablePlotArea && scrollablePlotArea.minHeight, scrollablePixelsX, scrollablePixelsY, corrections;
  93. if (!this.renderer.forExport) {
  94. // The amount of pixels to scroll, the difference between chart
  95. // width and scrollable width
  96. if (scrollableMinWidth) {
  97. this.scrollablePixelsX = scrollablePixelsX = Math.max(0, scrollableMinWidth - this.chartWidth);
  98. if (scrollablePixelsX) {
  99. this.plotWidth += scrollablePixelsX;
  100. if (this.inverted) {
  101. this.clipBox.height += scrollablePixelsX;
  102. this.plotBox.height += scrollablePixelsX;
  103. }
  104. else {
  105. this.clipBox.width += scrollablePixelsX;
  106. this.plotBox.width += scrollablePixelsX;
  107. }
  108. corrections = {
  109. // Corrections for right side
  110. 1: { name: 'right', value: scrollablePixelsX }
  111. };
  112. }
  113. // Currently we can only do either X or Y
  114. }
  115. else if (scrollableMinHeight) {
  116. this.scrollablePixelsY = scrollablePixelsY = Math.max(0, scrollableMinHeight - this.chartHeight);
  117. if (scrollablePixelsY) {
  118. this.plotHeight += scrollablePixelsY;
  119. if (this.inverted) {
  120. this.clipBox.width += scrollablePixelsY;
  121. this.plotBox.width += scrollablePixelsY;
  122. }
  123. else {
  124. this.clipBox.height += scrollablePixelsY;
  125. this.plotBox.height += scrollablePixelsY;
  126. }
  127. corrections = {
  128. 2: { name: 'bottom', value: scrollablePixelsY }
  129. };
  130. }
  131. }
  132. if (corrections && !e.skipAxes) {
  133. this.axes.forEach(function (axis) {
  134. // For right and bottom axes, only fix the plot line length
  135. if (corrections[axis.side]) {
  136. // Get the plot lines right in getPlotLinePath,
  137. // temporarily set it to the adjusted plot width.
  138. axis.getPlotLinePath = function () {
  139. var marginName = corrections[axis.side].name, correctionValue = corrections[axis.side].value,
  140. // axis.right or axis.bottom
  141. margin = this[marginName], path;
  142. // Temporarily adjust
  143. this[marginName] = margin - correctionValue;
  144. path = H.Axis.prototype.getPlotLinePath.apply(this, arguments);
  145. // Reset
  146. this[marginName] = margin;
  147. return path;
  148. };
  149. }
  150. else {
  151. // Apply the corrected plotWidth
  152. axis.setAxisSize();
  153. axis.setAxisTranslation();
  154. }
  155. });
  156. }
  157. }
  158. });
  159. addEvent(Chart, 'render', function () {
  160. if (this.scrollablePixelsX || this.scrollablePixelsY) {
  161. if (this.setUpScrolling) {
  162. this.setUpScrolling();
  163. }
  164. this.applyFixed();
  165. }
  166. else if (this.fixedDiv) { // Has been in scrollable mode
  167. this.applyFixed();
  168. }
  169. });
  170. /**
  171. * @private
  172. * @function Highcharts.Chart#setUpScrolling
  173. * @return {void}
  174. */
  175. Chart.prototype.setUpScrolling = function () {
  176. var _this = this;
  177. var attribs = {
  178. WebkitOverflowScrolling: 'touch',
  179. overflowX: 'hidden',
  180. overflowY: 'hidden'
  181. };
  182. if (this.scrollablePixelsX) {
  183. attribs.overflowX = 'auto';
  184. }
  185. if (this.scrollablePixelsY) {
  186. attribs.overflowY = 'auto';
  187. }
  188. // Insert a container with position relative
  189. // that scrolling and fixed container renders to (#10555)
  190. this.scrollingParent = createElement('div', {
  191. className: 'highcharts-scrolling-parent'
  192. }, {
  193. position: 'relative'
  194. }, this.renderTo);
  195. // Add the necessary divs to provide scrolling
  196. this.scrollingContainer = createElement('div', {
  197. 'className': 'highcharts-scrolling'
  198. }, attribs, this.scrollingParent);
  199. // On scroll, reset the chart position because it applies to the scrolled
  200. // container
  201. addEvent(this.scrollingContainer, 'scroll', function () {
  202. if (_this.pointer) {
  203. delete _this.pointer.chartPosition;
  204. }
  205. });
  206. this.innerContainer = createElement('div', {
  207. 'className': 'highcharts-inner-container'
  208. }, null, this.scrollingContainer);
  209. // Now move the container inside
  210. this.innerContainer.appendChild(this.container);
  211. // Don't run again
  212. this.setUpScrolling = null;
  213. };
  214. /**
  215. * These elements are moved over to the fixed renderer and stay fixed when the
  216. * user scrolls the chart
  217. * @private
  218. */
  219. Chart.prototype.moveFixedElements = function () {
  220. var container = this.container, fixedRenderer = this.fixedRenderer, fixedSelectors = [
  221. '.highcharts-contextbutton',
  222. '.highcharts-credits',
  223. '.highcharts-legend',
  224. '.highcharts-legend-checkbox',
  225. '.highcharts-navigator-series',
  226. '.highcharts-navigator-xaxis',
  227. '.highcharts-navigator-yaxis',
  228. '.highcharts-navigator',
  229. '.highcharts-reset-zoom',
  230. '.highcharts-scrollbar',
  231. '.highcharts-subtitle',
  232. '.highcharts-title'
  233. ], axisClass;
  234. if (this.scrollablePixelsX && !this.inverted) {
  235. axisClass = '.highcharts-yaxis';
  236. }
  237. else if (this.scrollablePixelsX && this.inverted) {
  238. axisClass = '.highcharts-xaxis';
  239. }
  240. else if (this.scrollablePixelsY && !this.inverted) {
  241. axisClass = '.highcharts-xaxis';
  242. }
  243. else if (this.scrollablePixelsY && this.inverted) {
  244. axisClass = '.highcharts-yaxis';
  245. }
  246. fixedSelectors.push(axisClass, axisClass + '-labels');
  247. fixedSelectors.forEach(function (className) {
  248. [].forEach.call(container.querySelectorAll(className), function (elem) {
  249. (elem.namespaceURI === fixedRenderer.SVG_NS ?
  250. fixedRenderer.box :
  251. fixedRenderer.box.parentNode).appendChild(elem);
  252. elem.style.pointerEvents = 'auto';
  253. });
  254. });
  255. };
  256. /**
  257. * @private
  258. * @function Highcharts.Chart#applyFixed
  259. * @return {void}
  260. */
  261. Chart.prototype.applyFixed = function () {
  262. var _a, _b;
  263. var fixedRenderer, scrollableWidth, scrollableHeight, firstTime = !this.fixedDiv, scrollableOptions = this.options.chart.scrollablePlotArea;
  264. // First render
  265. if (firstTime) {
  266. this.fixedDiv = createElement('div', {
  267. className: 'highcharts-fixed'
  268. }, {
  269. position: 'absolute',
  270. overflow: 'hidden',
  271. pointerEvents: 'none',
  272. zIndex: 2,
  273. top: 0
  274. }, null, true);
  275. (_a = this.scrollingContainer) === null || _a === void 0 ? void 0 : _a.parentNode.insertBefore(this.fixedDiv, this.scrollingContainer);
  276. this.renderTo.style.overflow = 'visible';
  277. this.fixedRenderer = fixedRenderer = new H.Renderer(this.fixedDiv, this.chartWidth, this.chartHeight, (_b = this.options.chart) === null || _b === void 0 ? void 0 : _b.style);
  278. // Mask
  279. this.scrollableMask = fixedRenderer
  280. .path()
  281. .attr({
  282. fill: this.options.chart.backgroundColor || '#fff',
  283. 'fill-opacity': pick(scrollableOptions.opacity, 0.85),
  284. zIndex: -1
  285. })
  286. .addClass('highcharts-scrollable-mask')
  287. .add();
  288. this.moveFixedElements();
  289. addEvent(this, 'afterShowResetZoom', this.moveFixedElements);
  290. addEvent(this, 'afterLayOutTitles', this.moveFixedElements);
  291. }
  292. else {
  293. // Set the size of the fixed renderer to the visible width
  294. this.fixedRenderer.setSize(this.chartWidth, this.chartHeight);
  295. }
  296. // Increase the size of the scrollable renderer and background
  297. scrollableWidth = this.chartWidth + (this.scrollablePixelsX || 0);
  298. scrollableHeight = this.chartHeight + (this.scrollablePixelsY || 0);
  299. stop(this.container);
  300. this.container.style.width = scrollableWidth + 'px';
  301. this.container.style.height = scrollableHeight + 'px';
  302. this.renderer.boxWrapper.attr({
  303. width: scrollableWidth,
  304. height: scrollableHeight,
  305. viewBox: [0, 0, scrollableWidth, scrollableHeight].join(' ')
  306. });
  307. this.chartBackground.attr({
  308. width: scrollableWidth,
  309. height: scrollableHeight
  310. });
  311. this.scrollingContainer.style.height = this.chartHeight + 'px';
  312. // Set scroll position
  313. if (firstTime) {
  314. if (scrollableOptions.scrollPositionX) {
  315. this.scrollingContainer.scrollLeft =
  316. this.scrollablePixelsX *
  317. scrollableOptions.scrollPositionX;
  318. }
  319. if (scrollableOptions.scrollPositionY) {
  320. this.scrollingContainer.scrollTop =
  321. this.scrollablePixelsY *
  322. scrollableOptions.scrollPositionY;
  323. }
  324. }
  325. // Mask behind the left and right side
  326. var axisOffset = this.axisOffset, maskTop = this.plotTop - axisOffset[0] - 1, maskLeft = this.plotLeft - axisOffset[3] - 1, maskBottom = this.plotTop + this.plotHeight + axisOffset[2] + 1, maskRight = this.plotLeft + this.plotWidth + axisOffset[1] + 1, maskPlotRight = this.plotLeft + this.plotWidth -
  327. (this.scrollablePixelsX || 0), maskPlotBottom = this.plotTop + this.plotHeight -
  328. (this.scrollablePixelsY || 0), d;
  329. if (this.scrollablePixelsX) {
  330. d = [
  331. // Left side
  332. ['M', 0, maskTop],
  333. ['L', this.plotLeft - 1, maskTop],
  334. ['L', this.plotLeft - 1, maskBottom],
  335. ['L', 0, maskBottom],
  336. ['Z'],
  337. // Right side
  338. ['M', maskPlotRight, maskTop],
  339. ['L', this.chartWidth, maskTop],
  340. ['L', this.chartWidth, maskBottom],
  341. ['L', maskPlotRight, maskBottom],
  342. ['Z']
  343. ];
  344. }
  345. else if (this.scrollablePixelsY) {
  346. d = [
  347. // Top side
  348. ['M', maskLeft, 0],
  349. ['L', maskLeft, this.plotTop - 1],
  350. ['L', maskRight, this.plotTop - 1],
  351. ['L', maskRight, 0],
  352. ['Z'],
  353. // Bottom side
  354. ['M', maskLeft, maskPlotBottom],
  355. ['L', maskLeft, this.chartHeight],
  356. ['L', maskRight, this.chartHeight],
  357. ['L', maskRight, maskPlotBottom],
  358. ['Z']
  359. ];
  360. }
  361. else {
  362. d = [['M', 0, 0]];
  363. }
  364. if (this.redrawTrigger !== 'adjustHeight') {
  365. this.scrollableMask.attr({ d: d });
  366. }
  367. };