in src/lib/d3_scales.ts [87:114]
var scale: any = function(x: number): number {
if (x == Infinity || x == -Infinity || isNaN(x)) {
return scaleOutput(-1);
}
if (x > values[domain_idx[domain_idx.length - 1]]) {
return scaleOutput(1.0);
} else if (x < values[domain_idx[0]]) {
return scaleOutput(0.0);
}
const upper = d3.bisectLeft(values, x, domain_idx[0], domain_idx[1]);
var pctile = (upper - domain_idx[0]) / (domain_idx[1] - domain_idx[0]);
if (values[upper] !== x && upper > domain_idx[0]) {
// For example when rendering axis ticks
const lower = upper - 1;
const lowerV = values[lower];
const upperV = values[upper];
console.assert(lowerV != upperV, "values should be distinct", lowerV, upperV);
console.assert(lowerV <= x && x <= upperV, `percentile_scale(${x}): lowerV=${lowerV}, x=${x}, upperV=${upperV}`, {
'x': x,
'values': values,
'lower': lower,
'domain_idx': domain_idx,
});
const a = (x - lowerV) / (upperV - lowerV);
pctile = (lower + a) / (domain_idx[1] - domain_idx[0]);
}
return scaleOutput(pctile);
};