helper.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 { getPrecision, round, nice, quantityExponent } from '../util/number.js';
  41. import { bind } from 'zrender/lib/core/util.js';
  42. export function isValueNice(val) {
  43. var exp10 = Math.pow(10, quantityExponent(Math.abs(val)));
  44. var f = Math.abs(val / exp10);
  45. return f === 0 || f === 1 || f === 2 || f === 3 || f === 5;
  46. }
  47. export function isIntervalOrLogScale(scale) {
  48. return scale.type === 'interval' || scale.type === 'log';
  49. }
  50. /**
  51. * @param extent Both extent[0] and extent[1] should be valid number.
  52. * Should be extent[0] < extent[1].
  53. * @param splitNumber splitNumber should be >= 1.
  54. */
  55. export function intervalScaleNiceTicks(extent, spanWithBreaks, splitNumber, minInterval, maxInterval) {
  56. var result = {};
  57. var interval = result.interval = nice(spanWithBreaks / splitNumber, true);
  58. if (minInterval != null && interval < minInterval) {
  59. interval = result.interval = minInterval;
  60. }
  61. if (maxInterval != null && interval > maxInterval) {
  62. interval = result.interval = maxInterval;
  63. }
  64. // Tow more digital for tick.
  65. var precision = result.intervalPrecision = getIntervalPrecision(interval);
  66. // Niced extent inside original extent
  67. var niceTickExtent = result.niceTickExtent = [round(Math.ceil(extent[0] / interval) * interval, precision), round(Math.floor(extent[1] / interval) * interval, precision)];
  68. fixExtent(niceTickExtent, extent);
  69. return result;
  70. }
  71. export function increaseInterval(interval) {
  72. var exp10 = Math.pow(10, quantityExponent(interval));
  73. // Increase interval
  74. var f = interval / exp10;
  75. if (!f) {
  76. f = 1;
  77. } else if (f === 2) {
  78. f = 3;
  79. } else if (f === 3) {
  80. f = 5;
  81. } else {
  82. // f is 1 or 5
  83. f *= 2;
  84. }
  85. return round(f * exp10);
  86. }
  87. /**
  88. * @return interval precision
  89. */
  90. export function getIntervalPrecision(interval) {
  91. // Tow more digital for tick.
  92. return getPrecision(interval) + 2;
  93. }
  94. function clamp(niceTickExtent, idx, extent) {
  95. niceTickExtent[idx] = Math.max(Math.min(niceTickExtent[idx], extent[1]), extent[0]);
  96. }
  97. // In some cases (e.g., splitNumber is 1), niceTickExtent may be out of extent.
  98. export function fixExtent(niceTickExtent, extent) {
  99. !isFinite(niceTickExtent[0]) && (niceTickExtent[0] = extent[0]);
  100. !isFinite(niceTickExtent[1]) && (niceTickExtent[1] = extent[1]);
  101. clamp(niceTickExtent, 0, extent);
  102. clamp(niceTickExtent, 1, extent);
  103. if (niceTickExtent[0] > niceTickExtent[1]) {
  104. niceTickExtent[0] = niceTickExtent[1];
  105. }
  106. }
  107. export function contain(val, extent) {
  108. return val >= extent[0] && val <= extent[1];
  109. }
  110. var ScaleCalculator = /** @class */function () {
  111. function ScaleCalculator() {
  112. this.normalize = normalize;
  113. this.scale = scale;
  114. }
  115. ScaleCalculator.prototype.updateMethods = function (brkCtx) {
  116. if (brkCtx.hasBreaks()) {
  117. this.normalize = bind(brkCtx.normalize, brkCtx);
  118. this.scale = bind(brkCtx.scale, brkCtx);
  119. } else {
  120. this.normalize = normalize;
  121. this.scale = scale;
  122. }
  123. };
  124. return ScaleCalculator;
  125. }();
  126. export { ScaleCalculator };
  127. function normalize(val, extent) {
  128. if (extent[1] === extent[0]) {
  129. return 0.5;
  130. }
  131. return (val - extent[0]) / (extent[1] - extent[0]);
  132. }
  133. function scale(val, extent) {
  134. return val * (extent[1] - extent[0]) + extent[0];
  135. }
  136. export function logTransform(base, extent, noClampNegative) {
  137. var loggedBase = Math.log(base);
  138. return [
  139. // log(negative) is NaN, so safe guard here.
  140. // PENDING: But even getting a -Infinity still does not make sense in extent.
  141. // Just keep it as is, getting a NaN to make some previous cases works by coincidence.
  142. Math.log(noClampNegative ? extent[0] : Math.max(0, extent[0])) / loggedBase, Math.log(noClampNegative ? extent[1] : Math.max(0, extent[1])) / loggedBase];
  143. }