axisAlignTicks.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { getPrecisionSafe, round } from '../util/number.js';
  41. import IntervalScale from '../scale/Interval.js';
  42. import { getScaleExtent, retrieveAxisBreaksOption } from './axisHelper.js';
  43. import { warn } from '../util/log.js';
  44. import { logTransform, increaseInterval, isValueNice } from '../scale/helper.js';
  45. export function alignScaleTicks(scale, axisModel, alignToScale) {
  46. var _a;
  47. var intervalScaleProto = IntervalScale.prototype;
  48. // NOTE: There is a precondition for log scale here:
  49. // In log scale we store _interval and _extent of exponent value.
  50. // So if we use the method of InternalScale to set/get these data.
  51. // It process the exponent value, which is linear and what we want here.
  52. var alignToTicks = intervalScaleProto.getTicks.call(alignToScale);
  53. var alignToNicedTicks = intervalScaleProto.getTicks.call(alignToScale, {
  54. expandToNicedExtent: true
  55. });
  56. var alignToSplitNumber = alignToTicks.length - 1;
  57. var alignToInterval = intervalScaleProto.getInterval.call(alignToScale);
  58. var scaleExtent = getScaleExtent(scale, axisModel);
  59. var rawExtent = scaleExtent.extent;
  60. var isMinFixed = scaleExtent.fixMin;
  61. var isMaxFixed = scaleExtent.fixMax;
  62. if (scale.type === 'log') {
  63. rawExtent = logTransform(scale.base, rawExtent, true);
  64. }
  65. scale.setBreaksFromOption(retrieveAxisBreaksOption(axisModel));
  66. scale.setExtent(rawExtent[0], rawExtent[1]);
  67. scale.calcNiceExtent({
  68. splitNumber: alignToSplitNumber,
  69. fixMin: isMinFixed,
  70. fixMax: isMaxFixed
  71. });
  72. var extent = intervalScaleProto.getExtent.call(scale);
  73. // Need to update the rawExtent.
  74. // Because value in rawExtent may be not parsed. e.g. 'dataMin', 'dataMax'
  75. if (isMinFixed) {
  76. rawExtent[0] = extent[0];
  77. }
  78. if (isMaxFixed) {
  79. rawExtent[1] = extent[1];
  80. }
  81. var interval = intervalScaleProto.getInterval.call(scale);
  82. var min = rawExtent[0];
  83. var max = rawExtent[1];
  84. if (isMinFixed && isMaxFixed) {
  85. // User set min, max, divide to get new interval
  86. interval = (max - min) / alignToSplitNumber;
  87. } else if (isMinFixed) {
  88. max = rawExtent[0] + interval * alignToSplitNumber;
  89. // User set min, expand extent on the other side
  90. while (max < rawExtent[1] && isFinite(max) && isFinite(rawExtent[1])) {
  91. interval = increaseInterval(interval);
  92. max = rawExtent[0] + interval * alignToSplitNumber;
  93. }
  94. } else if (isMaxFixed) {
  95. // User set max, expand extent on the other side
  96. min = rawExtent[1] - interval * alignToSplitNumber;
  97. while (min > rawExtent[0] && isFinite(min) && isFinite(rawExtent[0])) {
  98. interval = increaseInterval(interval);
  99. min = rawExtent[1] - interval * alignToSplitNumber;
  100. }
  101. } else {
  102. var nicedSplitNumber = scale.getTicks().length - 1;
  103. if (nicedSplitNumber > alignToSplitNumber) {
  104. interval = increaseInterval(interval);
  105. }
  106. var range = interval * alignToSplitNumber;
  107. max = Math.ceil(rawExtent[1] / interval) * interval;
  108. min = round(max - range);
  109. // Not change the result that crossing zero.
  110. if (min < 0 && rawExtent[0] >= 0) {
  111. min = 0;
  112. max = round(range);
  113. } else if (max > 0 && rawExtent[1] <= 0) {
  114. max = 0;
  115. min = -round(range);
  116. }
  117. }
  118. // Adjust min, max based on the extent of alignTo. When min or max is set in alignTo scale
  119. var t0 = (alignToTicks[0].value - alignToNicedTicks[0].value) / alignToInterval;
  120. var t1 = (alignToTicks[alignToSplitNumber].value - alignToNicedTicks[alignToSplitNumber].value) / alignToInterval;
  121. // NOTE: Must in setExtent -> setInterval -> setNiceExtent order.
  122. intervalScaleProto.setExtent.call(scale, min + interval * t0, max + interval * t1);
  123. intervalScaleProto.setInterval.call(scale, interval);
  124. if (t0 || t1) {
  125. intervalScaleProto.setNiceExtent.call(scale, min + interval, max - interval);
  126. }
  127. if (process.env.NODE_ENV !== 'production') {
  128. var ticks = intervalScaleProto.getTicks.call(scale);
  129. if (ticks[1] && (!isValueNice(interval) || getPrecisionSafe(ticks[1].value) > getPrecisionSafe(interval))) {
  130. warn("The ticks may be not readable when set min: " + axisModel.get('min') + ", max: " + axisModel.get('max') + (" and alignTicks: true. (" + ((_a = axisModel.axis) === null || _a === void 0 ? void 0 : _a.dim) + "AxisIndex: " + axisModel.componentIndex + ")"), true);
  131. }
  132. }
  133. }