Public
Edited
Jul 12, 2020
1 star
Insert cell
Insert cell
Insert cell
snapFactor = 10
Insert cell
chart = {
let focussed;
const zoom = d3
.zoom()
.scaleExtent([1, 40])
.on("zoom", zoomed)
function zoomed() {
if(d3.event.sourceEvent && d3.event.sourceEvent.type === "wheel"){
const { transform } = d3.event
const mouse = d3.mouse(this)
const mouseInverted = transform.invert(mouse)

const factor = Math.pow(transform.k, 1/snapFactor)
const x = -focussed[0] + (focussed[0] - mouseInverted[0]) / factor
const y = -focussed[1] + (focussed[1] - mouseInverted[1]) / factor
const snapTransform = d3.zoomIdentity
.translate(mouse[0], mouse[1])
.scale(transform.k)
.translate(x,y)

zoom.transform(svg, snapTransform)
return false
}
g.attr("transform", d3.event.transform);
}
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.on("click", reset);

const g = svg.append("g");

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))
.on("click", clicked);

svg.call(zoom).on("mousemove", mousemove);

function random() {
const [x, y] = data[Math.floor(Math.random() * data.length)];
svg
.transition()
.duration(2500)
.call(
zoom.transform,
d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(40)
.translate(-x, -y)
);
}

function reset() {
svg
.transition()
.duration(750)
.call(
zoom.transform,
d3.zoomIdentity,
d3.zoomTransform(svg.node()).invert([width / 2, height / 2])
);
}

function mousemove() {
const p = d3.zoomTransform(this).invert(d3.mouse(this));
focussed = quadtree.find(p[0], p[1]);

// used to be zoom.center(q)

g.selectAll("circle").attr("r", p => (p == focussed ? radius + 3 : radius));
}

function clicked([x, y]) {
d3.event.stopPropagation();
svg
.transition()
.duration(750)
.call(
zoom.transform,
d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(40)
.translate(-x, -y),
d3.mouse(svg.node())
);
}



return Object.assign(svg.node(), {
zoomIn: () => svg.transition().call(zoom.scaleBy, 2),
zoomOut: () => svg.transition().call(zoom.scaleBy, 0.5),
zoomRandom: random,
zoomReset: reset
});
}
Insert cell
height = 500
Insert cell
radius = 6
Insert cell
step = radius * 5
Insert cell
quadtree = d3.quadtree()
.addAll(data)
Insert cell
data = Array.from({length: 1000}, (_, 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@5")
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