1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var util_1 = require("@antv/util");
|
5 | var base_1 = require("./base");
|
6 |
|
7 |
|
8 |
|
9 | var Quantize = (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 |
|
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) {
|
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));
|
77 | exports.default = Quantize;
|
78 |
|
\ | No newline at end of file |