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. ;
  60. _proto.getTicks = function getTicks() {
  61. var self = this;
  62. var ticks = self.ticks;
  63. var rst = [];
  64. each(ticks, function (tick) {
  65. var obj;
  66. if (isObject(tick)) {
  67. obj = tick;
  68. } else {
  69. obj = {
  70. text: self.getText(tick),
  71. tickValue: tick,
  72. value: self.scale(tick)
  73. };
  74. }
  75. rst.push(obj);
  76. });
  77. return rst;
  78. }
  79. /**
  80. * 获取格式化后的文本
  81. * @param {*} value 输入的数据
  82. * @param {*} key 字段的 key
  83. * @return {String} 格式化的文本
  84. */
  85. ;
  86. _proto.getText = function getText(value, key) {
  87. var formatter = this.formatter;
  88. value = formatter ? formatter(value, key) : value;
  89. if (isNil(value) || !value.toString) {
  90. value = '';
  91. }
  92. return value.toString();
  93. }
  94. /**
  95. * 输出的值域最小值
  96. * @protected
  97. * @return {Number} 返回最小的值
  98. */
  99. ;
  100. _proto.rangeMin = function rangeMin() {
  101. return this.range[0];
  102. }
  103. /**
  104. * 输出的值域最大值
  105. * @protected
  106. * @return {Number} 返回最大的值
  107. */
  108. ;
  109. _proto.rangeMax = function rangeMax() {
  110. var range = this.range;
  111. return range[range.length - 1];
  112. }
  113. /**
  114. * 度量转换后的结果,翻转回输入域
  115. * @param {Number} value 需要翻转的数值
  116. * @return {*} 度量的输入值
  117. */
  118. ;
  119. _proto.invert = function invert(value) {
  120. return value;
  121. }
  122. /**
  123. * 将传入的值从非数值转换成数值格式,如分类字符串、时间字符串等
  124. * @param {*} value 传入的值
  125. * @return {Number} 转换的值
  126. */
  127. ;
  128. _proto.translate = function translate(value) {
  129. return value;
  130. }
  131. /**
  132. * 进行度量转换
  133. * @param {*} value 输入值
  134. * @return {Number} 输出值,在设定的输出值域之间,默认[0,1]
  135. */
  136. ;
  137. _proto.scale = function scale(value) {
  138. return value;
  139. }
  140. /**
  141. * 克隆一个新的scale,拥有跟当前scale相同的输入域、输出域等
  142. * @return {Scale} 克隆的度量
  143. */
  144. ;
  145. _proto.clone = function clone() {
  146. var self = this;
  147. var constr = self.constructor;
  148. var cfg = {};
  149. each(self, function (v, k) {
  150. cfg[k] = self[k];
  151. });
  152. return new constr(cfg);
  153. }
  154. /**
  155. * 更改度量的属性信息
  156. * @param {Object} info 属性信息
  157. * @chainable
  158. * @return {Scale} 返回自身的引用
  159. */
  160. ;
  161. _proto.change = function change(info) {
  162. this.ticks = null;
  163. mix(this, info);
  164. this.init();
  165. return this;
  166. };
  167. return Scale;
  168. }();
  169. module.exports = Scale;