Log.js 6.5 KB

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