1 | import { indexOf, isNil, isNumber } from '@antv/util';
|
2 | import Base from '../base';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | class Category extends Base {
|
9 | public readonly type: string = 'cat';
|
10 | public readonly isCategory: boolean = true;
|
11 |
|
12 | public translate(value: any): number {
|
13 | const index = indexOf(this.values, value);
|
14 | if (index === -1) {
|
15 | return isNumber(value) ? value : NaN;
|
16 | }
|
17 | return index;
|
18 | }
|
19 |
|
20 | public scale(value: any): number {
|
21 | const order = this.translate(value);
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const percent = this.calcPercent(order, this.min, this.max);
|
27 | return this.calcValue(percent, this.rangeMin(), this.rangeMax());
|
28 | }
|
29 |
|
30 | public invert(scaledValue: number) {
|
31 | const domainRange = this.max - this.min;
|
32 | const percent = this.calcPercent(scaledValue, this.rangeMin(), this.rangeMax());
|
33 | const idx = Math.round(domainRange * percent) + this.min;
|
34 | if (idx < this.min || idx > this.max) {
|
35 | return NaN;
|
36 | }
|
37 | return this.values[idx];
|
38 | }
|
39 |
|
40 | public getText(value: any, ...args: any[]): string {
|
41 | let v = value;
|
42 |
|
43 | if (isNumber(value) && !this.values.includes(value)) {
|
44 | v = this.values[v];
|
45 | }
|
46 | return super.getText(v, ...args);
|
47 | }
|
48 |
|
49 | protected initCfg() {
|
50 | this.tickMethod = 'cat';
|
51 | }
|
52 |
|
53 | protected setDomain() {
|
54 |
|
55 | if (isNil(this.getConfig('min'))) {
|
56 | this.min = 0;
|
57 | }
|
58 | if (isNil(this.getConfig('max'))) {
|
59 | const size = this.values.length;
|
60 | this.max = size > 1 ? size - 1 : size;
|
61 | }
|
62 | }
|
63 | }
|
64 |
|
65 | export default Category;
|