Skip to content

Commit

Permalink
fix(y-scale): update y scale min, max (#2013)
Browse files Browse the repository at this point in the history
* fix(y-scale): update y scale min, max

* fix(y-scale): update y scale min, max, with isNumber
  • Loading branch information
hustcc authored Nov 25, 2020
1 parent 50fee30 commit 125c4b7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
14 changes: 13 additions & 1 deletion __tests__/unit/utils/data-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@ import { adjustYMetaByZero } from '../../../src/utils/data';

describe('data', () => {
it('adjustYMetaByZero', () => {
expect(adjustYMetaByZero([{ y: 0 }, { y: 1 }], 'y')).toEqual({});
expect(adjustYMetaByZero([{ y: -1 }, { y: 1 }], 'y')).toEqual({});

expect(adjustYMetaByZero([{ y: 0 }, { y: 1 }], 'y')).toEqual({ min: 0 });
expect(adjustYMetaByZero([{ y: 20 }, { y: 20 }], 'y')).toEqual({ min: 0 });
expect(adjustYMetaByZero([{ y: -20 }, { y: -20 }], 'y')).toEqual({ max: 0 });

expect(adjustYMetaByZero([{ y: 20 }, { y: 20 }, { y: null }], 'y')).toEqual({ min: 0 });
expect(adjustYMetaByZero([{ y: -20 }, { y: -20 }, { y: 0 }], 'y')).toEqual({ max: 0 });
expect(adjustYMetaByZero([{ y: -20 }, { y: -20 }, { y: undefined }], 'y')).toEqual({ max: 0 });

expect(adjustYMetaByZero([{ y: null }, { y: 0 }, { y: undefined }], 'y')).toEqual({ min: 0 });

expect(adjustYMetaByZero([{ y: null }, { y: 0 }, { y: NaN }], 'y')).toEqual({ min: 0 });
expect(adjustYMetaByZero([{ y: 20 }, { y: 20 }, { y: NaN }], 'y')).toEqual({ min: 0 });
expect(adjustYMetaByZero([{ y: -20 }, { y: -20 }, { y: NaN }], 'y')).toEqual({ max: 0 });
});
});
17 changes: 11 additions & 6 deletions src/utils/data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get } from '@antv/util';
import { get, isNumber } from '@antv/util';
import { Data, Datum, Meta } from '../types';

/**
Expand All @@ -7,16 +7,21 @@ import { Data, Datum, Meta } from '../types';
* @param field
*/
export function adjustYMetaByZero(data: Data, field: string): Meta {
const gtZero = data.every((datum: Datum) => get(datum, [field]) > 0);
const ltZero = data.every((datum: Datum) => get(datum, [field]) < 0);
// 过滤出数字数据
const numberData = data.filter((datum: Datum) => {
const v = get(datum, [field]);
return isNumber(v) && !isNaN(v);
});

const gtZero = numberData.every((datum: Datum) => get(datum, [field]) >= 0);
const ltZero = numberData.every((datum: Datum) => get(datum, [field]) <= 0);

// 目前是增量更新,对 { min: 0, max: undefined } 进行 update({ max: 0 }) 会得到 { min: 0, max: 0 }
// 需要添加 undefined 进行覆盖
if (gtZero) {
return { min: 0, max: undefined };
return { min: 0 };
}
if (ltZero) {
return { max: 0, min: undefined };
return { max: 0 };
}
return {};
}

0 comments on commit 125c4b7

Please sign in to comment.