Public
Edited
Jan 1, 2024
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const context = DOM.context2d(width, height);
context.translate(200, 200);
const circlesPerGroup = {}; // store in object with keys = the gorups
for (const group of groups) {
circlesPerGroup[group] = d3.packSiblings(
d3
.range(circles.filter((circle) => circle.group === group).length)
.map(() => ({
r: subgroupRadius + padding,
group: group
}))
);
}
const xOffset = 500; // how far right to move horizontally from initial position
const yOffsetPerGroup = 350; // this can also be defined with a scale if we have more groups
let movedCircles = [];
for (const [group, data] of Object.entries(circlesPerGroup)) {
const groupNumber = groups.indexOf(group);
data.forEach((circle, index) => {
movedCircles.push({
x: data[index].x + xOffset,
y: data[index].y + yOffsetPerGroup * groupNumber,
fill: circle.group === "NFL" ? "#FC803B" : "#1B81F7", // can be done with a colour scheme
group: circle.group
});
});
}

const tl = gsap.to(circles, {
x: (index, target, targets) => movedCircles[index].x,
y: (index, target, targets) => movedCircles[index].y,
fill: (index, target, targets) => movedCircles[index].fill,
duration: 1,
ease: "power.3.out",
stagger: { amount: 2 },
onUpdate: animate
});
tl.pause(); // pause to begin with; play if button is clicked

// this is what happens on update of the gsap animation
function animate() {
context.fillStyle = "white";
context.fillRect(-200, -200, width, height); // make sure to clear or fill the rect
for (const { x, y, r, fill } of circles) {
context.beginPath();
context.arc(x, y, r - padding, 0, 2 * Math.PI);
context.fillStyle = fill;
context.fill();
}
}

// run the drawing logic once just so that we can see the initial circle packing
animate();
tl.play();
return context.canvas;
}
Insert cell
gsap = gs.gsap
Insert cell
Insert cell
gs = require("https://cdnjs.cloudflare.com/ajax/libs/gsap/3.6.1/gsap.min.js")
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