Log.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 zrUtil = require("zrender/lib/core/util");
  20. var Scale = require("./Scale");
  21. var numberUtil = require("../util/number");
  22. var IntervalScale = require("./Interval");
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. /**
  42. * Log scale
  43. * @module echarts/scale/Log
  44. */
  45. // Use some method of IntervalScale
  46. var scaleProto = Scale.prototype;
  47. var intervalScaleProto = IntervalScale.prototype;
  48. var getPrecisionSafe = numberUtil.getPrecisionSafe;
  49. var roundingErrorFix = numberUtil.round;
  50. var mathFloor = Math.floor;
  51. var mathCeil = Math.ceil;
  52. var mathPow = Math.pow;
  53. var mathLog = Math.log;
  54. var LogScale = Scale.extend({
  55. type: 'log',
  56. base: 10,
  57. $constructor: function () {
  58. Scale.apply(this, arguments);
  59. this._originalScale = new IntervalScale();
  60. },
  61. /**
  62. * @param {boolean} [expandToNicedExtent=false] If expand the ticks to niced extent.
  63. * @return {Array.<number>}
  64. */
  65. getTicks: function (expandToNicedExtent) {
  66. var originalScale = this._originalScale;
  67. var extent = this._extent;
  68. var originalExtent = originalScale.getExtent();
  69. return zrUtil.map(intervalScaleProto.getTicks.call(this, expandToNicedExtent), function (val) {
  70. var powVal = numberUtil.round(mathPow(this.base, val)); // Fix #4158
  71. powVal = val === extent[0] && originalScale.__fixMin ? fixRoundingError(powVal, originalExtent[0]) : powVal;
  72. powVal = val === extent[1] && originalScale.__fixMax ? fixRoundingError(powVal, originalExtent[1]) : powVal;
  73. return powVal;
  74. }, this);
  75. },
  76. /**
  77. * @param {number} splitNumber
  78. * @return {Array.<Array.<number>>}
  79. */
  80. getMinorTicks: intervalScaleProto.getMinorTicks,
  81. /**
  82. * @param {number} val
  83. * @return {string}
  84. */
  85. getLabel: intervalScaleProto.getLabel,
  86. /**
  87. * @param {number} val
  88. * @return {number}
  89. */
  90. scale: function (val) {
  91. val = scaleProto.scale.call(this, val);
  92. return mathPow(this.base, val);
  93. },
  94. /**
  95. * @param {number} start
  96. * @param {number} end
  97. */
  98. setExtent: function (start, end) {
  99. var base = this.base;
  100. start = mathLog(start) / mathLog(base);
  101. end = mathLog(end) / mathLog(base);
  102. intervalScaleProto.setExtent.call(this, start, end);
  103. },
  104. /**
  105. * @return {number} end
  106. */
  107. getExtent: function () {
  108. var base = this.base;
  109. var extent = scaleProto.getExtent.call(this);
  110. extent[0] = mathPow(base, extent[0]);
  111. extent[1] = mathPow(base, extent[1]); // Fix #4158
  112. var originalScale = this._originalScale;
  113. var originalExtent = originalScale.getExtent();
  114. originalScale.__fixMin && (extent[0] = fixRoundingError(extent[0], originalExtent[0]));
  115. originalScale.__fixMax && (extent[1] = fixRoundingError(extent[1], originalExtent[1]));
  116. return extent;
  117. },
  118. /**
  119. * @param {Array.<number>} extent
  120. */
  121. unionExtent: function (extent) {
  122. this._originalScale.unionExtent(extent);
  123. var base = this.base;
  124. extent[0] = mathLog(extent[0]) / mathLog(base);
  125. extent[1] = mathLog(extent[1]) / mathLog(base);
  126. scaleProto.unionExtent.call(this, extent);
  127. },
  128. /**
  129. * @override
  130. */
  131. unionExtentFromData: function (data, dim) {
  132. // TODO
  133. // filter value that <= 0
  134. this.unionExtent(data.getApproximateExtent(dim));
  135. },
  136. /**
  137. * Update interval and extent of intervals for nice ticks
  138. * @param {number} [approxTickNum = 10] Given approx tick number
  139. */
  140. niceTicks: function (approxTickNum) {
  141. approxTickNum = approxTickNum || 10;
  142. var extent = this._extent;
  143. var span = extent[1] - extent[0];
  144. if (span === Infinity || span <= 0) {
  145. return;
  146. }
  147. var interval = numberUtil.quantity(span);
  148. var err = approxTickNum / span * interval; // Filter ticks to get closer to the desired count.
  149. if (err <= 0.5) {
  150. interval *= 10;
  151. } // Interval should be integer
  152. while (!isNaN(interval) && Math.abs(interval) < 1 && Math.abs(interval) > 0) {
  153. interval *= 10;
  154. }
  155. var niceExtent = [numberUtil.round(mathCeil(extent[0] / interval) * interval), numberUtil.round(mathFloor(extent[1] / interval) * interval)];
  156. this._interval = interval;
  157. this._niceExtent = niceExtent;
  158. },
  159. /**
  160. * Nice extent.
  161. * @override
  162. */
  163. niceExtent: function (opt) {
  164. intervalScaleProto.niceExtent.call(this, opt);
  165. var originalScale = this._originalScale;
  166. originalScale.__fixMin = opt.fixMin;
  167. originalScale.__fixMax = opt.fixMax;
  168. }
  169. });
  170. zrUtil.each(['contain', 'normalize'], function (methodName) {
  171. LogScale.prototype[methodName] = function (val) {
  172. val = mathLog(val) / mathLog(this.base);
  173. return scaleProto[methodName].call(this, val);
  174. };
  175. });
  176. LogScale.create = function () {
  177. return new LogScale();
  178. };
  179. function fixRoundingError(val, originalVal) {
  180. return roundingErrorFix(val, getPrecisionSafe(originalVal));
  181. }
  182. var _default = LogScale;
  183. module.exports = _default;