forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockDataBarGridModel.ts
More file actions
169 lines (140 loc) · 4.46 KB
/
MockDataBarGridModel.ts
File metadata and controls
169 lines (140 loc) · 4.46 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* eslint-disable class-methods-use-this */
import { getOrThrow } from '@deephaven/utils';
import { type CellRenderType } from './CellRenderer';
import {
type AxisOption,
type ColorMap,
type ColumnAxisMap,
type DataBarGridModel,
type DataBarOptions,
type DirectionMap,
type MarkerMap,
type MaxMap,
type MinMap,
type OpacityMap,
type TextAlignmentMap,
type ValuePlacementMap,
} from './DataBarGridModel';
import { type ModelIndex } from './GridMetrics';
import GridModel from './GridModel';
import GridTheme from './GridTheme';
const DEFAULT_AXIS: AxisOption = 'proportional';
const DEFAULT_POSITIVE_COLOR = GridTheme.positiveBarColor;
const DEFAULT_NEGATIVE_COLOR = GridTheme.negativeBarColor;
const DEFAULT_VALUE_PLACEMENT = 'beside';
const DEFAULT_DIRECTION = 'LTR';
const DEFAULT_TEXT_ALIGNMENT = 'right';
function isArrayOfNumbers(value: unknown): value is number[] {
return Array.isArray(value) && value.every(item => typeof item === 'number');
}
class MockDataBarGridModel extends GridModel implements DataBarGridModel {
private numberOfColumns;
private numberOfRows;
private data: unknown[][];
columnMins: MinMap;
columnMaxs: MaxMap;
columnAxes: ColumnAxisMap;
valuePlacements: ValuePlacementMap;
directions: DirectionMap;
positiveColors: ColorMap;
negativeColors: ColorMap;
// Opacities should be between 0 and 1
opacities: OpacityMap;
textAlignments: TextAlignmentMap;
markers: MarkerMap;
constructor(
data: unknown[][],
columnAxes = new Map(),
positiveColors = new Map(),
negativeColors = new Map(),
valuePlacements = new Map(),
opacities = new Map(),
directions = new Map(),
textAlignments = new Map(),
markers: MarkerMap = new Map()
) {
super();
this.positiveColors = positiveColors;
this.negativeColors = negativeColors;
this.data = data;
this.columnAxes = columnAxes;
this.valuePlacements = valuePlacements;
this.opacities = opacities;
this.directions = directions;
this.textAlignments = textAlignments;
this.markers = markers;
this.numberOfRows = Math.max(...data.map(row => row.length));
this.numberOfColumns = data.length;
this.columnMins = new Map();
this.columnMaxs = new Map();
for (let i = 0; i < data.length; i += 1) {
const column = data[i];
if (isArrayOfNumbers(column)) {
this.columnMins.set(i, Math.min(...column));
this.columnMaxs.set(i, Math.max(...column));
}
}
}
get rowCount(): number {
return this.numberOfRows;
}
get columnCount(): number {
return this.numberOfColumns;
}
textForCell(column: number, row: number): string {
return `${this.data[column]?.[row]}`;
}
textForColumnHeader(column: number): string {
return `${column}`;
}
textAlignForCell(column: number, row: number): CanvasTextAlign {
return this.textAlignments.get(column) ?? DEFAULT_TEXT_ALIGNMENT;
}
renderTypeForCell(column: ModelIndex, row: ModelIndex): CellRenderType {
if (column < 20) {
return 'dataBar';
}
return column % 2 === row % 2 ? 'dataBar' : 'text';
}
dataBarOptionsForCell(column: ModelIndex, row: ModelIndex): DataBarOptions {
const columnMin = getOrThrow(this.columnMins, column);
const columnMax = getOrThrow(this.columnMaxs, column);
const axis = this.columnAxes.get(column) ?? DEFAULT_AXIS;
const valuePlacement =
this.valuePlacements.get(column) ?? DEFAULT_VALUE_PLACEMENT;
let opacity = this.opacities.get(column);
if (opacity == null || opacity > 1) {
opacity = 1;
} else if (opacity < 0) {
opacity = 0;
}
const direction = this.directions.get(column) ?? DEFAULT_DIRECTION;
const positiveColor =
this.positiveColors.get(column) ?? DEFAULT_POSITIVE_COLOR;
const negativeColor =
this.negativeColors.get(column) ?? DEFAULT_NEGATIVE_COLOR;
const value = Number(this.data[column]?.[row]);
const color = value >= 0 ? positiveColor : negativeColor;
const markers = this.markers.get(column) ?? [];
const hasGradient = Array.isArray(color) && color.length > 1;
let textColor: string;
if (hasGradient) {
textColor = value >= 0 ? color[color.length - 1] : color[0];
} else {
textColor = Array.isArray(color) ? color[0] : color;
}
return {
columnMin,
columnMax,
axis,
color,
textColor,
valuePlacement,
opacity,
markers,
direction,
value,
};
}
}
export default MockDataBarGridModel;