StaticScale.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* *
  2. *
  3. * (c) 2016-2021 Torstein Honsi, Lars Cabrera
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import Axis from '../Core/Axis/Axis.js';
  12. import Chart from '../Core/Chart/Chart.js';
  13. import U from '../Core/Utilities.js';
  14. var addEvent = U.addEvent, defined = U.defined, isNumber = U.isNumber, pick = U.pick;
  15. /* eslint-disable no-invalid-this */
  16. /**
  17. * For vertical axes only. Setting the static scale ensures that each tick unit
  18. * is translated into a fixed pixel height. For example, setting the static
  19. * scale to 24 results in each Y axis category taking up 24 pixels, and the
  20. * height of the chart adjusts. Adding or removing items will make the chart
  21. * resize.
  22. *
  23. * @sample gantt/xrange-series/demo/
  24. * X-range series with static scale
  25. *
  26. * @type {number}
  27. * @default 50
  28. * @since 6.2.0
  29. * @product gantt
  30. * @apioption yAxis.staticScale
  31. */
  32. addEvent(Axis, 'afterSetOptions', function () {
  33. var chartOptions = this.chart.options.chart;
  34. if (!this.horiz &&
  35. isNumber(this.options.staticScale) &&
  36. (!chartOptions.height ||
  37. (chartOptions.scrollablePlotArea &&
  38. chartOptions.scrollablePlotArea.minHeight))) {
  39. this.staticScale = this.options.staticScale;
  40. }
  41. });
  42. Chart.prototype.adjustHeight = function () {
  43. if (this.redrawTrigger !== 'adjustHeight') {
  44. (this.axes || []).forEach(function (axis) {
  45. var chart = axis.chart, animate = !!chart.initiatedScale &&
  46. chart.options.animation, staticScale = axis.options.staticScale, height, diff;
  47. if (axis.staticScale && defined(axis.min)) {
  48. height = pick(axis.brokenAxis && axis.brokenAxis.unitLength, axis.max + axis.tickInterval - axis.min) * staticScale;
  49. // Minimum height is 1 x staticScale.
  50. height = Math.max(height, staticScale);
  51. diff = height - chart.plotHeight;
  52. if (!chart.scrollablePixelsY && Math.abs(diff) >= 1) {
  53. chart.plotHeight = height;
  54. chart.redrawTrigger = 'adjustHeight';
  55. chart.setSize(void 0, chart.chartHeight + diff, animate);
  56. }
  57. // Make sure clip rects have the right height before initial
  58. // animation.
  59. axis.series.forEach(function (series) {
  60. var clipRect = series.sharedClipKey &&
  61. chart.sharedClips[series.sharedClipKey];
  62. if (clipRect) {
  63. clipRect.attr(chart.inverted ? {
  64. width: chart.plotHeight
  65. } : {
  66. height: chart.plotHeight
  67. });
  68. }
  69. });
  70. }
  71. });
  72. this.initiatedScale = true;
  73. }
  74. this.redrawTrigger = null;
  75. };
  76. addEvent(Chart, 'render', Chart.prototype.adjustHeight);