Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/log nice floor #127

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@visactor/vscale",
"comment": "fix: change log nice floor logic",
"type": "none"
}
],
"packageName": "@visactor/vscale"
}
24 changes: 12 additions & 12 deletions packages/vscale/__tests__/log-nice-option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ test('log.nice() width option forceMax', function () {

scale.nice(10, { forceMax: 700 });

expect(scale.domain()).toEqual([231, 700]);
expect(scale.ticks(5)).toEqual([231, 251, 316, 398, 501, 631, 700]);
expect(scale.domain()).toEqual([231, 700]);
expect(scale.scale(10)).toBeCloseTo(-2.832090257288676);
expect(scale.scale(200)).toBeCloseTo(-0.1299767312314118);
expect(scale.domain()).toEqual([100, 700]);
expect(scale.ticks(5)).toEqual([100, 158, 251, 398, 631, 700]);
expect(scale.domain()).toEqual([100, 700]);
expect(scale.scale(10)).toBeCloseTo(-1.1832946624549383);
expect(scale.scale(200)).toBeCloseTo(0.3562071871080222);
expect(scale.scale(789)).toBeUndefined();

scale.nice(10, { forceMax: 1123 });

expect(scale.domain()).toEqual([231, 1123]);
expect(scale.ticks(5)).toEqual([231, 251, 316, 398, 501, 631, 794, 1000, 1123]);
expect(scale.domain()).toEqual([231, 1123]);
expect(scale.scale(10)).toBeCloseTo(-1.9855503225574376);
expect(scale.scale(200)).toBeCloseTo(-0.0911253940291301);
expect(scale.domain()).toEqual([100, 1123]);
expect(scale.ticks(5)).toEqual([100, 158, 251, 398, 631, 1000, 1123]);
expect(scale.domain()).toEqual([100, 1123]);
expect(scale.scale(10)).toBeCloseTo(-0.952036626790323);
expect(scale.scale(200)).toBeCloseTo(0.28659158163464227);
expect(scale.scale(1200)).toBeUndefined();
});

Expand All @@ -64,7 +64,7 @@ test('log.nice() width option min and max', function () {
// only max is valid
scale.nice(10, { min: 28, max: 23456 });

expect(scale.domain()).toEqual([20, 23456]);
expect(scale.domain()).toEqual([10, 23456]);

// min and max area valid
scale.nice(10, { min: 13, max: 34677 });
Expand All @@ -80,5 +80,5 @@ test('log.nice() width option min and max', function () {

scale.nice(10, { min: 23, max: 1000 });

expect(scale.domain()).toEqual([20, 18990]);
expect(scale.domain()).toEqual([10, 18990]);
});
18 changes: 9 additions & 9 deletions packages/vscale/__tests__/log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ it('log.nice() nices the domain, extending it to powers of ten', () => {
x.domain([10.9, 1.1]).nice();
expect(x.domain()).toEqual([11, 1]);
x.domain([0.7, 11.001]).nice();
expect(x.domain()).toEqual([0, 12]);
expect(x.domain()).toEqual([0.1, 12]);
x.domain([123.1, 6.7]).nice();
expect(x.domain()).toEqual([124, 6]);
expect(x.domain()).toEqual([124, 1]);
x.domain([0.01, 0.49]).nice();
expect(x.domain()).toEqual([0, 1]);
expect(x.domain()).toEqual([0.01, 1]);
x.domain([1.5, 50]).nice();
expect(x.domain()).toEqual([1, 50]);
expect(x.scale(1)).toBe(0);
Expand All @@ -157,25 +157,25 @@ it('log.nice() works on degenerate domains', () => {
const x = new LogScale().domain([0, 0]).nice();
expect(x.domain()).toEqual([0, 0]);
x.domain([0.5, 0.5]).nice();
expect(x.domain()).toEqual([0, 1]);
expect(x.domain()).toEqual([0.1, 1]);
});

it('log.nice() on a polylog domain only affects the extent', () => {
const x = new LogScale().domain([1.1, 1.5, 10.9]).nice();
expect(x.domain()).toEqual([1, 1.5, 11]);
x.domain([-124, -1.5, -0]).nice();
expect(x.domain()).toEqual([-124, -1.5, -0]);
expect(x.domain()).toEqual([-1000, -1.5, -0]);
});

it('log.nice() works on large domains with large or small base', () => {
const x = new LogScale().base(1024).domain([1785, 11041]).nice();
expect(x.d3Ticks()).toEqual([2048, 3072, 4096, 5120, 6144, 7168, 8192, 9216, 10240]);
expect(x.d3Ticks()).toEqual([1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192, 9216, 10240]);
x.base(2).domain([1785, 11041]).nice();
expect(x.d3Ticks()).toEqual([1200, 2400, 3600, 4800, 6000, 7200, 8400, 9600, 10800, 12000]);
expect(x.d3Ticks()).toEqual([2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000]);
x.base(Math.E).domain([1785, 11041]).nice();
expect(x.d3Ticks()).toEqual([1096.6331584284585, 2980.9579870417283, 8103.083927575384, 22026.465794806718]);
expect(x.d3Ticks()).toEqual([403.4287934927351, 2980.9579870417283, 22026.465794806718]);
x.base(10).domain([1785, 11041]).nice();
expect(x.d3Ticks()).toEqual([2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000]);
expect(x.d3Ticks()).toEqual([1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000]);
});

it('log.nice() works on small domains with large or small base', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/vscale/src/log-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class LogScale extends ContinuousScale {
} else {
z = ticks(i, j, Math.min(j - i, count)).map(this._pows);
}
z = z.filter((t: number) => t !== 0);
return r ? z.reverse() : z;
}

Expand Down Expand Up @@ -185,7 +186,7 @@ export class LogScale extends ContinuousScale {

if (niceType) {
const niceDomain = nice(originalDomain.slice(), {
floor: (x: number) => Math.floor(x),
floor: (x: number) => this._pows(Math.floor(this._logs(x))),
ceil: (x: number) => Math.ceil(x)
});

Expand Down