From 2bc6f8e3153df4cedd6244ce1c11c99eb6ce15be Mon Sep 17 00:00:00 2001 From: pearmini Date: Tue, 6 Jul 2021 16:38:24 +0800 Subject: [PATCH] fix(band): computed wrong length of adjustRange --- __tests__/unit/scales/band.spec.ts | 10 ++++++++++ __tests__/unit/utils/sequence.spec.ts | 24 ------------------------ src/scales/band.ts | 3 +-- src/utils/sequence.ts | 23 ----------------------- 4 files changed, 11 insertions(+), 49 deletions(-) delete mode 100644 __tests__/unit/utils/sequence.spec.ts delete mode 100644 src/utils/sequence.ts diff --git a/__tests__/unit/scales/band.spec.ts b/__tests__/unit/scales/band.spec.ts index 63d039b0..76dd50ee 100644 --- a/__tests__/unit/scales/band.spec.ts +++ b/__tests__/unit/scales/band.spec.ts @@ -74,6 +74,16 @@ describe('band scale', () => { expect(bandScale.getStep()).toBeCloseTo(147.059, 3); }); + test('test padding-inner and padding-outer options', () => { + const scale = new Band({ + domain: ['A', 'B', 'C'], + paddingInner: 1, + }); + expect(scale.map('C')).toBe(1); + // @ts-ignore + expect(scale.adjustedRange).toEqual([0, 0.5, 1]); + }); + test('test round option', () => { // 取整测试 const bandScale = new Band({ diff --git a/__tests__/unit/utils/sequence.spec.ts b/__tests__/unit/utils/sequence.spec.ts deleted file mode 100644 index 780e74d2..00000000 --- a/__tests__/unit/utils/sequence.spec.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { sequence } from '../../../src/utils/sequence'; - -describe('sequence util test', () => { - test('Both the first term and the last term are greater than or equal to 0', () => { - expect(sequence(0, 100, 20)).toStrictEqual([0, 20, 40, 60, 80]); - }); - - test('Both the first term and the last term are less than or equal to 0', () => { - expect(sequence(-100, 0, 30)).toStrictEqual([-100, -70, -40, -10]); - expect(sequence(-100, -10, 20)).toStrictEqual([-100, -80, -60, -40, -20]); - }); - - test('The last item is less than the first item, but the tolerance is greater than 0, we return an empty array', () => { - expect(sequence(-100, -200, 50)).toStrictEqual([]); - }); - - test('Tolerance is equal to 0', () => { - expect(sequence(-100, -200, 0)).toStrictEqual([]); - }); - - test('The last term is equal to the first term', () => { - expect(sequence(0, 0, 1000)).toStrictEqual([]); - }); -}); diff --git a/src/scales/band.ts b/src/scales/band.ts index 0f5a0806..f2a97f11 100644 --- a/src/scales/band.ts +++ b/src/scales/band.ts @@ -1,6 +1,5 @@ import { BandOptions } from '../types'; import { Ordinal } from './ordinal'; -import { sequence } from '../utils/sequence'; interface BandStateOptions { /** step 的数目。一般是 domain 的长度 */ @@ -78,7 +77,7 @@ function getBandState(opt: BandStateOptions) { } // 转化后的 range - const adjustedRange = sequence(rangeStart, rangeEnd, step); + const adjustedRange = new Array(stepAmount).fill(0).map((_, i) => rangeStart + i * step); return { step, diff --git a/src/utils/sequence.ts b/src/utils/sequence.ts deleted file mode 100644 index d11420d3..00000000 --- a/src/utils/sequence.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** - * 构造区间 [a1, an) 的等差数列 - * - * @param a1 起始值 - * @param an 结束值 - * @param d 公差 - * @returns {array[string]} 由每一项组成的数组 - */ - -export function sequence(a1: number, an: number, d: number) { - const tmp = (an - a1) / d; - const size = Number.isInteger(tmp) ? tmp : Math.floor(tmp) + 1; - - if (size <= 0) { - return []; - } - - const arr = new Array(size - 1); - for (let i = 0; i < size; i += 1) { - arr[i] = a1 + i * d; - } - return arr; -}