Public
Edited
Nov 14, 2023
4 forks
Insert cell
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