-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(log): update nice * refactor: log nice * feat(log): update getTicks
- Loading branch information
Showing
9 changed files
with
184 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { d3Log } from '../../../src/tick-methods/d3-log'; | ||
|
||
function round(x: number) { | ||
return Math.round(x * 1e12) / 1e12; | ||
} | ||
|
||
describe('d3Log', () => { | ||
test('d3Log(a, b, n) generates the expected power-of-ten for ascending ticks', () => { | ||
expect(d3Log(1e-1, 1e1, 10).map(round)).toEqual([ | ||
0.1, | ||
0.2, | ||
0.3, | ||
0.4, | ||
0.5, | ||
0.6, | ||
0.7, | ||
0.8, | ||
0.9, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
6, | ||
7, | ||
8, | ||
9, | ||
10, | ||
]); | ||
expect(d3Log(1e-1, 1, 10).map(round)).toEqual([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]); | ||
expect(d3Log(-1, -1e-1, 10).map(round)).toEqual([-1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1]); | ||
}); | ||
|
||
test('d3Log(a, b, n) generates the expected power-of-ten ticks for descending domains', () => { | ||
expect(d3Log(-1e-1, -1e1, 10).map(round)).toEqual( | ||
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1].reverse() | ||
); | ||
expect(d3Log(-1e-1, -1, 10).map(round)).toEqual( | ||
[-1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1].reverse() | ||
); | ||
expect(d3Log(1, 1e-1, 10).map(round)).toEqual([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1].reverse()); | ||
}); | ||
|
||
test('d3Log(a, b, n) generates the expected power-of-ten ticks for small domains', () => { | ||
expect(d3Log(1, 5, 10)).toEqual([1, 2, 3, 4, 5]); | ||
expect(d3Log(5, 1, 10)).toEqual([5, 4, 3, 2, 1]); | ||
expect(d3Log(-1, -5, 10)).toEqual([-1, -2, -3, -4, -5]); | ||
expect(d3Log(-5, -1, 10)).toEqual([-5, -4, -3, -2, -1]); | ||
expect(d3Log(286.9252014, 329.4978332, 1)).toEqual([300]); | ||
expect(d3Log(286.9252014, 329.4978332, 2)).toEqual([300]); | ||
expect(d3Log(286.9252014, 329.4978332, 3)).toEqual([300, 320]); | ||
expect(d3Log(286.9252014, 329.4978332, 4)).toEqual([290, 300, 310, 320]); | ||
expect(d3Log(286.9252014, 329.4978332, 10)).toEqual([290, 295, 300, 305, 310, 315, 320, 325]); | ||
}); | ||
|
||
test('d3Log(a, b, n) generates linear ticks when the domain extent is small', () => { | ||
expect(d3Log(41, 42, 10)).toEqual([41, 41.1, 41.2, 41.3, 41.4, 41.5, 41.6, 41.7, 41.8, 41.9, 42]); | ||
expect(d3Log(42, 41, 10)).toEqual([42, 41.9, 41.8, 41.7, 41.6, 41.5, 41.4, 41.3, 41.2, 41.1, 41]); | ||
expect(d3Log(1600, 1400, 10)).toEqual([1600, 1580, 1560, 1540, 1520, 1500, 1480, 1460, 1440, 1420, 1400]); | ||
}); | ||
|
||
test('d3Log(a, b, n, base) generates the expected power-of-base ticks', () => { | ||
expect(d3Log(0.1, 100, 10, Math.E).map(round)).toEqual([ | ||
0.135335283237, | ||
0.367879441171, | ||
1, | ||
2.718281828459, | ||
7.389056098931, | ||
20.085536923188, | ||
54.598150033144, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { d3LogNice } from '../../../src/utils'; | ||
|
||
describe('d3LogNice', () => { | ||
test('d3LogNice(a, b, base) nices the a, b, extending it to powers of ten', () => { | ||
expect(d3LogNice(10.9, 1.1, 0, 10)).toEqual([100, 1]); | ||
expect(d3LogNice(0.7, 11.001, 0, 10)).toEqual([0.1, 100]); | ||
expect(d3LogNice(123.1, 6.7, 0, 10)).toEqual([1000, 1]); | ||
expect(d3LogNice(0.01, 0.49, 0, 10)).toEqual([0.01, 1]); | ||
expect(d3LogNice(1.5, 50, 0, 10)).toEqual([1, 100]); | ||
expect(d3LogNice(50, 1.5, 0, 10)).toEqual([100, 1]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { TickMethod } from 'types'; | ||
import { d3Linear } from './d3-linear'; | ||
import { pows, logs } from '../utils'; | ||
|
||
export const d3Log: TickMethod = (a, b, n, base = 10) => { | ||
const shouldReflect = a < 0; | ||
const pow = pows(base, shouldReflect); | ||
const log = logs(base, shouldReflect); | ||
|
||
const r = b < a; | ||
const min = r ? b : a; | ||
const max = r ? a : b; | ||
let i = log(min); | ||
let j = log(max); | ||
let ticks = []; | ||
|
||
// 如果 base 是整数 | ||
if (!(base % 1) && j - i < n) { | ||
i = Math.floor(i); | ||
j = Math.ceil(j); | ||
if (shouldReflect) { | ||
for (; i <= j; i += 1) { | ||
const p = pow(i); | ||
for (let k = base - 1; k >= 1; k -= 1) { | ||
const t = p * k; | ||
if (t > max) break; | ||
if (t >= min) ticks.push(t); | ||
} | ||
} | ||
} else { | ||
for (; i <= j; i += 1) { | ||
const p = pow(i); | ||
for (let k = 1; k < base; k += 1) { | ||
const t = p * k; | ||
if (t > max) break; | ||
if (t >= min) ticks.push(t); | ||
} | ||
} | ||
} | ||
if (ticks.length * 2 < n) ticks = d3Linear(min, max, n); | ||
} else { | ||
ticks = d3Linear(i, j, Math.min(j - i, n)).map(pow); | ||
} | ||
|
||
return r ? ticks.reverse() : ticks; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { TickMethod } from '../types'; | ||
import { logs, pows } from './log'; | ||
|
||
export const d3LogNice: TickMethod = (a, b, _, base) => { | ||
const shouldReflect = a < 0; | ||
const log = logs(base, shouldReflect); | ||
const pow = pows(base, shouldReflect); | ||
const r = a > b; | ||
const min = r ? b : a; | ||
const max = r ? a : b; | ||
const niceDomain = [pow(Math.floor(log(min))), pow(Math.ceil(log(max)))]; | ||
return r ? niceDomain.reverse() : niceDomain; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const reflect = (f: Function) => { | ||
return (x: number) => -f(-x); | ||
}; | ||
|
||
export const logs = (base: number, shouldReflect: boolean) => { | ||
const baseCache = Math.log(base); | ||
const log = base === Math.E ? Math.log : (x: number) => Math.log(x) / baseCache; | ||
return shouldReflect ? reflect(log) : log; | ||
}; | ||
|
||
export const pows = (base: number, shouldReflect: boolean) => { | ||
const pow = base === Math.E ? Math.exp : (x: number) => base ** x; | ||
return shouldReflect ? reflect(pow) : pow; | ||
}; |