UNPKG

1.02 kBPlain TextView Raw
1import { has, isNumber } from '@antv/util';
2import Base from '../base';
3import { ScaleType } from '../types';
4
5/**
6 * identity scale原则上是定义域和值域一致,scale/invert方法也是一致的
7 * 参考R的实现:https://github.com/r-lib/scales/blob/master/R/pal-identity.r
8 * 参考d3的实现(做了下转型):https://github.com/d3/d3-scale/blob/master/src/identity.js
9 */
10export default class Identity extends Base {
11 public readonly type: ScaleType = 'identity';
12 public readonly isIdentity: boolean = true;
13
14 public calculateTicks() {
15 return this.values;
16 }
17
18 public scale(value: any): number {
19 // 如果传入的值不等于 identity 的值,则直接返回,用于一维图时的 dodge
20 if (this.values[0] !== value && isNumber(value)) {
21 return value;
22 }
23 return this.range[0];
24 }
25
26 public invert(value?: number): number {
27 const range = this.range;
28 if (value < range[0] || value > range[1]) {
29 return NaN;
30 }
31 return this.values[0];
32 }
33}