helper.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 * as numberUtil from '../util/number';
  41. var roundNumber = numberUtil.round;
  42. /**
  43. * @param extent Both extent[0] and extent[1] should be valid number.
  44. * Should be extent[0] < extent[1].
  45. * @param splitNumber splitNumber should be >= 1.
  46. */
  47. export function intervalScaleNiceTicks(extent, splitNumber, minInterval, maxInterval) {
  48. var result = {};
  49. var span = extent[1] - extent[0];
  50. var interval = result.interval = numberUtil.nice(span / splitNumber, true);
  51. if (minInterval != null && interval < minInterval) {
  52. interval = result.interval = minInterval;
  53. }
  54. if (maxInterval != null && interval > maxInterval) {
  55. interval = result.interval = maxInterval;
  56. } // Tow more digital for tick.
  57. var precision = result.intervalPrecision = getIntervalPrecision(interval); // Niced extent inside original extent
  58. var niceTickExtent = result.niceTickExtent = [roundNumber(Math.ceil(extent[0] / interval) * interval, precision), roundNumber(Math.floor(extent[1] / interval) * interval, precision)];
  59. fixExtent(niceTickExtent, extent);
  60. return result;
  61. }
  62. /**
  63. * @return interval precision
  64. */
  65. export function getIntervalPrecision(interval) {
  66. // Tow more digital for tick.
  67. return numberUtil.getPrecision(interval) + 2;
  68. }
  69. function clamp(niceTickExtent, idx, extent) {
  70. niceTickExtent[idx] = Math.max(Math.min(niceTickExtent[idx], extent[1]), extent[0]);
  71. } // In some cases (e.g., splitNumber is 1), niceTickExtent may be out of extent.
  72. export function fixExtent(niceTickExtent, extent) {
  73. !isFinite(niceTickExtent[0]) && (niceTickExtent[0] = extent[0]);
  74. !isFinite(niceTickExtent[1]) && (niceTickExtent[1] = extent[1]);
  75. clamp(niceTickExtent, 0, extent);
  76. clamp(niceTickExtent, 1, extent);
  77. if (niceTickExtent[0] > niceTickExtent[1]) {
  78. niceTickExtent[0] = niceTickExtent[1];
  79. }
  80. }
  81. export function contain(val, extent) {
  82. return val >= extent[0] && val <= extent[1];
  83. }
  84. export function normalize(val, extent) {
  85. if (extent[1] === extent[0]) {
  86. return 0.5;
  87. }
  88. return (val - extent[0]) / (extent[1] - extent[0]);
  89. }
  90. export function scale(val, extent) {
  91. return val * (extent[1] - extent[0]) + extent[0];
  92. }