Published
Edited
Aug 27, 2020
21 forks
49 stars
Also listed in…
d3-drag
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const g = svg.append("g")
.attr("cursor", "grab");

g.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", ({x}) => x)
.attr("cy", ({y}) => y)
.attr("r", radius)
.attr("fill", (d, i) => d3.interpolateRainbow(i / 360))
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));

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

function dragstarted() {
d3.select(this).raise();
g.attr("cursor", "grabbing");
}

function dragged(event, d) {
d3.select(this).attr("cx", d.x = event.x).attr("cy", d.y = event.y);
}

function dragended() {
g.attr("cursor", "grab");
}

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 {
x: width / 2 + radius * Math.cos(a),
y: height / 2 + radius * Math.sin(a)
};
})
Insert cell
theta = Math.PI * (3 - Math.sqrt(5))
Insert cell
d3 = require("d3@6")
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