Skip to content

Commit

Permalink
Merge pull request #162 from VisActor/fix/log-ticks
Browse files Browse the repository at this point in the history
fix: fix the bug of log-scale ticks
  • Loading branch information
xile611 authored Dec 28, 2023
2 parents 8aee54e + f935b50 commit 1157094
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "fix: fix the bug of log-scale ticks\n\n",
"type": "none",
"packageName": "@visactor/vscale"
}
],
"packageName": "@visactor/vscale",
"email": "dingling112@gmail.com"
}
8 changes: 4 additions & 4 deletions packages/vscale/__tests__/log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ it('log.nice() nices the domain, extending it to powers of ten', () => {

it('log.nice() works on degenerate domains', () => {
const x = new LogScale().domain([0, 0]).nice();
expect(x.domain()).toEqual([0, 0]);
expect(x.domain()).toEqual([1e-16, 1e-15]);
x.domain([0.5, 0.5]).nice();
expect(x.domain()).toEqual([0.1, 1]);
});
Expand All @@ -164,7 +164,7 @@ 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([-1000, -1.5, -0]);
expect(x.domain()).toEqual([-1000, -1.5, -1e-16]);
});

it('log.nice() works on large domains with large or small base', () => {
Expand Down Expand Up @@ -369,15 +369,15 @@ function round(x: number) {
it('log scale dont throw error when domain contain 0', () => {
const scale = new LogScale().domain([0, 1000]).range([0, 10000]);

expect(scale.scale(100)).toBeCloseTo(9333.333333333334);
expect(scale.scale(100)).toBeCloseTo(9463.909295551413);
expect(scale.scale(0)).toBeCloseTo(0);
expect(scale.scale(1000)).toBeCloseTo(10000);
expect(scale.ticks(5)).toEqual([0, 1, 1000]);
});

it('log scale dont throw error when domain contain 0 and negative value', () => {
const scale = new LogScale().domain([-1000, 0]).range([0, 10000]);
expect(scale.scale(-160)).toBeCloseTo(530.5866782293836);
expect(scale.scale(-160)).toBeCloseTo(426.6638791545382);
expect(scale.scale(0)).toBeCloseTo(10000);
expect(scale.scale(-1000)).toBeCloseTo(0);
expect(scale.ticks(5)).toEqual([-1000, -1, -0]);
Expand Down
12 changes: 6 additions & 6 deletions packages/vscale/src/log-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ function reflect(f: (x: number) => number) {
return (x: number) => -f(-x);
}

function limitPositiveZero(min: number = 1e-12) {
function limitPositiveZero(min: number = Number.EPSILON) {
return (x: number) => {
return Math.max(x, min);
};
}

function limitNegativeZero(min: number = 1e-12) {
function limitNegativeZero(min: number = Number.EPSILON) {
return (x: number) => {
return Math.min(x, -min);
};
Expand Down Expand Up @@ -118,8 +118,8 @@ export class LogScale extends ContinuousScale {

d3Ticks(count: number = 10) {
const d = this.domain();
let u = d[0];
let v = d[d.length - 1];
let u = this._limit(d[0]);
let v = this._limit(d[d.length - 1]);
const r = v < u;

if (r) {
Expand Down Expand Up @@ -232,8 +232,8 @@ export class LogScale extends ContinuousScale {

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

if (niceType === 'min') {
Expand Down

0 comments on commit 1157094

Please sign in to comment.