Skip to content

Commit

Permalink
fix: pretty wilkinson-extended with precision problem close: #156 (#157)
Browse files Browse the repository at this point in the history
* fix: pretty wilkinson-extended with precision problem close: #156

* chore: update test coverage

* chore: update version
  • Loading branch information
pearmini authored Jun 18, 2021
1 parent 6634eea commit a07b2f9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 38 deletions.
7 changes: 6 additions & 1 deletion __tests__/unit/tick-methods/wilkinson-extended.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ describe('wilkinson-extended test', () => {
expect(wilkinsonExtended(0, 0.1, 5)).toStrictEqual([0, 0.025, 0.05, 0.075, 0.1]);
expect(wilkinsonExtended(0, 0.01, 5)).toStrictEqual([0, 0.0025, 0.005, 0.0075, 0.01]);
expect(wilkinsonExtended(0, 0.001, 5)).toStrictEqual([0, 0.00025, 0.0005, 0.00075, 0.001]);
expect(wilkinsonExtended(0, 0.0001, 6)).toStrictEqual([0, 0.00002, 0.00004, 0.00006, 0.00008, 0.0001]);
expect(wilkinsonExtended(0, 0.0001, 6)).toStrictEqual([0, 0.00002, 0.00004, 0.00006, 0.00008, 0.0001, 0.00012]);
expect(wilkinsonExtended(0, 0.00001, 6)).toStrictEqual([
0, 0.000002, 0.000004, 0.000006, 0.000008, 0.00001, 0.000012,
]);
expect(wilkinsonExtended(0, 0.000001, 6)).toStrictEqual([0, 0.0000002, 0.0000004, 0.0000006, 0.0000008, 0.000001]);
expect(wilkinsonExtended(0, 1e-15, 6)).toStrictEqual([0, 2e-16, 4e-16, 6e-16, 8e-16, 1e-15]);
});

test('precision', () => {
expect(wilkinsonExtended(0, 1.2, 5)).toStrictEqual([0, 0.3, 0.6, 0.9, 1.2]);
});
});
13 changes: 0 additions & 13 deletions __tests__/unit/utils/add.spec.ts

This file was deleted.

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.1",
"version": "0.4.2",
"description": "Toolkit for mapping abstract data into visual representation.",
"license": "MIT",
"main": "lib/index.js",
Expand Down
11 changes: 8 additions & 3 deletions src/tick-methods/wilkinson-extended.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { indexOf, size } from '@antv/util';
import { TickMethod } from '../types';
import { precisionAdd } from '../utils/precision-add';

export const DEFAULT_Q = [1, 5, 2, 2.5, 4, 3];

Expand Down Expand Up @@ -61,6 +60,11 @@ function legibility() {
return 1;
}

// 为了解决 js 运算的精度问题
function pretty(n: number) {
return n < 1e-15 ? n : parseFloat(n.toFixed(15));
}

/**
* An Extension of Wilkinson's Algorithm for Position Tick Labels on Axes
* https://www.yuque.com/preview/yuque/0/2019/pdf/185317/1546999150858-45c3b9c2-4e86-4223-bf1a-8a732e8195ed.pdf
Expand Down Expand Up @@ -165,9 +169,10 @@ export const wilkinsonExtended: TickMethod = (
// 步长为浮点数时处理精度
const range = new Array(Math.floor(size));

for (let tick = best.lmin; tick <= best.lmax; tick = precisionAdd(tick, best.lstep)) {
range[i] = tick;
for (let tick = best.lmin; tick <= best.lmax; tick += best.lstep) {
range[i] = pretty(tick);
i += 1;
}

return range;
};
20 changes: 0 additions & 20 deletions src/utils/precision-add.ts

This file was deleted.

0 comments on commit a07b2f9

Please sign in to comment.