base.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. var mix = require('@antv/util/lib/mix');
  2. var each = require('@antv/util/lib/each');
  3. var isObject = require('@antv/util/lib/type/is-object');
  4. var isNil = require('@antv/util/lib/type/is-nil');
  5. var Scale =
  6. /*#__PURE__*/
  7. function () {
  8. var _proto = Scale.prototype;
  9. _proto._initDefaultCfg = function _initDefaultCfg() {
  10. this.type = 'base';
  11. /**
  12. * 格式化函数,输出文本或者tick时的格式化函数
  13. * @type {Function}
  14. */
  15. this.formatter = null;
  16. /**
  17. * 输出的值域
  18. * @type {Array}
  19. */
  20. this.range = [0, 1];
  21. /**
  22. * 度量的标记
  23. * @type {Array}
  24. */
  25. this.ticks = null;
  26. /**
  27. * 参与度量计算的值,可选项
  28. * @type {Array}
  29. */
  30. this.values = [];
  31. };
  32. function Scale(cfg) {
  33. this._initDefaultCfg();
  34. mix(this, cfg);
  35. this.init();
  36. }
  37. /**
  38. * 度量初始化
  39. * @protected
  40. */
  41. _proto.init = function init() {};
  42. /**
  43. * 获取该度量的ticks,返回的是多个对象,
  44. * - text: tick 的文本
  45. * - value: 对应的度量转换后的值
  46. * <code>
  47. * [
  48. * {text: 0,value:0}
  49. * {text: 1,value:0.2}
  50. * {text: 2,value:0.4}
  51. * {text: 3,value:0.6}
  52. * {text: 4,value:0.8}
  53. * {text: 5,value:1}
  54. * ]
  55. * </code>
  56. * @param {Number} count 输出tick的个数的近似值,默认是 10
  57. * @return {Array} 返回 ticks 数组
  58. */
  59. _proto.getTicks = function getTicks() {
  60. var self = this;
  61. var ticks = self.ticks;
  62. var rst = [];
  63. each(ticks, function (tick) {
  64. var obj;
  65. if (isObject(tick)) {
  66. obj = tick;
  67. } else {
  68. obj = {
  69. text: self.getText(tick),
  70. tickValue: tick,
  71. value: self.scale(tick)
  72. };
  73. }
  74. rst.push(obj);
  75. });
  76. return rst;
  77. };
  78. /**
  79. * 获取格式化后的文本
  80. * @param {*} value 输入的数据
  81. * @param {*} key 字段的 key
  82. * @return {String} 格式化的文本
  83. */
  84. _proto.getText = function getText(value, key) {
  85. var formatter = this.formatter;
  86. value = formatter ? formatter(value, key) : value;
  87. if (isNil(value) || !value.toString) {
  88. value = '';
  89. }
  90. return value.toString();
  91. };
  92. /**
  93. * 输出的值域最小值
  94. * @protected
  95. * @return {Number} 返回最小的值
  96. */
  97. _proto.rangeMin = function rangeMin() {
  98. return this.range[0];
  99. };
  100. /**
  101. * 输出的值域最大值
  102. * @protected
  103. * @return {Number} 返回最大的值
  104. */
  105. _proto.rangeMax = function rangeMax() {
  106. var range = this.range;
  107. return range[range.length - 1];
  108. };
  109. /**
  110. * 度量转换后的结果,翻转回输入域
  111. * @param {Number} value 需要翻转的数值
  112. * @return {*} 度量的输入值
  113. */
  114. _proto.invert = function invert(value) {
  115. return value;
  116. };
  117. /**
  118. * 将传入的值从非数值转换成数值格式,如分类字符串、时间字符串等
  119. * @param {*} value 传入的值
  120. * @return {Number} 转换的值
  121. */
  122. _proto.translate = function translate(value) {
  123. return value;
  124. };
  125. /**
  126. * 进行度量转换
  127. * @param {*} value 输入值
  128. * @return {Number} 输出值,在设定的输出值域之间,默认[0,1]
  129. */
  130. _proto.scale = function scale(value) {
  131. return value;
  132. };
  133. /**
  134. * 克隆一个新的scale,拥有跟当前scale相同的输入域、输出域等
  135. * @return {Scale} 克隆的度量
  136. */
  137. _proto.clone = function clone() {
  138. var self = this;
  139. var constr = self.constructor;
  140. var cfg = {};
  141. each(self, function (v, k) {
  142. cfg[k] = self[k];
  143. });
  144. return new constr(cfg);
  145. };
  146. /**
  147. * 更改度量的属性信息
  148. * @param {Object} info 属性信息
  149. * @chainable
  150. * @return {Scale} 返回自身的引用
  151. */
  152. _proto.change = function change(info) {
  153. this.ticks = null;
  154. mix(this, info);
  155. this.init();
  156. return this;
  157. };
  158. return Scale;
  159. }();
  160. module.exports = Scale;