Skip to content

Commit

Permalink
fix(band): using normalizedFlex to compute bandWidth and step
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed Apr 27, 2022
1 parent e3d38d6 commit ade0745
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
21 changes: 21 additions & 0 deletions __tests__/unit/scales/band.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ describe('band scale', () => {
expect(bandScale.map('C')).toBeCloseTo(416.67);
});

test('test non-normalized flex options', () => {
const bandScale = new Band({
domain: ['A', 'B', 'C'],
flex: [20, 30, 10],
range: [0, 500],
});

const ba = bandScale.getBandWidth('A');
const bb = bandScale.getBandWidth('B');
const bc = bandScale.getBandWidth('C');
expect([ba, bb, bc].map((d) => d / bc)).toEqual([2, 3, 1]);

expect(bandScale.getStep('A')).toBeCloseTo(166.67);
expect(bandScale.getStep('B')).toBe(250);
expect(bandScale.getStep('C')).toBeCloseTo(83.33);

expect(bandScale.map('A')).toBe(0);
expect(bandScale.map('B')).toBeCloseTo(166.67);
expect(bandScale.map('C')).toBeCloseTo(416.67);
});

test('test patch flex options', () => {
const bandScale = new Band({
domain: ['A', 'B', 'C'],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/scale",
"version": "0.4.7",
"version": "0.4.8",
"description": "Toolkit for mapping abstract data into visual representation.",
"license": "MIT",
"main": "lib/index.js",
Expand Down
4 changes: 2 additions & 2 deletions src/scales/band.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ function computeBandState(options: BandStateOptions) {
// 计算每个 bandWidth 和 step,并且用定义域内的值索引
const valueBandWidth = new Map(
domain.map((d, i) => {
const bandWidth = flex[i] * minBandWidth;
const bandWidth = normalizedFlex[i] * minBandWidth;
return [d, round ? Math.floor(bandWidth) : bandWidth];
})
);
const valueStep = new Map(
domain.map((d, i) => {
const bandWidth = flex[i] * minBandWidth;
const bandWidth = normalizedFlex[i] * minBandWidth;
const step = bandWidth + PI;
return [d, round ? Math.floor(step) : step];
})
Expand Down

0 comments on commit ade0745

Please sign in to comment.