UNPKG

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