Published
Edited
Aug 23, 2020
Fork of Zoom (SVG)
2 forks
19 stars
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const g = svg.append("g")
.attr("class", "circles");

g.append("style").text(`
.circles {
stroke: transparent;
stroke-width: 1.5px;
}
.circles circle:hover {
stroke: black;
}
`);

g.selectAll("circle")
.data(data)
.join("circle")
.datum(([x, y], i) => [x, y, i])
.attr("cx", ([x]) => x)
.attr("cy", ([, y]) => y)
.attr("r", radius)
.attr("fill", ([,, i]) => d3.interpolateRainbow(i / 360))
.on("mousedown", mousedowned)
.append("title")
.text((d, i) => `circle ${i}`);

svg.call(d3.zoom()
.extent([[0, 0], [width, height]])
.scaleExtent([1, 8])
.on("zoom", zoomed));

function mousedowned(event, [,, i]) {
d3.select(this).transition()
.attr("fill", "black")
.attr("r", radius * 2)
.transition()
.attr("fill", d3.interpolateRainbow(i / 360))
.attr("r", radius);
}

function zoomed({transform}) {
g.attr("transform", transform);
}

return svg.node();
}
Insert cell
height = 500
Insert cell
radius = 6
Insert cell
step = radius * 2
Insert cell
data = Array.from({length: 2000}, (_, i) => {
const radius = step * Math.sqrt(i += 0.5), a = theta * i;
return [
width / 2 + radius * Math.cos(a),
height / 2 + radius * Math.sin(a)
];
})
Insert cell
theta = Math.PI * (3 - Math.sqrt(5))
Insert cell
d3 = require("d3-selection@2", "d3-zoom@2", "d3-scale-chromatic@2")
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