Published
Edited
Aug 27, 2020
22 forks
50 stars
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

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