MapAxis.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * (c) 2010-2019 Torstein Honsi
  3. *
  4. * License: www.highcharts.com/license
  5. */
  6. 'use strict';
  7. import H from '../parts/Globals.js';
  8. import '../parts/Utilities.js';
  9. import '../parts/Axis.js';
  10. var addEvent = H.addEvent,
  11. Axis = H.Axis,
  12. pick = H.pick;
  13. // Override to use the extreme coordinates from the SVG shape, not the data
  14. // values
  15. addEvent(Axis, 'getSeriesExtremes', function () {
  16. var xData = [];
  17. // Remove the xData array and cache it locally so that the proceed method
  18. // doesn't use it
  19. if (this.isXAxis) {
  20. this.series.forEach(function (series, i) {
  21. if (series.useMapGeometry) {
  22. xData[i] = series.xData;
  23. series.xData = [];
  24. }
  25. });
  26. this.seriesXData = xData;
  27. }
  28. });
  29. addEvent(Axis, 'afterGetSeriesExtremes', function () {
  30. var xData = this.seriesXData,
  31. dataMin,
  32. dataMax,
  33. useMapGeometry;
  34. // Run extremes logic for map and mapline
  35. if (this.isXAxis) {
  36. dataMin = pick(this.dataMin, Number.MAX_VALUE);
  37. dataMax = pick(this.dataMax, -Number.MAX_VALUE);
  38. this.series.forEach(function (series, i) {
  39. if (series.useMapGeometry) {
  40. dataMin = Math.min(dataMin, pick(series.minX, dataMin));
  41. dataMax = Math.max(dataMax, pick(series.maxX, dataMax));
  42. series.xData = xData[i]; // Reset xData array
  43. useMapGeometry = true;
  44. }
  45. });
  46. if (useMapGeometry) {
  47. this.dataMin = dataMin;
  48. this.dataMax = dataMax;
  49. }
  50. delete this.seriesXData;
  51. }
  52. });
  53. // Override axis translation to make sure the aspect ratio is always kept
  54. addEvent(Axis, 'afterSetAxisTranslation', function () {
  55. var chart = this.chart,
  56. mapRatio,
  57. plotRatio = chart.plotWidth / chart.plotHeight,
  58. adjustedAxisLength,
  59. xAxis = chart.xAxis[0],
  60. padAxis,
  61. fixTo,
  62. fixDiff,
  63. preserveAspectRatio;
  64. // Check for map-like series
  65. if (this.coll === 'yAxis' && xAxis.transA !== undefined) {
  66. this.series.forEach(function (series) {
  67. if (series.preserveAspectRatio) {
  68. preserveAspectRatio = true;
  69. }
  70. });
  71. }
  72. // On Y axis, handle both
  73. if (preserveAspectRatio) {
  74. // Use the same translation for both axes
  75. this.transA = xAxis.transA = Math.min(this.transA, xAxis.transA);
  76. mapRatio = plotRatio /
  77. ((xAxis.max - xAxis.min) / (this.max - this.min));
  78. // What axis to pad to put the map in the middle
  79. padAxis = mapRatio < 1 ? this : xAxis;
  80. // Pad it
  81. adjustedAxisLength = (padAxis.max - padAxis.min) * padAxis.transA;
  82. padAxis.pixelPadding = padAxis.len - adjustedAxisLength;
  83. padAxis.minPixelPadding = padAxis.pixelPadding / 2;
  84. fixTo = padAxis.fixTo;
  85. if (fixTo) {
  86. fixDiff = fixTo[1] - padAxis.toValue(fixTo[0], true);
  87. fixDiff *= padAxis.transA;
  88. if (
  89. Math.abs(fixDiff) > padAxis.minPixelPadding ||
  90. (
  91. padAxis.min === padAxis.dataMin &&
  92. padAxis.max === padAxis.dataMax
  93. )
  94. ) { // zooming out again, keep within restricted area
  95. fixDiff = 0;
  96. }
  97. padAxis.minPixelPadding -= fixDiff;
  98. }
  99. }
  100. });
  101. // Override Axis.render in order to delete the fixTo prop
  102. addEvent(Axis, 'render', function () {
  103. this.fixTo = null;
  104. });