ScrollablePlotArea.js 14 KB

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