Skip to content

Commit

Permalink
refactor: reduce repeated codes
Browse files Browse the repository at this point in the history
  • Loading branch information
xile611 committed Jul 16, 2024
1 parent 0e474c4 commit d816741
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 319 deletions.
89 changes: 89 additions & 0 deletions packages/vscale/src/log-nice-mixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { NiceOptions, NiceType } from './interface';
import { parseNiceOptions } from './utils/tick-sample';
import { nice } from './utils/utils';

export class LogNiceMixin {
protected _domain: number[];
protected _domainValidator?: (val: number) => boolean;
protected _niceDomain: number[];

nice(count: number = 10, option?: NiceOptions): this {
const originalDomain = this._domain;
let niceMinMax: number[] = [];
let niceType: NiceType = null;

if (option) {
const res = parseNiceOptions(originalDomain, option);
niceMinMax = res.niceMinMax;
this._domainValidator = res.domainValidator;

niceType = res.niceType;

if (res.niceDomain) {
this._niceDomain = res.niceDomain;
(this as any).rescale();
return this;
}
} else {
niceType = 'all';
}

if (niceType) {
const niceDomain = nice(
originalDomain.slice(),
(this as any).getNiceConfig?.() ?? {
floor: (x: number) => Math.floor(x),
ceil: (x: number) => Math.ceil(x)
}
);

if (niceType === 'min') {
niceDomain[niceDomain.length - 1] = niceMinMax[1] ?? niceDomain[niceDomain.length - 1];
} else if (niceType === 'max') {
niceDomain[0] = niceMinMax[0] ?? niceDomain[0];
}

this._niceDomain = niceDomain as number[];
(this as any).rescale();
return this;
}

return this;
}

/**
* 只对min区间进行nice
* 如果保持某一边界的值,就很难有好的nice效果,所以这里实现就是nice之后还原固定的边界值
*/
niceMin(): this {
const maxD = this._domain[this._domain.length - 1];
this.nice();
const niceDomain = this._domain.slice();

if (this._domain) {
niceDomain[niceDomain.length - 1] = maxD;
this._niceDomain = niceDomain;
(this as any).rescale();
}

return this;
}

/**
* 只对max区间进行nice
* 如果保持某一边界的值,就很难有好的nice效果,所以这里实现就是nice之后还原固定的边界值
*/
niceMax(): this {
const minD = this._domain[0];
this.nice();
const niceDomain = this._domain.slice();

if (this._domain) {
niceDomain[0] = minD;
this._niceDomain = niceDomain;
(this as any).rescale();
}

return this;
}
}
84 changes: 9 additions & 75 deletions packages/vscale/src/log-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ContinuousScale } from './continuous-scale';
import { ScaleEnum } from './type';
import { logp, nice, powp, logNegative, expNegative, identity } from './utils/utils';
import type { ContinuousScaleType, NiceOptions, NiceType, PolymapType } from './interface';
import { mixin } from '@visactor/vutils';
import { LogNiceMixin } from './log-nice-mixin';

/**
* 逆反函数
Expand Down Expand Up @@ -160,80 +162,12 @@ export class LogScale extends ContinuousScale {
);
}

nice(count: number = 10, option?: NiceOptions): this {
const originalDomain = this._domain;
let niceMinMax: number[] = [];
let niceType: NiceType = null;

if (option) {
const res = parseNiceOptions(originalDomain, option);
niceMinMax = res.niceMinMax;
this._domainValidator = res.domainValidator;

niceType = res.niceType;

if (res.niceDomain) {
this._niceDomain = res.niceDomain;
this.rescale();
return this;
}
} else {
niceType = 'all';
}

if (niceType) {
const niceDomain = nice(originalDomain.slice(), {
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') {
niceDomain[niceDomain.length - 1] = niceMinMax[1] ?? niceDomain[niceDomain.length - 1];
} else if (niceType === 'max') {
niceDomain[0] = niceMinMax[0] ?? niceDomain[0];
}

this._niceDomain = niceDomain as number[];
this.rescale();
return this;
}

return this;
}

/**
* 只对min区间进行nice
* 如果保持某一边界的值,就很难有好的nice效果,所以这里实现就是nice之后还原固定的边界值
*/
niceMin(): this {
const maxD = this._domain[this._domain.length - 1];
this.nice();
const niceDomain = this._domain.slice();

if (this._domain) {
niceDomain[niceDomain.length - 1] = maxD;
this._niceDomain = niceDomain;
this.rescale();
}

return this;
}

/**
* 只对max区间进行nice
* 如果保持某一边界的值,就很难有好的nice效果,所以这里实现就是nice之后还原固定的边界值
*/
niceMax(): this {
const minD = this._domain[0];
this.nice();
const niceDomain = this._domain.slice();

if (this._domain) {
niceDomain[0] = minD;
this._niceDomain = niceDomain;
this.rescale();
}

return this;
protected getNiceConfig() {
return {
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)))))
};
}
}

mixin(LogScale, LogNiceMixin);
89 changes: 5 additions & 84 deletions packages/vscale/src/symlog-scale.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { mixin } from '@visactor/vutils';
import type { ContinuousScaleType, NiceOptions, NiceType } from './interface';
import { LinearScale } from './linear-scale';
import { ScaleEnum } from './type';
import {
d3TicksForLog,
forceTicksBaseTransform,
parseNiceOptions,
tickIncrement,
ticksBaseTransform
} from './utils/tick-sample';
import { d3TicksForLog, forceTicksBaseTransform, parseNiceOptions, ticksBaseTransform } from './utils/tick-sample';
import { symlog, symexp, nice } from './utils/utils';
import { LogNiceMixin } from './log-nice-mixin';

export class SymlogScale extends LinearScale {
readonly type: ContinuousScaleType = ScaleEnum.Symlog;
Expand Down Expand Up @@ -74,81 +70,6 @@ export class SymlogScale extends LinearScale {
const d = this.calculateVisibleDomain(this._range);
return forceTicksBaseTransform(d[0], d[d.length - 1], step, this.transformer, this.untransformer);
}

nice(count: number = 10, option?: NiceOptions): this {
const originalDomain = this._domain;
let niceMinMax: number[] = [];
let niceType: NiceType = null;

if (option) {
const res = parseNiceOptions(originalDomain, option);
niceMinMax = res.niceMinMax;
this._domainValidator = res.domainValidator;

niceType = res.niceType;

if (res.niceDomain) {
this._niceDomain = res.niceDomain;
this.rescale();
return this;
}
} else {
niceType = 'all';
}

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

if (niceType === 'min') {
niceDomain[niceDomain.length - 1] = niceMinMax[1] ?? niceDomain[niceDomain.length - 1];
} else if (niceType === 'max') {
niceDomain[0] = niceMinMax[0] ?? niceDomain[0];
}

this._niceDomain = niceDomain as number[];
this.rescale();
return this;
}

return this;
}

/**
* 只对min区间进行nice
* 如果保持某一边界的值,就很难有好的nice效果,所以这里实现就是nice之后还原固定的边界值
*/
niceMin(): this {
const maxD = this._domain[this._domain.length - 1];
this.nice();
const niceDomain = this._domain.slice();

if (this._domain) {
niceDomain[niceDomain.length - 1] = maxD;
this._niceDomain = niceDomain;
this.rescale();
}

return this;
}

/**
* 只对max区间进行nice
* 如果保持某一边界的值,就很难有好的nice效果,所以这里实现就是nice之后还原固定的边界值
*/
niceMax(): this {
const minD = this._domain[0];
this.nice();
const niceDomain = this._domain.slice();

if (this._domain) {
niceDomain[0] = minD;
this._niceDomain = niceDomain;
this.rescale();
}

return this;
}
}

mixin(SymlogScale, LogNiceMixin);
38 changes: 16 additions & 22 deletions packages/vscale/src/utils/tick-sample-int.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { clamper } from '@visactor/vutils';

function generateTicks(start: number, stop: number, step: number, reverse: boolean) {
const ticks: number[] = [];
let ptr = start;
while (ptr <= stop) {
ticks.push(ptr);
ptr += step;
}
if (reverse) {
ticks.reverse();
}

return ticks;
}

/**
* 根据start、stop、count进行分割,不要求count完全准确,但是保证均匀,输出为整数数组
* @param start
Expand All @@ -10,7 +24,6 @@ import { clamper } from '@visactor/vutils';
*/
export function ticks(start: number, stop: number, count: number, allowExcessive?: boolean) {
let reverse: boolean;
const ticks: number[] = [];
let step: number;

stop = Math.floor(+stop);
Expand Down Expand Up @@ -40,16 +53,7 @@ export function ticks(start: number, stop: number, count: number, allowExcessive
}
}

let ptr = start;
while (ptr <= stop) {
ticks.push(ptr);
ptr += step;
}
if (reverse) {
ticks.reverse();
}

return ticks;
return generateTicks(start, stop, step, reverse);
}

/**
Expand All @@ -60,7 +64,6 @@ export function ticks(start: number, stop: number, count: number, allowExcessive
* @returns
*/
export function stepTicks(start: number, stop: number, step: number) {
const ticks: number[] = [];
let reverse: boolean;

stop = Math.floor(+stop);
Expand All @@ -71,14 +74,5 @@ export function stepTicks(start: number, stop: number, step: number) {
start = stop;
stop = n;
}

let ptr = start;
while (ptr <= stop) {
ticks.push(ptr);
ptr += step;
}
if (reverse) {
ticks.reverse();
}
return ticks;
return generateTicks(start, stop, step, reverse);
}
Loading

0 comments on commit d816741

Please sign in to comment.