UNPKG

2.61 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var util_1 = require("@antv/util");
5var base_1 = require("./base");
6/**
7 * 分段度量
8 */
9var Quantize = /** @class */ (function (_super) {
10 tslib_1.__extends(Quantize, _super);
11 function Quantize() {
12 var _this = _super !== null && _super.apply(this, arguments) || this;
13 _this.type = 'quantize';
14 return _this;
15 }
16 Quantize.prototype.invert = function (value) {
17 var ticks = this.ticks;
18 var length = ticks.length;
19 var percent = this.getInvertPercent(value);
20 var minIndex = Math.floor(percent * (length - 1));
21 // 最后一个
22 if (minIndex >= length - 1) {
23 return util_1.last(ticks);
24 }
25 // 超出左边界, 则取第一个
26 if (minIndex < 0) {
27 return util_1.head(ticks);
28 }
29 var minTick = ticks[minIndex];
30 var nextTick = ticks[minIndex + 1];
31 // 比当前值小的 tick 在度量上的占比
32 var minIndexPercent = minIndex / (length - 1);
33 var maxIndexPercent = (minIndex + 1) / (length - 1);
34 return minTick + (percent - minIndexPercent) / (maxIndexPercent - minIndexPercent) * (nextTick - minTick);
35 };
36 Quantize.prototype.initCfg = function () {
37 this.tickMethod = 'r-pretty';
38 this.tickCount = 5;
39 this.nice = true;
40 };
41 Quantize.prototype.calculateTicks = function () {
42 var ticks = _super.prototype.calculateTicks.call(this);
43 if (!this.nice) { // 如果 nice = false ,补充 min, max
44 if (util_1.last(ticks) !== this.max) {
45 ticks.push(this.max);
46 }
47 if (util_1.head(ticks) !== this.min) {
48 ticks.unshift(this.min);
49 }
50 }
51 return ticks;
52 };
53 // 计算当前值在刻度中的占比
54 Quantize.prototype.getScalePercent = function (value) {
55 var ticks = this.ticks;
56 // 超出左边界
57 if (value < util_1.head(ticks)) {
58 return 0;
59 }
60 // 超出右边界
61 if (value > util_1.last(ticks)) {
62 return 1;
63 }
64 var minIndex = 0;
65 util_1.each(ticks, function (tick, index) {
66 if (value >= tick) {
67 minIndex = index;
68 }
69 else {
70 return false;
71 }
72 });
73 return minIndex / (ticks.length - 1);
74 };
75 return Quantize;
76}(base_1.default));
77exports.default = Quantize;
78//# sourceMappingURL=quantize.js.map
\No newline at end of file