UNPKG

2.18 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Quantize = void 0;
4const threshold_1 = require("./threshold");
5const wilkinson_extended_1 = require("../tick-methods/wilkinson-extended");
6const utils_1 = require("../utils");
7/**
8 * 类似 Threshold 比例尺,区别在于 thresholds 是根据连续的 domain 根据离散的 range 的数量计算而得到的。
9 */
10class Quantize extends threshold_1.Threshold {
11 getDefaultOptions() {
12 return {
13 domain: [0, 1],
14 range: [0.5],
15 nice: false,
16 tickCount: 5,
17 tickMethod: wilkinson_extended_1.wilkinsonExtended,
18 };
19 }
20 constructor(options) {
21 super(options);
22 }
23 nice() {
24 const { nice } = this.options;
25 if (nice) {
26 const [min, max, tickCount] = this.getTickMethodOptions();
27 this.options.domain = (0, utils_1.d3LinearNice)(min, max, tickCount);
28 }
29 }
30 getTicks() {
31 const { tickMethod } = this.options;
32 const [min, max, tickCount] = this.getTickMethodOptions();
33 return tickMethod(min, max, tickCount);
34 }
35 getTickMethodOptions() {
36 const { domain, tickCount } = this.options;
37 const min = domain[0];
38 const max = domain[domain.length - 1];
39 return [min, max, tickCount];
40 }
41 rescale() {
42 this.nice();
43 const { range, domain } = this.options;
44 const [x0, x1] = domain;
45 this.n = range.length - 1;
46 this.thresholds = new Array(this.n);
47 for (let i = 0; i < this.n; i += 1) {
48 this.thresholds[i] = ((i + 1) * x1 - (i - this.n) * x0) / (this.n + 1);
49 }
50 }
51 /**
52 * 如果是在第一段后或者最后一段就把两端的值添加上
53 */
54 invert(y) {
55 const [a, b] = super.invert(y);
56 const [x0, x1] = this.options.domain;
57 return a === undefined && b === undefined ? [a, b] : [a || x0, b || x1];
58 }
59 getThresholds() {
60 return this.thresholds;
61 }
62 clone() {
63 return new Quantize(this.options);
64 }
65}
66exports.Quantize = Quantize;
67//# sourceMappingURL=quantize.js.map
\No newline at end of file