Public
Edited
Aug 15, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
overlapDistance = (box1, box2, radius) => {
const bottomOfBox1 = box1.y + boxSize(box1) * radius;
const topOfBox2 = box2.y - boxSize(box2) * radius;
return bottomOfBox1 - topOfBox2;
}
Insert cell
boxSize = (box) => box.i1 - box.i0 + 1
Insert cell
// OcclusionY is the algorithm for label placement
// (The notebook from which this one is forked uses a force simulation)
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) {
const nodes = Array.from(index, (i) => ({
y: sy(Y[i]),
i
}));
nodes.sort((a, b) => a.y - b.y);
let boxes = [];
nodes.forEach((node, idx) => {
boxes.push({ y: node.y, i0: idx, i1: idx });
while (boxes.length > 1) {
let b = boxes[boxes.length - 2];
let c = boxes[boxes.length - 1];
let d = overlapDistance(b, c, RADIUS);
if (d < 0) break;
const sb = boxSize(b),
sc = boxSize(c);
b.y += sc * RADIUS - (d * sc) / (sb + sc);
b.i1 = c.i1;
boxes.pop();
}
});
for (const b of boxes) {
let y = b.y - RADIUS * (boxSize(b) - 1);
for (let idx = b.i0; idx <= b.i1; idx++) {
let node = nodes[idx];
Y[node.i] = y;
y += RADIUS * 2;
}
}
}
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