Published
Edited
Jan 18, 2020
2 stars
Insert cell
Insert cell
{
const boxHeight = 300;
const svg = d3.create('svg').attr('viewBox', [0, 0, width, boxHeight]);

// offset represents the delta between the click/touch point
// and the x-y coordinates of the circle
let offset = { x: 0, y: 0 };

// dragStart is called once
function dragStart() {
// Set the offset values
offset.x = d3.event.x - d3.select(this).attr('cx');
offset.y = d3.event.y - d3.select(this).attr('cy');
}

// drag is called continuously
function drag() {
// Set the value of the circle coordinates
// and maintain the click/touch offset
d3.select(this)
.attr('cx', d3.event.x - offset.x)
.attr('cy', d3.event.y - offset.y);
}

svg
.append('circle')
.attr('cx', width / 2)
.attr('cy', boxHeight / 2)
.attr('r', 50)
.style('fill', 'green')
.call(
// Attach drag event handlers to the circle
d3
.drag()
.on('start', dragStart)
.on('drag', drag)
);

return svg.node();
}
Insert cell
d3 = require('d3')
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