Skip to content

Commit

Permalink
fix(ticks): handle decimal and negative tickCount (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed Jul 8, 2021
1 parent 920928d commit 31fa6b5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
12 changes: 11 additions & 1 deletion __tests__/unit/tick-methods/d3-ticks.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { d3Ticks as fn } from '../../../src';

describe('linear ticks', () => {
describe('d3 ticks', () => {
test('common usage', () => {
expect(fn(0, 1000, 10)).toStrictEqual([0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]);

Expand All @@ -19,4 +19,14 @@ describe('linear ticks', () => {
test('reverse data', () => {
expect(fn(0.1, 0.8, 5)).toStrictEqual([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]);
});

test('handle decimal tickCount', () => {
expect(fn(0, 5, 0.4)).toStrictEqual([0]);
expect(fn(0, 5, 1.5)).toStrictEqual([0, 5]);
});

test('handle negative tickCount', () => {
expect(fn(0, 5, -1)).toStrictEqual(fn(0, 5, 0));
expect(fn(0, 5, -1.2)).toStrictEqual(fn(0, 5, 0));
});
});
10 changes: 10 additions & 0 deletions __tests__/unit/tick-methods/r-pretty.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@ describe('rPretty ticks', () => {
it('rPretty for tiny number', () => {
expect(rPretty(9.899999999999999, 9.9)).toStrictEqual([9.899999999999999, 9.899999999999999, 9.9, 9.9]);
});

it('handle decimal tickCount', () => {
expect(rPretty(0, 5, 0.4)).toStrictEqual(rPretty(0, 5, 0));
expect(rPretty(0, 5, 0.5)).toStrictEqual(rPretty(0, 5, 1));
});

it('handle negative tickCount', () => {
expect(rPretty(0, 5, -1)).toStrictEqual(rPretty(0, 5, 0));
expect(rPretty(0, 5, -1.2)).toStrictEqual(rPretty(0, 5, 0));
});
});
10 changes: 10 additions & 0 deletions __tests__/unit/tick-methods/wilkinson-extended.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ describe('wilkinson-extended test', () => {
test('precision', () => {
expect(wilkinsonExtended(0, 1.2, 5)).toStrictEqual([0, 0.3, 0.6, 0.9, 1.2]);
});

test('handle decimal tickCount', () => {
expect(wilkinsonExtended(0, 5, 0.4)).toStrictEqual(wilkinsonExtended(0, 5, 0));
expect(wilkinsonExtended(0, 5, 0.5)).toStrictEqual(wilkinsonExtended(0, 5, 1));
});

test('handle negative tickCount', () => {
expect(wilkinsonExtended(0, 5, -1)).toStrictEqual(wilkinsonExtended(0, 5, 0));
expect(wilkinsonExtended(0, 5, -1.2)).toStrictEqual(wilkinsonExtended(0, 5, 0));
});
});
5 changes: 4 additions & 1 deletion src/tick-methods/r-pretty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import { prettyNumber } from '../utils/pretty-number';
* @see R pretty https://svn.r-project.org/R/trunk/src/appl/pretty.c
* @see R pretty https://www.rdocumentation.org/packages/base/versions/3.5.2/topics/pretty
*/
export const rPretty: TickMethod = (min, max, n = 5) => {
export const rPretty: TickMethod = (min, max, m = 5) => {
if (min === max) {
return [min];
}

const n = m < 0 ? 0 : Math.round(m);
if (n === 0) return [];

// high.u.bias
const h = 1.5;
// u5.bias
Expand Down
3 changes: 2 additions & 1 deletion src/tick-methods/wilkinson-extended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ function legibility() {
export const wilkinsonExtended: TickMethod = (
dMin: number,
dMax: number,
m: number = 5,
n: number = 5,
onlyLoose: boolean = true,
Q: number[] = DEFAULT_Q,
w: [number, number, number, number] = [0.25, 0.2, 0.5, 0.05]
) => {
const m = n < 0 ? 0 : Math.round(n);
// nan 涔熶細瀵艰嚧寮傚父
if (Number.isNaN(dMin) || Number.isNaN(dMax) || typeof dMin !== 'number' || typeof dMax !== 'number' || !m) {
return [];
Expand Down

0 comments on commit 31fa6b5

Please sign in to comment.