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. _proto.init = function init() {
  86. var self = this;
  87. if (!self.ticks) {
  88. self.min = self.translate(self.min);
  89. self.max = self.translate(self.max);
  90. self.initTicks();
  91. } else {
  92. var ticks = self.ticks;
  93. var firstValue = self.translate(ticks[0]);
  94. var lastValue = self.translate(ticks[ticks.length - 1]);
  95. if (isNil(self.min) || self.min > firstValue) {
  96. self.min = firstValue;
  97. }
  98. if (isNil(self.max) || self.max < lastValue) {
  99. self.max = lastValue;
  100. }
  101. }
  102. };
  103. /**
  104. * 计算坐标点
  105. * @protected
  106. * @return {Array} 计算完成的坐标点
  107. */
  108. _proto.calculateTicks = function calculateTicks() {
  109. var min = this.min,
  110. max = this.max,
  111. minLimit = this.minLimit,
  112. maxLimit = this.maxLimit,
  113. tickCount = this.tickCount,
  114. tickInterval = this.tickInterval,
  115. minTickInterval = this.minTickInterval,
  116. snapArray = this.snapArray;
  117. if (tickCount === 1) {
  118. throw new Error('linear scale\'tickCount should not be 1');
  119. }
  120. if (max < min) {
  121. throw new Error("max: " + max + " should not be less than min: " + min);
  122. }
  123. var tmp = numberAuto({
  124. min: min,
  125. max: max,
  126. minLimit: minLimit,
  127. maxLimit: maxLimit,
  128. minCount: tickCount,
  129. maxCount: tickCount,
  130. interval: tickInterval,
  131. minTickInterval: minTickInterval,
  132. snapArray: snapArray
  133. });
  134. return tmp.ticks;
  135. }; // 初始化ticks
  136. _proto.initTicks = function initTicks() {
  137. var self = this;
  138. var calTicks = self.calculateTicks();
  139. if (self.nice) {
  140. // 如果需要优化显示的tick
  141. self.ticks = calTicks;
  142. self.min = calTicks[0];
  143. self.max = calTicks[calTicks.length - 1];
  144. } else {
  145. var ticks = [];
  146. each(calTicks, function (tick) {
  147. if (tick >= self.min && tick <= self.max) {
  148. ticks.push(tick);
  149. }
  150. }); // 如果 ticks 为空,直接输入最小值、最大值
  151. if (!ticks.length) {
  152. ticks.push(self.min);
  153. ticks.push(self.max);
  154. }
  155. self.ticks = ticks;
  156. }
  157. };
  158. /**
  159. * @override
  160. */
  161. _proto.scale = function scale(value) {
  162. if (isNil(value)) {
  163. return NaN;
  164. }
  165. var max = this.max;
  166. var min = this.min;
  167. if (max === min) {
  168. return 0;
  169. }
  170. var percent = (value - min) / (max - min);
  171. var rangeMin = this.rangeMin();
  172. var rangeMax = this.rangeMax();
  173. return rangeMin + percent * (rangeMax - rangeMin);
  174. };
  175. /**
  176. * @override
  177. */
  178. _proto.invert = function invert(value) {
  179. var percent = (value - this.rangeMin()) / (this.rangeMax() - this.rangeMin());
  180. return this.min + percent * (this.max - this.min);
  181. };
  182. return Linear;
  183. }(Base);
  184. Base.Linear = Linear;
  185. module.exports = Linear;