Skip to content

Commit

Permalink
cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jan 3, 2021
1 parent 71bd65d commit 075fa75
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions src/transforms/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,28 @@ import {valueof, first, second, maybeValue, range, offsetRange} from "../mark.js
export function bin1(options = {}) {
let {value, domain, thresholds, cumulative} = maybeValue(options);
const bin = binof({value, domain, thresholds});
return (data, facets) => {
let bins = bin(data);
if (facets !== undefined) return binfacets(bins, facets, subset1, cumulative);
if (cumulative) bins = accumulate(cumulative < 0 ? bins.reverse() : bins);
bins = bins.filter(nonempty);
return {index: range(bins), data: bins};
};
return (data, facets) => rebin(bin(data), facets, subset1, cumulative);
}

export function bin2({x = {}, y = {}, domain, thresholds} = {}) {
const binX = binof({domain, thresholds, value: first, ...maybeValue(x)});
const binY = binof({domain, thresholds, value: second, ...maybeValue(y)});
return (data, facets) => {
let bins = cross(binX(data).filter(nonempty), binY(data).filter(nonempty).map(binset), (x, y) => {
const subbin = x.filter(i => y.has(i));
subbin.x0 = x.x0;
subbin.x1 = x.x1;
subbin.y0 = y.x0;
subbin.y1 = y.x1;
return subbin;
});
if (facets !== undefined) return binfacets(bins, facets, subset2);
bins = bins.filter(nonempty);
return {index: range(bins), data: bins};
};
return (data, facets) => rebin(
cross(
binX(data).filter(nonempty),
binY(data).filter(nonempty).map(binset),
(x, y) => {
const subbin = x.filter(i => y.has(i));
subbin.x0 = x.x0;
subbin.x1 = x.x1;
subbin.y0 = y.x0;
subbin.y1 = y.x1;
return subbin;
}
),
facets,
subset2
);
}

function binof({value, domain, thresholds}) {
Expand All @@ -41,13 +38,19 @@ function binof({value, domain, thresholds}) {
};
}

function binfacets(bins, facets, subset, cumulative) {
// When faceting, subdivides the given bins according to the facet indexes.
function rebin(bins, facets, subset, cumulative) {
if (facets === undefined) {
if (cumulative) bins = accumulate(cumulative < 0 ? bins.reverse() : bins);
bins = bins.filter(nonempty);
return {index: range(bins), data: bins};
}
const index = [];
const data = [];
let k = 0;
for (const facet of facets.map(subset)) {
let b = bins.map(facet);
b = cumulative ? accumulate(cumulative < 0 ? b.reverse() : b) : b;
if (cumulative) b = accumulate(cumulative < 0 ? b.reverse() : b);
b = b.filter(nonempty);
index.push(offsetRange(b, k));
data.push(b);
Expand Down

0 comments on commit 075fa75

Please sign in to comment.