static-scale.src.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* *
  2. * (c) 2016-2019 Torstein Honsi, Lars Cabrera
  3. *
  4. * License: www.highcharts.com/license
  5. */
  6. 'use strict';
  7. import H from '../parts/Globals.js';
  8. import '../parts/Utilities.js';
  9. var Chart = H.Chart,
  10. pick = H.pick;
  11. /**
  12. * For vertical axes only. Setting the static scale ensures that each tick unit
  13. * is translated into a fixed pixel height. For example, setting the static
  14. * scale to 24 results in each Y axis category taking up 24 pixels, and the
  15. * height of the chart adjusts. Adding or removing items will make the chart
  16. * resize.
  17. *
  18. * @sample gantt/xrange-series/demo/
  19. * X-range series with static scale
  20. *
  21. * @type {number}
  22. * @default 50
  23. * @since 6.2.0
  24. * @product gantt
  25. * @apioption yAxis.staticScale
  26. */
  27. H.addEvent(H.Axis, 'afterSetOptions', function () {
  28. if (
  29. !this.horiz &&
  30. H.isNumber(this.options.staticScale) &&
  31. !this.chart.options.chart.height
  32. ) {
  33. this.staticScale = this.options.staticScale;
  34. }
  35. });
  36. Chart.prototype.adjustHeight = function () {
  37. if (this.redrawTrigger !== 'adjustHeight') {
  38. (this.axes || []).forEach(function (axis) {
  39. var chart = axis.chart,
  40. animate = !!chart.initiatedScale && chart.options.animation,
  41. staticScale = axis.options.staticScale,
  42. height,
  43. diff;
  44. if (axis.staticScale && H.defined(axis.min)) {
  45. height = pick(
  46. axis.unitLength,
  47. axis.max + axis.tickInterval - axis.min
  48. ) * staticScale;
  49. // Minimum height is 1 x staticScale.
  50. height = Math.max(height, staticScale);
  51. diff = height - chart.plotHeight;
  52. if (Math.abs(diff) >= 1) {
  53. chart.plotHeight = height;
  54. chart.redrawTrigger = 'adjustHeight';
  55. chart.setSize(undefined, 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 =
  61. series.sharedClipKey && chart[series.sharedClipKey];
  62. if (clipRect) {
  63. clipRect.attr({
  64. height: chart.plotHeight
  65. });
  66. }
  67. });
  68. }
  69. });
  70. this.initiatedScale = true;
  71. }
  72. this.redrawTrigger = null;
  73. };
  74. H.addEvent(Chart, 'render', Chart.prototype.adjustHeight);