Published
Edited
Sep 16, 2020
Insert cell
md`# Pattern image`
Insert cell
faceURL = await FileAttachment("face.jpg").url()
Insert cell
chart = {
let svg = d3
.create("svg")
.attr("width", svgWidth)
.attr("height", svgHeight);

let defs = svg.append("defs");
let pattern = defs
.append("pattern")
.attr("id", "face")
.attr("width", 1)
.attr("height", 1)
.attr("patternUnits", "objectBoundingBox")
.append("image")
.attr("xlink:href", faceURL)
.attr("x", 0)
.attr("y", 0)
.attr("width", 100)
.attr("height", 100);

let g = svg
.append("g")
.attr("transform", "translate(" + chartPadding + "," + chartPadding + ")");

let circle = g
.append("g")
.attr(
"transform",
"translate(" + chartWidth / 2 + "," + chartHeight / 2 + ")"
)
.append("circle")
.attr("cx", 0)
.attr("cy", 0)
.attr("r", 50)
.style("fill", `url(#face)`)
.style("stroke", "black")
.attr("transform-origin", "0% 0%")
.on("mouseover", function(d) {
d3.select(this)
.transition()
.attr("transform", "scale(2)");
})
.on("mouseout", function(d) {
d3.select(this)
.transition()
.attr("transform", "scale(1)");
});

return svg.node();
}
Insert cell
Insert cell
svgWidth = d3.min([width, 400])
Insert cell
svgHeight = svgWidth
Insert cell
chartPadding = 50
Insert cell
chartHeight = svgHeight - chartPadding - chartPadding
Insert cell
chartWidth = svgWidth - chartPadding - chartPadding
Insert cell
Insert cell
function roundValue(n) {
n = Math.round(n * 10) / 10;
return n;
}
Insert cell
Insert cell
Insert cell
Insert cell
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