Published
Edited
Oct 14, 2021
Insert cell
md`# Test Rectangle Drag`
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
svg.style('border', '1px solid black')
let offset = { x: 0, y: 0 };

function dragstarted(event) {
d3.select(this).raise().attr("stroke", "black");
offset.x = event.x - d3.select(this).attr('x');
offset.y = event.y - d3.select(this).attr('y');
}

function dragged(event) {
d3.select(this)
.attr('x', event.x - offset.x)
//.attr('y', event.y - offset.y);
}

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

const rect = svg.append('rect')
.attr('width', '100')
.attr('height', '100')
.attr('x', 10)
.attr('y', 100)
.attr('fill', 'red')
.style('border', '1px solid black')
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended))

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

}
Insert cell
a
Insert cell
a
Insert cell
a
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