Public
Edited
Dec 21, 2023
Insert cell
Insert cell
Insert cell
Insert cell
{
let xs = [];
let top = 100;
for (let i = 0; i < top; i++) {
xs.push(i);
}
for (let i = 0; i < top; i++) {
xs.push(top - i);
}
let start = performance.now();
let result = f(xs);
return result; //performance.now() - start;
}
Insert cell
f([3, 2, 3, 4, 5, 4])
Insert cell
f = (zs) => {
let batches = [];
for (let i = 0; i < zs.length; i++) {
let z = zs[i];
batches.push({ size: 1, mean: z });
while (batches.length > 1) {
let b = batches[batches.length - 2];
let c = batches[batches.length - 1];
if (b.mean < c.mean) break;
b.mean = (b.mean * b.size + c.mean * c.size) / (b.size + c.size);
b.size = b.size + c.size;
batches.pop();
}
}
let xs = [];
for (const batch of batches)
for (let i = 0; i < batch.size; i++) xs.push(batch.mean);
return xs;
}
Insert cell
Insert cell
g = (ys, radius) => {
let zs = ys.map((y, i) => y - i * radius * 2);
let xs = f(zs);
return xs.map((x, i) => x + i * radius * 2);
}
Insert cell
occlusionY = (options) =>
Plot.initializer(
options,
(
data,
facets,
{ y: { value: Y }, text: { value: T } },
{ y: sy },
dimensions,
context
) => {
let RADIUS = 5.5;
for (const index of facets) {
// Sort the nodes and call the label placement function g
const nodes = Array.from(index, (i) => ({
y: sy(Y[i]),
i
}));
nodes.sort((a, b) => a.y - b.y);
const positions = g(
nodes.map((d) => d.y),
RADIUS
);
positions.forEach((p, i) => {
Y[nodes[i].i] = p;
});
}
return { data, facets, channels: { y: { value: Y } } };
}
)
Insert cell
receipts = FileAttachment("gdp-receipts.csv").csv({ typed: true })
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more