linear.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. /**
  3. * @fileOverview The measurement of linear data scale function
  4. * @author dxq613@gmail.com
  5. */
  6. var isNil = require('@antv/util/lib/type/is-nil');
  7. var each = require('@antv/util/lib/each');
  8. var Base = require('./base');
  9. var numberAuto = require('./auto/number');
  10. /**
  11. * 线性度量
  12. * @class Scale.Linear
  13. */
  14. var Linear =
  15. /*#__PURE__*/
  16. function (_Base) {
  17. _inheritsLoose(Linear, _Base);
  18. function Linear() {
  19. return _Base.apply(this, arguments) || this;
  20. }
  21. var _proto = Linear.prototype;
  22. _proto._initDefaultCfg = function _initDefaultCfg() {
  23. _Base.prototype._initDefaultCfg.call(this);
  24. var self = this;
  25. self.type = 'linear';
  26. self.isLinear = true;
  27. /**
  28. * 是否为了用户习惯,优化min,max和ticks,如果进行优化,则会根据生成的ticks调整min,max,否则舍弃(min,max)范围之外的ticks
  29. * @type {Boolean}
  30. * @default false
  31. */
  32. self.nice = false;
  33. /**
  34. * min value of the scale
  35. * @type {Number}
  36. * @default null
  37. */
  38. self.min = null;
  39. /**
  40. * min value limitted of the scale
  41. * @type {Number}
  42. * @default null
  43. */
  44. self.minLimit = null;
  45. /**
  46. * max value of the scale
  47. * @type {Number}
  48. * @default null
  49. */
  50. self.max = null;
  51. /**
  52. * max value limitted of the scale
  53. * @type {Number}
  54. * @default null
  55. */
  56. self.maxLimit = null;
  57. /**
  58. * 自动生成标记时的个数
  59. * @type {Number}
  60. * @default null
  61. */
  62. self.tickCount = null;
  63. /**
  64. * 坐标轴点之间的间距,指的是真实数据的差值
  65. * @type {Number}
  66. * @default null
  67. */
  68. self.tickInterval = null;
  69. /**
  70. * 坐标轴点之间的最小间距,指的是真实数据的差值
  71. * @type {Number}
  72. * @default null
  73. */
  74. self.minTickInterval = null;
  75. /**
  76. * 用于计算坐标点时逼近的数组
  77. * @type {Array}
  78. */
  79. self.snapArray = null;
  80. }
  81. /**
  82. * @protected
  83. * @override
  84. */
  85. ;
  86. _proto.init = function init() {
  87. var self = this;
  88. if (!self.ticks) {
  89. self.min = self.translate(self.min);
  90. self.max = self.translate(self.max);
  91. self.initTicks();
  92. } else {
  93. var ticks = self.ticks;
  94. var firstValue = self.translate(ticks[0]);
  95. var lastValue = self.translate(ticks[ticks.length - 1]);
  96. if (isNil(self.min) || self.min > firstValue) {
  97. self.min = firstValue;
  98. }
  99. if (isNil(self.max) || self.max < lastValue) {
  100. self.max = lastValue;
  101. }
  102. }
  103. }
  104. /**
  105. * 计算坐标点
  106. * @protected
  107. * @return {Array} 计算完成的坐标点
  108. */
  109. ;
  110. _proto.calculateTicks = function calculateTicks() {
  111. var min = this.min,
  112. max = this.max,
  113. minLimit = this.minLimit,
  114. maxLimit = this.maxLimit,
  115. tickCount = this.tickCount,
  116. tickInterval = this.tickInterval,
  117. minTickInterval = this.minTickInterval,
  118. snapArray = this.snapArray;
  119. if (tickCount === 1) {
  120. throw new Error('linear scale\'tickCount should not be 1');
  121. }
  122. if (max < min) {
  123. throw new Error("max: " + max + " should not be less than min: " + min);
  124. }
  125. var tmp = numberAuto({
  126. min: min,
  127. max: max,
  128. minLimit: minLimit,
  129. maxLimit: maxLimit,
  130. minCount: tickCount,
  131. maxCount: tickCount,
  132. interval: tickInterval,
  133. minTickInterval: minTickInterval,
  134. snapArray: snapArray
  135. });
  136. return tmp.ticks;
  137. } // 初始化ticks
  138. ;
  139. _proto.initTicks = function initTicks() {
  140. var self = this;
  141. var calTicks = self.calculateTicks();
  142. if (self.nice) {
  143. // 如果需要优化显示的tick
  144. self.ticks = calTicks;
  145. self.min = calTicks[0];
  146. self.max = calTicks[calTicks.length - 1];
  147. } else {
  148. var ticks = [];
  149. each(calTicks, function (tick) {
  150. if (tick >= self.min && tick <= self.max) {
  151. ticks.push(tick);
  152. }
  153. }); // 如果 ticks 为空,直接输入最小值、最大值
  154. if (!ticks.length) {
  155. ticks.push(self.min);
  156. ticks.push(self.max);
  157. }
  158. self.ticks = ticks;
  159. }
  160. }
  161. /**
  162. * @override
  163. */
  164. ;
  165. _proto.scale = function scale(value) {
  166. if (isNil(value)) {
  167. return NaN;
  168. }
  169. var max = this.max;
  170. var min = this.min;
  171. if (max === min) {
  172. return 0;
  173. }
  174. var percent = (value - min) / (max - min);
  175. var rangeMin = this.rangeMin();
  176. var rangeMax = this.rangeMax();
  177. return rangeMin + percent * (rangeMax - rangeMin);
  178. }
  179. /**
  180. * @override
  181. */
  182. ;
  183. _proto.invert = function invert(value) {
  184. var percent = (value - this.rangeMin()) / (this.rangeMax() - this.rangeMin());
  185. return this.min + percent * (this.max - this.min);
  186. };
  187. return Linear;
  188. }(Base);
  189. Base.Linear = Linear;
  190. module.exports = Linear;