Public
Edited
Jan 25, 2023
Insert cell
Insert cell
{
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

// Append clipping path circles
svg
.selectAll('clipPath')
.data(students)
.join("clipPath")
.attr("id", d =>
d.name
.toLowerCase()
.split(' ')
.join('_')
)
.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(#${d.name
.toLowerCase()
.split(' ')
.join('_')})`
)
.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="https://ca.slack-edge.com/T0WA5NWKG-U02LH3WM421-f5cb959ac1ca-48"
height="100" width="100" clip-path="url(#clipCircle1)"/>

<clipPath id="clipCircle2">
<circle r="50" cx="200" cy="100"/>
</clipPath>
<image href="https://ca.slack-edge.com/T0WA5NWKG-U01M7D3NCP5-a3dd87748d6b-512"
x="150" y="50" height="100" width="100" clip-path="url(#clipCircle2)"/>
</svg>`
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(".domain").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
yAxis = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g =>
g
.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text("Variable 2")
)
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