Public
Edited
Oct 26, 2023
1 fork
16 stars
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const brush = d3.brush()
.filter(event => !event.ctrlKey
&& !event.button
&& (event.metaKey
|| event.target.__data__.type !== "overlay"))
.on("start brush end", brushed);

const circle = svg.append("g")
.attr("fill-opacity", 0.2)
.selectAll("circle")
.data(Array.from({length: 2000}, () => [rx(), ry()]))
.join("circle")
.attr("transform", d => `translate(${d})`)
.attr("r", 3.5);

svg.append("g")
.attr("class", "brush")
.call(brush)
.call(brush.move, [[100, 100], [200, 200]])
.call(g => g.select(".overlay").style("cursor", "default"));

function brushed({selection}) {
if (selection === null) {
circle.attr("stroke", null);
} else {
const [[x0, y0], [x1, y1]] = selection;
circle.attr("stroke", ([x, y]) => {
return x0 <= x && x <= x1
&& y0 <= y && y <= y1
? "red" : null;
});
}
}

return svg.node();
}
Insert cell
rx = d3.randomUniform(0, width)
Insert cell
ry = d3.randomUniform(0, height)
Insert cell
height = 500
Insert cell
import {keys} from "@mbostock/keys"
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