Public
Edited
Nov 6, 2024
Insert cell
Insert cell
scrollama = require("scrollama")
Insert cell
/* global d3, scrollama */
{
const width = 400,
height = 40p0,
margin = { top: 5, right: 5, bottom: 5, left: 5 },
side = 30;
const students = [
{
name: "Adithi",
img: "https://ca.slack-edge.com/T0WA5NWKG-U05Q6NBPU4R-b3196d8d3fcd-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Aimee",
img: "https://ca.slack-edge.com/T0WA5NWKG-U067V9B20GM-7082a64518b0-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Chloe",
img: "https://ca.slack-edge.com/T0WA5NWKG-U05EASBCY66-03459d5ab8c0-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Dan",
img: "https://ca.slack-edge.com/T0WA5NWKG-U052DATFX38-5c53b05cb8fa-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Eric",
img: "https://ca.slack-edge.com/T0WA5NWKG-U06832BDQQ1-g7ce1229c2f5-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Eun-Hae",
img: "https://ca.slack-edge.com/T0WA5NWKG-U064Y7T9TSB-3f2a30e3dec7-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Gina",
img: "https://ca.slack-edge.com/T0WA5NWKG-U05P7ELB5JT-234ebd6606ce-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Hyung",
img: "https://ca.slack-edge.com/T0WA5NWKG-U0670A26K9N-8e04cc130e00-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Indu",
img: "https://ca.slack-edge.com/T0WA5NWKG-U051DPE4M6J-57554b5b4667-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Jae",
img: "https://ca.slack-edge.com/T0WA5NWKG-U05GB5K23HR-cfce9c7d625f-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Jesse",
img: "https://ca.slack-edge.com/T0WA5NWKG-U06A19FTUE4-7756fc6b8a81-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Joshua",
img: "https://ca.slack-edge.com/T0WA5NWKG-U053JE2TRNG-fd164ed7389a-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Justin",
img: "https://ca.slack-edge.com/T0WA5NWKG-U0686D72Q56-bb3952bc0528-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Mohamed",
img: "https://ca.slack-edge.com/T0WA5NWKG-U05K82D9ALC-c1f938d66e6b-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Ryan",
img: "https://ca.slack-edge.com/T0WA5NWKG-U05QHMAFZ4Z-db8e37c6e608-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Subhasis",
img: "https://ca.slack-edge.com/T0WA5NWKG-U06BYJXL7V1-b93ba674e9dc-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Yifan",
img: "https://ca.slack-edge.com/T0WA5NWKG-U07CUQDE6PP-a0b482e70d25-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
},
{
name: "Zakaria",
img: "https://ca.slack-edge.com/T0WA5NWKG-U05385DR3QW-e9423c222785-72",
var1: Math.random() * 150,
var2: Math.random() * 2.0
}
];

const images = d3
.select("svg")
.append("g")
.selectAll("image")
.data(students)
.join("image")
.attr("href", (d) => d.img)
.attr("x", (d) => width / 2 - 15)
.attr("y", (d) => height / 2 - 15)
.attr("width", side)
.attr("height", side)
.attr("preserveAspectRatio", "xMidYMin slice");

function moveLeft() {
images.transition().duration(750).attr("x", 20);
}

function moveX() {
images
.transition()
.duration(750)
.attr(
"x",
(d) => Math.random() * (width - margin.left - margin.right - side / 2)
);
}

function moveY() {
images
.transition()
.duration(750)
.attr(
"y",
(d) => Math.random() * (height - margin.top - margin.bottom - side / 2)
);
}

//window.addEventListener("scroll", function (e) {
// console.log(window.scrollY)
//})

const callbacks = [
moveLeft,
moveX,
moveY,
moveLeft,
moveX,
moveY,
moveLeft,
moveX,
moveY,
moveLeft,
moveX
];

const steps = d3.selectAll(".step");

// instantiate the scrollama
const scroller = scrollama();

// setup the instance, pass callback functions
scroller
.setup({
step: ".step"
})
.onStepEnter((response) => {
// { element, index, direction }
callbacks[response.index]();

steps.style("opacity", 0.1);
d3.select(response.element).style("opacity", 1.0);

console.log("enter", response);
})
.onStepExit((response) => {
// { element, index, direction }
console.log("exit", response);
});

// setup resize event
window.addEventListener("resize", scroller.resize);
}
Insert cell
{
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

const lastname = function(d) {
const split = d.toLowerCase().split(' ');
return split[split.length - 1]
}
// Append clipping path circles
svg
.selectAll("anything")
.data(students)
.enter()
.append("clipPath")
.attr("id", d => lastname(d.name))
.append("circle")
.attr("id", d => d.name)
.attr("r", 20)
.attr("cx", d => x(d.var1))
.attr("cy", d => y(d.var2));

// Append images
svg
.append("g")
.selectAll('image')
.data(students)
.join('image')
.attr('href', d => d.img)
.attr("x", d => x(d.var1) - 25)
.attr('y', d => y(d.var2) - 25)
.attr('width', 50)
.attr('height', 50)
.attr(
'clip-path',
d =>
`url(#${lastname(d.name)})`
)
.attr('preserveAspectRatio', "xMidYMin slice");

// Append students' first names
svg
.append("g")
.selectAll("text")
.data(students)
.join("text")
.attr("dy", "0.35em")
.attr("x", d => x(d.var1))
.attr("y", d => y(d.var2) - 28)
.attr("font-family", "sans-serif")
.attr("font-size", 12)
.attr("font-weight", "bold")
.attr("text-anchor", "middle")
.text(d => d.name.split(' ')[0]);

svg.append("g").call(xAxis);

svg.append("g").call(yAxis);

return svg.node();
}
Insert cell
html`
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
<clipPath id="clipCircle1">
<circle r="50" cx="50" cy="50"/>
</clipPath>
<image href="${students[0].img}"
height="100" width="100" clip-path="url(#clipCircle1)"/>

<clipPath id="clipCircle2">
<circle r="50" cx="200" cy="100"/>
</clipPath>
<image href="${students[1].img}"
x="150" y="50" height="100" width="100" clip-path="url(#clipCircle2)"/>
</svg>`
Insert cell
{ const svg = d3.create("svg");
svg.append("g")
return svg.node()
}
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

svg
.append("g")
.selectAll('image')
.data(students)
.join('image')
.attr('href', d => d.img)
.attr("x", d => x(d.var1) - 17)
.attr('y', d => y(d.var2))
.attr('width', 35)
.attr('height', 35);
//.attr("stroke", "steelblue")
//.attr("stroke-width", 1.5)
//.attr("fill", "none")
//.selectAll("circle")
//.data(students)
//.join("circle")
//.attr("cx", d => x(d.var1))
//.attr("cy", d => y(d.var2))
//.attr("r", 3);

svg
.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("text")
.data(students)
.join("text")
.attr("dy", "0.35em")
.attr("x", d => x(d.var1))
.attr("y", d => y(d.var2) - 10)
.attr("text-anchor", "middle")
.text(d => d.name.split(' ')[0]);

svg.append("g").call(xAxis);

svg.append("g").call(yAxis);

return svg.node();
}
Insert cell
x = d3
.scaleLinear()
.domain(d3.extent(students, d => d.var1))
.nice()
.range([margin.left, width - margin.right])
Insert cell
y = d3
.scaleLinear()
.domain(d3.extent(students, d => d.var2))
.nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x)) //.ticks(width / 80))
.call(g => g.select("path").remove())
.call(g =>
g
.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text("Variable 1")
)
Insert cell
d3.axisLeft(y)
Insert cell
yAxis = function(g) {
g
.attr("transform", `translate(${margin.left},0)`);
const axisLeftFunction = d3.axisLeft(y);
axisLeftFunction(g);
// g.select(".domain").remove();
g
.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text("Variable 2 Hi")
}
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