1 | import { each, isNumber } from '@antv/util';
|
2 | import { timeFormat, toTimeStamp } from '../util/time';
|
3 | import Category from './base';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | class TimeCat extends Category {
|
10 | public readonly type: string = 'timeCat';
|
11 | public mask;
|
12 | |
13 |
|
14 |
|
15 | public translate(value) {
|
16 | value = toTimeStamp(value);
|
17 | let index = this.values.indexOf(value);
|
18 | if (index === -1) {
|
19 | if (isNumber(value) && value < this.values.length) {
|
20 | index = value;
|
21 | } else {
|
22 | index = NaN;
|
23 | }
|
24 | }
|
25 | return index;
|
26 | }
|
27 |
|
28 | |
29 |
|
30 |
|
31 |
|
32 | public getText(value: string | number, tickIndex?: number) {
|
33 | const index = this.translate(value);
|
34 | if (index > -1) {
|
35 | let result = this.values[index];
|
36 | const formatter = this.formatter;
|
37 | result = formatter ? formatter(result, tickIndex) : timeFormat(result, this.mask);
|
38 | return result;
|
39 | }
|
40 | return value;
|
41 | }
|
42 | protected initCfg() {
|
43 | this.tickMethod = 'time-cat';
|
44 | this.mask = 'YYYY-MM-DD';
|
45 | this.tickCount = 7;
|
46 | }
|
47 |
|
48 | protected setDomain() {
|
49 | const values = this.values;
|
50 |
|
51 | each(values, (v, i) => {
|
52 | values[i] = toTimeStamp(v);
|
53 | });
|
54 | values.sort((v1, v2) => {
|
55 | return v1 - v2;
|
56 | });
|
57 | super.setDomain();
|
58 | }
|
59 | }
|
60 |
|
61 | export default TimeCat;
|