UNPKG

1.5 kBPlain TextView Raw
1import { each, isNumber } from '@antv/util';
2import { timeFormat, toTimeStamp } from '../util/time';
3import Category from './base';
4
5/**
6 * 时间分类度量
7 * @class
8 */
9class TimeCat extends Category {
10 public readonly type: string = 'timeCat';
11 public mask;
12 /**
13 * @override
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 * 由于时间类型数据需要转换一下,所以复写 getText
30 * @override
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; // 一般时间数据会显示 7, 14, 30 天的数字
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
61export default TimeCat;