ScrollablePlotArea.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* *
  2. *
  3. * (c) 2010-2021 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 Axis from '../Core/Axis/Axis.js';
  23. import Chart from '../Core/Chart/Chart.js';
  24. import Series from '../Core/Series/Series.js';
  25. import H from '../Core/Globals.js';
  26. import U from '../Core/Utilities.js';
  27. var addEvent = U.addEvent, createElement = U.createElement, merge = U.merge, pick = U.pick;
  28. /**
  29. * Options for a scrollable plot area. This feature provides a minimum size for
  30. * the plot area of the chart. If the size gets smaller than this, typically
  31. * on mobile devices, a native browser scrollbar is presented. This scrollbar
  32. * provides smooth scrolling for the contents of the plot area, whereas the
  33. * title, legend and unaffected axes are fixed.
  34. *
  35. * Since v7.1.2, a scrollable plot area can be defined for either horizontal or
  36. * vertical scrolling, depending on whether the `minWidth` or `minHeight`
  37. * option is set.
  38. *
  39. * @sample highcharts/chart/scrollable-plotarea
  40. * Scrollable plot area
  41. * @sample highcharts/chart/scrollable-plotarea-vertical
  42. * Vertically scrollable plot area
  43. * @sample {gantt} highcharts/chart/scrollable-plotarea-vertical
  44. * Gantt chart with vertically scrollable plot area
  45. *
  46. * @since 6.1.0
  47. * @product highcharts gantt
  48. * @apioption chart.scrollablePlotArea
  49. */
  50. /**
  51. * The minimum height for the plot area. If it gets smaller than this, the plot
  52. * area will become scrollable.
  53. *
  54. * @type {number}
  55. * @apioption chart.scrollablePlotArea.minHeight
  56. */
  57. /**
  58. * The minimum width for the plot area. If it gets smaller than this, the plot
  59. * area will become scrollable.
  60. *
  61. * @type {number}
  62. * @apioption chart.scrollablePlotArea.minWidth
  63. */
  64. /**
  65. * The initial scrolling position of the scrollable plot area. Ranges from 0 to
  66. * 1, where 0 aligns the plot area to the left and 1 aligns it to the right.
  67. * Typically we would use 1 if the chart has right aligned Y axes.
  68. *
  69. * @type {number}
  70. * @apioption chart.scrollablePlotArea.scrollPositionX
  71. */
  72. /**
  73. * The initial scrolling position of the scrollable plot area. Ranges from 0 to
  74. * 1, where 0 aligns the plot area to the top and 1 aligns it to the bottom.
  75. *
  76. * @type {number}
  77. * @apioption chart.scrollablePlotArea.scrollPositionY
  78. */
  79. /**
  80. * The opacity of mask applied on one of the sides of the plot
  81. * area.
  82. *
  83. * @sample {highcharts} highcharts/chart/scrollable-plotarea-opacity
  84. * Disabled opacity for the mask
  85. *
  86. * @type {number}
  87. * @default 0.85
  88. * @since 7.1.1
  89. * @apioption chart.scrollablePlotArea.opacity
  90. */
  91. ''; // detach API doclets
  92. /* eslint-disable no-invalid-this, valid-jsdoc */
  93. addEvent(Chart, 'afterSetChartSize', function (e) {
  94. var scrollablePlotArea = this.options.chart.scrollablePlotArea, scrollableMinWidth = scrollablePlotArea && scrollablePlotArea.minWidth, scrollableMinHeight = scrollablePlotArea && scrollablePlotArea.minHeight, scrollablePixelsX, scrollablePixelsY, corrections;
  95. if (!this.renderer.forExport) {
  96. // The amount of pixels to scroll, the difference between chart
  97. // width and scrollable width
  98. if (scrollableMinWidth) {
  99. this.scrollablePixelsX = scrollablePixelsX = Math.max(0, scrollableMinWidth - this.chartWidth);
  100. if (scrollablePixelsX) {
  101. this.scrollablePlotBox = this.renderer.scrollablePlotBox = merge(this.plotBox);
  102. this.plotBox.width = this.plotWidth += scrollablePixelsX;
  103. if (this.inverted) {
  104. this.clipBox.height += scrollablePixelsX;
  105. }
  106. else {
  107. this.clipBox.width += scrollablePixelsX;
  108. }
  109. corrections = {
  110. // Corrections for right side
  111. 1: { name: 'right', value: scrollablePixelsX }
  112. };
  113. }
  114. // Currently we can only do either X or Y
  115. }
  116. else if (scrollableMinHeight) {
  117. this.scrollablePixelsY = scrollablePixelsY = Math.max(0, scrollableMinHeight - this.chartHeight);
  118. if (scrollablePixelsY) {
  119. this.scrollablePlotBox = this.renderer.scrollablePlotBox = merge(this.plotBox);
  120. this.plotBox.height = this.plotHeight += scrollablePixelsY;
  121. if (this.inverted) {
  122. this.clipBox.width += scrollablePixelsY;
  123. }
  124. else {
  125. this.clipBox.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 css = {
  178. WebkitOverflowScrolling: 'touch',
  179. overflowX: 'hidden',
  180. overflowY: 'hidden'
  181. };
  182. if (this.scrollablePixelsX) {
  183. css.overflowX = 'auto';
  184. }
  185. if (this.scrollablePixelsY) {
  186. css.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. }, css, 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-drillup-button',
  231. '.highcharts-scrollbar',
  232. '.highcharts-subtitle',
  233. '.highcharts-title'
  234. ], axisClass;
  235. if (this.scrollablePixelsX && !this.inverted) {
  236. axisClass = '.highcharts-yaxis';
  237. }
  238. else if (this.scrollablePixelsX && this.inverted) {
  239. axisClass = '.highcharts-xaxis';
  240. }
  241. else if (this.scrollablePixelsY && !this.inverted) {
  242. axisClass = '.highcharts-xaxis';
  243. }
  244. else if (this.scrollablePixelsY && this.inverted) {
  245. axisClass = '.highcharts-yaxis';
  246. }
  247. if (axisClass) {
  248. fixedSelectors.push(axisClass + ":not(.highcharts-radial-axis)", axisClass + "-labels:not(.highcharts-radial-axis-labels)");
  249. }
  250. fixedSelectors.forEach(function (className) {
  251. [].forEach.call(container.querySelectorAll(className), function (elem) {
  252. (elem.namespaceURI === fixedRenderer.SVG_NS ?
  253. fixedRenderer.box :
  254. fixedRenderer.box.parentNode).appendChild(elem);
  255. elem.style.pointerEvents = 'auto';
  256. });
  257. });
  258. };
  259. /**
  260. * @private
  261. * @function Highcharts.Chart#applyFixed
  262. * @return {void}
  263. */
  264. Chart.prototype.applyFixed = function () {
  265. var fixedRenderer, scrollableWidth, scrollableHeight, firstTime = !this.fixedDiv, chartOptions = this.options.chart, scrollableOptions = chartOptions.scrollablePlotArea;
  266. // First render
  267. if (firstTime) {
  268. this.fixedDiv = createElement('div', {
  269. className: 'highcharts-fixed'
  270. }, {
  271. position: 'absolute',
  272. overflow: 'hidden',
  273. pointerEvents: 'none',
  274. zIndex: (chartOptions.style && chartOptions.style.zIndex || 0) + 2,
  275. top: 0
  276. }, null, true);
  277. if (this.scrollingContainer) {
  278. this.scrollingContainer.parentNode.insertBefore(this.fixedDiv, this.scrollingContainer);
  279. }
  280. this.renderTo.style.overflow = 'visible';
  281. this.fixedRenderer = fixedRenderer = new H.Renderer(this.fixedDiv, this.chartWidth, this.chartHeight, this.options.chart.style);
  282. // Mask
  283. this.scrollableMask = fixedRenderer
  284. .path()
  285. .attr({
  286. fill: this.options.chart.backgroundColor || '#fff',
  287. 'fill-opacity': pick(scrollableOptions.opacity, 0.85),
  288. zIndex: -1
  289. })
  290. .addClass('highcharts-scrollable-mask')
  291. .add();
  292. addEvent(this, 'afterShowResetZoom', this.moveFixedElements);
  293. addEvent(this, 'afterDrilldown', this.moveFixedElements);
  294. addEvent(this, 'afterLayOutTitles', this.moveFixedElements);
  295. }
  296. else {
  297. // Set the size of the fixed renderer to the visible width
  298. this.fixedRenderer.setSize(this.chartWidth, this.chartHeight);
  299. }
  300. if (this.scrollableDirty || firstTime) {
  301. this.scrollableDirty = false;
  302. this.moveFixedElements();
  303. }
  304. // Increase the size of the scrollable renderer and background
  305. scrollableWidth = this.chartWidth + (this.scrollablePixelsX || 0);
  306. scrollableHeight = this.chartHeight + (this.scrollablePixelsY || 0);
  307. stop(this.container);
  308. this.container.style.width = scrollableWidth + 'px';
  309. this.container.style.height = scrollableHeight + 'px';
  310. this.renderer.boxWrapper.attr({
  311. width: scrollableWidth,
  312. height: scrollableHeight,
  313. viewBox: [0, 0, scrollableWidth, scrollableHeight].join(' ')
  314. });
  315. this.chartBackground.attr({
  316. width: scrollableWidth,
  317. height: scrollableHeight
  318. });
  319. this.scrollingContainer.style.height = this.chartHeight + 'px';
  320. // Set scroll position
  321. if (firstTime) {
  322. if (scrollableOptions.scrollPositionX) {
  323. this.scrollingContainer.scrollLeft =
  324. this.scrollablePixelsX *
  325. scrollableOptions.scrollPositionX;
  326. }
  327. if (scrollableOptions.scrollPositionY) {
  328. this.scrollingContainer.scrollTop =
  329. this.scrollablePixelsY *
  330. scrollableOptions.scrollPositionY;
  331. }
  332. }
  333. // Mask behind the left and right side
  334. 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 -
  335. (this.scrollablePixelsX || 0), maskPlotBottom = this.plotTop + this.plotHeight -
  336. (this.scrollablePixelsY || 0), d;
  337. if (this.scrollablePixelsX) {
  338. d = [
  339. // Left side
  340. ['M', 0, maskTop],
  341. ['L', this.plotLeft - 1, maskTop],
  342. ['L', this.plotLeft - 1, maskBottom],
  343. ['L', 0, maskBottom],
  344. ['Z'],
  345. // Right side
  346. ['M', maskPlotRight, maskTop],
  347. ['L', this.chartWidth, maskTop],
  348. ['L', this.chartWidth, maskBottom],
  349. ['L', maskPlotRight, maskBottom],
  350. ['Z']
  351. ];
  352. }
  353. else if (this.scrollablePixelsY) {
  354. d = [
  355. // Top side
  356. ['M', maskLeft, 0],
  357. ['L', maskLeft, this.plotTop - 1],
  358. ['L', maskRight, this.plotTop - 1],
  359. ['L', maskRight, 0],
  360. ['Z'],
  361. // Bottom side
  362. ['M', maskLeft, maskPlotBottom],
  363. ['L', maskLeft, this.chartHeight],
  364. ['L', maskRight, this.chartHeight],
  365. ['L', maskRight, maskPlotBottom],
  366. ['Z']
  367. ];
  368. }
  369. else {
  370. d = [['M', 0, 0]];
  371. }
  372. if (this.redrawTrigger !== 'adjustHeight') {
  373. this.scrollableMask.attr({ d: d });
  374. }
  375. };
  376. addEvent(Axis, 'afterInit', function () {
  377. this.chart.scrollableDirty = true;
  378. });
  379. addEvent(Series, 'show', function () {
  380. this.chart.scrollableDirty = true;
  381. });