Scale.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 clazzUtil = require("../util/clazz");
  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. * // Scale class management
  40. * @module echarts/scale/Scale
  41. */
  42. /**
  43. * @param {Object} [setting]
  44. */
  45. function Scale(setting) {
  46. this._setting = setting || {};
  47. /**
  48. * Extent
  49. * @type {Array.<number>}
  50. * @protected
  51. */
  52. this._extent = [Infinity, -Infinity];
  53. /**
  54. * Step is calculated in adjustExtent
  55. * @type {Array.<number>}
  56. * @protected
  57. */
  58. this._interval = 0;
  59. this.init && this.init.apply(this, arguments);
  60. }
  61. /**
  62. * Parse input val to valid inner number.
  63. * @param {*} val
  64. * @return {number}
  65. */
  66. Scale.prototype.parse = function (val) {
  67. // Notice: This would be a trap here, If the implementation
  68. // of this method depends on extent, and this method is used
  69. // before extent set (like in dataZoom), it would be wrong.
  70. // Nevertheless, parse does not depend on extent generally.
  71. return val;
  72. };
  73. Scale.prototype.getSetting = function (name) {
  74. return this._setting[name];
  75. };
  76. Scale.prototype.contain = function (val) {
  77. var extent = this._extent;
  78. return val >= extent[0] && val <= extent[1];
  79. };
  80. /**
  81. * Normalize value to linear [0, 1], return 0.5 if extent span is 0
  82. * @param {number} val
  83. * @return {number}
  84. */
  85. Scale.prototype.normalize = function (val) {
  86. var extent = this._extent;
  87. if (extent[1] === extent[0]) {
  88. return 0.5;
  89. }
  90. return (val - extent[0]) / (extent[1] - extent[0]);
  91. };
  92. /**
  93. * Scale normalized value
  94. * @param {number} val
  95. * @return {number}
  96. */
  97. Scale.prototype.scale = function (val) {
  98. var extent = this._extent;
  99. return val * (extent[1] - extent[0]) + extent[0];
  100. };
  101. /**
  102. * Set extent from data
  103. * @param {Array.<number>} other
  104. */
  105. Scale.prototype.unionExtent = function (other) {
  106. var extent = this._extent;
  107. other[0] < extent[0] && (extent[0] = other[0]);
  108. other[1] > extent[1] && (extent[1] = other[1]); // not setExtent because in log axis it may transformed to power
  109. // this.setExtent(extent[0], extent[1]);
  110. };
  111. /**
  112. * Set extent from data
  113. * @param {module:echarts/data/List} data
  114. * @param {string} dim
  115. */
  116. Scale.prototype.unionExtentFromData = function (data, dim) {
  117. this.unionExtent(data.getApproximateExtent(dim));
  118. };
  119. /**
  120. * Get extent
  121. * @return {Array.<number>}
  122. */
  123. Scale.prototype.getExtent = function () {
  124. return this._extent.slice();
  125. };
  126. /**
  127. * Set extent
  128. * @param {number} start
  129. * @param {number} end
  130. */
  131. Scale.prototype.setExtent = function (start, end) {
  132. var thisExtent = this._extent;
  133. if (!isNaN(start)) {
  134. thisExtent[0] = start;
  135. }
  136. if (!isNaN(end)) {
  137. thisExtent[1] = end;
  138. }
  139. };
  140. /**
  141. * When axis extent depends on data and no data exists,
  142. * axis ticks should not be drawn, which is named 'blank'.
  143. */
  144. Scale.prototype.isBlank = function () {
  145. return this._isBlank;
  146. },
  147. /**
  148. * When axis extent depends on data and no data exists,
  149. * axis ticks should not be drawn, which is named 'blank'.
  150. */
  151. Scale.prototype.setBlank = function (isBlank) {
  152. this._isBlank = isBlank;
  153. };
  154. /**
  155. * @abstract
  156. * @param {*} tick
  157. * @return {string} label of the tick.
  158. */
  159. Scale.prototype.getLabel = null;
  160. clazzUtil.enableClassExtend(Scale);
  161. clazzUtil.enableClassManagement(Scale, {
  162. registerWhenExtend: true
  163. });
  164. var _default = Scale;
  165. module.exports = _default;