1 | import { __extends } from "tslib";
|
2 | import { each, head, last } from '@antv/util';
|
3 | import Continuous from './base';
|
4 |
|
5 |
|
6 |
|
7 | var Quantize = (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 |
|
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) {
|
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));
|
75 | export default Quantize;
|
76 |
|
\ | No newline at end of file |