Log.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // Use some method of IntervalScale
  45. import IntervalScale from './Interval.js';
  46. import { getIntervalPrecision, logTransform } from './helper.js';
  47. import { getScaleBreakHelper } from './break.js';
  48. var fixRound = 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 = /** @class */function (_super) {
  54. __extends(LogScale, _super);
  55. function LogScale() {
  56. var _this = _super !== null && _super.apply(this, arguments) || this;
  57. _this.type = 'log';
  58. _this.base = 10;
  59. _this._originalScale = new IntervalScale();
  60. return _this;
  61. }
  62. /**
  63. * @param Whether expand the ticks to niced extent.
  64. */
  65. LogScale.prototype.getTicks = function (opt) {
  66. opt = opt || {};
  67. var extent = this._extent.slice();
  68. var originalExtent = this._originalScale.getExtent();
  69. var ticks = _super.prototype.getTicks.call(this, opt);
  70. var base = this.base;
  71. var originalBreaks = this._originalScale._innerGetBreaks();
  72. var scaleBreakHelper = getScaleBreakHelper();
  73. return zrUtil.map(ticks, function (tick) {
  74. var val = tick.value;
  75. var roundingCriterion = null;
  76. var powVal = mathPow(base, val);
  77. // Fix #4158
  78. if (val === extent[0] && this._fixMin) {
  79. roundingCriterion = originalExtent[0];
  80. } else if (val === extent[1] && this._fixMax) {
  81. roundingCriterion = originalExtent[1];
  82. }
  83. var vBreak;
  84. if (scaleBreakHelper) {
  85. var transformed = scaleBreakHelper.getTicksLogTransformBreak(tick, base, originalBreaks, fixRoundingError);
  86. vBreak = transformed.vBreak;
  87. if (roundingCriterion == null) {
  88. roundingCriterion = transformed.brkRoundingCriterion;
  89. }
  90. }
  91. if (roundingCriterion != null) {
  92. powVal = fixRoundingError(powVal, roundingCriterion);
  93. }
  94. return {
  95. value: powVal,
  96. "break": vBreak
  97. };
  98. }, this);
  99. };
  100. LogScale.prototype._getNonTransBreaks = function () {
  101. return this._originalScale._innerGetBreaks();
  102. };
  103. LogScale.prototype.setExtent = function (start, end) {
  104. this._originalScale.setExtent(start, end);
  105. var loggedExtent = logTransform(this.base, [start, end]);
  106. _super.prototype.setExtent.call(this, loggedExtent[0], loggedExtent[1]);
  107. };
  108. /**
  109. * @return {number} end
  110. */
  111. LogScale.prototype.getExtent = function () {
  112. var base = this.base;
  113. var extent = _super.prototype.getExtent.call(this);
  114. extent[0] = mathPow(base, extent[0]);
  115. extent[1] = mathPow(base, extent[1]);
  116. // Fix #4158
  117. var originalExtent = this._originalScale.getExtent();
  118. this._fixMin && (extent[0] = fixRoundingError(extent[0], originalExtent[0]));
  119. this._fixMax && (extent[1] = fixRoundingError(extent[1], originalExtent[1]));
  120. return extent;
  121. };
  122. LogScale.prototype.unionExtentFromData = function (data, dim) {
  123. this._originalScale.unionExtentFromData(data, dim);
  124. var loggedOther = logTransform(this.base, data.getApproximateExtent(dim), true);
  125. this._innerUnionExtent(loggedOther);
  126. };
  127. /**
  128. * Update interval and extent of intervals for nice ticks
  129. * @param approxTickNum default 10 Given approx tick number
  130. */
  131. LogScale.prototype.calcNiceTicks = function (approxTickNum) {
  132. approxTickNum = approxTickNum || 10;
  133. var extent = this._extent.slice();
  134. var span = this._getExtentSpanWithBreaks();
  135. if (!isFinite(span) || span <= 0) {
  136. return;
  137. }
  138. var interval = numberUtil.quantity(span);
  139. var err = approxTickNum / span * interval;
  140. // Filter ticks to get closer to the desired count.
  141. if (err <= 0.5) {
  142. interval *= 10;
  143. }
  144. // Interval should be integer
  145. while (!isNaN(interval) && Math.abs(interval) < 1 && Math.abs(interval) > 0) {
  146. interval *= 10;
  147. }
  148. var niceExtent = [fixRound(mathCeil(extent[0] / interval) * interval), fixRound(mathFloor(extent[1] / interval) * interval)];
  149. this._interval = interval;
  150. this._intervalPrecision = getIntervalPrecision(interval);
  151. this._niceExtent = niceExtent;
  152. };
  153. LogScale.prototype.calcNiceExtent = function (opt) {
  154. _super.prototype.calcNiceExtent.call(this, opt);
  155. this._fixMin = opt.fixMin;
  156. this._fixMax = opt.fixMax;
  157. };
  158. LogScale.prototype.contain = function (val) {
  159. val = mathLog(val) / mathLog(this.base);
  160. return _super.prototype.contain.call(this, val);
  161. };
  162. LogScale.prototype.normalize = function (val) {
  163. val = mathLog(val) / mathLog(this.base);
  164. return _super.prototype.normalize.call(this, val);
  165. };
  166. LogScale.prototype.scale = function (val) {
  167. val = _super.prototype.scale.call(this, val);
  168. return mathPow(this.base, val);
  169. };
  170. LogScale.prototype.setBreaksFromOption = function (breakOptionList) {
  171. var scaleBreakHelper = getScaleBreakHelper();
  172. if (!scaleBreakHelper) {
  173. return;
  174. }
  175. var _a = scaleBreakHelper.logarithmicParseBreaksFromOption(breakOptionList, this.base, zrUtil.bind(this.parse, this)),
  176. parsedOriginal = _a.parsedOriginal,
  177. parsedLogged = _a.parsedLogged;
  178. this._originalScale._innerSetBreak(parsedOriginal);
  179. this._innerSetBreak(parsedLogged);
  180. };
  181. LogScale.type = 'log';
  182. return LogScale;
  183. }(IntervalScale);
  184. function fixRoundingError(val, originalVal) {
  185. return fixRound(val, numberUtil.getPrecision(originalVal));
  186. }
  187. Scale.registerClass(LogScale);
  188. export default LogScale;