Published unlisted
Edited
Oct 7, 2020
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const drag = d3.drag()
.subject((event, d) => ({x: fromCartesianX(d.x), y: fromCartesianY(d.y)}))
.on("drag", dragged);

svg.selectAll("circle")
.data([{x: 0, y: 0}])
.join("circle")
.attr("cx", d => fromCartesianX(d.x))
.attr("cy", d => fromCartesianY(d.y))
.attr("r", 40)
.call(drag);

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

return svg.node();
}
Insert cell
fromCartesianX = x => originX + x
Insert cell
fromCartesianY = y => originY - y
Insert cell
toCartesianX = x => x - originX
Insert cell
toCartesianY = y => -y + originY
Insert cell
height = 500
Insert cell
originX = width / 2
Insert cell
originY = height / 2
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