helper.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. var numberUtil = require("../util/number");
  20. /*
  21. * Licensed to the Apache Software Foundation (ASF) under one
  22. * or more contributor license agreements. See the NOTICE file
  23. * distributed with this work for additional information
  24. * regarding copyright ownership. The ASF licenses this file
  25. * to you under the Apache License, Version 2.0 (the
  26. * "License"); you may not use this file except in compliance
  27. * with the License. You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing,
  32. * software distributed under the License is distributed on an
  33. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  34. * KIND, either express or implied. See the License for the
  35. * specific language governing permissions and limitations
  36. * under the License.
  37. */
  38. /**
  39. * For testable.
  40. */
  41. var roundNumber = numberUtil.round;
  42. /**
  43. * @param {Array.<number>} extent Both extent[0] and extent[1] should be valid number.
  44. * Should be extent[0] < extent[1].
  45. * @param {number} splitNumber splitNumber should be >= 1.
  46. * @param {number} [minInterval]
  47. * @param {number} [maxInterval]
  48. * @return {Object} {interval, intervalPrecision, niceTickExtent}
  49. */
  50. function intervalScaleNiceTicks(extent, splitNumber, minInterval, maxInterval) {
  51. var result = {};
  52. var span = extent[1] - extent[0];
  53. var interval = result.interval = numberUtil.nice(span / splitNumber, true);
  54. if (minInterval != null && interval < minInterval) {
  55. interval = result.interval = minInterval;
  56. }
  57. if (maxInterval != null && interval > maxInterval) {
  58. interval = result.interval = maxInterval;
  59. } // Tow more digital for tick.
  60. var precision = result.intervalPrecision = getIntervalPrecision(interval); // Niced extent inside original extent
  61. var niceTickExtent = result.niceTickExtent = [roundNumber(Math.ceil(extent[0] / interval) * interval, precision), roundNumber(Math.floor(extent[1] / interval) * interval, precision)];
  62. fixExtent(niceTickExtent, extent);
  63. return result;
  64. }
  65. /**
  66. * @param {number} interval
  67. * @return {number} interval precision
  68. */
  69. function getIntervalPrecision(interval) {
  70. // Tow more digital for tick.
  71. return numberUtil.getPrecisionSafe(interval) + 2;
  72. }
  73. function clamp(niceTickExtent, idx, extent) {
  74. niceTickExtent[idx] = Math.max(Math.min(niceTickExtent[idx], extent[1]), extent[0]);
  75. } // In some cases (e.g., splitNumber is 1), niceTickExtent may be out of extent.
  76. function fixExtent(niceTickExtent, extent) {
  77. !isFinite(niceTickExtent[0]) && (niceTickExtent[0] = extent[0]);
  78. !isFinite(niceTickExtent[1]) && (niceTickExtent[1] = extent[1]);
  79. clamp(niceTickExtent, 0, extent);
  80. clamp(niceTickExtent, 1, extent);
  81. if (niceTickExtent[0] > niceTickExtent[1]) {
  82. niceTickExtent[0] = niceTickExtent[1];
  83. }
  84. }
  85. exports.intervalScaleNiceTicks = intervalScaleNiceTicks;
  86. exports.getIntervalPrecision = getIntervalPrecision;
  87. exports.fixExtent = fixExtent;