Skip to content

Commit

Permalink
feat: attr映射支持自定义map处理
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Sep 24, 2021
1 parent f1921fc commit 460fefb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 详细配置请移步:https://prettier.io/docs/en/configuration.html
---
singleQuote: true
15 changes: 13 additions & 2 deletions packages/f2-next/src/attr/base.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Scale } from '@antv/scale';
import { mix } from '@antv/util';
import { mix, isFunction, isNil } from '@antv/util';
import { values as arrayValues } from '../util/array';

class Base {
data: any;
field: string;
scale: Scale;
range: any[];
map: Function;

constructor(options) {
mix(this, options);
Expand All @@ -22,8 +23,18 @@ class Base {
return null;
}

// 数据映射方法
_mapping(value): any {
return value;
}

// 数据映射方法
mapping(value): any {
const rst = isFunction(this.map) ? this.map(value) : null;
if (!isNil(rst)) {
return rst;
}
return this._mapping(value);
}

update(options) {
Expand All @@ -43,4 +54,4 @@ class Base {
}
}

export default Base;
export default Base;
3 changes: 1 addition & 2 deletions packages/f2-next/src/attr/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { Category as CategoryScale } from '@antv/scale';
import Base from './base';

class Category extends Base {

createScale(scale) {
return new CategoryScale(scale);
}

mapping(value: any) {
_mapping(value: any) {
const { scale, range } = this;
const index = scale.translate(value);
return range[index % range.length];
Expand Down
2 changes: 1 addition & 1 deletion packages/f2-next/src/attr/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Identity extends Base {
return new IdentityScale(scale);
}

mapping(value: any) {
_mapping(value: any) {
return value;
}
}
Expand Down
13 changes: 6 additions & 7 deletions packages/f2-next/src/attr/linear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import { isArray } from '@antv/util';
import Base from './base';

class Linear extends Base {

createScale(scale) {
return new LinearScale(scale);
}

mapping(value: any) {
_mapping(value: any) {
const { scale, range } = this;
const [ min, max ] = range;
const [min, max] = range;

if (isArray(value)) {
return value.map(v => {
return value.map((v) => {
return min + (max - min) * scale.scale(v);
});
}
Expand All @@ -24,7 +23,7 @@ class Linear extends Base {
const { scale } = this;

if (isArray(value)) {
return value.map(v => {
return value.map((v) => {
return scale.scale(v);
});
}
Expand All @@ -33,10 +32,10 @@ class Linear extends Base {

convert(value) {
const { range } = this;
const [ min, max ] = range;
const [min, max] = range;

if (isArray(value)) {
return value.map(v => {
return value.map((v) => {
return min + (max - min) * v;
});
}
Expand Down

0 comments on commit 460fefb

Please sign in to comment.