123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- var mix = require('@antv/util/lib/mix');
- var each = require('@antv/util/lib/each');
- var isObject = require('@antv/util/lib/type/is-object');
- var isNil = require('@antv/util/lib/type/is-nil');
- var Scale =
- /*#__PURE__*/
- function () {
- var _proto = Scale.prototype;
- _proto._initDefaultCfg = function _initDefaultCfg() {
- this.type = 'base';
- /**
- * 格式化函数,输出文本或者tick时的格式化函数
- * @type {Function}
- */
- this.formatter = null;
- /**
- * 输出的值域
- * @type {Array}
- */
- this.range = [0, 1];
- /**
- * 度量的标记
- * @type {Array}
- */
- this.ticks = null;
- /**
- * 参与度量计算的值,可选项
- * @type {Array}
- */
- this.values = [];
- };
- function Scale(cfg) {
- this._initDefaultCfg();
- mix(this, cfg);
- this.init();
- }
- /**
- * 度量初始化
- * @protected
- */
- _proto.init = function init() {}
- /**
- * 获取该度量的ticks,返回的是多个对象,
- * - text: tick 的文本
- * - value: 对应的度量转换后的值
- * <code>
- * [
- * {text: 0,value:0}
- * {text: 1,value:0.2}
- * {text: 2,value:0.4}
- * {text: 3,value:0.6}
- * {text: 4,value:0.8}
- * {text: 5,value:1}
- * ]
- * </code>
- * @param {Number} count 输出tick的个数的近似值,默认是 10
- * @return {Array} 返回 ticks 数组
- */
- ;
- _proto.getTicks = function getTicks() {
- var self = this;
- var ticks = self.ticks;
- var rst = [];
- each(ticks, function (tick) {
- var obj;
- if (isObject(tick)) {
- obj = tick;
- } else {
- obj = {
- text: self.getText(tick),
- tickValue: tick,
- value: self.scale(tick)
- };
- }
- rst.push(obj);
- });
- return rst;
- }
- /**
- * 获取格式化后的文本
- * @param {*} value 输入的数据
- * @param {*} key 字段的 key
- * @return {String} 格式化的文本
- */
- ;
- _proto.getText = function getText(value, key) {
- var formatter = this.formatter;
- value = formatter ? formatter(value, key) : value;
- if (isNil(value) || !value.toString) {
- value = '';
- }
- return value.toString();
- }
- /**
- * 输出的值域最小值
- * @protected
- * @return {Number} 返回最小的值
- */
- ;
- _proto.rangeMin = function rangeMin() {
- return this.range[0];
- }
- /**
- * 输出的值域最大值
- * @protected
- * @return {Number} 返回最大的值
- */
- ;
- _proto.rangeMax = function rangeMax() {
- var range = this.range;
- return range[range.length - 1];
- }
- /**
- * 度量转换后的结果,翻转回输入域
- * @param {Number} value 需要翻转的数值
- * @return {*} 度量的输入值
- */
- ;
- _proto.invert = function invert(value) {
- return value;
- }
- /**
- * 将传入的值从非数值转换成数值格式,如分类字符串、时间字符串等
- * @param {*} value 传入的值
- * @return {Number} 转换的值
- */
- ;
- _proto.translate = function translate(value) {
- return value;
- }
- /**
- * 进行度量转换
- * @param {*} value 输入值
- * @return {Number} 输出值,在设定的输出值域之间,默认[0,1]
- */
- ;
- _proto.scale = function scale(value) {
- return value;
- }
- /**
- * 克隆一个新的scale,拥有跟当前scale相同的输入域、输出域等
- * @return {Scale} 克隆的度量
- */
- ;
- _proto.clone = function clone() {
- var self = this;
- var constr = self.constructor;
- var cfg = {};
- each(self, function (v, k) {
- cfg[k] = self[k];
- });
- return new constr(cfg);
- }
- /**
- * 更改度量的属性信息
- * @param {Object} info 属性信息
- * @chainable
- * @return {Scale} 返回自身的引用
- */
- ;
- _proto.change = function change(info) {
- this.ticks = null;
- mix(this, info);
- this.init();
- return this;
- };
- return Scale;
- }();
- module.exports = Scale;
|