Skip to content

Commit

Permalink
refactor(threshold): search from thresholds instead of getDomain()
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed Jun 6, 2021
1 parent d743570 commit 5e894d5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 16 deletions.
3 changes: 0 additions & 3 deletions src/scales/quantile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import { createQuartile } from '../utils/create-quartile';
* 输入域被指定为一组离散的样本值,输出域中的值的数量决定了分位数的数量。
*/
export class Quantile extends Threshold<QuantileOptions> {
// 这里不能给 thresholds 赋值,否者会编译后,会在 constructor 后面执行:this.thresholds = []
private thresholds: QuantileOptions['domain'];

protected getDefaultOptions(): QuantileOptions {
return {
domain: [],
Expand Down
7 changes: 0 additions & 7 deletions src/scales/quantize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { d3LinearNice } from '../utils';
* 类似 Threshold 比例尺,区别在于 thresholds 是根据连续的 domain 根据离散的 range 的数量计算而得到的。
*/
export class Quantize extends Threshold<QuantizeOptions> {
// 这里不能给 thresholds 赋值,否者会编译后,会在 constructor 后面执行:this.thresholds = []
private thresholds: QuantizeOptions['domain'];

protected getDefaultOptions(): QuantizeOptions {
return {
domain: [0, 1],
Expand All @@ -24,10 +21,6 @@ export class Quantize extends Threshold<QuantizeOptions> {
super(options);
}

protected getDomain() {
return this.thresholds;
}

protected nice() {
const { nice } = this.options;
if (nice) {
Expand Down
11 changes: 5 additions & 6 deletions src/scales/threshold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export class Threshold<O extends ThresholdOptions> extends Base<O> {
/** threshold 的数量 */
protected n: number;

protected thresholds: number[];

protected getDefaultOptions() {
return {
domain: [0.5],
Expand All @@ -25,7 +27,7 @@ export class Threshold<O extends ThresholdOptions> extends Base<O> {
*/
public map(x: Domain<ThresholdOptions>) {
if (!isValid(x)) return this.options.unknown;
const index = bisect(this.getDomain(), x, 0, this.n);
const index = bisect(this.thresholds, x, 0, this.n);
return this.options.range[index];
}

Expand All @@ -35,7 +37,7 @@ export class Threshold<O extends ThresholdOptions> extends Base<O> {
public invert(y: Range<ThresholdOptions>) {
const { range } = this.options;
const index = range.indexOf(y);
const domain = this.getDomain();
const domain = this.thresholds;
return [domain[index - 1], domain[index]];
}

Expand All @@ -46,9 +48,6 @@ export class Threshold<O extends ThresholdOptions> extends Base<O> {
protected rescale() {
const { domain, range } = this.options;
this.n = Math.min(domain.length, range.length - 1);
}

protected getDomain() {
return this.options.domain;
this.thresholds = domain;
}
}

0 comments on commit 5e894d5

Please sign in to comment.