forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
72 lines (65 loc) · 2.17 KB
/
map.js
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
import {count, group, rank} from "d3";
import {maybeZ, take, valueof, maybeInput, column} from "../options.js";
import {basic} from "./basic.js";
export function mapX(m, options = {}) {
return map(Object.fromEntries(["x", "x1", "x2"]
.filter(key => options[key] != null)
.map(key => [key, m])), options);
}
export function mapY(m, options = {}) {
return map(Object.fromEntries(["y", "y1", "y2"]
.filter(key => options[key] != null)
.map(key => [key, m])), options);
}
export function map(outputs = {}, options = {}) {
const z = maybeZ(options);
const channels = Object.entries(outputs).map(([key, map]) => {
const input = maybeInput(key, options);
if (input == null) throw new Error(`missing channel: ${key}`);
const [output, setOutput] = column(input);
return {key, input, output, setOutput, map: maybeMap(map)};
});
return {
...basic(options, (data, facets) => {
const Z = valueof(data, z);
const X = channels.map(({input}) => valueof(data, input));
const MX = channels.map(({setOutput}) => setOutput(new Array(data.length)));
for (const facet of facets) {
for (const I of Z ? group(facet, i => Z[i]).values() : [facet]) {
channels.forEach(({map}, i) => map.map(I, X[i], MX[i]));
}
}
return {data, facets};
}),
...Object.fromEntries(channels.map(({key, output}) => [key, output]))
};
}
function maybeMap(map) {
if (map && typeof map.map === "function") return map;
if (typeof map === "function") return mapFunction(map);
switch (`${map}`.toLowerCase()) {
case "cumsum": return mapCumsum;
case "rank": return mapFunction(rank);
case "quantile": return mapFunction(rankQuantile);
}
throw new Error(`invalid map: ${map}`);
}
function rankQuantile(V) {
const n = count(V) - 1;
return rank(V).map(r => r / n);
}
function mapFunction(f) {
return {
map(I, S, T) {
const M = f(take(S, I));
if (M.length !== I.length) throw new Error("map function returned a mismatched length");
for (let i = 0, n = I.length; i < n; ++i) T[I[i]] = M[i];
}
};
}
const mapCumsum = {
map(I, S, T) {
let sum = 0;
for (const i of I) T[i] = sum += S[i];
}
};