chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
const g = svg.append("g");
let spaceKeyPressed = false;
const onKey = isDown => {
return e => {
const k = d3.event.code;
const isSpace = k === "Space";
if (isSpace) {
spaceKeyPressed = isDown;
svg.style("cursor", isDown ? "move" : "default");
}
};
};
d3.select("body").on("keydown", onKey(true));
d3.select("body").on("keyup", onKey(false));
g.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", ([x]) => x)
.attr("cy", ([, y]) => y)
.attr("r", 1.5);
svg.call(
d3
.zoom()
.filter(() => {
console.log(d3.event);
return (
!d3.event.button &&
!(d3.event.type === "mousedown" && !spaceKeyPressed)
);
})
.extent([[0, 0], [width, height]])
.scaleExtent([1, 8])
.on("zoom", zoomed)
);
function zoomed() {
g.attr("transform", d3.event.transform);
}
return svg.node();
}