Published
Edited
Mar 15, 2021
Fork of Drag (SVG)
3 stars
Insert cell
Insert cell
tick = {
let i = 0;
while (true) {
yield Promises.delay(1000, i++);
}
}
Insert cell
{tick; // update once per second.

let c0x = d3.select('#c0').attr('cx')
let c0y = d3.select('#c0').attr('cy')
let c1x = d3.select('#c1').attr('cx')
let c1y = d3.select('#c1').attr('cy')
let c2x = d3.select('#c2').attr('cx')
let c2y = d3.select('#c2').attr('cy')
let c3x = d3.select('#c3').attr('cx')
let c3y = d3.select('#c3').attr('cy')
return {c0x,c0y,c1x,c1y,c2x,c2y,c3x,c3y}
}
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("stroke-width", 2);

const circles = d3.range(4).map(i => ({
x: Math.random() * (width - radius * 2) + radius,
y: Math.random() * (height - radius * 2) + radius,
}));

svg.selectAll("circle")
.data(circles)
.join("circle")
.attr('id', (d,i) => `c${i}`)
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", radius)
.attr("fill", (d, i) => d3.schemeCategory10[i % 10])
.call(drag);

return svg.node();
}
Insert cell
drag = {

function dragstarted(event, d) {
d3.select(this).raise().attr("stroke", "black");
}

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

function dragended(event, d) {
d3.select(this).attr("stroke", null);
}

return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
height = 600
Insert cell
radius = 32
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