helper.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. export function isValueNice(val) {
  42. var exp10 = Math.pow(10, quantityExponent(Math.abs(val)));
  43. var f = Math.abs(val / exp10);
  44. return f === 0 || f === 1 || f === 2 || f === 3 || f === 5;
  45. }
  46. export function isIntervalOrLogScale(scale) {
  47. return scale.type === 'interval' || scale.type === 'log';
  48. }
  49. /**
  50. * @param extent Both extent[0] and extent[1] should be valid number.
  51. * Should be extent[0] < extent[1].
  52. * @param splitNumber splitNumber should be >= 1.
  53. */
  54. export function intervalScaleNiceTicks(extent, splitNumber, minInterval, maxInterval) {
  55. var result = {};
  56. var span = extent[1] - extent[0];
  57. var interval = result.interval = nice(span / 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. } // Tow more digital for tick.
  64. var precision = result.intervalPrecision = getIntervalPrecision(interval); // Niced extent inside original extent
  65. var niceTickExtent = result.niceTickExtent = [round(Math.ceil(extent[0] / interval) * interval, precision), round(Math.floor(extent[1] / interval) * interval, precision)];
  66. fixExtent(niceTickExtent, extent);
  67. return result;
  68. }
  69. export function increaseInterval(interval) {
  70. var exp10 = Math.pow(10, quantityExponent(interval)); // Increase interval
  71. var f = interval / exp10;
  72. if (!f) {
  73. f = 1;
  74. } else if (f === 2) {
  75. f = 3;
  76. } else if (f === 3) {
  77. f = 5;
  78. } else {
  79. // f is 1 or 5
  80. f *= 2;
  81. }
  82. return round(f * exp10);
  83. }
  84. /**
  85. * @return interval precision
  86. */
  87. export function getIntervalPrecision(interval) {
  88. // Tow more digital for tick.
  89. return getPrecision(interval) + 2;
  90. }
  91. function clamp(niceTickExtent, idx, extent) {
  92. niceTickExtent[idx] = Math.max(Math.min(niceTickExtent[idx], extent[1]), extent[0]);
  93. } // In some cases (e.g., splitNumber is 1), niceTickExtent may be out of extent.
  94. export function fixExtent(niceTickExtent, extent) {
  95. !isFinite(niceTickExtent[0]) && (niceTickExtent[0] = extent[0]);
  96. !isFinite(niceTickExtent[1]) && (niceTickExtent[1] = extent[1]);
  97. clamp(niceTickExtent, 0, extent);
  98. clamp(niceTickExtent, 1, extent);
  99. if (niceTickExtent[0] > niceTickExtent[1]) {
  100. niceTickExtent[0] = niceTickExtent[1];
  101. }
  102. }
  103. export function contain(val, extent) {
  104. return val >= extent[0] && val <= extent[1];
  105. }
  106. export function normalize(val, extent) {
  107. if (extent[1] === extent[0]) {
  108. return 0.5;
  109. }
  110. return (val - extent[0]) / (extent[1] - extent[0]);
  111. }
  112. export function scale(val, extent) {
  113. return val * (extent[1] - extent[0]) + extent[0];
  114. }