/
ICategoricalColumn.ts
111 lines (88 loc) · 3.4 KB
/
ICategoricalColumn.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import type { IForEachAble } from '../internal';
import Column from './Column';
import { type IArrayColumn, isArrayColumn } from './IArrayColumn';
import type { IValueColumnDesc, IColumnDesc, IDataRow, ITypedDump } from './interfaces';
export interface ICategoricalDesc {
categories?: (string | Partial<ICategory>)[];
/**
* @default given or name if not provided
*/
categoryOrder?: 'given' | 'small-to-large' | 'large-to-small' | ((categories: readonly ICategory[]) => ICategory[]);
}
export declare type ICategoricalColumnDesc = IValueColumnDesc<string> & ICategoricalDesc;
export interface ICategoricalColorMappingFunction {
apply(v: ICategory): string;
toJSON(): ITypedDump | null;
clone(): ICategoricalColorMappingFunction;
eq(other: ICategoricalColorMappingFunction): boolean;
}
export interface ICategoricalColorMappingFunctionConstructor {
new (dump: ITypedDump): ICategoricalColorMappingFunction;
}
export interface ICategoricalLikeColumn extends Column {
readonly categories: ICategory[];
getColorMapping(): ICategoricalColorMappingFunction;
setColorMapping(mapping: ICategoricalColorMappingFunction): void;
iterCategory(row: IDataRow): IForEachAble<ICategory | null>;
getCategories(row: IDataRow): (ICategory | null)[];
}
export function isCategoricalLikeColumn(col: Column): col is ICategoricalLikeColumn {
return (
typeof (col as ICategoricalLikeColumn).categories !== 'undefined' &&
typeof (col as ICategoricalLikeColumn).iterCategory === 'function'
);
}
export interface ISetColumn extends IArrayColumn<boolean>, ICategoricalLikeColumn {
getSet(row: IDataRow): Set<ICategory>;
}
export function isSetColumn(col: Column): col is ISetColumn {
return isCategoricalLikeColumn(col) && typeof (col as ISetColumn).getSet === 'function';
}
export interface ICategoricalColumn extends ISetColumn {
getCategory(row: IDataRow): ICategory | null;
}
export interface ICategory {
readonly name: string;
/**
* optional label of this category (the one to render)
*/
readonly label: string;
/**
* category color
* @default next in d3 color 10 range
*/
readonly color: string;
value: number;
}
/**
* checks whether the given column or description is a categorical column, i.e. the value is a list of categories
* @param col
* @returns {boolean}
*/
export function isCategoricalColumn(col: Column): col is ICategoricalColumn;
export function isCategoricalColumn(col: IColumnDesc): col is ICategoricalColumnDesc & IColumnDesc;
export function isCategoricalColumn(col: Column | IColumnDesc) {
return (
(col instanceof Column && typeof (col as ICategoricalColumn).getCategory === 'function') ||
(!(col instanceof Column) && (col as IColumnDesc).type.match(/(categorical|ordinal|hierarchy)/) != null)
);
}
export declare type ICategoricalsColumn = ICategoricalLikeColumn & IArrayColumn<string | null>;
export function isCategoricalsColumn(col: Column): col is ICategoricalsColumn {
return isCategoricalLikeColumn(col) && isArrayColumn(col) && !isSetColumn(col);
}
export interface ICategoricalFilter {
filter: string[] | string | RegExp;
filterMissing: boolean;
}
export interface ISetCategoricalFilter extends ICategoricalFilter {
mode?: 'every' | 'some';
}
export function isCategory(v: any): v is ICategory {
return (
typeof v.name === 'string' &&
typeof v.label === 'string' &&
typeof v.color === 'string' &&
typeof v.value === 'number'
);
}