UNPKG

1.38 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Threshold = void 0;
4const base_1 = require("./base");
5const utils_1 = require("../utils");
6/**
7 * 将连续的定义域分段,每一段所有的值对应离散的值域中一个值
8 */
9class Threshold extends base_1.Base {
10 constructor(options) {
11 super(options);
12 }
13 getDefaultOptions() {
14 return {
15 domain: [0.5],
16 range: [0, 1],
17 };
18 }
19 /**
20 * 二分查找到输入值在哪一段,返回对应的值域中的值
21 */
22 map(x) {
23 if (!(0, utils_1.isValid)(x))
24 return this.options.unknown;
25 const index = (0, utils_1.bisect)(this.thresholds, x, 0, this.n);
26 return this.options.range[index];
27 }
28 /**
29 * 在值域中找到对应的值,并返回在定义域中属于哪一段
30 */
31 invert(y) {
32 const { range } = this.options;
33 const index = range.indexOf(y);
34 const domain = this.thresholds;
35 return [domain[index - 1], domain[index]];
36 }
37 clone() {
38 return new Threshold(this.options);
39 }
40 rescale() {
41 const { domain, range } = this.options;
42 this.n = Math.min(domain.length, range.length - 1);
43 this.thresholds = domain;
44 }
45}
46exports.Threshold = Threshold;
47//# sourceMappingURL=threshold.js.map
\No newline at end of file