helper.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }
  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. export function normalize(val, extent) {
  111. if (extent[1] === extent[0]) {
  112. return 0.5;
  113. }
  114. return (val - extent[0]) / (extent[1] - extent[0]);
  115. }
  116. export function scale(val, extent) {
  117. return val * (extent[1] - extent[0]) + extent[0];
  118. }