axisAlignTicks.js 5.7 KB

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