Log.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 { __extends } from "tslib";
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. import Scale from './Scale.js';
  43. import * as numberUtil from '../util/number.js';
  44. import * as scaleHelper from './helper.js';
  45. // Use some method of IntervalScale
  46. import IntervalScale from './Interval.js';
  47. var scaleProto = Scale.prototype;
  48. // FIXME:TS refactor: not good to call it directly with `this`?
  49. var intervalScaleProto = IntervalScale.prototype;
  50. var roundingErrorFix = numberUtil.round;
  51. var mathFloor = Math.floor;
  52. var mathCeil = Math.ceil;
  53. var mathPow = Math.pow;
  54. var mathLog = Math.log;
  55. var LogScale = /** @class */function (_super) {
  56. __extends(LogScale, _super);
  57. function LogScale() {
  58. var _this = _super !== null && _super.apply(this, arguments) || this;
  59. _this.type = 'log';
  60. _this.base = 10;
  61. _this._originalScale = new IntervalScale();
  62. // FIXME:TS actually used by `IntervalScale`
  63. _this._interval = 0;
  64. return _this;
  65. }
  66. /**
  67. * @param Whether expand the ticks to niced extent.
  68. */
  69. LogScale.prototype.getTicks = function (expandToNicedExtent) {
  70. var originalScale = this._originalScale;
  71. var extent = this._extent;
  72. var originalExtent = originalScale.getExtent();
  73. var ticks = intervalScaleProto.getTicks.call(this, expandToNicedExtent);
  74. return zrUtil.map(ticks, function (tick) {
  75. var val = tick.value;
  76. var powVal = numberUtil.round(mathPow(this.base, val));
  77. // Fix #4158
  78. powVal = val === extent[0] && this._fixMin ? fixRoundingError(powVal, originalExtent[0]) : powVal;
  79. powVal = val === extent[1] && this._fixMax ? fixRoundingError(powVal, originalExtent[1]) : powVal;
  80. return {
  81. value: powVal
  82. };
  83. }, this);
  84. };
  85. LogScale.prototype.setExtent = function (start, end) {
  86. var base = mathLog(this.base);
  87. // log(-Infinity) is NaN, so safe guard here
  88. start = mathLog(Math.max(0, start)) / base;
  89. end = mathLog(Math.max(0, end)) / base;
  90. intervalScaleProto.setExtent.call(this, start, end);
  91. };
  92. /**
  93. * @return {number} end
  94. */
  95. LogScale.prototype.getExtent = function () {
  96. var base = this.base;
  97. var extent = scaleProto.getExtent.call(this);
  98. extent[0] = mathPow(base, extent[0]);
  99. extent[1] = mathPow(base, extent[1]);
  100. // Fix #4158
  101. var originalScale = this._originalScale;
  102. var originalExtent = originalScale.getExtent();
  103. this._fixMin && (extent[0] = fixRoundingError(extent[0], originalExtent[0]));
  104. this._fixMax && (extent[1] = fixRoundingError(extent[1], originalExtent[1]));
  105. return extent;
  106. };
  107. LogScale.prototype.unionExtent = function (extent) {
  108. this._originalScale.unionExtent(extent);
  109. var base = this.base;
  110. extent[0] = mathLog(extent[0]) / mathLog(base);
  111. extent[1] = mathLog(extent[1]) / mathLog(base);
  112. scaleProto.unionExtent.call(this, extent);
  113. };
  114. LogScale.prototype.unionExtentFromData = function (data, dim) {
  115. // TODO
  116. // filter value that <= 0
  117. this.unionExtent(data.getApproximateExtent(dim));
  118. };
  119. /**
  120. * Update interval and extent of intervals for nice ticks
  121. * @param approxTickNum default 10 Given approx tick number
  122. */
  123. LogScale.prototype.calcNiceTicks = function (approxTickNum) {
  124. approxTickNum = approxTickNum || 10;
  125. var extent = this._extent;
  126. var span = extent[1] - extent[0];
  127. if (span === Infinity || span <= 0) {
  128. return;
  129. }
  130. var interval = numberUtil.quantity(span);
  131. var err = approxTickNum / span * interval;
  132. // Filter ticks to get closer to the desired count.
  133. if (err <= 0.5) {
  134. interval *= 10;
  135. }
  136. // Interval should be integer
  137. while (!isNaN(interval) && Math.abs(interval) < 1 && Math.abs(interval) > 0) {
  138. interval *= 10;
  139. }
  140. var niceExtent = [numberUtil.round(mathCeil(extent[0] / interval) * interval), numberUtil.round(mathFloor(extent[1] / interval) * interval)];
  141. this._interval = interval;
  142. this._niceExtent = niceExtent;
  143. };
  144. LogScale.prototype.calcNiceExtent = function (opt) {
  145. intervalScaleProto.calcNiceExtent.call(this, opt);
  146. this._fixMin = opt.fixMin;
  147. this._fixMax = opt.fixMax;
  148. };
  149. LogScale.prototype.parse = function (val) {
  150. return val;
  151. };
  152. LogScale.prototype.contain = function (val) {
  153. val = mathLog(val) / mathLog(this.base);
  154. return scaleHelper.contain(val, this._extent);
  155. };
  156. LogScale.prototype.normalize = function (val) {
  157. val = mathLog(val) / mathLog(this.base);
  158. return scaleHelper.normalize(val, this._extent);
  159. };
  160. LogScale.prototype.scale = function (val) {
  161. val = scaleHelper.scale(val, this._extent);
  162. return mathPow(this.base, val);
  163. };
  164. LogScale.type = 'log';
  165. return LogScale;
  166. }(Scale);
  167. var proto = LogScale.prototype;
  168. proto.getMinorTicks = intervalScaleProto.getMinorTicks;
  169. proto.getLabel = intervalScaleProto.getLabel;
  170. function fixRoundingError(val, originalVal) {
  171. return roundingErrorFix(val, numberUtil.getPrecision(originalVal));
  172. }
  173. Scale.registerClass(LogScale);
  174. export default LogScale;