Public
Edited
Feb 19, 2024
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function compactBeeswarm(data, { diameter = 1, x = d => d } = {}) {
const diameter2 = diameter ** 2;

// For placed circles, circle.y is the y position; for unplaced
// circles, circle.y is the lowest currently-permitted y position.
const circles = data
.map((d, i, data) => ({ x: +x(d, i, data), placed: false, y: 0, data: d }))
.sort((a, b) => a.x - b.x);

// next() returns the point that is to be placed next
function next() {
let best;
for (let c of circles) {
if (c.placed) continue;
if (!best || c.y < best.y) best = c;
}
return best;
}

// updateYValues(circle) updates the y values of unplaced points
// that horizontally overlap circle.
function updateYValues(circle) {
for (let c of circles) {
if (c.placed) continue;
let xDiff = Math.abs(circle.x - c.x);
if (xDiff >= diameter) continue;
let yDiff = Math.sqrt(diameter2 - xDiff ** 2);
c.y = Math.max(c.y, circle.y + yDiff);
}
}

let time = 0;

for (let i = 0; i < circles.length; i++) {
let c = next();
c.placed = true;
c.insertionTime = ++time;
updateYValues(c);
}

return circles;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more