axisAlignTicks.js 5.7 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 } from './axisHelper.js';
  43. import { warn } from '../util/log.js';
  44. import { increaseInterval, isValueNice } from '../scale/helper.js';
  45. var mathLog = Math.log;
  46. export function alignScaleTicks(scale, axisModel, alignToScale) {
  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, true);
  54. var alignToSplitNumber = alignToTicks.length - 1;
  55. var alignToInterval = intervalScaleProto.getInterval.call(alignToScale);
  56. var scaleExtent = getScaleExtent(scale, axisModel);
  57. var rawExtent = scaleExtent.extent;
  58. var isMinFixed = scaleExtent.fixMin;
  59. var isMaxFixed = scaleExtent.fixMax;
  60. if (scale.type === 'log') {
  61. var logBase = mathLog(scale.base);
  62. rawExtent = [mathLog(rawExtent[0]) / logBase, mathLog(rawExtent[1]) / logBase];
  63. }
  64. scale.setExtent(rawExtent[0], rawExtent[1]);
  65. scale.calcNiceExtent({
  66. splitNumber: alignToSplitNumber,
  67. fixMin: isMinFixed,
  68. fixMax: isMaxFixed
  69. });
  70. var extent = intervalScaleProto.getExtent.call(scale);
  71. // Need to update the rawExtent.
  72. // Because value in rawExtent may be not parsed. e.g. 'dataMin', 'dataMax'
  73. if (isMinFixed) {
  74. rawExtent[0] = extent[0];
  75. }
  76. if (isMaxFixed) {
  77. rawExtent[1] = extent[1];
  78. }
  79. var interval = intervalScaleProto.getInterval.call(scale);
  80. var min = rawExtent[0];
  81. var max = rawExtent[1];
  82. if (isMinFixed && isMaxFixed) {
  83. // User set min, max, divide to get new interval
  84. interval = (max - min) / alignToSplitNumber;
  85. } else if (isMinFixed) {
  86. max = rawExtent[0] + interval * alignToSplitNumber;
  87. // User set min, expand extent on the other side
  88. while (max < rawExtent[1] && isFinite(max) && isFinite(rawExtent[1])) {
  89. interval = increaseInterval(interval);
  90. max = rawExtent[0] + interval * alignToSplitNumber;
  91. }
  92. } else if (isMaxFixed) {
  93. // User set max, expand extent on the other side
  94. min = rawExtent[1] - interval * alignToSplitNumber;
  95. while (min > rawExtent[0] && isFinite(min) && isFinite(rawExtent[0])) {
  96. interval = increaseInterval(interval);
  97. min = rawExtent[1] - interval * alignToSplitNumber;
  98. }
  99. } else {
  100. var nicedSplitNumber = scale.getTicks().length - 1;
  101. if (nicedSplitNumber > alignToSplitNumber) {
  102. interval = increaseInterval(interval);
  103. }
  104. var range = interval * alignToSplitNumber;
  105. max = Math.ceil(rawExtent[1] / interval) * interval;
  106. min = round(max - range);
  107. // Not change the result that crossing zero.
  108. if (min < 0 && rawExtent[0] >= 0) {
  109. min = 0;
  110. max = round(range);
  111. } else if (max > 0 && rawExtent[1] <= 0) {
  112. max = 0;
  113. min = -round(range);
  114. }
  115. }
  116. // Adjust min, max based on the extent of alignTo. When min or max is set in alignTo scale
  117. var t0 = (alignToTicks[0].value - alignToNicedTicks[0].value) / alignToInterval;
  118. var t1 = (alignToTicks[alignToSplitNumber].value - alignToNicedTicks[alignToSplitNumber].value) / alignToInterval;
  119. // NOTE: Must in setExtent -> setInterval -> setNiceExtent order.
  120. intervalScaleProto.setExtent.call(scale, min + interval * t0, max + interval * t1);
  121. intervalScaleProto.setInterval.call(scale, interval);
  122. if (t0 || t1) {
  123. intervalScaleProto.setNiceExtent.call(scale, min + interval, max - interval);
  124. }
  125. if (process.env.NODE_ENV !== 'production') {
  126. var ticks = intervalScaleProto.getTicks.call(scale);
  127. if (ticks[1] && (!isValueNice(interval) || getPrecisionSafe(ticks[1].value) > getPrecisionSafe(interval))) {
  128. warn(
  129. // eslint-disable-next-line
  130. "The ticks may be not readable when set min: " + axisModel.get('min') + ", max: " + axisModel.get('max') + " and alignTicks: true");
  131. }
  132. }
  133. }