Skip to content

Commit

Permalink
Merge pull request #15 from antvis/fix-issue-3737
Browse files Browse the repository at this point in the history
fix(g2-issue-3737): 允许外部传入 dimValuesMap,替代从数据中获取
  • Loading branch information
visiky committed Jan 23, 2022
2 parents 7447e75 + 1b55bf1 commit 9f1b441
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/adjusts/adjust.ts
Expand Up @@ -50,12 +50,16 @@ export default abstract class Adjust {
/** 宽度比例 */
public columnWidthRatio: number;

constructor(cfg: AdjustCfg) {
const { xField, yField, adjustNames = ['x', 'y'] } = cfg;
/** 用户自定义的dimValuesMap */
public dimValuesMap: DimValuesMapType;

constructor(cfg: AdjustCfg & { dimValuesMap?: DimValuesMapType }) {
const { xField, yField, adjustNames = ['x', 'y'], dimValuesMap } = cfg;

this.adjustNames = adjustNames;
this.xField = xField;
this.yField = yField;
this.dimValuesMap = dimValuesMap;
}

// 需要各自实现的方法
Expand Down Expand Up @@ -154,7 +158,7 @@ export default abstract class Adjust {
private getDimValues(mergedData: Data[]): DimValuesMapType {
const { xField, yField } = this;

const dimValuesMap: DimValuesMapType = {};
const dimValuesMap: DimValuesMapType = _.assign({}, this.dimValuesMap);

// 所有的维度
const dims = [];
Expand All @@ -166,6 +170,9 @@ export default abstract class Adjust {
}

dims.forEach((dim: string): void => {
if (dimValuesMap && dimValuesMap[dim]) {
return;
}
// 在每个维度上,所有的值
dimValuesMap[dim] = _.valuesOfKey(mergedData, dim).sort((v1, v2) => v1 - v2) as number[];
});
Expand Down
54 changes: 53 additions & 1 deletion tests/unit/jitter-spec.ts
Expand Up @@ -48,7 +48,10 @@ describe('adjust jitter', () => {
});

describe('adjust one dim.', () => {
const data = [{ a: 0, b: 2, c: 1 }, { a: 0, b: 3, c: 2 }];
const data = [
{ a: 0, b: 2, c: 1 },
{ a: 0, b: 3, c: 2 },
];

let newData;

Expand Down Expand Up @@ -79,4 +82,53 @@ describe('adjust jitter', () => {
expect(obj1.a < 0.95).toBe(true);
});
});

describe('pass in dimValuesMap', () => {
const data = [
{ a: 1, b: 2, c: 1 },
{ a: 1, b: 3, c: 2 },
{ a: 2, b: 1, c: 1 },
{ a: 2, b: 6, c: 2 },
{ a: 3, b: 5, c: 1 },
{ a: 3, b: 1, c: 2 },
];

const dimValuesMap={b:[1,2,3,4,5,6]};

let newData;

const adjust = new Jitter({
xField: 'a',
yField: 'b',
dimValuesMap
});

it('is adjust', () => {
expect(adjust.isAdjust('x')).toBe(true);
expect(adjust.isAdjust('y')).toBe(true);
});

it('get dim values', () => {
// @ts-ignore
const map = adjust.getDimValues(data);
expect(map.a.length).toBe(3);
expect(map.b.length).toBe(6);
});

it('process adjust', () => {
[newData] = adjust.process([data]);
});

it('adjust result', () => {
const obj1 = newData[0];
expect(obj1.b > 1.5).toBe(true);
expect(obj1.b < 2.5).toBe(true);
});

it('adjust second', () => {
const obj2 = data[2];
expect(obj2.b > 0.5).toBe(true);
expect(obj2.b < 1.5).toBe(true);
});
});
});

0 comments on commit 9f1b441

Please sign in to comment.