-
Notifications
You must be signed in to change notification settings - Fork 592
/
rect.test.ts
366 lines (332 loc) · 12.3 KB
/
rect.test.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import {PositionFieldDef} from '../../../src/channeldef';
import {rect} from '../../../src/compile/mark/rect';
import * as log from '../../../src/log';
import {parseUnitModelWithScaleAndLayoutSize} from '../../util';
describe('Mark: Rect', () => {
describe('simple with width and height', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: {type: 'rect', width: 50, height: 49},
encoding: {
x: {field: 'x', type: 'quantitative'},
y: {type: 'quantitative', field: 'y'}
}
});
const props = rect.encodeEntry(model);
it('should draw centered rect', () => {
expect(props.xc).toEqual({scale: 'x', field: 'x'});
expect(props.width).toEqual({value: 50});
expect(props.yc).toEqual({scale: 'y', field: 'y'});
expect(props.height).toEqual({value: 49});
});
});
describe('simple with width and height with left align and top baseline', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: {type: 'rect', width: 50, height: 49, align: 'left', baseline: 'top'},
encoding: {
x: {field: 'x', type: 'quantitative'},
y: {type: 'quantitative', field: 'y'}
}
});
const props = rect.encodeEntry(model);
it('should draw rect with proper alignment', () => {
expect(props.x).toEqual({scale: 'x', field: 'x'});
expect(props.width).toEqual({value: 50});
expect(props.y).toEqual({scale: 'y', field: 'y'});
expect(props.height).toEqual({value: 49});
});
});
it(
'should throw warning if align is expression',
log.wrap(localLogger => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: {type: 'rect', width: 50, height: 49, align: {expr: 'test'}, baseline: 'top'},
encoding: {
x: {field: 'x', type: 'quantitative'},
y: {type: 'quantitative', field: 'y'}
}
});
rect.encodeEntry(model);
expect(localLogger.warns[0]).toEqual(log.message.rangeMarkAlignmentCannotBeExpression('align'));
})
);
describe('simple with width and height with right align and bottom baseline', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: {type: 'rect', width: 50, height: 49, align: 'right', baseline: 'bottom'},
encoding: {
x: {field: 'x', type: 'quantitative'},
y: {type: 'quantitative', field: 'y'}
}
});
const props = rect.encodeEntry(model);
it('should draw rect with proper alignment', () => {
expect(props.x2).toEqual({scale: 'x', field: 'x'});
expect(props.width).toEqual({value: 50});
expect(props.y2).toEqual({scale: 'y', field: 'y'});
expect(props.height).toEqual({value: 49});
});
});
describe('simple with fixed x2 and y2', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: {type: 'rect', x2: -1, y2: -2},
encoding: {
x: {field: 'x', type: 'quantitative'},
y: {type: 'quantitative', field: 'y'}
}
});
const props = rect.encodeEntry(model);
it('should draw centered rect', () => {
expect(props.x).toEqual({scale: 'x', field: 'x'});
expect(props.x2).toEqual({value: -1});
expect(props.y).toEqual({scale: 'y', field: 'y'});
expect(props.y2).toEqual({value: -2});
});
});
describe('simple vertical', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: 'rect',
encoding: {
x: {field: 'Origin', type: 'nominal'},
y: {type: 'quantitative', field: 'Acceleration', aggregate: 'mean'}
}
});
const props = rect.encodeEntry(model);
it('should draw bar, with y from zero to field value and x band', () => {
expect(props.x).toEqual({scale: 'x', field: 'Origin'});
expect(props.width).toEqual({signal: `max(0.25, bandwidth('x'))`});
expect(props.y).toEqual({scale: 'y', field: 'mean_Acceleration'});
expect(props.y2).toEqual({scale: 'y', value: 0});
expect(props.height).toBeUndefined();
});
});
describe('simple vertical with signal-based band size', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: {type: 'rect', width: {expr: 'a'}},
encoding: {
x: {field: 'Origin', type: 'nominal'},
y: {type: 'quantitative', field: 'Acceleration', aggregate: 'mean'}
}
});
const props = rect.encodeEntry(model);
it('should draw bar, with y from zero to field value and x band', () => {
expect(props.xc).toEqual({scale: 'x', field: 'Origin', band: 0.5});
expect(props.width).toEqual({signal: 'a'});
expect(props.y).toEqual({scale: 'y', field: 'mean_Acceleration'});
expect(props.y2).toEqual({scale: 'y', value: 0});
expect(props.height).toBeUndefined();
});
});
describe('simple vertical 1D', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: 'rect',
encoding: {
y: {type: 'quantitative', field: 'Acceleration', aggregate: 'mean'}
}
});
const props = rect.encodeEntry(model);
it('should draw bar, with y from zero to field value and x band', () => {
expect(props).toMatchObject({
x: {field: {group: 'width'}},
x2: {value: 0},
y: {scale: 'y', field: 'mean_Acceleration'},
y2: {scale: 'y', value: 0}
});
});
});
describe('simple horizontal', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: 'rect',
encoding: {
y: {field: 'Origin', type: 'nominal'},
x: {aggregate: 'mean', field: 'Acceleration', type: 'quantitative'}
}
});
const props = rect.encodeEntry(model);
it('should draw bar from zero to field value and y band', () => {
expect(props.y).toEqual({scale: 'y', field: 'Origin'});
expect(props.height).toEqual({signal: `max(0.25, bandwidth('y'))`});
expect(props.x).toEqual({scale: 'x', field: 'mean_Acceleration'});
expect(props.x2).toEqual({scale: 'x', value: 0});
expect(props.width).toBeUndefined();
});
});
describe('simple horizontal with size field', () => {
it(
'should draw bar from zero to field value and with band value for x/width',
log.wrap(localLogger => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: 'rect',
encoding: {
y: {field: 'Origin', type: 'nominal'},
x: {aggregate: 'mean', field: 'Acceleration', type: 'quantitative'},
size: {aggregate: 'mean', field: 'Horsepower', type: 'quantitative'}
}
});
const props = rect.encodeEntry(model);
expect(props.y).toEqual({scale: 'y', field: 'Origin'});
expect(props.height).toEqual({signal: `max(0.25, bandwidth('y'))`});
expect(props.x).toEqual({scale: 'x', field: 'mean_Acceleration'});
expect(props.x2).toEqual({scale: 'x', value: 0});
expect(props.width).toBeUndefined();
expect(localLogger.warns[0]).toEqual(log.message.incompatibleChannel('size', 'rect'));
})
);
});
describe('horizontal bin', () => {
const y: PositionFieldDef<string> = {bin: true, field: 'Horsepower', type: 'quantitative'};
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: 'rect',
encoding: {
y,
x: {aggregate: 'mean', field: 'Acceleration', type: 'quantitative'}
}
});
const props = rect.encodeEntry(model);
it('should draw bar with y and y2', () => {
expect(props.y2).toEqual({scale: 'y', field: 'bin_maxbins_10_Horsepower', offset: 0.5});
expect(props.y).toEqual({scale: 'y', field: 'bin_maxbins_10_Horsepower_end', offset: 0.5});
expect(props.height).toBeUndefined();
});
});
describe('vertical bin', () => {
const x: PositionFieldDef<string> = {bin: true, field: 'Horsepower', type: 'quantitative'};
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: 'rect',
encoding: {
x,
y: {aggregate: 'mean', field: 'Acceleration', type: 'quantitative'}
}
});
const props = rect.encodeEntry(model);
it('should draw bar with x and x2', () => {
expect(props.x2).toEqual({scale: 'x', field: 'bin_maxbins_10_Horsepower', offset: 0.5});
expect(props.x).toEqual({scale: 'x', field: 'bin_maxbins_10_Horsepower_end', offset: 0.5});
expect(props.width).toBeUndefined();
});
});
describe('simple ranged', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: 'rect',
encoding: {
y: {field: 'hp1', type: 'quantitative'},
y2: {field: 'hp2'},
x: {field: 'origin1', type: 'ordinal'},
x2: {field: 'origin2'}
}
});
const props = rect.encodeEntry(model);
it('should draw rectangle with x, x2, y, y2', () => {
expect(props.x).toEqual({scale: 'x', field: 'origin1', band: 0.5});
expect(props.x2).toEqual({scale: 'x', field: 'origin2', band: 0.5});
expect(props.y).toEqual({scale: 'y', field: 'hp1'});
expect(props.y2).toEqual({scale: 'y', field: 'hp2'});
});
});
describe('simple heatmap', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: 'rect',
encoding: {
y: {field: 'Origin', type: 'ordinal'},
x: {field: 'Cylinders', type: 'ordinal'},
color: {aggregate: 'mean', field: 'Horsepower', type: 'quantitative'}
}
});
const props = rect.encodeEntry(model);
it('should draw rect with x and y bands', () => {
expect(props.x).toEqual({scale: 'x', field: 'Cylinders'});
expect(props.width).toEqual({signal: `max(0.25, bandwidth('x'))`});
expect(props.y).toEqual({scale: 'y', field: 'Origin'});
expect(props.height).toEqual({signal: `max(0.25, bandwidth('y'))`});
});
});
describe('vertical binned data', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
mark: 'bar',
encoding: {
x: {
field: 'bin_start',
bin: 'binned',
type: 'quantitative',
axis: {
tickMinStep: 2
}
},
x2: {
field: 'bin_end'
},
y: {
field: 'count',
type: 'quantitative'
}
}
});
const props = rect.encodeEntry(model);
it('should draw bar with x and x2', () => {
expect(props.x2).toEqual({scale: 'x', field: 'bin_start', offset: 1});
expect(props.x).toEqual({scale: 'x', field: 'bin_end'});
expect(props.y).toEqual({scale: 'y', field: 'count_end'});
expect(props.y2).toEqual({scale: 'y', field: 'count_start'});
expect(props.width).toBeUndefined();
});
});
describe('horizontal binned data', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
mark: 'bar',
encoding: {
y: {
field: 'bin_start',
bin: 'binned',
type: 'quantitative',
axis: {
tickMinStep: 2
}
},
y2: {
field: 'bin_end'
},
x: {
field: 'count',
type: 'quantitative'
}
}
});
const props = rect.encodeEntry(model);
it('should draw bar with y and y2', () => {
expect(props.y2).toEqual({scale: 'y', field: 'bin_start'});
expect(props.y).toEqual({scale: 'y', field: 'bin_end', offset: 1});
expect(props.x).toEqual({scale: 'x', field: 'count_end'});
expect(props.x2).toEqual({scale: 'x', field: 'count_start'});
expect(props.width).toBeUndefined();
});
});
describe('with size', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: {type: 'rect', width: 50, height: 49, size: 20},
encoding: {
x: {field: 'x', type: 'ordinal'},
y: {field: 'y', type: 'ordinal'}
}
});
it(
'should throw warning',
log.wrap(localLogger => {
rect.encodeEntry(model);
expect(localLogger.warns[0]).toEqual(log.message.cannotApplySizeToNonOrientedMark('rect'));
})
);
});
});