Public
Edited
Feb 6, 2021
Insert cell
Insert cell
Insert cell
Insert cell
{
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

const initiator = null;
const pathBeingDrawn = svg
.append("path")
.datum([])
.attr("fill", "none")
.attr("stroke-width", brushTickness)
.attr("stroke", "black");

function drawPathBeingDrawn(e) {
pathBeingDrawn.datum().push({ x: e.layerX, y: e.layerY });
pathBeingDrawn.attr("d", d => pathGenerator(d));
}

// Remove the temporary element drawn and add it as a permanent element.
function removeDrawPathBeingDrawn(e) {
window.removeEventListener("mousemove", drawPathBeingDrawn);
window.removeEventListener("mouseup", removeDrawPathBeingDrawn);

const data = pathBeingDrawn.datum();
pathBeingDrawn.datum([]);
pathBeingDrawn.attr("d", d => pathGenerator(d));

svg
.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke-width", brushTickness)
.attr("stroke", "black")
.attr("d", d => pathGenerator(d));
}

svg.on("mousedown", function(e) {
pathBeingDrawn.datum().push({ x: e.layerX, y: e.layerY });
window.addEventListener("mousemove", drawPathBeingDrawn);
window.addEventListener("mouseup", removeDrawPathBeingDrawn);
});

return svg.node();
}
Insert cell
pathGenerator = d3
.line()
.x(d => d.x) // .x -> x values to use for line
.y(d => d.y) // .y -> y values to use for line
Insert cell
height = 500
Insert cell
import { dropdown, number, slider, checkbox, select } from "@jashkenas/inputs"
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