Skip to content

Commit

Permalink
fix: log-scale should handle niceDomain in scale() and invert()
Browse files Browse the repository at this point in the history
  • Loading branch information
xile611 authored and xiaoluoHe committed Aug 10, 2023
1 parent 5ef3b6c commit b1eb9a2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
8 changes: 6 additions & 2 deletions packages/vscale/src/continuous-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import type {
TransformType,
IContinuousScale,
ContinuousScaleType,
TickData
TickData,
NiceType
} from './interface';
import { interpolate } from './utils/interpolate';
import { bimap, identity, polymap } from './utils/utils';
Expand All @@ -18,6 +19,7 @@ export class ContinuousScale extends BaseScale implements IContinuousScale {
protected untransformer: TransformType;

protected _niceDomain: number[];
protected _niceType?: NiceType;
protected _domain: number[];
protected _range: any[];
protected _unknown: any = undefined;
Expand Down Expand Up @@ -75,7 +77,7 @@ export class ContinuousScale extends BaseScale implements IContinuousScale {
if (!this._input) {
this._input = (this._piecewise as PolymapType<any>)(
this._calculateRange(this._range),
this._domain.map(this.transformer),
(this._niceDomain ?? this._domain).map(this.transformer),
interpolateNumber
);
}
Expand All @@ -89,6 +91,8 @@ export class ContinuousScale extends BaseScale implements IContinuousScale {
return (this._niceDomain ?? this._domain).slice();
}
this._domainValidator = null;
this._niceType = null;
this._niceDomain = null;
const nextDomain = Array.from(_, toNumber) as [number, number];

this._domain = nextDomain;
Expand Down
20 changes: 1 addition & 19 deletions packages/vscale/src/linear-scale.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ScaleEnum } from './type';
import { d3Ticks, forceTicks, niceLinear, parseNiceOptions, stepTicks, ticks } from './utils/tick-sample';
import { ContinuousScale } from './continuous-scale';
import type { ContinuousScaleType, NiceOptions, NiceType } from './interface';
import { toNumber } from '@visactor/vutils';
import type { ContinuousScaleType, NiceOptions } from './interface';

/**
* TODO:
Expand All @@ -11,7 +10,6 @@ import { toNumber } from '@visactor/vutils';
*/
export class LinearScale extends ContinuousScale {
readonly type: ContinuousScaleType = ScaleEnum.Linear;
protected _niceType?: NiceType;

clone(): LinearScale {
return new LinearScale()
Expand All @@ -29,22 +27,6 @@ export class LinearScale extends ContinuousScale {
};
}

domain(): any[];
domain(_: any[], slience?: boolean): this;
domain(_?: any[], slience?: boolean): this | any[] {
if (!_) {
return (this._niceDomain ?? this._domain).slice();
}

this._niceType = null;
this._niceDomain = null;
this._domainValidator = null;
const nextDomain = Array.from(_, toNumber) as [number, number];

this._domain = nextDomain;
return this.rescale(slience);
}

d3Ticks(count: number = 10, options?: { noDecimals?: boolean }) {
const d = this.calculateVisibleDomain(this._range);
return d3Ticks(d[0], d[d.length - 1], count, options);
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 @@ -47,8 +47,9 @@ export class LogScale extends ContinuousScale {

const logs = logp(this._base);
const pows = powp(this._base);
const domain = this._niceDomain ?? this._domain;

if (this._domain[0] < 0) {
if (domain[0] < 0) {
this._logs = reflect(logs);
this._pows = reflect(pows);

Expand Down

0 comments on commit b1eb9a2

Please sign in to comment.