forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.js
More file actions
63 lines (52 loc) · 1.48 KB
/
select.js
File metadata and controls
63 lines (52 loc) · 1.48 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
import {greatest, group, least} from "d3";
import {maybeTransform, maybeZ, valueof} from "../mark.js";
export function selectFirst(options) {
return select(first, undefined, options);
}
export function selectLast(options) {
return select(last, undefined, options);
}
export function selectMinX(options = {}) {
return select(min, options.x, options);
}
export function selectMinY(options = {}) {
return select(min, options.y, options);
}
export function selectMaxX(options = {}) {
return select(max, options.x, options);
}
export function selectMaxY(options = {}) {
return select(max, options.y, options);
}
// TODO If the value (for some required channel) is undefined, scan forward?
function* first(I) {
yield I[0];
}
// TODO If the value (for some required channel) is undefined, scan backward?
function* last(I) {
yield I[I.length - 1];
}
function* min(I, X) {
yield least(I, i => X[i]);
}
function* max(I, X) {
yield greatest(I, i => X[i]);
}
function select(selectIndex, v, options) {
const z = maybeZ(options);
return maybeTransform(options, (data, facets) => {
const Z = valueof(data, z);
const V = valueof(data, v);
const selectFacets = [];
for (const facet of facets) {
const selectFacet = [];
for (const I of Z ? group(facet, i => Z[i]).values() : [facet]) {
for (const i of selectIndex(I, V)) {
selectFacet.push(i);
}
}
selectFacets.push(selectFacet);
}
return {data, facets: selectFacets};
});
}